-
Notifications
You must be signed in to change notification settings - Fork 3
/
clean.py
executable file
·148 lines (114 loc) · 4.34 KB
/
clean.py
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
#!/usr/bin/env python3
################################################################################
#### ABOUT: ####
#### remove old renders, while leaving those that are meant to be there ####
################################################################################
def main():
RENDER_DIR = "Renders" # default: "Renders"
MANIFEST_FILE = "MANIFEST.json" # default: "MANIFEST.json"
################################################################################
#### You shouldn't need to ever edit anything below this comment. ####
################################################################################
VERSION = "0.0.2"
import subprocess
import argparse
import json
import os.path
import shutil
import sys
import logging
parser = argparse.ArgumentParser(
description="Remove old renders, but keep current ones.",
prog="PPAU-Graphics Render Cleanup",
epilog="For a complete cleanup, simply remove your render directory...",
)
parser.add_argument(
"-d",
"--render_dir",
dest="render_dir",
action="store",
default=RENDER_DIR,
help="the directory containing the rendered files",
)
parser.add_argument(
"-m",
"--manifest-file",
dest="manifest",
action="store",
default=MANIFEST_FILE,
help="the manifest JSON as output by render.py",
)
parser.add_argument("--version", action="version", version="%(prog)s " + VERSION)
parser.add_argument("--verbose", action="count", help="tell me more", default=0)
parser.add_argument("--quiet", action="count", help="tell me less", default=0)
parser.add_argument(
"--log",
type=argparse.FileType("a"),
default=sys.stderr,
help="file to log to (default: stderr)",
)
arguments = parser.parse_args()
__loglevel = logging.INFO
if arguments.quiet > arguments.verbose:
__loglevel = logging.WARNING
elif arguments.quiet < arguments.verbose:
__loglevel = logging.DEBUG
logging.basicConfig(
stream=arguments.log,
level=__loglevel,
format="%(levelname)s: %(message)s"
if arguments.log.name == "<stderr>"
else "%(asctime)s %(levelname)s: %(message)s",
)
def printv(*args, sep=" ", **kwargs):
logging.debug(sep.join([str(x) for x in args]), **kwargs)
def printq(*args, sep=" ", **kwargs):
logging.info(sep.join([str(x) for x in args]), **kwargs)
def failure(*args, sep=" ", code=1, **kwargs):
logging.critical(sep.join([str(x) for x in args]), **kwargs)
sys.exit(code)
# we don't remove files, we remove whole subdirectories that aren't in the manifest
# manifest is keyed by e.g. a/b/c where a and b are directories, c is a source file name
keep_folders = set()
with open(arguments.manifest, "r") as mani:
jm = json.load(mani)
for k in jm.keys():
keep_folders.add(os.path.dirname(k))
folders = (
subprocess.run(
["find", RENDER_DIR, "-type", "d"],
stdout=subprocess.PIPE,
universal_newlines=True,
)
.stdout.strip()
.split(sep="\n")
)
total_deletions = 0
total_skips = 0
for f in folders:
# remove Renders/ or equivalent off the front
fbit = os.path.relpath(f, start=arguments.render_dir)
if len(fbit) == 0 or fbit == ".":
continue
if fbit not in keep_folders:
# it's possible that there are one or more subdirectories of fbit
# that *are* in keep_folders though; if so, keep fbit
subdir_ok = False
for kf in keep_folders:
if kf.startswith(fbit):
subdir_ok = True
break
if subdir_ok:
printv("skipping", fbit)
total_skips += 1
continue
else:
printv("Deleting:", f)
try:
shutil.rmtree(f)
except Exception as e:
printv(e)
total_deletions += 1
printq("Performed", total_deletions, "deletions", "and skipped", total_skips)
if __name__ == "__main__":
main()