|
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 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,139 @@ 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, encoding="utf-8") as ignore_file: |
| 96 | + patterns = [line.strip() for line in ignore_file if line.strip() and not line.lstrip().startswith("#")] |
| 97 | + except OSError: |
| 98 | + return None |
| 99 | + |
| 100 | + if not patterns: |
| 101 | + return None |
| 102 | + |
| 103 | + return PathSpec.from_lines("gitwildmatch", patterns) |
| 104 | + |
| 105 | + |
| 106 | +def list_git_tracked_files(base_path: Union[str, os.PathLike] = ".") -> list[str]: |
| 107 | + try: |
| 108 | + result = subprocess.check_output( |
| 109 | + ["git", "-C", os.fspath(base_path), "ls-files"], |
| 110 | + text=True, |
| 111 | + ) |
| 112 | + except (subprocess.SubprocessError, FileNotFoundError): |
| 113 | + return [] |
| 114 | + |
| 115 | + return [line for line in result.splitlines() if line.strip()] |
| 116 | + |
| 117 | + |
| 118 | +def _normalize_path(path: str) -> str: |
| 119 | + rel_path = os.path.relpath(path, start=".") |
| 120 | + if rel_path == ".": |
| 121 | + return "" |
| 122 | + return rel_path.replace("\\", "/") |
| 123 | + |
| 124 | + |
| 125 | +def _is_force_included(rel_path: str, include_prefixes: list[str]) -> bool: |
| 126 | + return any(rel_path == prefix or rel_path.startswith(prefix + "/") for prefix in include_prefixes if prefix) |
| 127 | + |
| 128 | + |
89 | 129 | 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 | | - """ |
| 130 | + """Zip git-tracked files respecting optional .comfyignore patterns.""" |
94 | 131 | includes = includes or [] |
95 | | - included_paths = set() |
96 | | - git_files = [] |
| 132 | + include_prefixes: list[str] = [_normalize_path(os.path.normpath(include.lstrip("/"))) for include in includes] |
97 | 133 |
|
98 | | - try: |
99 | | - import subprocess |
| 134 | + included_paths: set[str] = set() |
| 135 | + git_files: list[str] = [] |
100 | 136 |
|
101 | | - git_files = subprocess.check_output(["git", "ls-files"], text=True).splitlines() |
102 | | - except (subprocess.SubprocessError, FileNotFoundError): |
| 137 | + ignore_spec = _load_comfyignore_spec() |
| 138 | + |
| 139 | + def should_ignore(rel_path: str) -> bool: |
| 140 | + if not ignore_spec: |
| 141 | + return False |
| 142 | + if _is_force_included(rel_path, include_prefixes): |
| 143 | + return False |
| 144 | + return ignore_spec.match_file(rel_path) |
| 145 | + |
| 146 | + zip_target = os.fspath(zip_filename) |
| 147 | + zip_abs_path = os.path.abspath(zip_target) |
| 148 | + zip_basename = os.path.basename(zip_abs_path) |
| 149 | + |
| 150 | + git_files = list_git_tracked_files(".") |
| 151 | + if not git_files: |
103 | 152 | print("Warning: Not in a git repository or git not installed. Zipping all files.") |
104 | 153 |
|
105 | | - # Zip only git-tracked files |
106 | | - with zipfile.ZipFile(zip_filename, "w", zipfile.ZIP_DEFLATED) as zipf: |
| 154 | + with zipfile.ZipFile(zip_target, "w", zipfile.ZIP_DEFLATED) as zipf: |
107 | 155 | if git_files: |
108 | 156 | for file_path in git_files: |
109 | | - if zip_filename in file_path: |
| 157 | + if file_path == zip_basename: |
| 158 | + continue |
| 159 | + |
| 160 | + rel_path = _normalize_path(file_path) |
| 161 | + if should_ignore(rel_path): |
| 162 | + continue |
| 163 | + |
| 164 | + actual_path = os.path.normpath(file_path) |
| 165 | + if os.path.abspath(actual_path) == zip_abs_path: |
110 | 166 | continue |
111 | | - if os.path.exists(file_path): |
112 | | - zipf.write(file_path) |
113 | | - included_paths.add(file_path) |
| 167 | + if os.path.exists(actual_path): |
| 168 | + arcname = rel_path or os.path.basename(actual_path) |
| 169 | + zipf.write(actual_path, arcname) |
| 170 | + included_paths.add(rel_path) |
114 | 171 | else: |
115 | 172 | print(f"File not found. Not including in zip: {file_path}") |
116 | 173 | else: |
117 | 174 | for root, dirs, files in os.walk("."): |
118 | 175 | if ".git" in dirs: |
119 | 176 | dirs.remove(".git") |
| 177 | + dirs[:] = [d for d in dirs if not should_ignore(_normalize_path(os.path.join(root, d)))] |
120 | 178 | for file in files: |
121 | 179 | file_path = os.path.join(root, file) |
122 | | - # Skip zipping the zip file itself |
123 | | - if zip_filename in file_path: |
| 180 | + rel_path = _normalize_path(file_path) |
| 181 | + if ( |
| 182 | + os.path.abspath(file_path) == zip_abs_path |
| 183 | + or rel_path in included_paths |
| 184 | + or should_ignore(rel_path) |
| 185 | + ): |
124 | 186 | continue |
125 | | - relative_path = os.path.relpath(file_path, start=".") |
126 | | - zipf.write(file_path, relative_path) |
127 | | - included_paths.add(file_path) |
| 187 | + arcname = rel_path or file_path |
| 188 | + zipf.write(file_path, arcname) |
| 189 | + included_paths.add(rel_path) |
128 | 190 |
|
129 | 191 | for include_dir in includes: |
130 | | - include_dir = include_dir.lstrip("/") |
| 192 | + include_dir = os.path.normpath(include_dir.lstrip("/")) |
| 193 | + rel_include = _normalize_path(include_dir) |
| 194 | + |
| 195 | + if os.path.isfile(include_dir): |
| 196 | + if not should_ignore(rel_include) and rel_include not in included_paths: |
| 197 | + arcname = rel_include or include_dir |
| 198 | + zipf.write(include_dir, arcname) |
| 199 | + included_paths.add(rel_include) |
| 200 | + continue |
| 201 | + |
131 | 202 | if not os.path.exists(include_dir): |
132 | 203 | print(f"Warning: Included directory '{include_dir}' does not exist, creating empty directory") |
133 | | - zipf.writestr(f"{include_dir}/", "") |
| 204 | + arcname = rel_include or include_dir |
| 205 | + if not arcname.endswith("/"): |
| 206 | + arcname = arcname + "/" |
| 207 | + zipf.writestr(arcname, "") |
134 | 208 | continue |
135 | 209 |
|
136 | 210 | for root, dirs, files in os.walk(include_dir): |
| 211 | + dirs[:] = [d for d in dirs if not should_ignore(_normalize_path(os.path.join(root, d)))] |
137 | 212 | for file in files: |
138 | 213 | file_path = os.path.join(root, file) |
139 | | - if zip_filename in file_path or file_path in included_paths: |
| 214 | + rel_path = _normalize_path(file_path) |
| 215 | + if ( |
| 216 | + os.path.abspath(file_path) == zip_abs_path |
| 217 | + or rel_path in included_paths |
| 218 | + or should_ignore(rel_path) |
| 219 | + ): |
140 | 220 | continue |
141 | | - relative_path = os.path.relpath(file_path, start=".") |
142 | | - zipf.write(file_path, relative_path) |
143 | | - included_paths.add(file_path) |
| 221 | + arcname = rel_path or file_path |
| 222 | + zipf.write(file_path, arcname) |
| 223 | + included_paths.add(rel_path) |
144 | 224 |
|
145 | 225 |
|
146 | 226 | def upload_file_to_signed_url(signed_url: str, file_path: str): |
|
0 commit comments