Passed
Pull Request — main (#93)
by
unknown
01:12
created

pyclean.modern.should_ignore()   C

Complexity

Conditions 9

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 36
rs 6.6666
c 0
b 0
f 0
cc 9
nop 2
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 _normalize_pattern(pattern: str) -> str:
94
    """
95
    Normalize path separators in a pattern for cross-platform support.
96
97
    On Windows, both forward slash and backslash are valid path separators.
98
    On Unix/Posix, only forward slash is valid (backslash can be part of filename).
99
    """
100
    return pattern.replace(os.sep, os.altsep or os.sep)
101
102
103
def should_ignore(path: Path, ignore_patterns: list) -> bool:
104
    """
105
    Check if a path should be ignored based on ignore patterns.
106
107
    Patterns can be:
108
    - Simple names like 'bar': matches any directory with that name
109
    - Paths like 'foo/bar': matches 'bar' directory inside 'foo' directory
110
      and also ignores everything inside that directory
111
    """
112
    if not ignore_patterns:
113
        return False
114
115
    for pattern in ignore_patterns:
116
        normalized_pattern = _normalize_pattern(pattern)
117
118
        # Check if pattern has multiple components (is a path)
119
        pattern_path = Path(normalized_pattern)
120
        if len(pattern_path.parts) > 1:
121
            # Pattern contains path separator - match relative path
122
            # Check if the pattern matches anywhere in the path hierarchy
123
            try:
124
                pattern_parts = pattern_path.parts
125
                # Path must have at least as many parts as the pattern
126
                if len(path.parts) < len(pattern_parts):
127
                    continue
128
                # Check if pattern matches at any position in the path
129
                for i in range(len(path.parts) - len(pattern_parts) + 1):
130
                    path_slice = path.parts[i : i + len(pattern_parts)]
131
                    if path_slice == pattern_parts:
132
                        return True
133
            except (ValueError, IndexError):
134
                continue
135
        # Simple name - match the directory name anywhere
136
        elif path.name == pattern:
137
            return True
138
    return False
139
140
141
def remove_file(fileobj):
142
    """Attempt to delete a file object for real."""
143
    log.debug('Deleting file: %s', fileobj)
144
    try:
145
        fileobj.unlink()
146
        Runner.unlink_count += 1
147
    except OSError as err:
148
        log.debug('File not deleted. %s', err)
149
        Runner.unlink_failed += 1
150
151
152
def remove_directory(dirobj):
153
    """Attempt to remove a directory object for real."""
154
    log.debug('Removing directory: %s', dirobj)
155
    try:
156
        dirobj.rmdir()
157
        Runner.rmdir_count += 1
158
    except OSError as err:
159
        log.debug('Directory not removed. %s', err)
160
        Runner.rmdir_failed += 1
161
162
163
def print_filename(fileobj):
164
    """Only display the file name, used with --dry-run."""
165
    log.debug('Would delete file: %s', fileobj)
166
    Runner.unlink_count += 1
167
168
169
def print_dirname(dirobj):
170
    """Only display the directory name, used with --dry-run."""
171
    log.debug('Would delete directory: %s', dirobj)
172
    Runner.rmdir_count += 1
173
174
175
def pyclean(args):
176
    """Cross-platform cleaning of Python bytecode."""
177
    Runner.configure(args)
178
179
    for dir_name in args.directory:
180
        dir_path = Path(dir_name)
181
182
        log.info('Cleaning directory %s', dir_path)
183
        descend_and_clean(dir_path, BYTECODE_FILES, BYTECODE_DIRS)
184
185
        for topic in args.debris:
186
            remove_debris_for(topic, dir_path)
187
188
        remove_freeform_targets(args.erase, args.yes, dir_path)
189
190
    log.info(
191
        'Total %d files, %d directories %s.',
192
        Runner.unlink_count,
193
        Runner.rmdir_count,
194
        'would be removed' if args.dry_run else 'removed',
195
    )
196
197
    if Runner.unlink_failed or Runner.rmdir_failed:
198
        log.debug(
199
            '%d files, %d directories %s not be removed.',
200
            Runner.unlink_failed,
201
            Runner.rmdir_failed,
202
            'would' if args.dry_run else 'could',
203
        )
204
205
    # Suggest --debris option if it wasn't used
206
    if not args.debris:
207
        suggest_debris_option(args)
208
209
210
def descend_and_clean(directory, file_types, dir_names):
211
    """
212
    Walk and descend a directory tree, cleaning up files of a certain type
213
    along the way. Only delete directories if they are empty, in the end.
214
    """
215
    for child in sorted(directory.iterdir()):
216
        if child.is_file():
217
            if child.suffix in file_types:
218
                Runner.unlink(child)
219
        elif child.is_dir():
220
            if should_ignore(child, Runner.ignore):
221
                log.debug('Skipping %s', child)
222
            else:
223
                descend_and_clean(child, file_types, dir_names)
224
225
            if child.name in dir_names:
226
                Runner.rmdir(child)
227
        else:
228
            log.debug('Ignoring %s (neither a file nor a folder)', child)
229
230
231
def remove_debris_for(topic, directory):
232
    """
233
    Clean up debris for a specific topic.
234
    """
235
    log.debug('Scanning for debris of %s ...', topic.title())
236
237
    for path_glob in DEBRIS_TOPICS[topic]:
238
        delete_filesystem_objects(directory, path_glob, recursive=True)
239
240
241
def remove_freeform_targets(glob_patterns, yes, directory):
242
    """
243
    Remove free-form targets using globbing.
244
245
    This is **potentially dangerous** since users can delete everything
246
    anywhere in their file system, including the entire project they're
247
    working on. For this reason, the implementation imposes the following
248
    (user experience-related) restrictions:
249
250
    - Deleting (directories) is not recursive, directory contents must be
251
      explicitly specified using globbing (e.g. ``dirname/**/*``).
252
    - The user is responsible for the deletion order, so that a directory
253
      is empty when it is attempted to be deleted.
254
    - A confirmation prompt for the deletion of every single file system
255
      object is shown (unless the ``--yes`` option is used, in addition).
256
    """
257
    for path_glob in glob_patterns:
258
        log.debug('Erase file system objects matching: %s', path_glob)
259
        delete_filesystem_objects(directory, path_glob, prompt=not yes)
260
261
262
def delete_filesystem_objects(directory, path_glob, prompt=False, recursive=False):
263
    """
264
    Identifies all pathnames matching a specific glob pattern, and attempts
265
    to delete them in the proper order, optionally asking for confirmation.
266
267
    Implementation Note: We sort the file system objects in *reverse order*
268
    and first delete *all files* before removing directories. This way we
269
    make sure that the directories that are deepest down in the hierarchy
270
    are empty (for both files & directories) when we attempt to remove them.
271
    """
272
    all_names = sorted(directory.glob(path_glob), reverse=True)
273
    dirs = (name for name in all_names if name.is_dir() and not name.is_symlink())
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable name does not seem to be defined.
Loading history...
274
    files = (name for name in all_names if not name.is_dir() or name.is_symlink())
275
276
    for file_object in files:
277
        file_type = 'symlink' if file_object.is_symlink() else 'file'
278
        if prompt and not confirm('Delete %s %s' % (file_type, file_object)):
279
            Runner.unlink_failed += 1
280
            continue
281
        Runner.unlink(file_object)
282
283
    for dir_object in dirs:
284
        if prompt and not confirm('Remove empty directory %s' % dir_object):
285
            Runner.rmdir_failed += 1
286
            continue
287
        Runner.rmdir(dir_object)
288
289
    if recursive:
290
        subdirs = (Path(name.path) for name in os.scandir(directory) if name.is_dir())
291
        for subdir in subdirs:
292
            if should_ignore(subdir, Runner.ignore):
293
                log.debug('Skipping %s', subdir)
294
            else:
295
                delete_filesystem_objects(subdir, path_glob, prompt, recursive)
296
297
298
def confirm(message):
299
    """An interactive confirmation prompt."""
300
    try:
301
        answer = input('%s? ' % message)
302
        return answer.strip().lower() in ['y', 'yes']
303
    except KeyboardInterrupt:
304
        msg = 'Aborted by user.'
305
        raise SystemExit(msg)
306
307
308
def detect_debris_in_directory(directory):
309
    """
310
    Scan a directory for debris artifacts and return a list of detected topics.
311
    """
312
    detected_topics = []
313
314
    for topic, patterns in DEBRIS_TOPICS.items():
315
        for pattern in patterns:
316
            # Skip patterns that are for recursive cleanup (contain **)
317
            if '**' in pattern:
318
                continue
319
            # Check if the pattern matches anything in the directory
320
            matches = list(directory.glob(pattern))
321
            if matches:
322
                detected_topics.append(topic)
323
                break  # Found at least one match for this topic, move to next
324
325
    return detected_topics
326
327
328
def suggest_debris_option(args):
329
    """
330
    Suggest using the --debris option when it wasn't used.
331
    Optionally provide targeted suggestions based on detected artifacts.
332
    """
333
    # Collect all detected debris topics across all directories
334
    all_detected = set()
335
    for dir_name in args.directory:
336
        dir_path = Path(dir_name)
337
        if dir_path.exists():
338
            detected = detect_debris_in_directory(dir_path)
339
            all_detected.update(detected)
340
341
    if all_detected:
342
        # Provide targeted suggestion
343
        topics_str = ' '.join(sorted(all_detected))
344
        log.info(
345
            'Hint: Use --debris to also clean up build artifacts. Detected: %s',
346
            topics_str,
347
        )
348
    else:
349
        # Provide general suggestion
350
        log.info(
351
            'Hint: Use --debris to also clean up build artifacts '
352
            'from common Python development tools.',
353
        )
354