|
2 | 2 |
|
3 | 3 | import hashlib |
4 | 4 | import os |
| 5 | +import warnings |
5 | 6 | import zipfile |
6 | 7 | from pathlib import Path |
7 | 8 | from typing import TYPE_CHECKING |
8 | 9 |
|
9 | 10 | if TYPE_CHECKING: |
10 | 11 | from openapi_fal_rest.client import Client |
11 | 12 |
|
| 13 | +import pathspec |
| 14 | +from packaging.version import Version |
12 | 15 | from pathspec import PathSpec |
13 | 16 |
|
| 17 | +_PATHSPEC_IS_1X = Version(pathspec.__version__).major >= 1 |
| 18 | + |
14 | 19 |
|
15 | 20 | def _check_hash(client: Client, target_path: str, hash_string: str) -> bool: |
16 | 21 | import openapi_fal_rest.api.files.check_dir_hash as check_dir_hash_api |
@@ -80,21 +85,29 @@ def _load_gitignore_patterns(dir_path: str) -> list: |
80 | 85 | return gitignore_patterns |
81 | 86 |
|
82 | 87 |
|
83 | | -def _is_ignored(file_path: str, gitignore_patterns: list[str]) -> bool: |
84 | | - pathspec = PathSpec.from_lines("gitwildmatch", gitignore_patterns) |
85 | | - return pathspec.match_file(file_path) |
| 88 | +def _build_ignore_spec(gitignore_patterns: list[str]) -> PathSpec: |
| 89 | + # "gitwildmatch" is deprecated on pathspec 1.x, but do not rename it to |
| 90 | + # "gitignore": that name binds different pattern classes on 0.x vs 1.x. |
| 91 | + if _PATHSPEC_IS_1X: |
| 92 | + # Pin the stdlib backend; 1.x auto-picks re2/hyperscan when installed. |
| 93 | + with warnings.catch_warnings(): |
| 94 | + warnings.simplefilter("ignore", DeprecationWarning) |
| 95 | + return PathSpec.from_lines( |
| 96 | + "gitwildmatch", gitignore_patterns, backend="simple" |
| 97 | + ) |
| 98 | + return PathSpec.from_lines("gitwildmatch", gitignore_patterns) |
86 | 99 |
|
87 | 100 |
|
88 | 101 | def _zip_directory(dir_path: str, zip_path: str) -> None: |
89 | | - gitignore_patterns = _load_gitignore_patterns(dir_path) |
| 102 | + ignore_spec = _build_ignore_spec(_load_gitignore_patterns(dir_path)) |
90 | 103 |
|
91 | 104 | with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf: |
92 | 105 | for root, _, files in os.walk(dir_path): |
93 | 106 | for file in files: |
94 | 107 | file_path = os.path.join(root, file) |
95 | 108 | relative_path = os.path.relpath(file_path, dir_path) |
96 | 109 |
|
97 | | - if not _is_ignored(relative_path, gitignore_patterns): |
| 110 | + if not ignore_spec.match_file(relative_path): |
98 | 111 | arcname = relative_path |
99 | 112 | zipf.write(file_path, arcname) |
100 | 113 |
|
|
0 commit comments