Repository navigation
Expand file tree
/
Copy pathFiles.py
More file actions
374 lines (299 loc) · 13.6 KB
/
Copy pathFiles.py
File metadata and controls
374 lines (299 loc) · 13.6 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
"""
Files.py — Collection class for batch file operations.
Wraps list[File] with chainable batch methods: zip, tar, filter, map,
totalSize, allExist, deleteAll, copyAllTo, moveAllTo.
Usage:
from File import File
from Files import Files
bundle = Files([
File('C:/project/main.py'),
File('C:/project/data.py'),
File('C:/project/README.md'),
])
bundle.zip('C:/Desktop/bundle.zip', compression=5)
bundle.tar('C:/Desktop/bundle.tar.gz', mode='gz')
# Filter to only existing files then copy
bundle.filter(lambda f: f.exists).copyAllTo('C:/backup/')
# Map to get all sizes
sizes = bundle.map(lambda f: f.size)
print(bundle.totalSize()) # bytes across all files
print(bundle.allExist()) # True if every file exists
Glob constructor:
bundle = Files.glob('C:/project', '**/*.py')
Directory constructor:
bundle = Files.fromDir('C:/project/src', recursive=True)
"""
from __future__ import annotations
import os
import sys
import zipfile
import tarfile
from pathlib import Path
from typing import Any, Callable, Iterator
try:
from data import File # type: ignore # canonical TrioDesktop File gateway
except ImportError:
from File import File # type: ignore # standalone fallback
EMPTY_STRING = ''
try:
from data import recordException as _recordException # type: ignore
except ImportError:
def _recordException(context: str, error=None, *, handled: bool = True, source: str = 'Files.py') -> int: # type: ignore
print(f'[EXCEPTION:{context}] {type(error).__name__}: {error}', file=sys.stderr)
return 0
def _record(context: str, error=None) -> int:
return _recordException(context, error, source='Files.py')
class Files:
"""
A collection of File objects with batch operations.
Constructors:
Files([File(...), File(...)]) from a list
Files.glob(root, pattern) from a glob pattern
Files.fromDir(directory, recursive) from a directory
All batch methods return self or a new Files so calls can be chained:
Files.glob('.', '**/*.py').filter(lambda f: f.size > 0).zip('out.zip')
"""
def __init__(self, files: list[File] | None = None) -> None:
self._files: list[File] = list(files or [])
# ------------------------------------------------------------------
# Alternate constructors
# ------------------------------------------------------------------
@classmethod
def glob(cls, root: str | Path, pattern: str) -> 'Files':
"""Build a Files collection from a glob pattern under *root*."""
base = Path(root)
return cls([File(p) for p in sorted(base.glob(pattern)) if p.is_file()])
@classmethod
def fromDir(cls, directory: str | Path, *, recursive: bool = False, pattern: str = '*') -> 'Files': # noqa: N802
"""
Build a Files collection from all files in a directory.
recursive=True uses rglob instead of glob.
"""
base = Path(directory)
method = base.rglob if recursive else base.glob
return cls([File(p) for p in sorted(method(pattern)) if p.is_file()])
# ------------------------------------------------------------------
# Collection interface
# ------------------------------------------------------------------
def __len__(self) -> int:
return len(self._files)
def __iter__(self) -> Iterator[File]:
return iter(self._files)
def __getitem__(self, index: int) -> File:
return self._files[index]
def __repr__(self) -> str:
return f'Files([{", ".join(repr(f) for f in self._files)}])'
def append(self, file: File) -> 'Files':
self._files.append(file)
return self
def extend(self, files: list[File]) -> 'Files':
self._files.extend(files)
return self
def toList(self) -> list[File]: # noqa: N802
return list(self._files)
def toPaths(self) -> list[Path]: # noqa: N802
return [f.path for f in self._files]
# ------------------------------------------------------------------
# Filtering / mapping
# ------------------------------------------------------------------
def filter(self, predicate: Callable[[File], bool]) -> 'Files':
"""Return a new Files containing only files where predicate(file) is True."""
return Files([f for f in self._files if predicate(f)])
def map(self, fn: Callable[[File], Any]) -> list[Any]:
"""Apply fn to each file and return a plain list of results."""
return [fn(f) for f in self._files]
def existing(self) -> 'Files':
"""Convenience filter: return only files that exist on disk."""
return self.filter(lambda f: f.exists)
def withSuffix(self, suffix: str) -> 'Files': # noqa: N802
"""Return only files matching the given suffix (e.g. '.py')."""
s = suffix.lower() if not suffix.startswith('.') else suffix.lower()
s = s if s.startswith('.') else f'.{s}'
return self.filter(lambda f: f.suffix == s)
# ------------------------------------------------------------------
# Aggregate stats
# ------------------------------------------------------------------
def totalSize(self) -> int: # noqa: N802
"""Total size in bytes across all files."""
return sum(f.size for f in self._files)
def allExist(self) -> bool: # noqa: N802
"""True if every file in the collection exists on disk."""
return all(f.exists for f in self._files)
def anyExist(self) -> bool: # noqa: N802
"""True if at least one file exists."""
return any(f.exists for f in self._files)
def missing(self) -> 'Files':
"""Return files that do NOT exist on disk."""
return self.filter(lambda f: not f.exists)
# ------------------------------------------------------------------
# Batch operations
# ------------------------------------------------------------------
def deleteAll(self) -> int: # noqa: N802
"""Delete all files. Returns count of successfully deleted files."""
return sum(1 for f in self._files if f.delete())
def copyAllTo(self, directory: str | Path) -> int: # noqa: N802
"""
Copy all files into *directory*, preserving only the filename (not subdirs).
Returns count of successfully copied files.
"""
dest = Path(directory)
dest.mkdir(parents=True, exist_ok=True)
return sum(1 for f in self._files if f.copyTo(dest / f.name))
def moveAllTo(self, directory: str | Path) -> int: # noqa: N802
"""Move all files into *directory*. Returns count of successful moves."""
dest = Path(directory)
dest.mkdir(parents=True, exist_ok=True)
return sum(1 for f in self._files if f.moveTo(dest / f.name))
def touchAll(self) -> int: # noqa: N802
"""Set mtime to now for every file. Returns count of successes."""
return sum(1 for f in self._files if f.setMtime())
# ------------------------------------------------------------------
# Archive: zip
# ------------------------------------------------------------------
def zip(
self,
dest: str | Path,
*,
compression: int = 5,
flatten: bool = True,
root: str | Path | None = None,
) -> bool:
"""
Zip all files into *dest*.
compression: 0-9 (0=store, 9=maximum). Default 5.
flatten=True: all files go into the zip root with their filename only.
flatten=False: preserve relative paths from *root* (or cwd if root=None).
root: base path for relative-path preservation when flatten=False.
Returns True on success.
Example:
Files([File('a.py'), File('b.py')]).zip('C:/Desktop/bundle.zip', compression=6)
"""
try:
destination = Path(dest)
destination.parent.mkdir(parents=True, exist_ok=True)
base = Path(root) if root else Path.cwd()
compress_type = zipfile.ZIP_DEFLATED if compression > 0 else zipfile.ZIP_STORED
level = max(0, min(9, int(compression)))
with zipfile.ZipFile( # file-io-ok: canonical Files archive surface
destination,
mode='w',
compression=compress_type,
compresslevel=level,
) as zf:
for f in self._files:
if not f.path.exists():
print(f'[WARN:Files.zip] skipping missing file: {f.path}')
continue
if flatten:
arcname = f.name
else:
try:
arcname = str(f.path.relative_to(base))
except ValueError:
arcname = f.name
zf.write(str(f.path), arcname=arcname) # file-io-ok: canonical Files archive surface
return True
except Exception as exc:
_record('Files.zip', exc)
return False
def zipWithStructure(self, dest: str | Path, root: str | Path, *, compression: int = 5) -> bool: # noqa: N802
"""Convenience: zip preserving relative paths from *root*."""
return self.zip(dest, compression=compression, flatten=False, root=root)
# ------------------------------------------------------------------
# Archive: tar
# ------------------------------------------------------------------
def tar(
self,
dest: str | Path,
*,
mode: str = 'gz',
flatten: bool = True,
root: str | Path | None = None,
) -> bool:
"""
Create a tar archive at *dest*.
mode: 'gz' (gzip, default), 'bz2' (bzip2), 'xz', or '' (uncompressed).
flatten / root: same semantics as .zip().
Returns True on success.
Example:
Files.glob('.', '**/*.py').tar('C:/Desktop/src.tar.gz', mode='gz')
"""
try:
destination = Path(dest)
destination.parent.mkdir(parents=True, exist_ok=True)
base = Path(root) if root else Path.cwd()
tar_mode = f'w:{mode}' if mode else 'w'
with tarfile.open(str(destination), tar_mode) as tf: # file-io-ok: canonical Files archive surface
for f in self._files:
if not f.path.exists():
print(f'[WARN:Files.tar] skipping missing file: {f.path}')
continue
if flatten:
arcname = f.name
else:
try:
arcname = str(f.path.relative_to(base))
except ValueError:
arcname = f.name
tf.add(str(f.path), arcname=arcname)
return True
except Exception as exc:
_record('Files.tar', exc)
return False
# ------------------------------------------------------------------
# Archive: rar (optional — requires rarfile + WinRAR binary)
# ------------------------------------------------------------------
def rar(self, dest: str | Path, *, winrar_path: str | None = None) -> bool:
"""
Create a RAR archive. Requires WinRAR installed and optionally the
rarfile package (pip install rarfile).
winrar_path: explicit path to WinRAR.exe. If None, looks in PATH and
common Windows install locations.
Returns True on success, False if WinRAR is not available.
Note: prefer .zip() or .tar() for portable/CI use. .rar() is
Windows-only and requires a licensed WinRAR binary.
"""
import subprocess
rar_exe = winrar_path
if not rar_exe:
candidates = [
r'C:\Program Files\WinRAR\WinRAR.exe',
r'C:\Program Files (x86)\WinRAR\WinRAR.exe',
'rar',
'WinRAR',
]
for candidate in candidates:
try:
result = subprocess.run([candidate, '--version'], capture_output=True, timeout=3)
if result.returncode in (0, 1):
rar_exe = candidate
break
except (FileNotFoundError, OSError):
continue
if not rar_exe:
print('[WARN:Files.rar] WinRAR not found. Install WinRAR or use .zip()/.tar() instead.')
return False
try:
destination = Path(dest)
destination.parent.mkdir(parents=True, exist_ok=True)
if destination.exists():
destination.unlink()
args = [rar_exe, 'a', str(destination)]
args += [str(f.path) for f in self._files if f.path.exists()]
result = subprocess.run(args, capture_output=True, text=True, timeout=120)
if result.returncode not in (0, 1):
_record('Files.rar', RuntimeError(f'WinRAR exited {result.returncode}: {result.stderr.strip()}'))
return False
return True
except Exception as exc:
_record('Files.rar', exc)
return False
# ------------------------------------------------------------------
# Checksum manifest
# ------------------------------------------------------------------
def md5Manifest(self) -> dict[str, str]: # noqa: N802
"""Return {filename: md5hex} for all existing files."""
return {f.name: f.md5Hex() for f in self._files if f.exists}
def sha1Manifest(self) -> dict[str, str]: # noqa: N802
"""Return {filename: sha1hex} for all existing files."""
return {f.name: f.sha1Hex() for f in self._files if f.exists}