Files
computer-playbook/utils/cleanup/pycache_only_dirs.py
kevinveenbirkenbachandClaude Opus 5 5137465c6f style: delete every comment the comments-valid policy rejects
3529 invalid comments in 528 files, grandfathered in while the linter
only saw the working-tree diff. Removal ran through the linter's own
collectors, so detection and deletion cannot drift: a comment whose
opener line is blank before it and whose closer line is blank after it
loses all its lines, anything else loses only the comment region, and a
line left holding a bare delimiter goes with it.

Five files keep their comments behind a nocheck marker instead. Four are
upstream configs whose inline docs are upstream's own (mautrix, systemd
journald, nextcloud-oidc-login, MediaWiki). In littlejs the
commented-out entries ARE the deactivation mechanism for an example
tile, so deleting them would delete configuration.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-18 03:22:02 +02:00

56 lines
1.6 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
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())