-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean_memory.py
More file actions
167 lines (123 loc) · 3.18 KB
/
Copy pathclean_memory.py
File metadata and controls
167 lines (123 loc) · 3.18 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
"""
Project Junk Cleaner
Safely removes Python cache files, build artifacts, and temporary files.
Example:
python clean.py
python clean.py --dry-run
python clean.py --yes
"""
from __future__ import annotations
import argparse
import shutil
from pathlib import Path
# ---------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------
PROTECTED_DIRS = {
".git",
"venv",
".venv",
"env",
".idea",
}
JUNK_DIRS = {
"__pycache__",
".pytest_cache",
".mypy_cache",
".ruff_cache",
".tox",
".coverage",
".hypothesis",
"build",
"dist",
"*.egg-info",
}
JUNK_FILES = {
"*.pyc",
"*.pyo",
"*.pyd",
"*.tmp",
"*.log",
"*.bak",
"*.swp",
"*.swo",
".DS_Store",
"Thumbs.db",
".coverage",
}
# ---------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------
def find_junk(root: Path):
directories = []
files = []
for path in root.rglob("*"):
if path.is_symlink():
continue
if any(parent.name in PROTECTED_DIRS for parent in path.parents):
continue
if path.is_dir():
for pattern in JUNK_DIRS:
if path.match(pattern):
directories.append(path)
break
elif path.is_file():
for pattern in JUNK_FILES:
if path.match(pattern):
files.append(path)
break
return directories, files
def delete_directory(path: Path):
shutil.rmtree(path, ignore_errors=True)
def delete_file(path: Path):
path.unlink(missing_ok=True)
# ---------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------
def cleaner():
parser = argparse.ArgumentParser(
description="Clean Python project junk files."
)
parser.add_argument(
"--path",
type=Path,
default=Path.cwd(),
help="Project root (default=current directory)",
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Show what would be removed.",
)
parser.add_argument(
"--yes",
action="store_true",
help="Skip confirmation.",
)
args = parser.parse_args()
root = args.path.resolve()
dirs, files = find_junk(root)
print(f"\nProject : {root}")
print(f"Directories : {len(dirs)}")
print(f"Files : {len(files)}\n")
if not dirs and not files:
print("Nothing to clean.")
return
for d in dirs:
print("[DIR ]", d)
for f in files:
print("[FILE]", f)
if args.dry_run:
return
removed_dirs = 0
removed_files = 0
for d in dirs:
delete_directory(d)
removed_dirs += 1
for f in files:
delete_file(f)
removed_files += 1
print("\nCleanup complete.")
print(f"Directories removed : {removed_dirs}")
print(f"Files removed : {removed_files}")