-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean_build_dirs.py
More file actions
51 lines (43 loc) · 1.4 KB
/
Copy pathclean_build_dirs.py
File metadata and controls
51 lines (43 loc) · 1.4 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
#!/usr/bin/env python3
"""
General Python project cleanup script.
Removes build artifacts, cache folders, temporary files,
and setuptools_scm version files (e.g., _version.py).
Usage:
python clean_build_dirs.py [project_root]
If no argument is given, uses the current working directory.
"""
import os
import shutil
import sys
from pathlib import Path
# Common directories to remove
CLEAN_DIRS = [
"build", "dist", "*.egg-info", "__pycache__", ".pytest_cache",
".mypy_cache", ".ruff_cache", ".nox", ".tox"
]
# Common file patterns to remove
CLEAN_FILES = [
"*.pyc", "*.pyo", "*~", "._*", "_version.py"
]
def remove_path(path: Path):
"""Remove a file or directory safely."""
try:
if path.is_dir():
shutil.rmtree(path)
print(f"🗑️ Removed directory: {path}")
elif path.is_file():
path.unlink()
print(f"🗑️ Removed file: {path}")
except Exception as e:
print(f"⚠️ Could not remove {path}: {e}")
def clean(root: Path):
"""Recursively remove unwanted build artifacts."""
for pattern in CLEAN_DIRS + CLEAN_FILES:
for match in root.rglob(pattern):
remove_path(match)
if __name__ == "__main__":
project_root = Path(sys.argv[1]) if len(sys.argv) > 1 else Path.cwd()
print(f"🚀 Cleaning Python project at: {project_root}")
clean(project_root)
print("✅ Cleanup complete.")