|
1 | 1 | import json |
2 | 2 | import os |
3 | 3 | import pathlib |
| 4 | +import subprocess |
4 | 5 | import zipfile |
5 | | -from typing import Optional |
| 6 | +from typing import List, Optional, Union |
6 | 7 |
|
7 | 8 | import httpx |
8 | 9 | import requests |
| 10 | +from pathspec import PathSpec |
9 | 11 |
|
10 | 12 | from comfy_cli import constants, ui |
11 | 13 |
|
@@ -86,61 +88,160 @@ def download_file(url: str, local_filepath: pathlib.Path, headers: Optional[dict |
86 | 88 | raise DownloadException(f"Failed to download file.\n{status_reason}") |
87 | 89 |
|
88 | 90 |
|
| 91 | +def _load_comfyignore_spec(ignore_filename: str = ".comfyignore") -> Optional[PathSpec]: |
| 92 | + if not os.path.exists(ignore_filename): |
| 93 | + return None |
| 94 | + try: |
| 95 | + with open(ignore_filename, "r", encoding="utf-8") as ignore_file: |
| 96 | + patterns = [ |
| 97 | + line.strip() |
| 98 | + for line in ignore_file |
| 99 | + if line.strip() and not line.lstrip().startswith("#") |
| 100 | + ] |
| 101 | + except OSError: |
| 102 | + return None |
| 103 | + |
| 104 | + if not patterns: |
| 105 | + return None |
| 106 | + |
| 107 | + return PathSpec.from_lines("gitwildmatch", patterns) |
| 108 | + |
| 109 | + |
| 110 | +def list_git_tracked_files(base_path: Union[str, os.PathLike] = ".") -> List[str]: |
| 111 | + try: |
| 112 | + result = subprocess.check_output( |
| 113 | + ["git", "-C", os.fspath(base_path), "ls-files"], |
| 114 | + text=True, |
| 115 | + ) |
| 116 | + except (subprocess.SubprocessError, FileNotFoundError): |
| 117 | + return [] |
| 118 | + |
| 119 | + return [line for line in result.splitlines() if line.strip()] |
| 120 | + |
| 121 | + |
| 122 | +def _normalize_path(path: str) -> str: |
| 123 | + rel_path = os.path.relpath(path, start=".") |
| 124 | + if rel_path == ".": |
| 125 | + return "" |
| 126 | + return rel_path.replace("\\", "/") |
| 127 | + |
| 128 | + |
| 129 | +def _is_force_included(rel_path: str, include_prefixes: List[str]) -> bool: |
| 130 | + return any( |
| 131 | + rel_path == prefix or rel_path.startswith(prefix + "/") |
| 132 | + for prefix in include_prefixes |
| 133 | + if prefix |
| 134 | + ) |
| 135 | + |
| 136 | + |
89 | 137 | def zip_files(zip_filename, includes=None): |
90 | | - """ |
91 | | - Zip all files in the current directory that are tracked by git, |
92 | | - plus any additional directories specified in includes. |
93 | | - """ |
| 138 | + """Zip git-tracked files respecting optional .comfyignore patterns.""" |
94 | 139 | includes = includes or [] |
95 | | - included_paths = set() |
96 | | - git_files = [] |
| 140 | + include_prefixes: List[str] = [ |
| 141 | + _normalize_path(os.path.normpath(include.lstrip("/"))) |
| 142 | + for include in includes |
| 143 | + ] |
97 | 144 |
|
98 | | - try: |
99 | | - import subprocess |
| 145 | + included_paths: set[str] = set() |
| 146 | + git_files: list[str] = [] |
100 | 147 |
|
101 | | - git_files = subprocess.check_output(["git", "ls-files"], text=True).splitlines() |
102 | | - except (subprocess.SubprocessError, FileNotFoundError): |
| 148 | + ignore_spec = _load_comfyignore_spec() |
| 149 | + |
| 150 | + def should_ignore(rel_path: str) -> bool: |
| 151 | + if not ignore_spec: |
| 152 | + return False |
| 153 | + if _is_force_included(rel_path, include_prefixes): |
| 154 | + return False |
| 155 | + return ignore_spec.match_file(rel_path) |
| 156 | + |
| 157 | + zip_target = os.fspath(zip_filename) |
| 158 | + zip_abs_path = os.path.abspath(zip_target) |
| 159 | + zip_basename = os.path.basename(zip_abs_path) |
| 160 | + |
| 161 | + git_files = list_git_tracked_files(".") |
| 162 | + if not git_files: |
103 | 163 | print("Warning: Not in a git repository or git not installed. Zipping all files.") |
104 | 164 |
|
105 | | - # Zip only git-tracked files |
106 | | - with zipfile.ZipFile(zip_filename, "w", zipfile.ZIP_DEFLATED) as zipf: |
| 165 | + with zipfile.ZipFile(zip_target, "w", zipfile.ZIP_DEFLATED) as zipf: |
107 | 166 | if git_files: |
108 | 167 | for file_path in git_files: |
109 | | - if zip_filename in file_path: |
| 168 | + if file_path == zip_basename: |
| 169 | + continue |
| 170 | + |
| 171 | + rel_path = _normalize_path(file_path) |
| 172 | + if should_ignore(rel_path): |
110 | 173 | continue |
111 | | - if os.path.exists(file_path): |
112 | | - zipf.write(file_path) |
113 | | - included_paths.add(file_path) |
| 174 | + |
| 175 | + actual_path = os.path.normpath(file_path) |
| 176 | + if os.path.abspath(actual_path) == zip_abs_path: |
| 177 | + continue |
| 178 | + if os.path.exists(actual_path): |
| 179 | + arcname = rel_path or os.path.basename(actual_path) |
| 180 | + zipf.write(actual_path, arcname) |
| 181 | + included_paths.add(rel_path) |
114 | 182 | else: |
115 | 183 | print(f"File not found. Not including in zip: {file_path}") |
116 | 184 | else: |
117 | 185 | for root, dirs, files in os.walk("."): |
118 | 186 | if ".git" in dirs: |
119 | 187 | dirs.remove(".git") |
| 188 | + dirs[:] = [ |
| 189 | + d |
| 190 | + for d in dirs |
| 191 | + if not should_ignore(_normalize_path(os.path.join(root, d))) |
| 192 | + ] |
120 | 193 | for file in files: |
121 | 194 | file_path = os.path.join(root, file) |
122 | | - # Skip zipping the zip file itself |
123 | | - if zip_filename in file_path: |
| 195 | + rel_path = _normalize_path(file_path) |
| 196 | + if ( |
| 197 | + os.path.abspath(file_path) == zip_abs_path |
| 198 | + or rel_path in included_paths |
| 199 | + or should_ignore(rel_path) |
| 200 | + ): |
124 | 201 | continue |
125 | | - relative_path = os.path.relpath(file_path, start=".") |
126 | | - zipf.write(file_path, relative_path) |
127 | | - included_paths.add(file_path) |
| 202 | + arcname = rel_path or file_path |
| 203 | + zipf.write(file_path, arcname) |
| 204 | + included_paths.add(rel_path) |
128 | 205 |
|
129 | 206 | for include_dir in includes: |
130 | | - include_dir = include_dir.lstrip("/") |
| 207 | + include_dir = os.path.normpath(include_dir.lstrip("/")) |
| 208 | + rel_include = _normalize_path(include_dir) |
| 209 | + |
| 210 | + if os.path.isfile(include_dir): |
| 211 | + if not should_ignore(rel_include) and rel_include not in included_paths: |
| 212 | + arcname = rel_include or include_dir |
| 213 | + zipf.write(include_dir, arcname) |
| 214 | + included_paths.add(rel_include) |
| 215 | + continue |
| 216 | + |
131 | 217 | if not os.path.exists(include_dir): |
132 | | - print(f"Warning: Included directory '{include_dir}' does not exist, creating empty directory") |
133 | | - zipf.writestr(f"{include_dir}/", "") |
| 218 | + print( |
| 219 | + f"Warning: Included directory '{include_dir}' does not exist, creating empty directory" |
| 220 | + ) |
| 221 | + arcname = rel_include or include_dir |
| 222 | + if not arcname.endswith("/"): |
| 223 | + arcname = arcname + "/" |
| 224 | + zipf.writestr(arcname, "") |
134 | 225 | continue |
135 | 226 |
|
136 | 227 | for root, dirs, files in os.walk(include_dir): |
| 228 | + dirs[:] = [ |
| 229 | + d |
| 230 | + for d in dirs |
| 231 | + if not should_ignore(_normalize_path(os.path.join(root, d))) |
| 232 | + ] |
137 | 233 | for file in files: |
138 | 234 | file_path = os.path.join(root, file) |
139 | | - if zip_filename in file_path or file_path in included_paths: |
| 235 | + rel_path = _normalize_path(file_path) |
| 236 | + if ( |
| 237 | + os.path.abspath(file_path) == zip_abs_path |
| 238 | + or rel_path in included_paths |
| 239 | + or should_ignore(rel_path) |
| 240 | + ): |
140 | 241 | continue |
141 | | - relative_path = os.path.relpath(file_path, start=".") |
142 | | - zipf.write(file_path, relative_path) |
143 | | - included_paths.add(file_path) |
| 242 | + arcname = rel_path or file_path |
| 243 | + zipf.write(file_path, arcname) |
| 244 | + included_paths.add(rel_path) |
144 | 245 |
|
145 | 246 |
|
146 | 247 | def upload_file_to_signed_url(signed_url: str, file_path: str): |
|
0 commit comments