Skip to content

Instantly share code, notes, and snippets.

@ml31415
Last active October 19, 2023 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ml31415/98f8070dcb7682628df02521607b105b to your computer and use it in GitHub Desktop.
Save ml31415/98f8070dcb7682628df02521607b105b to your computer and use it in GitHub Desktop.
Clean up script for all temporary binaries and build artifacts, that might pile up within a python project and possibly disturb a clean execution.
#!/usr/bin/env python3
from shutil import rmtree
from sys import argv
from pathlib import Path
from typing import Iterable
DELETE_DIRS: set[str] = {
"build",
"dist",
"*.egg-info",
"**/__pycache__",
".hypothesis",
".pytest_cache",
".mypy_cache",
".cache",
".eggs"
}
DEFAULT_DIRS: list[str] = ["."]
def delete_path(path: Path, dry=False):
if path.is_dir():
print("Deleting", path)
if not dry:
rmtree(path)
elif path.is_file():
print("Deleting", path)
if not dry:
path.unlink(missing_ok=True)
def get_artifacts(path: Path) -> Iterable[Path]:
for pattern in DELETE_DIRS:
for artifact in path.glob(pattern):
yield artifact
def main(dry=False):
paths = argv[1:] or DEFAULT_DIRS
for path in paths:
for fpath in get_artifacts(Path(path)):
delete_path(fpath, dry=dry)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment