-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrf.py
More file actions
263 lines (214 loc) · 8.12 KB
/
rf.py
File metadata and controls
263 lines (214 loc) · 8.12 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#! /usr/bin/env python
"""Script to remove temporary files
The script contains a known list of globs for temporary files
"""
from configparser import ConfigParser
import argparse
import bdb
import fnmatch
import os
import shutil
import sys
from pysyte.types import paths
def has_true(value):
return value.lower() in ["true", "t", "yes", "y", "1"]
def default_options():
options = {
"all": False,
"recursive": False,
"quiet": False,
"temporary": True,
"Trial-Run": False,
}
globs = {
"development": "tags a.out *.log *.retry",
"old": "*.old",
"swaps": ".*.sw[a-z] .sw[a-z] *.recovered",
"python": "*.pyc *.pyo *.fail *$py.class *.profile __pycache__ htmlcov *.egg-info",
"temporary": "*.bak *.orig temp.* *.tmp *~ .*~ fred fred.* mary mary.* one two tmp*-vim.sh *.retry out.log out.err *-e =*",
"big": "*.hprof",
"extras": ".venv .tox dist .pytest_cache .mypy_cache .coverage .coverage.* coverage.xml .ruff_cache",
}
return options, globs
def read_configuration():
def path_to_config() -> paths.FilePath:
module_name, _ = os.path.splitext(os.path.basename(__file__))
path = paths.path(f"~/.config/{module_name}/config")
return os.path.expanduser(f"~/.config/{module_name}/config")
path = path_to_config()
if not os.path.isfile(path):
return default_options()
parser = ConfigParser()
parser.read(path)
options = {k: has_true(v) for k, v in parser.items("options")}
globs = dict(parser.items("globs"))
return options, globs
def as_configuration_name(name):
return "-".join([s.lower() for s in name.split("-")])
def compare_options(a, b):
if a[0][0].isupper() and b[0][0].islower():
return +1
if a[0][0].islower() and b[0][0].isupper():
return -1
if a[0] > b[0]:
return +1
if a[0] < b[0]:
return -1
return 0
def get_help_text(configured_globs):
all_glob_names = ", ".join([k for k in configured_globs.keys()])
explanations = [
("all", "remove all (%s)" % all_glob_names),
("recursive", "remove from subdirectories too"),
("quiet", "do not show files being removed"),
("Trial-Run", "show which files would be removed, but do nothing"),
]
explanation_names = [a for a, _ in explanations]
glob_explations = [
(name, 'remove "%s"' % value)
for name, value in configured_globs.items()
if name not in explanation_names
]
try:
return sorted(explanations + glob_explations, cmp=compare_options)
except TypeError:
return sorted(explanations + glob_explations, key=lambda x: x[0])
def add_argument(parser, name, default, explanation):
letter = "-%s" % name[0]
word = "--%s" % name
action = "store_true"
if default:
explanation = "do not %s" % explanation
action = "store_false"
parser.add_argument(letter, word, action=action, default=default, help=explanation)
def add_arguments(parser, configured_options, configured_globs):
explanations = get_help_text(configured_globs)
for name, explanation in explanations:
configuration_name = as_configuration_name(name)
default = configured_options.get(configuration_name, False)
add_argument(parser, name, default, explanation)
def wanted_globs(options, configured_globs):
"""A list of globs for all files to be deleted"""
result = []
for key, value in configured_globs.items():
# Special handling for extras - only include if both python and extras are set
if key == "extras":
if getattr(options, "python", False) and getattr(options, "extras", False):
result.extend(value.split())
elif getattr(options, key, False):
result.extend(value.split())
return result
def get_names_in(directory):
"""A list of names for all items in that directory"""
try:
return os.listdir(directory)
except (IOError, OSError):
return []
def get_paths_in(directory, glob):
"""A list of all items in that directory, which match that glob"""
names = get_names_in(directory)
names = fnmatch.filter(names, glob)
return [os.path.join(directory, name) for name in names]
def get_paths_under(directory, glob):
"""Get a list of directories under that directory, matching those globs"""
result = []
for name in get_names_in(directory):
if name in (".git", ".idea", ".venv", ".tox", ".pytest_cache"):
continue
path = os.path.join(directory, name)
if 'egg' in name and 'egg' in glob:
pass
if fnmatch.fnmatch(name, glob):
result.append(path)
elif os.path.isdir(path):
if os.path.realpath(directory).startswith(os.path.realpath(path)):
continue
more = get_paths_under(path, glob)
result.extend(more)
return result
def get_files_under(directory, globs):
"""Get a list of files under that directory, matching those globs"""
paths = get_paths_under(directory, globs)
return [_ for _ in paths if os.path.isfile(_)]
def get_files_in(directory, globs):
"""Get a list of files in that directory, matching those globs"""
paths = get_paths_in(directory, globs)
return [_ for _ in paths if os.path.isfile(_)]
def get_paths(directory, globs, recursive, filter_):
"""Get a list of files under that directory, matching those globs"""
get_paths_ = get_paths_under if recursive else get_paths_in
result = []
for glob in globs:
paths = get_paths_(directory, glob)
result.extend([_ for _ in paths if filter_(_)])
return result
def remove_paths(paths: list[str], quiet: bool, trial_run: bool) -> int:
"""Remove all those paths
Print out each path removed, unless quiet is True
Do not actually delete if trial_run is True
If deleting paths leaves directories empty, delete them too
"""
dirs = []
result = os.EX_OK
for path in paths:
try:
if not trial_run:
if os.path.exists(path):
remover = os.remove if os.path.isfile(path) else shutil.rmtree
try:
remover(path)
dirs.append(os.path.dirname(path))
except NotADirectoryError:
pass
if not quiet:
print(path)
except (IOError, OSError) as e:
print(e)
result = os.EX_IOERR
for dir_ in dirs:
if not os.listdir(dir_):
try:
os.removedirs(dir_)
except NotADirectoryError:
continue
if not quiet:
print(dir_)
return result
def script(paths, args, globs):
"""Run the script"""
result = os.EX_OK
for path_ in paths:
sub_paths = get_paths(path_, globs, args.recursive, os.path.exists)
remove_result = remove_paths(sub_paths, args.quiet, args.Trial_Run)
if remove_result != os.EX_OK:
result = remove_result
return result
def parse_options():
"""Find out what user wants at command line"""
configured_options, configured_globs = read_configuration()
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
add_arguments(parser, configured_options, configured_globs)
parser.add_argument(
"paths", default=["."], nargs="*", help="paths to clean (default .)"
)
args = parser.parse_args(None)
globs = wanted_globs(args, configured_globs)
paths = args.paths
delattr(args, "paths")
if args.all:
_ = [setattr(args, name, True) for name in configured_globs.keys()]
if args.quiet and args.Trial_Run:
raise NotImplementedError("Using --quiet and --Trial-Run: Do nothing")
return paths, args, globs
def main():
try:
paths, args, globs = parse_options()
return script(paths, args, globs)
except bdb.BdbQuit:
return os.EX_OK
except NotImplementedError as e:
breakpoint()
print(e, file=sys.stderr)
return os.EX_USAGE
if __name__ == "__main__":
sys.exit(main())