Repository navigation
Expand file tree
/
Copy pathhygiene_scan.py
More file actions
421 lines (347 loc) · 12.6 KB
/
Copy pathhygiene_scan.py
File metadata and controls
421 lines (347 loc) · 12.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/usr/bin/env python3
"""Read-only local dev hygiene scanner.
This prints a compact report. It does not modify files.
"""
from __future__ import annotations
import os
import re
import signal
import subprocess
import time
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
HOME = Path.home()
PROJECTS = HOME / "Projects"
PROJECT_SCAN_ROOTS = [
PROJECTS,
HOME / "Documents",
HOME / "Desktop",
HOME / "Downloads",
]
CACHE_SCAN_ROOTS = [
*PROJECT_SCAN_ROOTS,
HOME / ".opencode",
HOME / ".continue",
HOME / ".codex",
HOME / ".agents",
HOME / ".grok",
HOME / ".npm",
]
PROJECT_MARKERS = {
".git",
"package.json",
"pyproject.toml",
"requirements.txt",
"Cargo.toml",
"go.mod",
"Makefile",
"README.md",
"AGENTS.md",
"SKILL.md",
}
CACHE_NAMES = {
"node_modules",
".venv",
"venv",
".next",
"dist",
"build",
"out",
"target",
"__pycache__",
".pytest_cache",
".ruff_cache",
"_npx",
".tmp",
"backups",
"state-snapshots",
}
PRUNE_NAMES = CACHE_NAMES | {
".git",
"Archive",
"Library",
"Movies",
"Music",
"Pictures",
".Trash",
}
TOOL_MANAGED_PARTS = {
".codex/skills",
".codex/plugins",
".codex/.tmp/marketplaces",
".agents/skills",
".grok/skills",
".grok/bundled",
".grok/marketplace-cache",
}
ORPHAN_NAME = re.compile(
r"(^|[-_ .])(old|tmp|temp|backup|bak|copy|final|test|demo|draft|untitled)([-_ .]|$)",
re.IGNORECASE,
)
@dataclass
class ProjectHit:
path: Path
size: str
modified: str
markers: list[str]
reason: str
@dataclass
class CacheHit:
path: Path
size: str
note: str = "cleanup candidate"
@dataclass
class LaunchAgentHit:
label: str
pid: str
last_exit: str
note: str = ""
ACTIVE_CACHE_PATHS = {
HOME / ".codex" / ".tmp",
HOME / ".codex" / "backups",
HOME / ".npm" / "_npx",
HOME / ".opencode" / "node_modules",
}
APP_OWNED_EMPTY_DIRS: set[Path] = set()
IGNORED_BROKEN_LINK_PREFIXES: set[Path] = set()
def run(args: list[str]) -> str:
try:
return subprocess.run(args, capture_output=True, text=True, timeout=8).stdout.strip()
except Exception:
return ""
def size_of(path: Path) -> str:
out = run(["/usr/bin/du", "-sh", str(path)])
return out.split()[0] if out else "unknown"
def modified(path: Path) -> str:
try:
return datetime.fromtimestamp(path.stat().st_mtime).strftime("%Y-%m-%d")
except OSError:
return "unknown"
def existing_roots(roots: list[Path]) -> list[Path]:
return [root for root in roots if root.exists()]
def is_tool_managed(path: Path) -> bool:
text = str(path.relative_to(HOME)) if path.is_relative_to(HOME) else str(path)
return any(part in text for part in TOOL_MANAGED_PARTS)
def is_active_runtime_cache(path: Path) -> bool:
return path in ACTIVE_CACHE_PATHS
def is_app_owned_empty_dir(path: Path) -> bool:
if path in APP_OWNED_EMPTY_DIRS:
return True
recordings = HOME / "Documents" / "superwhisper" / "recordings"
return path.parent == recordings
def is_ignored_broken_link(path: Path) -> bool:
return any(path.is_relative_to(prefix) for prefix in IGNORED_BROKEN_LINK_PREFIXES)
def find_projects(max_depth: int = 4) -> list[ProjectHit]:
hits: dict[Path, ProjectHit] = {}
for root in existing_roots(PROJECT_SCAN_ROOTS):
for dirpath, dirnames, _filenames in os.walk(root):
path = Path(dirpath)
if is_tool_managed(path):
dirnames[:] = []
continue
depth = len(path.relative_to(root).parts)
if depth > max_depth:
dirnames[:] = []
continue
markers = sorted(name for name in PROJECT_MARKERS if (path / name).exists())
if markers:
reason = "project markers"
if not path.is_relative_to(PROJECTS):
reason = "outside /Projects"
elif "README.md" not in markers and ".git" not in markers:
reason = "no README or Git metadata"
hits[path] = ProjectHit(path, size_of(path), modified(path), markers, reason)
if ".git" in markers:
dirnames[:] = []
continue
dirnames[:] = [name for name in dirnames if name not in PRUNE_NAMES]
return sorted(hits.values(), key=lambda hit: str(hit.path))
def find_caches(max_depth: int = 6, min_mb: int = 50) -> tuple[list[CacheHit], list[CacheHit]]:
hits: list[CacheHit] = []
watched: list[CacheHit] = []
for root in existing_roots(CACHE_SCAN_ROOTS):
for dirpath, dirnames, _filenames in os.walk(root):
path = Path(dirpath)
depth = len(path.relative_to(root).parts)
if depth > max_depth:
dirnames[:] = []
continue
keep: list[str] = []
for name in dirnames:
child = path / name
if name in CACHE_NAMES:
size = size_of(child)
if size.endswith("G") or (size.endswith("M") and float(size[:-1]) >= min_mb):
if is_active_runtime_cache(child):
watched.append(CacheHit(child, size, "active runtime cache"))
else:
hits.append(CacheHit(child, size))
else:
keep.append(name)
dirnames[:] = [name for name in keep if name not in PRUNE_NAMES]
return (
sorted(hits, key=lambda hit: hit.path.name),
sorted(watched, key=lambda hit: hit.path.name),
)
def find_broken_links(max_depth: int = 6) -> list[Path]:
broken: list[Path] = []
for root in existing_roots(PROJECT_SCAN_ROOTS):
for dirpath, dirnames, filenames in os.walk(root, followlinks=False):
path = Path(dirpath)
depth = len(path.relative_to(root).parts)
if depth > max_depth:
dirnames[:] = []
continue
for name in [*dirnames, *filenames]:
child = path / name
try:
if child.is_symlink() and not child.exists() and not is_ignored_broken_link(child):
broken.append(child)
except OSError:
pass
dirnames[:] = [name for name in dirnames if name not in PRUNE_NAMES]
return sorted(broken)
def user_launchagent_labels() -> set[str]:
agents_dir = HOME / "Library" / "LaunchAgents"
if not agents_dir.exists():
return set()
labels: set[str] = set()
for plist in agents_dir.glob("*.plist"):
labels.add(plist.stem)
return labels
def find_launchagent_issues() -> list[LaunchAgentHit]:
hits: list[LaunchAgentHit] = []
labels = user_launchagent_labels()
out = run(["/bin/launchctl", "list"])
if not out:
return hits
for line in out.splitlines():
parts = line.split()
if len(parts) < 3 or parts[2] not in labels:
continue
pid, last_exit, label = parts[0], parts[1], parts[2]
if last_exit in {"0", "-"}:
continue
hits.append(
LaunchAgentHit(
label,
pid,
last_exit,
"last run failed — check StandardErrorPath in plist",
)
)
return sorted(hits, key=lambda hit: hit.label)
def count_broken_links(roots: list[Path], max_depth: int = 4) -> int:
total = 0
for root in existing_roots(roots):
for dirpath, dirnames, filenames in os.walk(root, followlinks=False):
path = Path(dirpath)
depth = len(path.relative_to(root).parts)
if depth > max_depth:
dirnames[:] = []
continue
for name in [*dirnames, *filenames]:
child = path / name
try:
if child.is_symlink() and not child.exists() and not is_ignored_broken_link(child):
total += 1
except OSError:
pass
dirnames[:] = [name for name in dirnames if name not in PRUNE_NAMES]
return total
def find_large_home_files(min_mb: int = 500) -> list[tuple[Path, str]]:
hits: list[tuple[Path, str]] = []
for root in [HOME / "Desktop", HOME / "Movies", HOME / "Downloads"]:
if not root.exists():
continue
for dirpath, dirnames, filenames in os.walk(root):
path = Path(dirpath)
dirnames[:] = [name for name in dirnames if name not in PRUNE_NAMES]
for name in filenames:
child = path / name
try:
if child.stat().st_size >= min_mb * 1024 * 1024:
hits.append((child, size_of(child)))
except OSError:
pass
return sorted(hits, key=lambda item: str(item[0]))
def find_orphans(max_depth: int = 3) -> list[Path]:
orphans: list[Path] = []
for root in [PROJECTS, HOME / "Documents", HOME / "Desktop", HOME / "Downloads"]:
if not root.exists():
continue
for dirpath, dirnames, _filenames in os.walk(root):
path = Path(dirpath)
depth = len(path.relative_to(root).parts)
if depth > max_depth:
dirnames[:] = []
continue
has_marker = any((path / marker).exists() for marker in PROJECT_MARKERS)
empty = not any(path.iterdir()) if path.exists() else False
if empty and is_app_owned_empty_dir(path):
dirnames[:] = [name for name in dirnames if name not in PRUNE_NAMES]
continue
if path != root and (empty or (ORPHAN_NAME.search(path.name) and not has_marker)):
orphans.append(path)
dirnames[:] = [name for name in dirnames if name not in PRUNE_NAMES]
return sorted(orphans)
def print_section(title: str) -> None:
print(f"\n## {title}")
def main() -> int:
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
print("# Machine Hygiene Scan")
print(f"Generated: {datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M')}")
projects = find_projects()
caches, watched_caches = find_caches()
broken = find_broken_links()
orphans = find_orphans()
launch_issues = find_launchagent_issues()
scoped_broken = count_broken_links([PROJECTS, HOME / "Library" / "LaunchAgents"])
large_files = find_large_home_files()
print_section("Summary")
print(f"- Project-like folders: {len(projects)}")
print(f"- Cleanup cache candidates: {len(caches)}")
print(f"- Watched active caches: {len(watched_caches)}")
print(f"- Orphan signals: {len(orphans)}")
print(f"- Broken symlinks (Projects + LaunchAgents): {scoped_broken}")
print(f"- Broken symlinks (project scan): {len(broken)}")
print(f"- LaunchAgent issues (~/Library/LaunchAgents): {len(launch_issues)}")
print(f"- Large home files (>=500MB): {len(large_files)}")
print_section("Project-Like Folders")
for hit in projects:
marker_text = ", ".join(hit.markers[:6])
print(f"- {hit.path} | {hit.size} | {hit.modified} | {hit.reason} | {marker_text}")
print_section("Cleanup Cache Candidates")
for hit in caches:
print(f"- {hit.path} | {hit.size} | suggested action: move to Trash after review")
print_section("Watched Active Caches")
for hit in watched_caches:
print(f"- {hit.path} | {hit.size} | {hit.note}; leave alone unless intentionally resetting that tool")
print_section("Orphan Signals")
for path in orphans:
print(f"- {path} | {size_of(path)} | suggested action: archive after review")
print_section("Broken Symlinks")
for path in broken:
print(f"- {path} -> {os.readlink(path)}")
print_section("LaunchAgent Issues")
if launch_issues:
for hit in launch_issues:
print(
f"- {hit.label} | pid={hit.pid} | last_exit={hit.last_exit} | {hit.note}"
)
else:
print("- none")
print_section("Large Home Files")
if large_files:
for path, size in large_files:
print(f"- {path} | {size}")
else:
print("- none")
print_section("Review Command Pattern")
print('mkdir -p "$HOME/Projects/Archive/YYYY-MM-machine-hygiene" "$HOME/.Trash/YYYY-MM-machine-hygiene"')
print('mv "/path/to/orphan" "$HOME/Projects/Archive/YYYY-MM-machine-hygiene/"')
print('mv "/path/to/cache" "$HOME/.Trash/YYYY-MM-machine-hygiene/"')
return 0
if __name__ == "__main__":
raise SystemExit(main())