pathflow-toolkit 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- path_file/__init__.py +223 -0
- path_file/__main__.py +6 -0
- path_file/_internal/__init__.py +87 -0
- path_file/_internal/_iterators.py +349 -0
- path_file/_internal/_log.py +153 -0
- path_file/_internal/_parsers.py +234 -0
- path_file/_internal/_safety.py +268 -0
- path_file/_version.py +1 -0
- path_file/actions.py +855 -0
- path_file/archive.py +447 -0
- path_file/chain.py +1028 -0
- path_file/cli.py +582 -0
- path_file/entries.py +459 -0
- path_file/exceptions.py +174 -0
- path_file/filters.py +654 -0
- path_file/hash.py +338 -0
- path_file/progress.py +321 -0
- path_file/py.typed +0 -0
- path_file/report.py +348 -0
- path_file/rules.py +481 -0
- path_file/search.py +472 -0
- path_file/sync.py +384 -0
- path_file/units.py +420 -0
- path_file/utils.py +509 -0
- path_file/watch.py +310 -0
- pathflow_toolkit-0.1.0.dist-info/METADATA +219 -0
- pathflow_toolkit-0.1.0.dist-info/RECORD +30 -0
- pathflow_toolkit-0.1.0.dist-info/WHEEL +4 -0
- pathflow_toolkit-0.1.0.dist-info/entry_points.txt +2 -0
- pathflow_toolkit-0.1.0.dist-info/licenses/LICENSE +21 -0
path_file/cli.py
ADDED
|
@@ -0,0 +1,582 @@
|
|
|
1
|
+
"""Command-line interface for path_file using stdlib argparse."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import contextlib
|
|
7
|
+
import json
|
|
8
|
+
import sys
|
|
9
|
+
from collections.abc import Callable
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import TYPE_CHECKING, Any, cast
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from path_file.watch import WatchEvent
|
|
15
|
+
|
|
16
|
+
import path_file as pf
|
|
17
|
+
from path_file._version import __version__
|
|
18
|
+
from path_file.exceptions import DryRunRequiredError, PathflowError
|
|
19
|
+
|
|
20
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
21
|
+
# MAIN
|
|
22
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def main(argv: list[str] | None = None) -> int:
|
|
26
|
+
"""Main CLI entry point."""
|
|
27
|
+
# Avoid crashes on Windows consoles that cannot encode box-drawing /
|
|
28
|
+
# non-ASCII characters; replace unencodable characters instead of raising.
|
|
29
|
+
for stream in (sys.stdout, sys.stderr):
|
|
30
|
+
reconfigure = getattr(stream, "reconfigure", None)
|
|
31
|
+
if reconfigure is not None:
|
|
32
|
+
with contextlib.suppress(ValueError, OSError):
|
|
33
|
+
reconfigure(errors="replace")
|
|
34
|
+
|
|
35
|
+
parser = _create_parser()
|
|
36
|
+
args = parser.parse_args(argv)
|
|
37
|
+
|
|
38
|
+
# Set verbosity
|
|
39
|
+
if args.verbose:
|
|
40
|
+
pf.verbose()
|
|
41
|
+
elif args.quiet:
|
|
42
|
+
pf.quiet()
|
|
43
|
+
|
|
44
|
+
try:
|
|
45
|
+
# Every command handler registered via set_defaults(func=...) is
|
|
46
|
+
# annotated to return ``int``.
|
|
47
|
+
return cast(int, args.func(args))
|
|
48
|
+
except DryRunRequiredError as e:
|
|
49
|
+
print(f"Error: {e}", file=sys.stderr)
|
|
50
|
+
return 3
|
|
51
|
+
except PathflowError as e:
|
|
52
|
+
print(f"Error: {e}", file=sys.stderr)
|
|
53
|
+
return 1
|
|
54
|
+
except FileNotFoundError as e:
|
|
55
|
+
print(f"Error: {e}", file=sys.stderr)
|
|
56
|
+
return 1
|
|
57
|
+
except KeyboardInterrupt:
|
|
58
|
+
print("\nCancelled.", file=sys.stderr)
|
|
59
|
+
return 130
|
|
60
|
+
except Exception as e:
|
|
61
|
+
if args.verbose:
|
|
62
|
+
import traceback
|
|
63
|
+
|
|
64
|
+
traceback.print_exc()
|
|
65
|
+
print(f"Error: {e}", file=sys.stderr)
|
|
66
|
+
return 1
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
70
|
+
# PARSER CREATION
|
|
71
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _create_parser() -> argparse.ArgumentParser:
|
|
75
|
+
parser = argparse.ArgumentParser(
|
|
76
|
+
prog="path_file",
|
|
77
|
+
description="The elegant file automation library for Python",
|
|
78
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
79
|
+
epilog="""
|
|
80
|
+
Examples:
|
|
81
|
+
path_file find ~/Downloads --ext .jpg --larger 2MB
|
|
82
|
+
path_file report ~/Documents --by-ext
|
|
83
|
+
path_file copy ~/Photos /backup/Photos --dry-run
|
|
84
|
+
path_file delete ~/Downloads/*.tmp --confirm
|
|
85
|
+
path_file sync ~/Documents /mnt/backup --delete-extras
|
|
86
|
+
path_file watch ~/Downloads --on-new "echo New file: {}"
|
|
87
|
+
""",
|
|
88
|
+
)
|
|
89
|
+
parser.add_argument("-v", "--verbose", action="store_true", help="Verbose output")
|
|
90
|
+
parser.add_argument("-q", "--quiet", action="store_true", help="Quiet output")
|
|
91
|
+
parser.add_argument("-n", "--dry-run", action="store_true", help="Dry run (no changes)")
|
|
92
|
+
parser.add_argument("--json", action="store_true", help="Output JSON")
|
|
93
|
+
parser.add_argument("--version", action="version", version=f"path_file {__version__}")
|
|
94
|
+
|
|
95
|
+
# Common flags shared by all subcommands (usable before OR after the command).
|
|
96
|
+
# default=SUPPRESS so a value set by the main parser is not overwritten
|
|
97
|
+
# by the subparser's default.
|
|
98
|
+
common = argparse.ArgumentParser(add_help=False)
|
|
99
|
+
common.add_argument(
|
|
100
|
+
"-v", "--verbose", action="store_true", default=argparse.SUPPRESS, help="Verbose output"
|
|
101
|
+
)
|
|
102
|
+
common.add_argument(
|
|
103
|
+
"-q", "--quiet", action="store_true", default=argparse.SUPPRESS, help="Quiet output"
|
|
104
|
+
)
|
|
105
|
+
common.add_argument(
|
|
106
|
+
"-n",
|
|
107
|
+
"--dry-run",
|
|
108
|
+
action="store_true",
|
|
109
|
+
default=argparse.SUPPRESS,
|
|
110
|
+
help="Dry run (no changes)",
|
|
111
|
+
)
|
|
112
|
+
common.add_argument(
|
|
113
|
+
"--json", action="store_true", default=argparse.SUPPRESS, help="Output JSON"
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
subparsers = parser.add_subparsers(dest="command", required=True)
|
|
117
|
+
|
|
118
|
+
# find
|
|
119
|
+
_add_find_parser(subparsers, common)
|
|
120
|
+
# report
|
|
121
|
+
_add_report_parser(subparsers, common)
|
|
122
|
+
# copy
|
|
123
|
+
_add_copy_parser(subparsers, common)
|
|
124
|
+
# move
|
|
125
|
+
_add_move_parser(subparsers, common)
|
|
126
|
+
# delete
|
|
127
|
+
_add_delete_parser(subparsers, common)
|
|
128
|
+
# hash
|
|
129
|
+
_add_hash_parser(subparsers, common)
|
|
130
|
+
# duplicates
|
|
131
|
+
_add_duplicates_parser(subparsers, common)
|
|
132
|
+
# archive
|
|
133
|
+
_add_archive_parser(subparsers, common)
|
|
134
|
+
# extract
|
|
135
|
+
_add_extract_parser(subparsers, common)
|
|
136
|
+
# watch
|
|
137
|
+
_add_watch_parser(subparsers, common)
|
|
138
|
+
# sync
|
|
139
|
+
_add_sync_parser(subparsers, common)
|
|
140
|
+
# rules
|
|
141
|
+
_add_rules_parser(subparsers, common)
|
|
142
|
+
|
|
143
|
+
return parser
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
147
|
+
# COMMAND PARSERS
|
|
148
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _add_find_parser(subparsers: Any, parents: argparse.ArgumentParser) -> None:
|
|
152
|
+
p = subparsers.add_parser("find", help="Find files", parents=[parents])
|
|
153
|
+
p.add_argument("path", type=Path, help="Root directory")
|
|
154
|
+
p.add_argument(
|
|
155
|
+
"-r", "--recursive", action="store_true", default=True, help="Recursive (default)"
|
|
156
|
+
)
|
|
157
|
+
p.add_argument("--no-recursive", dest="recursive", action="store_false", help="Non-recursive")
|
|
158
|
+
p.add_argument(
|
|
159
|
+
"--ext", "--extension", action="append", dest="extensions", help="File extension"
|
|
160
|
+
)
|
|
161
|
+
p.add_argument("--name", help="Name contains")
|
|
162
|
+
p.add_argument("--pattern", help="Glob pattern")
|
|
163
|
+
p.add_argument("--regex", help="Regex pattern")
|
|
164
|
+
p.add_argument("--larger", help="Larger than size (e.g., 1MB)")
|
|
165
|
+
p.add_argument("--smaller", help="Smaller than size")
|
|
166
|
+
p.add_argument("--newer", help="Newer than duration (e.g., 7 days)")
|
|
167
|
+
p.add_argument("--older", help="Older than duration")
|
|
168
|
+
p.add_argument("--hidden", action="store_true", help="Include hidden")
|
|
169
|
+
p.add_argument("--empty", action="store_true", help="Only empty")
|
|
170
|
+
p.add_argument("--sort", choices=["name", "size", "date", "path"], help="Sort by")
|
|
171
|
+
p.add_argument("--reverse", action="store_true", help="Reverse sort")
|
|
172
|
+
p.add_argument("--limit", type=int, help="Limit results")
|
|
173
|
+
p.add_argument("--files", action="store_true", help="Only files")
|
|
174
|
+
p.add_argument("--dirs", action="store_true", help="Only directories")
|
|
175
|
+
p.set_defaults(func=_cmd_find)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def _add_report_parser(subparsers: Any, parents: argparse.ArgumentParser) -> None:
|
|
179
|
+
p = subparsers.add_parser("report", help="Generate report", parents=[parents])
|
|
180
|
+
p.add_argument("path", type=Path, help="Root directory")
|
|
181
|
+
p.add_argument(
|
|
182
|
+
"-r", "--recursive", action="store_true", default=True, help="Recursive (default)"
|
|
183
|
+
)
|
|
184
|
+
p.add_argument("--no-recursive", dest="recursive", action="store_false", help="Non-recursive")
|
|
185
|
+
p.add_argument("--by-ext", action="store_true", help="Show by extension")
|
|
186
|
+
p.add_argument("--by-size", action="store_true", help="Show by size")
|
|
187
|
+
p.add_argument("--extremes", action="store_true", help="Show largest/smallest/newest/oldest")
|
|
188
|
+
p.set_defaults(func=_cmd_report)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def _add_copy_parser(subparsers: Any, parents: argparse.ArgumentParser) -> None:
|
|
192
|
+
p = subparsers.add_parser("copy", help="Copy files", parents=[parents])
|
|
193
|
+
p.add_argument("source", type=Path, help="Source path")
|
|
194
|
+
p.add_argument("dest", type=Path, help="Destination directory")
|
|
195
|
+
p.add_argument("-r", "--recursive", action="store_true", default=True)
|
|
196
|
+
p.add_argument("--ext", action="append", help="Extension filter")
|
|
197
|
+
p.add_argument("--overwrite", action="store_true", help="Overwrite existing")
|
|
198
|
+
p.add_argument("--preserve", action="store_true", default=True, help="Preserve metadata")
|
|
199
|
+
p.add_argument("-p", "--parallel", type=int, default=1, help="Parallel workers")
|
|
200
|
+
p.set_defaults(func=_cmd_copy)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def _add_move_parser(subparsers: Any, parents: argparse.ArgumentParser) -> None:
|
|
204
|
+
p = subparsers.add_parser("move", help="Move files", parents=[parents])
|
|
205
|
+
p.add_argument("source", type=Path, help="Source path")
|
|
206
|
+
p.add_argument("dest", type=Path, help="Destination directory")
|
|
207
|
+
p.add_argument("-r", "--recursive", action="store_true", default=True)
|
|
208
|
+
p.add_argument("--ext", action="append", help="Extension filter")
|
|
209
|
+
p.add_argument("--overwrite", action="store_true", help="Overwrite existing")
|
|
210
|
+
p.set_defaults(func=_cmd_move)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def _add_delete_parser(subparsers: Any, parents: argparse.ArgumentParser) -> None:
|
|
214
|
+
p = subparsers.add_parser("delete", help="Delete files", parents=[parents])
|
|
215
|
+
p.add_argument("path", type=Path, help="Path to delete from")
|
|
216
|
+
p.add_argument("-r", "--recursive", action="store_true", default=True)
|
|
217
|
+
p.add_argument("--ext", action="append", help="Extension filter")
|
|
218
|
+
p.add_argument("--older", help="Older than duration")
|
|
219
|
+
p.add_argument("--larger", help="Larger than size")
|
|
220
|
+
p.add_argument("--pattern", help="Glob pattern")
|
|
221
|
+
p.add_argument("--confirm", action="store_true", required=False, help="Confirm deletion")
|
|
222
|
+
p.add_argument("--trash", action="store_true", help="Move to trash instead")
|
|
223
|
+
p.set_defaults(func=_cmd_delete)
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def _add_hash_parser(subparsers: Any, parents: argparse.ArgumentParser) -> None:
|
|
227
|
+
p = subparsers.add_parser("hash", help="Compute file hashes", parents=[parents])
|
|
228
|
+
p.add_argument("path", type=Path, help="File or directory")
|
|
229
|
+
p.add_argument(
|
|
230
|
+
"-a", "--algorithm", default="sha256", choices=["md5", "sha1", "sha256", "sha512"]
|
|
231
|
+
)
|
|
232
|
+
p.add_argument("-r", "--recursive", action="store_true", default=True)
|
|
233
|
+
p.set_defaults(func=_cmd_hash)
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def _add_duplicates_parser(subparsers: Any, parents: argparse.ArgumentParser) -> None:
|
|
237
|
+
p = subparsers.add_parser("duplicates", help="Find duplicate files", parents=[parents])
|
|
238
|
+
p.add_argument("path", type=Path, help="Directory to scan")
|
|
239
|
+
p.add_argument("-a", "--algorithm", default="sha256")
|
|
240
|
+
p.add_argument("-r", "--recursive", action="store_true", default=True)
|
|
241
|
+
p.add_argument("--by-name", action="store_true", help="Find by name instead of content")
|
|
242
|
+
p.set_defaults(func=_cmd_duplicates)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def _add_archive_parser(subparsers: Any, parents: argparse.ArgumentParser) -> None:
|
|
246
|
+
p = subparsers.add_parser("archive", help="Create archive", parents=[parents])
|
|
247
|
+
p.add_argument("path", type=Path, help="Directory or files to archive")
|
|
248
|
+
p.add_argument("-r", "--recursive", action="store_true", default=True)
|
|
249
|
+
p.add_argument("-o", "--output", type=Path, required=True, help="Output archive path")
|
|
250
|
+
p.add_argument(
|
|
251
|
+
"-f", "--format", default="zip", choices=["zip", "tar", "tar.gz", "tar.bz2", "tar.xz"]
|
|
252
|
+
)
|
|
253
|
+
p.set_defaults(func=_cmd_archive)
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def _add_extract_parser(subparsers: Any, parents: argparse.ArgumentParser) -> None:
|
|
257
|
+
p = subparsers.add_parser("extract", help="Extract archive", parents=[parents])
|
|
258
|
+
p.add_argument("archive", type=Path, help="Archive file")
|
|
259
|
+
p.add_argument("-r", "--recursive", action="store_true", default=True)
|
|
260
|
+
p.add_argument("-o", "--output", type=Path, required=True, help="Output directory")
|
|
261
|
+
p.set_defaults(func=_cmd_extract)
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def _add_watch_parser(subparsers: Any, parents: argparse.ArgumentParser) -> None:
|
|
265
|
+
p = subparsers.add_parser("watch", help="Watch directory for changes", parents=[parents])
|
|
266
|
+
p.add_argument("path", type=Path, help="Directory to watch")
|
|
267
|
+
p.add_argument("-i", "--interval", type=float, default=1.0, help="Poll interval")
|
|
268
|
+
p.add_argument("--filter", help="Glob filter")
|
|
269
|
+
p.add_argument("--debounce", type=float, default=0, help="Debounce seconds")
|
|
270
|
+
p.add_argument("--on-new", help="Command for new files (use {} for path)")
|
|
271
|
+
p.add_argument("--on-modified", help="Command for modified files")
|
|
272
|
+
p.add_argument("--on-deleted", help="Command for deleted files")
|
|
273
|
+
p.add_argument("--duration", type=float, help="Run for N seconds")
|
|
274
|
+
p.set_defaults(func=_cmd_watch)
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def _add_sync_parser(subparsers: Any, parents: argparse.ArgumentParser) -> None:
|
|
278
|
+
p = subparsers.add_parser("sync", help="Synchronize directories", parents=[parents])
|
|
279
|
+
p.add_argument("source", type=Path, help="Source directory")
|
|
280
|
+
p.add_argument("dest", type=Path, help="Destination directory")
|
|
281
|
+
p.add_argument("-r", "--recursive", action="store_true", default=True)
|
|
282
|
+
p.add_argument(
|
|
283
|
+
"--direction",
|
|
284
|
+
choices=["source_to_dest", "dest_to_source", "bidirectional"],
|
|
285
|
+
default="source_to_dest",
|
|
286
|
+
)
|
|
287
|
+
p.add_argument(
|
|
288
|
+
"--delete-extras", action="store_true", help="Delete files in dest not in source"
|
|
289
|
+
)
|
|
290
|
+
p.add_argument("--ignore", action="append", help="Ignore pattern")
|
|
291
|
+
p.add_argument("--ignore-file", type=Path, help="Ignore file (.gitignore style)")
|
|
292
|
+
p.add_argument(
|
|
293
|
+
"--conflict", choices=["error", "source_wins", "dest_wins", "newer_wins"], default="error"
|
|
294
|
+
)
|
|
295
|
+
p.add_argument("-p", "--parallel", type=int, default=4)
|
|
296
|
+
p.set_defaults(func=_cmd_sync)
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def _add_rules_parser(subparsers: Any, parents: argparse.ArgumentParser) -> None:
|
|
300
|
+
p = subparsers.add_parser("rules", help="Apply rules from TOML file", parents=[parents])
|
|
301
|
+
p.add_argument("path", type=Path, help="Directory to apply rules to")
|
|
302
|
+
p.add_argument("-r", "--recursive", action="store_true", default=True)
|
|
303
|
+
p.add_argument("-f", "--file", type=Path, required=True, help="Rules file")
|
|
304
|
+
p.add_argument("--apply", action="store_true", help="Actually apply (not dry-run)")
|
|
305
|
+
p.set_defaults(func=_cmd_rules)
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
309
|
+
# COMMAND IMPLEMENTATIONS
|
|
310
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def _build_chain(args: argparse.Namespace) -> pf.FileChain:
|
|
314
|
+
"""Build FileChain from common find arguments.
|
|
315
|
+
|
|
316
|
+
Tolerant of different subparsers: uses ``source``/``path`` for the root and
|
|
317
|
+
``extensions``/``ext`` for extension filters.
|
|
318
|
+
"""
|
|
319
|
+
root = getattr(args, "path", None) or getattr(args, "source", None) or "."
|
|
320
|
+
chain = pf.find(root)
|
|
321
|
+
|
|
322
|
+
if not getattr(args, "recursive", True):
|
|
323
|
+
chain = chain.recursive(False)
|
|
324
|
+
|
|
325
|
+
if getattr(args, "files", False):
|
|
326
|
+
chain = chain.files()
|
|
327
|
+
if getattr(args, "dirs", False):
|
|
328
|
+
chain = chain.dirs()
|
|
329
|
+
|
|
330
|
+
extensions = getattr(args, "extensions", None) or getattr(args, "ext", None)
|
|
331
|
+
if extensions:
|
|
332
|
+
chain = chain.with_extensions(*extensions)
|
|
333
|
+
|
|
334
|
+
if getattr(args, "name", None):
|
|
335
|
+
chain = chain.name_contains(args.name)
|
|
336
|
+
|
|
337
|
+
if getattr(args, "pattern", None):
|
|
338
|
+
chain = chain.glob(args.pattern)
|
|
339
|
+
|
|
340
|
+
if getattr(args, "regex", None):
|
|
341
|
+
chain = chain.name_matches_regex(args.regex)
|
|
342
|
+
|
|
343
|
+
if getattr(args, "larger", None):
|
|
344
|
+
chain = chain.larger_than(args.larger)
|
|
345
|
+
|
|
346
|
+
if getattr(args, "smaller", None):
|
|
347
|
+
chain = chain.smaller_than(args.smaller)
|
|
348
|
+
|
|
349
|
+
if getattr(args, "newer", None):
|
|
350
|
+
chain = chain.newer_than(args.newer)
|
|
351
|
+
|
|
352
|
+
if getattr(args, "older", None):
|
|
353
|
+
chain = chain.older_than(args.older)
|
|
354
|
+
|
|
355
|
+
if getattr(args, "hidden", False):
|
|
356
|
+
chain = chain.hidden()
|
|
357
|
+
|
|
358
|
+
if getattr(args, "empty", False):
|
|
359
|
+
chain = chain.empty()
|
|
360
|
+
|
|
361
|
+
if getattr(args, "sort", None):
|
|
362
|
+
chain = chain.sort_by(args.sort, desc=getattr(args, "reverse", False))
|
|
363
|
+
|
|
364
|
+
if getattr(args, "limit", None):
|
|
365
|
+
chain = chain.limit(args.limit)
|
|
366
|
+
|
|
367
|
+
if getattr(args, "dry_run", False):
|
|
368
|
+
chain = chain.dry_run(True)
|
|
369
|
+
|
|
370
|
+
if getattr(args, "parallel", 1) > 1:
|
|
371
|
+
chain = chain.parallel(args.parallel)
|
|
372
|
+
|
|
373
|
+
return chain
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
def _cmd_find(args: argparse.Namespace) -> int:
|
|
377
|
+
chain = _build_chain(args)
|
|
378
|
+
|
|
379
|
+
if args.json:
|
|
380
|
+
print(chain.to_json())
|
|
381
|
+
else:
|
|
382
|
+
print(chain.to_table())
|
|
383
|
+
|
|
384
|
+
return 0 if chain.count() > 0 else 4
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
def _cmd_report(args: argparse.Namespace) -> int:
|
|
388
|
+
chain = pf.find(args.path)
|
|
389
|
+
if not args.recursive:
|
|
390
|
+
chain = chain.recursive(False)
|
|
391
|
+
|
|
392
|
+
report = chain.report()
|
|
393
|
+
|
|
394
|
+
if args.json:
|
|
395
|
+
print(report.to_json())
|
|
396
|
+
else:
|
|
397
|
+
print(report.to_table())
|
|
398
|
+
|
|
399
|
+
return 0
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
def _cmd_copy(args: argparse.Namespace) -> int:
|
|
403
|
+
chain = _build_chain(args)
|
|
404
|
+
result = chain.copy_to(
|
|
405
|
+
args.dest, overwrite=args.overwrite, preserve_metadata=args.preserve
|
|
406
|
+
).execute()
|
|
407
|
+
print(result.summary())
|
|
408
|
+
return 0 if result.success() else 1
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
def _cmd_move(args: argparse.Namespace) -> int:
|
|
412
|
+
chain = _build_chain(args).dry_run(args.dry_run)
|
|
413
|
+
result = chain.move_to(args.dest, overwrite=args.overwrite).execute()
|
|
414
|
+
print(result.summary())
|
|
415
|
+
return 0 if result.success() else 1
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
def _cmd_delete(args: argparse.Namespace) -> int:
|
|
419
|
+
chain = _build_chain(args)
|
|
420
|
+
|
|
421
|
+
if args.trash:
|
|
422
|
+
result = chain.trash(dry_run=args.dry_run, confirm=args.confirm).execute()
|
|
423
|
+
else:
|
|
424
|
+
result = chain.delete(dry_run=args.dry_run, confirm=args.confirm).execute()
|
|
425
|
+
|
|
426
|
+
print(result.summary())
|
|
427
|
+
return 0 if result.success() else 1
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
def _cmd_hash(args: argparse.Namespace) -> int:
|
|
431
|
+
from path_file.utils import hash_file
|
|
432
|
+
|
|
433
|
+
path = args.path
|
|
434
|
+
if path.is_file():
|
|
435
|
+
files = [path]
|
|
436
|
+
else:
|
|
437
|
+
chain = pf.find(path)
|
|
438
|
+
if not args.recursive:
|
|
439
|
+
chain = chain.recursive(False)
|
|
440
|
+
files = [e.path for e in chain.files()]
|
|
441
|
+
|
|
442
|
+
if args.json:
|
|
443
|
+
data = [{"hash": hash_file(f, algorithm=args.algorithm), "path": str(f)} for f in files]
|
|
444
|
+
print(json.dumps(data, indent=2))
|
|
445
|
+
else:
|
|
446
|
+
for f in files:
|
|
447
|
+
print(f"{hash_file(f, algorithm=args.algorithm)} {f}")
|
|
448
|
+
|
|
449
|
+
return 0
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
def _cmd_duplicates(args: argparse.Namespace) -> int:
|
|
453
|
+
chain = pf.find(args.path)
|
|
454
|
+
if not args.recursive:
|
|
455
|
+
chain = chain.recursive(False)
|
|
456
|
+
chain = chain.files()
|
|
457
|
+
|
|
458
|
+
dupes = chain.duplicates_by_name() if args.by_name else chain.duplicates_by_hash(args.algorithm)
|
|
459
|
+
|
|
460
|
+
if args.json:
|
|
461
|
+
print(json.dumps(dupes, indent=2, default=str))
|
|
462
|
+
else:
|
|
463
|
+
for hash_val, entries in dupes.items():
|
|
464
|
+
print(f"\n{hash_val[:16]}... ({len(entries)} files):")
|
|
465
|
+
for e in entries:
|
|
466
|
+
print(f" {e.path} ({e.size_human})")
|
|
467
|
+
|
|
468
|
+
return 0
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
def _cmd_archive(args: argparse.Namespace) -> int:
|
|
472
|
+
chain = _build_chain(args).files()
|
|
473
|
+
|
|
474
|
+
result = chain.archive_to(args.output, format=args.format).execute()
|
|
475
|
+
print(result.summary())
|
|
476
|
+
return 0 if result.success() else 1
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
def _cmd_extract(args: argparse.Namespace) -> int:
|
|
480
|
+
if not args.archive.is_file():
|
|
481
|
+
raise PathflowError(f"Archive not found: {args.archive}")
|
|
482
|
+
chain = pf.find(args.archive).files()
|
|
483
|
+
result = chain.extract_to(args.output).execute()
|
|
484
|
+
print(result.summary())
|
|
485
|
+
return 0 if result.success() else 1
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
def _cmd_watch(args: argparse.Namespace) -> int:
|
|
489
|
+
watcher = pf.watch(args.path, poll_interval=args.interval)
|
|
490
|
+
|
|
491
|
+
if args.filter:
|
|
492
|
+
watcher.filter(args.filter)
|
|
493
|
+
|
|
494
|
+
if args.debounce:
|
|
495
|
+
watcher.debounce(args.debounce)
|
|
496
|
+
|
|
497
|
+
def make_callback(cmd_template: str) -> Callable[[WatchEvent], None]:
|
|
498
|
+
def callback(event: WatchEvent) -> None:
|
|
499
|
+
cmd = cmd_template.replace("{}", str(event.path))
|
|
500
|
+
import subprocess
|
|
501
|
+
|
|
502
|
+
# shell command template is the documented user-facing API
|
|
503
|
+
subprocess.run(cmd, shell=True) # noqa: S602 # user-provided shell command API
|
|
504
|
+
|
|
505
|
+
return callback
|
|
506
|
+
|
|
507
|
+
if args.on_new:
|
|
508
|
+
watcher.on_new(make_callback(args.on_new))
|
|
509
|
+
if args.on_modified:
|
|
510
|
+
watcher.on_modified(make_callback(args.on_modified))
|
|
511
|
+
if args.on_deleted:
|
|
512
|
+
watcher.on_deleted(make_callback(args.on_deleted))
|
|
513
|
+
|
|
514
|
+
if args.duration:
|
|
515
|
+
watcher.run_for(args.duration)
|
|
516
|
+
else:
|
|
517
|
+
watcher.run_forever()
|
|
518
|
+
|
|
519
|
+
return 0
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
def _cmd_sync(args: argparse.Namespace) -> int:
|
|
523
|
+
syncer = pf.sync(args.source, args.dest)
|
|
524
|
+
syncer.direction(args.direction)
|
|
525
|
+
syncer.delete_extras(args.delete_extras)
|
|
526
|
+
|
|
527
|
+
if args.ignore:
|
|
528
|
+
syncer.ignore(args.ignore)
|
|
529
|
+
if args.ignore_file:
|
|
530
|
+
syncer.ignore_file(args.ignore_file)
|
|
531
|
+
|
|
532
|
+
syncer.conflict_strategy(args.conflict)
|
|
533
|
+
syncer.parallel(args.parallel)
|
|
534
|
+
syncer.dry_run(args.dry_run)
|
|
535
|
+
syncer.progress(True)
|
|
536
|
+
|
|
537
|
+
if args.dry_run:
|
|
538
|
+
diff = syncer.compare()
|
|
539
|
+
print(diff.summary())
|
|
540
|
+
for rel in diff.to_add:
|
|
541
|
+
print(f" + {rel}")
|
|
542
|
+
for rel in diff.to_update:
|
|
543
|
+
print(f" ~ {rel}")
|
|
544
|
+
for rel in diff.to_delete:
|
|
545
|
+
print(f" - {rel}")
|
|
546
|
+
return 0 if diff.has_changes() else 4
|
|
547
|
+
else:
|
|
548
|
+
result = syncer.execute()
|
|
549
|
+
if args.json:
|
|
550
|
+
print(json.dumps(result.to_dict(), indent=2, default=str))
|
|
551
|
+
else:
|
|
552
|
+
print(result.summary())
|
|
553
|
+
return 0 if result.success() else 1
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
def _cmd_rules(args: argparse.Namespace) -> int:
|
|
557
|
+
rules = pf.Rules.from_file(args.file)
|
|
558
|
+
errors = rules.validate()
|
|
559
|
+
if errors:
|
|
560
|
+
for e in errors:
|
|
561
|
+
print(f"Error: {e}", file=sys.stderr)
|
|
562
|
+
return 1
|
|
563
|
+
|
|
564
|
+
chain = pf.find(args.path)
|
|
565
|
+
chain = chain.dry_run(False) if args.apply else chain.dry_run(True)
|
|
566
|
+
|
|
567
|
+
result_chain = rules.apply(chain)
|
|
568
|
+
|
|
569
|
+
if args.apply:
|
|
570
|
+
results = rules.run(chain, dry_run=False)
|
|
571
|
+
executed = sum(1 for r in results if r.get("status") == "executed")
|
|
572
|
+
rule_errors = [r for r in results if r.get("status") == "error"]
|
|
573
|
+
print(f"Rules applied: {executed} file(s) affected.")
|
|
574
|
+
for r in rule_errors:
|
|
575
|
+
print(f" Error on {r['entry'].name}: {r['error']}", file=sys.stderr)
|
|
576
|
+
return 0 if not rule_errors else 1
|
|
577
|
+
else:
|
|
578
|
+
print(rules.explain())
|
|
579
|
+
print(f"\nDry run: {result_chain.count()} files would be affected")
|
|
580
|
+
print(result_chain.preview())
|
|
581
|
+
|
|
582
|
+
return 0
|