Last active
April 26, 2026 15:42
-
-
Save antonymarcano/807af7f276f6ad5e24e61e32ded86ef3 to your computer and use it in GitHub Desktop.
Illustration of an rm gateway (would be called via a console script entry point)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pathlib import Path | |
| import subprocess | |
| import sys | |
| GIT_TRACKED = 0 | |
| GIT_UNTRACKED = 1 | |
| # Better to load from a config file | |
| _deny = frozenset({Path("/"), Path("/home"), Path("/usr")}) | |
| _strategies = { | |
| GIT_TRACKED: lambda path: subprocess.run(["git", "rm", "-rf", path], check=True), | |
| GIT_UNTRACKED: lambda path: subprocess.run(["rm", "-rf", path], check=True), | |
| } | |
| def _validate(path: Path) -> None: | |
| if any(path == denied or path.is_relative_to(denied) for denied in _deny): | |
| raise ValueError(f"refusing to remove: {path}") | |
| def _remove(path: Path) -> None: | |
| result = subprocess.run(["git", "ls-files", "--error-unmatch", path]) | |
| strategy = _strategies.get(result.returncode) | |
| if strategy is None: | |
| raise RuntimeError(f"unexpected exit code {result.returncode} for {path}") | |
| strategy(path) | |
| def remove_dirs(paths: list[Path]) -> None: | |
| for path in paths: | |
| _validate(path) | |
| for path in paths: | |
| _remove(path) | |
| def main() -> None: | |
| remove_dirs([Path(p) for p in sys.argv[1:]]) | |
| [project.scripts] | |
| remove-dir = "mypackage.remove_dir:main" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment