-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilters.py
More file actions
265 lines (212 loc) · 8.01 KB
/
Copy pathfilters.py
File metadata and controls
265 lines (212 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"""Filters module for RED-Python handling rule matching and file utilities."""
import os
import re
import stat
import time
import fnmatch
# ---------------------------------------------------------------------------
# Constants exposed to the UI
# ---------------------------------------------------------------------------
METHODS = [
"wildcard",
"contains",
"startswith",
"endswith",
"exact",
"exact_path",
"regex_name",
"regex_path",
]
TYPES = ["ignore_file", "ignore_dir", "never_empty"]
METHOD_LABELS = {
"wildcard": "Wildcard (e.g. *.tmp)",
"contains": "Contains",
"startswith": "Starts with",
"endswith": "Ends with",
"exact": "Exact name",
"exact_path": "Exact path",
"regex_name": "Regex (name)",
"regex_path": "Regex (full path)",
}
TYPE_LABELS = {
"ignore_file": "Ignore file",
"ignore_dir": "Ignore folder",
"never_empty": "Never empty",
}
# ---------------------------------------------------------------------------
# Core rule matching - 7 methods
# ---------------------------------------------------------------------------
def match_rule(name: str, full_path: str, rule: dict) -> bool:
"""
Return True if name / full_path satisfies the filter rule.
rule keys: enabled, type, method, pattern
"""
if not rule.get("enabled", True):
return False
pattern = rule.get("pattern", "").strip()
method = rule.get("method", "wildcard")
if not pattern:
return False
n_lo = name.lower()
p_lo = pattern.lower()
fp = full_path or name
if method == "wildcard":
return fnmatch.fnmatch(n_lo, p_lo)
if method == "contains":
return p_lo in n_lo
if method == "startswith":
return n_lo.startswith(p_lo)
if method == "endswith":
return n_lo.endswith(p_lo)
if method == "exact":
return n_lo == p_lo
if method == "exact_path":
return os.path.normcase(fp) == os.path.normcase(pattern)
if method == "regex_name":
try:
return bool(re.search(pattern, name, re.IGNORECASE))
except re.error:
return False
if method == "regex_path":
try:
return bool(re.search(pattern, fp, re.IGNORECASE))
except re.error:
return False
return False
def _active(filter_rules: list, rtype: str) -> list:
return [
r for r in filter_rules if r.get("type") == rtype and r.get("enabled", True)
]
def is_file_ignored(name: str, full_path: str, filter_rules: list) -> bool:
return any(
match_rule(name, full_path, r) for r in _active(filter_rules, "ignore_file")
)
def is_dir_ignored(name: str, full_path: str, filter_rules: list) -> bool:
return any(
match_rule(name, full_path, r) for r in _active(filter_rules, "ignore_dir")
)
def is_never_empty(name: str, full_path: str, filter_rules: list) -> bool:
"""Return True if this directory should never be marked as empty."""
return any(
match_rule(name, full_path, r) for r in _active(filter_rules, "never_empty")
)
# ---------------------------------------------------------------------------
# Long-path helpers
# ---------------------------------------------------------------------------
def long_path(path: str) -> str:
"""Add the \\?\\ prefix for Windows paths longer than 260 chars."""
if os.name == "nt" and not path.startswith("\\\\?\\"):
path = "\\\\?\\" + os.path.abspath(path)
return path
def strip_long_prefix(path: str) -> str:
if path.startswith("\\\\?\\"):
return path[4:]
return path
# ---------------------------------------------------------------------------
# File / directory attribute helpers
# ---------------------------------------------------------------------------
def is_hidden(path: str) -> bool:
if os.path.basename(path).startswith("."):
return True
if os.name == "nt":
try:
return bool(os.stat(path).st_file_attributes & stat.FILE_ATTRIBUTE_HIDDEN)
except Exception as _e:
import sys; print(f'[DEBUG] Ignored Exception: {_e}', file=sys.stderr)
return False
def is_system(path: str) -> bool:
if os.name == "nt":
try:
return bool(os.stat(path).st_file_attributes & stat.FILE_ATTRIBUTE_SYSTEM)
except Exception as _e:
import sys; print(f'[DEBUG] Ignored Exception: {_e}', file=sys.stderr)
return False
def get_age_hours(path: str) -> float:
try:
return (time.time() - os.path.getmtime(path)) / 3600
except Exception:
return float("inf")
def is_protected(path: str, protected_dirs: list) -> bool:
norm = os.path.normcase(os.path.abspath(path))
name = os.path.basename(norm)
for protected in protected_dirs:
p = protected.strip()
if not p:
continue
pnorm = os.path.normcase(os.path.abspath(p))
pname = os.path.basename(pnorm)
if norm == pnorm or norm.startswith(pnorm + os.sep):
return True
if name in (pname, pname.lower()):
return True
return False
# ---------------------------------------------------------------------------
# Directory emptiness checks
# ---------------------------------------------------------------------------
def has_only_ignorable_files(lpath: str, settings) -> bool:
"""
Return True if the directory contains no real files -
only files that are ignorable (by filter rules, zero-byte, or hidden/system).
Subdirectories are NOT checked here; the caller handles them.
"""
try:
entries = os.listdir(lpath)
except (PermissionError, OSError):
return False
filter_rules = settings.get("filter_rules", [])
for entry in entries:
entry_path = os.path.join(lpath, entry)
try:
if os.path.isdir(entry_path):
continue
except Exception as _e:
import sys; print(f'[DEBUG] Ignored Exception: {_e}', file=sys.stderr); continue
if not settings.get("follow_symlinks", False) and os.path.islink(entry_path):
continue
if is_file_ignored(entry, entry_path, filter_rules):
continue
if settings.get("ignore_empty_files", True):
try:
if os.path.getsize(entry_path) == 0:
continue
except Exception as _e:
import sys; print(f'[DEBUG] Ignored Exception: {_e}', file=sys.stderr)
if not settings.get("scan_hidden", False):
try:
if is_hidden(entry_path) or is_system(entry_path):
continue
except Exception as _e:
import sys; print(f'[DEBUG] Ignored Exception: {_e}', file=sys.stderr)
return False # Real file found
return True
def collect_ignorable_files(lpath: str, settings) -> list:
"""Return full paths of ignorable files inside lpath (used before os.rmdir)."""
result = []
try:
entries = os.listdir(lpath)
except Exception:
return result
filter_rules = settings.get("filter_rules", [])
for entry in entries:
entry_path = os.path.join(lpath, entry)
try:
if os.path.isdir(entry_path):
continue
except Exception as _e:
import sys; print(f'[DEBUG] Ignored Exception: {_e}', file=sys.stderr); continue
ignorable = False
if is_file_ignored(entry, entry_path, filter_rules):
ignorable = True
elif settings.get("ignore_empty_files", True):
try:
ignorable = os.path.getsize(entry_path) == 0
except Exception as _e:
import sys; print(f'[DEBUG] Ignored Exception: {_e}', file=sys.stderr)
if not ignorable and not settings.get("scan_hidden", False):
try:
ignorable = is_hidden(entry_path) or is_system(entry_path)
except Exception as _e:
import sys; print(f'[DEBUG] Ignored Exception: {_e}', file=sys.stderr)
if ignorable:
result.append(entry_path)
return result