1
|
|
|
# SPDX-FileCopyrightText: 2020 Peter Bittner <[email protected]> |
2
|
|
|
# |
3
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later |
4
|
|
|
|
5
|
|
|
""" |
6
|
|
|
Modern, cross-platform, pure-Python pyclean implementation. |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
import logging |
10
|
|
|
import os |
11
|
|
|
from pathlib import Path |
12
|
|
|
|
13
|
|
|
BYTECODE_FILES = ['.pyc', '.pyo'] |
14
|
|
|
BYTECODE_DIRS = ['__pycache__'] |
15
|
|
|
DEBRIS_TOPICS = { |
16
|
|
|
'cache': [ |
17
|
|
|
'.cache/**/*', |
18
|
|
|
'.cache/', |
19
|
|
|
], |
20
|
|
|
'coverage': [ |
21
|
|
|
'.coverage', |
22
|
|
|
'coverage.json', |
23
|
|
|
'coverage.lcov', |
24
|
|
|
'coverage.xml', |
25
|
|
|
'htmlcov/**/*', |
26
|
|
|
'htmlcov/', |
27
|
|
|
], |
28
|
|
|
'jupyter': [ |
29
|
|
|
'.ipynb_checkpoints/**/*', |
30
|
|
|
'.ipynb_checkpoints/', |
31
|
|
|
], |
32
|
|
|
'mypy': [ |
33
|
|
|
'.mypy_cache/**/*', |
34
|
|
|
'.mypy_cache/', |
35
|
|
|
], |
36
|
|
|
'package': [ |
37
|
|
|
'build/bdist.*/**/*', |
38
|
|
|
'build/bdist.*/', |
39
|
|
|
'build/lib/**/*', |
40
|
|
|
'build/lib/', |
41
|
|
|
'build/', |
42
|
|
|
'dist/**/*', |
43
|
|
|
'dist/', |
44
|
|
|
'sdist/**/*', |
45
|
|
|
'sdist/', |
46
|
|
|
'*.egg-info/**/*', |
47
|
|
|
'*.egg-info/', |
48
|
|
|
], |
49
|
|
|
'pytest': [ |
50
|
|
|
'.pytest_cache/**/*', |
51
|
|
|
'.pytest_cache/', |
52
|
|
|
'pytestdebug.log', |
53
|
|
|
], |
54
|
|
|
'ruff': [ |
55
|
|
|
'.ruff_cache/**/*', |
56
|
|
|
'.ruff_cache/', |
57
|
|
|
], |
58
|
|
|
'tox': [ |
59
|
|
|
'.tox/**/*', |
60
|
|
|
'.tox/', |
61
|
|
|
], |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
class CleanupRunner: |
66
|
|
|
"""Module-level configuration and value store.""" |
67
|
|
|
|
68
|
|
|
def __init__(self): |
69
|
|
|
"""Cleanup runner with optional dry-run behavior.""" |
70
|
|
|
self.unlink = None |
71
|
|
|
self.rmdir = None |
72
|
|
|
self.ignore = None |
73
|
|
|
self.unlink_count = None |
74
|
|
|
self.unlink_failed = None |
75
|
|
|
self.rmdir_count = None |
76
|
|
|
self.rmdir_failed = None |
77
|
|
|
|
78
|
|
|
def configure(self, args): |
79
|
|
|
"""Set up runner according to command line options.""" |
80
|
|
|
self.unlink = print_filename if args.dry_run else remove_file |
81
|
|
|
self.rmdir = print_dirname if args.dry_run else remove_directory |
82
|
|
|
self.ignore = args.ignore |
83
|
|
|
self.unlink_count = 0 |
84
|
|
|
self.unlink_failed = 0 |
85
|
|
|
self.rmdir_count = 0 |
86
|
|
|
self.rmdir_failed = 0 |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
log = logging.getLogger(__name__) |
90
|
|
|
Runner = CleanupRunner() |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
def should_ignore(path, ignore_patterns): |
94
|
|
|
""" |
95
|
|
|
Check if a path should be ignored based on ignore patterns. |
96
|
|
|
|
97
|
|
|
Patterns can be: |
98
|
|
|
- Simple names like 'bar': matches any directory with that name |
99
|
|
|
- Paths like 'foo/bar': matches 'bar' directory inside 'foo' directory |
100
|
|
|
|
101
|
|
|
Args: |
102
|
|
|
path: Path object to check |
103
|
|
|
ignore_patterns: List of ignore patterns |
104
|
|
|
|
105
|
|
|
Returns: |
106
|
|
|
True if the path should be ignored, False otherwise |
107
|
|
|
""" |
108
|
|
|
for pattern in ignore_patterns: |
109
|
|
|
if '/' in pattern: |
110
|
|
|
# Pattern contains path separator - match relative path |
111
|
|
|
# We need to check if the path ends with this pattern |
112
|
|
|
try: |
113
|
|
|
# Get parts from the pattern |
114
|
|
|
pattern_parts = Path(pattern).parts |
115
|
|
|
# Get the trailing parts of the path that match the pattern length |
116
|
|
|
path_parts = path.parts[-len(pattern_parts):] |
117
|
|
|
if path_parts == pattern_parts: |
118
|
|
|
return True |
119
|
|
|
except (ValueError, IndexError): |
120
|
|
|
continue |
121
|
|
|
else: |
122
|
|
|
# Simple name - match the directory name anywhere |
123
|
|
|
if path.name == pattern: |
124
|
|
|
return True |
125
|
|
|
return False |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
def remove_file(fileobj): |
129
|
|
|
"""Attempt to delete a file object for real.""" |
130
|
|
|
log.debug('Deleting file: %s', fileobj) |
131
|
|
|
try: |
132
|
|
|
fileobj.unlink() |
133
|
|
|
Runner.unlink_count += 1 |
134
|
|
|
except OSError as err: |
135
|
|
|
log.debug('File not deleted. %s', err) |
136
|
|
|
Runner.unlink_failed += 1 |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
def remove_directory(dirobj): |
140
|
|
|
"""Attempt to remove a directory object for real.""" |
141
|
|
|
log.debug('Removing directory: %s', dirobj) |
142
|
|
|
try: |
143
|
|
|
dirobj.rmdir() |
144
|
|
|
Runner.rmdir_count += 1 |
145
|
|
|
except OSError as err: |
146
|
|
|
log.debug('Directory not removed. %s', err) |
147
|
|
|
Runner.rmdir_failed += 1 |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
def print_filename(fileobj): |
151
|
|
|
"""Only display the file name, used with --dry-run.""" |
152
|
|
|
log.debug('Would delete file: %s', fileobj) |
153
|
|
|
Runner.unlink_count += 1 |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
def print_dirname(dirobj): |
157
|
|
|
"""Only display the directory name, used with --dry-run.""" |
158
|
|
|
log.debug('Would delete directory: %s', dirobj) |
159
|
|
|
Runner.rmdir_count += 1 |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
def pyclean(args): |
163
|
|
|
"""Cross-platform cleaning of Python bytecode.""" |
164
|
|
|
Runner.configure(args) |
165
|
|
|
|
166
|
|
|
for dir_name in args.directory: |
167
|
|
|
dir_path = Path(dir_name) |
168
|
|
|
|
169
|
|
|
log.info('Cleaning directory %s', dir_path) |
170
|
|
|
descend_and_clean(dir_path, BYTECODE_FILES, BYTECODE_DIRS) |
171
|
|
|
|
172
|
|
|
for topic in args.debris: |
173
|
|
|
remove_debris_for(topic, dir_path) |
174
|
|
|
|
175
|
|
|
remove_freeform_targets(args.erase, args.yes, dir_path) |
176
|
|
|
|
177
|
|
|
log.info( |
178
|
|
|
'Total %d files, %d directories %s.', |
179
|
|
|
Runner.unlink_count, |
180
|
|
|
Runner.rmdir_count, |
181
|
|
|
'would be removed' if args.dry_run else 'removed', |
182
|
|
|
) |
183
|
|
|
|
184
|
|
|
if Runner.unlink_failed or Runner.rmdir_failed: |
185
|
|
|
log.debug( |
186
|
|
|
'%d files, %d directories %s not be removed.', |
187
|
|
|
Runner.unlink_failed, |
188
|
|
|
Runner.rmdir_failed, |
189
|
|
|
'would' if args.dry_run else 'could', |
190
|
|
|
) |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
def descend_and_clean(directory, file_types, dir_names): |
194
|
|
|
""" |
195
|
|
|
Walk and descend a directory tree, cleaning up files of a certain type |
196
|
|
|
along the way. Only delete directories if they are empty, in the end. |
197
|
|
|
""" |
198
|
|
|
for child in sorted(directory.iterdir()): |
199
|
|
|
if child.is_file(): |
200
|
|
|
if child.suffix in file_types: |
201
|
|
|
Runner.unlink(child) |
202
|
|
|
elif child.is_dir(): |
203
|
|
|
if should_ignore(child, Runner.ignore): |
204
|
|
|
log.debug('Skipping %s', child) |
205
|
|
|
else: |
206
|
|
|
descend_and_clean(child, file_types, dir_names) |
207
|
|
|
|
208
|
|
|
if child.name in dir_names: |
209
|
|
|
Runner.rmdir(child) |
210
|
|
|
else: |
211
|
|
|
log.debug('Ignoring %s (neither a file nor a folder)', child) |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
def remove_debris_for(topic, directory): |
215
|
|
|
""" |
216
|
|
|
Clean up debris for a specific topic. |
217
|
|
|
""" |
218
|
|
|
log.debug('Scanning for debris of %s ...', topic.title()) |
219
|
|
|
|
220
|
|
|
for path_glob in DEBRIS_TOPICS[topic]: |
221
|
|
|
delete_filesystem_objects(directory, path_glob, recursive=True) |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
def remove_freeform_targets(glob_patterns, yes, directory): |
225
|
|
|
""" |
226
|
|
|
Remove free-form targets using globbing. |
227
|
|
|
|
228
|
|
|
This is **potentially dangerous** since users can delete everything |
229
|
|
|
anywhere in their file system, including the entire project they're |
230
|
|
|
working on. For this reason, the implementation imposes the following |
231
|
|
|
(user experience-related) restrictions: |
232
|
|
|
|
233
|
|
|
- Deleting (directories) is not recursive, directory contents must be |
234
|
|
|
explicitly specified using globbing (e.g. ``dirname/**/*``). |
235
|
|
|
- The user is responsible for the deletion order, so that a directory |
236
|
|
|
is empty when it is attempted to be deleted. |
237
|
|
|
- A confirmation prompt for the deletion of every single file system |
238
|
|
|
object is shown (unless the ``--yes`` option is used, in addition). |
239
|
|
|
""" |
240
|
|
|
for path_glob in glob_patterns: |
241
|
|
|
log.debug('Erase file system objects matching: %s', path_glob) |
242
|
|
|
delete_filesystem_objects(directory, path_glob, prompt=not yes) |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
def delete_filesystem_objects(directory, path_glob, prompt=False, recursive=False): |
246
|
|
|
""" |
247
|
|
|
Identifies all pathnames matching a specific glob pattern, and attempts |
248
|
|
|
to delete them in the proper order, optionally asking for confirmation. |
249
|
|
|
|
250
|
|
|
Implementation Note: We sort the file system objects in *reverse order* |
251
|
|
|
and first delete *all files* before removing directories. This way we |
252
|
|
|
make sure that the directories that are deepest down in the hierarchy |
253
|
|
|
are empty (for both files & directories) when we attempt to remove them. |
254
|
|
|
""" |
255
|
|
|
all_names = sorted(directory.glob(path_glob), reverse=True) |
256
|
|
|
dirs = (name for name in all_names if name.is_dir() and not name.is_symlink()) |
257
|
|
|
files = (name for name in all_names if not name.is_dir() or name.is_symlink()) |
258
|
|
|
|
259
|
|
|
for file_object in files: |
260
|
|
|
file_type = 'symlink' if file_object.is_symlink() else 'file' |
261
|
|
|
if prompt and not confirm('Delete %s %s' % (file_type, file_object)): |
262
|
|
|
Runner.unlink_failed += 1 |
263
|
|
|
continue |
264
|
|
|
Runner.unlink(file_object) |
265
|
|
|
|
266
|
|
|
for dir_object in dirs: |
267
|
|
|
if prompt and not confirm('Remove empty directory %s' % dir_object): |
268
|
|
|
Runner.rmdir_failed += 1 |
269
|
|
|
continue |
270
|
|
|
Runner.rmdir(dir_object) |
271
|
|
|
|
272
|
|
|
if recursive: |
273
|
|
|
subdirs = (Path(name.path) for name in os.scandir(directory) if name.is_dir()) |
274
|
|
|
for subdir in subdirs: |
275
|
|
|
if should_ignore(subdir, Runner.ignore): |
276
|
|
|
log.debug('Skipping %s', subdir) |
277
|
|
|
else: |
278
|
|
|
delete_filesystem_objects(subdir, path_glob, prompt, recursive) |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
def confirm(message): |
282
|
|
|
"""An interactive confirmation prompt.""" |
283
|
|
|
try: |
284
|
|
|
answer = input('%s? ' % message) |
285
|
|
|
return answer.strip().lower() in ['y', 'yes'] |
286
|
|
|
except KeyboardInterrupt: |
287
|
|
|
msg = 'Aborted by user.' |
288
|
|
|
raise SystemExit(msg) |
289
|
|
|
|