-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean
executable file
·73 lines (60 loc) · 1.64 KB
/
clean
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
#!/usr/bin/env python
import glob
import os
import shutil
from pathlib import Path
SUBJECTS = {
"studio": {
"rust/*": [
"target/",
"*/target/",
],
"crea/**": [
"*.blend?",
"*.blend??",
],
},
"lab/lang": {
"rust/*": "target/",
"typst/*": "*.pdf",
"latex/*": ["*.pdf", "*.aux", "*.log"],
},
"notes": "**/view.pdf",
}
def main():
clean(SUBJECTS)
def clean(subject: dict | list | str | Path, under=Path.home(), destructive=True):
"""
Interprets the given structure as a tree to glob,
removing everything that matches.
Nested subjects have their paths joined.
"""
# just recurse down until we have something that is not a collection (→ a str or Path)
if isinstance(subject, dict):
for mid, next in subject.items():
clean(
next,
under = under / mid,
destructive = destructive,
)
return
elif isinstance(subject, list):
for next in subject:
clean(next, under=under, destructive=destructive)
return
# alright, it's not a collection!
# then let's instantiate the glob and remove each yielded path
full = str(under / subject)
matches = glob.iglob(full, recursive=True)
for target in matches:
print(target)
if destructive:
remove(Path(target))
def remove(target: Path):
"""Removes whatever is under the given path."""
if target.is_dir():
shutil.rmtree(target)
else:
os.remove(target)
if __name__ == "__main__":
main()