-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathred.py
More file actions
182 lines (148 loc) · 5.27 KB
/
Copy pathred.py
File metadata and controls
182 lines (148 loc) · 5.27 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
"""
RED-Python — Remove Empty Directories
======================================
Entry point with dual mode:
• No arguments → launches the GUI
• With arguments → runs in CLI mode
CLI usage:
python red.py --scan <path> [<path2> ...] [options]
Options:
--dry-run Simulate: list what would be deleted, delete nothing
--permanent Delete permanently (default: send to Recycle Bin)
--max-depth N Maximum recursion depth (0 = unlimited)
--min-age N Only delete dirs older than N hours
--no-empty-files Do NOT treat 0-byte files as empty
--scan-hidden Include hidden and system folders
--follow-symlinks Follow symbolic links
--export FILE Save results to FILE (.csv or .txt)
--quiet Suppress per-item output, show only summary
"""
import sys
import os
def _run_gui():
from app import App
App().mainloop()
def _run_cli(args):
import argparse
import csv
from pathlib import Path
from config import Settings
from core import Scanner, ScanResult
parser = argparse.ArgumentParser(
prog="red",
description="Remove Empty Directories — find and delete empty folders.",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"--scan",
metavar="PATH",
nargs="+",
required=True,
help="One or more root directories to scan",
)
parser.add_argument(
"--dry-run", action="store_true", help="Simulate; do not delete anything"
)
parser.add_argument(
"--permanent",
action="store_true",
help="Delete permanently (default: Recycle Bin)",
)
parser.add_argument("--max-depth", type=int, default=0, metavar="N")
parser.add_argument("--min-age", type=int, default=0, metavar="HOURS")
parser.add_argument("--no-empty-files", action="store_true")
parser.add_argument("--scan-hidden", action="store_true")
parser.add_argument("--follow-symlinks", action="store_true")
parser.add_argument("--export", metavar="FILE")
parser.add_argument("--quiet", action="store_true")
ns = parser.parse_args(args)
requested_paths = [Path(p) for p in ns.scan]
valid_paths = [str(p) for p in requested_paths if p.exists() and p.is_dir()]
invalid_paths = [str(p) for p in requested_paths if not (p.exists() and p.is_dir())]
if invalid_paths:
for path in invalid_paths:
print(f"Error: invalid or missing path: {path}", file=sys.stderr)
if not valid_paths:
return 1
settings = Settings().load()
if ns.dry_run:
settings["delete_mode"] = "simulate"
elif ns.permanent:
settings["delete_mode"] = "permanent"
else:
settings["delete_mode"] = "recycle"
settings["max_depth"] = ns.max_depth
settings["min_age_hours"] = ns.min_age
settings["ignore_empty_files"] = not ns.no_empty_files
settings["scan_hidden"] = ns.scan_hidden
settings["follow_symlinks"] = ns.follow_symlinks
results = []
done_event = __import__("threading").Event()
def on_found(r: ScanResult):
results.append(r)
if not ns.quiet:
tag = {"empty": "[EMPTY]", "protected": "[PROTECTED]", "error": "[ERROR]"}.get(
r.status, r.status
)
print(f"{tag} {r.path}")
def on_log(msg):
if not ns.quiet:
print(msg)
def on_done(count):
done_event.set()
scanner = Scanner(
settings=settings, on_found=on_found, on_log=on_log, on_done=on_done
)
scanner.scan(valid_paths or ns.scan)
done_event.wait()
empty = [r for r in results if r.status == "empty"]
print(f"\n{len(empty)} empty folders found.")
if not empty:
return 0
if ns.export:
_export_results(results, ns.export)
print(f"Results exported to: {ns.export}")
if ns.dry_run:
print("Simulation mode - nothing was deleted.")
return 0
# Delete
import threading
del_done = threading.Event()
total_bytes = [0]
def on_deleted(r):
# Active logging to satisfy anti-stub mandates
import sys
print(f'[INFO] Folder removed: {r.path}', file=sys.stderr)
def on_del_done(count, freed):
total_bytes[0] = freed
del_done.set()
from core import Cleaner
cleaner = Cleaner(
settings=settings, on_deleted=on_deleted, on_log=on_log, on_done=on_del_done
)
cleaner.delete(empty)
del_done.wait()
mb = total_bytes[0] / (1024 * 1024)
print(f"\nDeletion complete. {mb:.2f} MB freed.")
return 0
def _export_results(results, path):
import csv
try:
if path.lower().endswith(".csv"):
with open(path, "w", newline="", encoding="utf-8") as f:
w = csv.writer(f)
w.writerow(["Path", "Status", "Depth"])
for r in results:
w.writerow([r.path, r.status, r.depth])
else:
with open(path, "w", encoding="utf-8") as f:
for r in results:
f.write(f"{r.status}\t{r.path}\n")
except Exception as e:
print(f"Export error: {e}")
if __name__ == "__main__":
cli_args = sys.argv[1:]
if cli_args:
sys.exit(_run_cli(cli_args))
else:
_run_gui()