Files
computer-playbook/utils/cleanup/pycache_only_dirs.py
T
kevinveenbirkenbachandClaude Opus 4.7 c02229b4be refactor(make): align targets to domain prefixes (compose-, network-, etc.)
Apply a consistent naming convention to the Make target inventory so
every target is grouped by its domain:

- compose-*  (compose-up/down/restart/stop/exec/inner-run)
- network-*  (network-dns-setup/remove, network-ipv6-disable/restore,
              network-refresh, network-trust-ca)
- security-* (security-apparmor-restore/teardown)
- diagnose-* (diagnose-network, diagnose-disk-usage)
- fix-*      (fix-chmod, fix-dockerignore)
- clean-*    (clean-cache, clean-pycache-dirs)
- install-*  (install-agent)
- meta-*     (meta-list, meta-tree, meta-mig)

Drop the stale debug-network target and reintroduce its functionality
as diagnose-network. Cascade renames through:

- Makefile (.PHONY entries, prereqs, recursive $(MAKE) calls)
- contributor docs, agent-iteration docs, role-level docs
- bundle README, environment scripts, install/lint/test wrappers
- AGENTS.md, CLAUDE.md, compose/cache.override.yml, .github/workflows

Historical entries under docs/requirements/ and changelog/ stay
untouched as audit logs.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 21:00:45 +02:00

58 lines
1.7 KiB
Python

"""Remove directories whose only child is a ``__pycache__`` folder.
Such directories show up after Python test modules / source files are moved or
deleted: the ``__pycache__`` cache remains and the now-orphan parent directory
holds nothing else. ``git clean -fdX`` does not touch the parent because the
parent itself is tracked, so they stick around forever.
Run via ``python -m utils.cleanup.pycache_only_dirs`` (or ``make clean-pycache-dirs``).
"""
from __future__ import annotations
import os
import shutil
import sys
from pathlib import Path
_SKIP_PARTS = frozenset({".git", ".venv", "venv", "node_modules"})
def _depth(path: str) -> int:
return path.count(os.sep)
def main() -> int:
root = Path()
removed = 0
# Post-order: deepest directories first so emptying a child can collapse
# its parent on a subsequent pass through the sorted list.
for d, _dirs, _files in sorted(os.walk(root), key=lambda t: -_depth(t[0])):
p = Path(d)
if p == root or p.name == "__pycache__":
continue
if any(part in _SKIP_PARTS for part in p.parts):
continue
if not p.is_dir():
continue
try:
entries = [c.name for c in p.iterdir()]
except PermissionError as e:
print(f"skipped (unreadable) {p}: {e}", file=sys.stderr)
continue
if entries != ["__pycache__"]:
continue
try:
shutil.rmtree(p)
except PermissionError as e:
print(f"skipped (no perm) {p}: {e}", file=sys.stderr)
continue
removed += 1
print(f"removed {p}", file=sys.stderr)
print(f"done: {removed} dir(s) removed", file=sys.stderr)
return 0
if __name__ == "__main__":
raise SystemExit(main())