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

pyclean.modern.should_ignore()   C

Complexity

Conditions 10

Size

Total Lines 42
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 42
rs 5.9999
c 0
b 0
f 0
cc 10
nop 2

How to fix   Complexity   

Complexity

Complex classes like pyclean.modern.should_ignore() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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
      and also ignores everything inside that directory
101
102
    Args:
103
        path: Path object to check
104
        ignore_patterns: List of ignore patterns
105
106
    Returns:
107
        True if the path should be ignored, False otherwise
108
    """
109
    if ignore_patterns is None:
110
        return False
111
    if not ignore_patterns:
112
        return False
113
114
    for pattern in ignore_patterns:
115
        if '/' in pattern:
116
            # Pattern contains path separator - match relative path
117
            # Check if the pattern matches anywhere in the path hierarchy
118
            try:
119
                # Get parts from the pattern
120
                pattern_parts = Path(pattern).parts
121
                # Path must have at least as many parts as the pattern
122
                if len(path.parts) < len(pattern_parts):
123
                    continue
124
                # Check if pattern matches at any position in the path
125
                for i in range(len(path.parts) - len(pattern_parts) + 1):
126
                    path_slice = path.parts[i:i + len(pattern_parts)]
127
                    if path_slice == pattern_parts:
128
                        return True
129
            except (ValueError, IndexError):
130
                continue
131
        # Simple name - match the directory name anywhere
132
        elif path.name == pattern:
133
            return True
134
    return False
135
136
137
def remove_file(fileobj):
138
    """Attempt to delete a file object for real."""
139
    log.debug('Deleting file: %s', fileobj)
140
    try:
141
        fileobj.unlink()
142
        Runner.unlink_count += 1
143
    except OSError as err:
144
        log.debug('File not deleted. %s', err)
145
        Runner.unlink_failed += 1
146
147
148
def remove_directory(dirobj):
149
    """Attempt to remove a directory object for real."""
150
    log.debug('Removing directory: %s', dirobj)
151
    try:
152
        dirobj.rmdir()
153
        Runner.rmdir_count += 1
154
    except OSError as err:
155
        log.debug('Directory not removed. %s', err)
156
        Runner.rmdir_failed += 1
157
158
159
def print_filename(fileobj):
160
    """Only display the file name, used with --dry-run."""
161
    log.debug('Would delete file: %s', fileobj)
162
    Runner.unlink_count += 1
163
164
165
def print_dirname(dirobj):
166
    """Only display the directory name, used with --dry-run."""
167
    log.debug('Would delete directory: %s', dirobj)
168
    Runner.rmdir_count += 1
169
170
171
def pyclean(args):
172
    """Cross-platform cleaning of Python bytecode."""
173
    Runner.configure(args)
174
175
    for dir_name in args.directory:
176
        dir_path = Path(dir_name)
177
178
        log.info('Cleaning directory %s', dir_path)
179
        descend_and_clean(dir_path, BYTECODE_FILES, BYTECODE_DIRS)
180
181
        for topic in args.debris:
182
            remove_debris_for(topic, dir_path)
183
184
        remove_freeform_targets(args.erase, args.yes, dir_path)
185
186
    log.info(
187
        'Total %d files, %d directories %s.',
188
        Runner.unlink_count,
189
        Runner.rmdir_count,
190
        'would be removed' if args.dry_run else 'removed',
191
    )
192
193
    if Runner.unlink_failed or Runner.rmdir_failed:
194
        log.debug(
195
            '%d files, %d directories %s not be removed.',
196
            Runner.unlink_failed,
197
            Runner.rmdir_failed,
198
            'would' if args.dry_run else 'could',
199
        )
200
201
202
def descend_and_clean(directory, file_types, dir_names):
203
    """
204
    Walk and descend a directory tree, cleaning up files of a certain type
205
    along the way. Only delete directories if they are empty, in the end.
206
    """
207
    for child in sorted(directory.iterdir()):
208
        if child.is_file():
209
            if child.suffix in file_types:
210
                Runner.unlink(child)
211
        elif child.is_dir():
212
            if should_ignore(child, Runner.ignore):
213
                log.debug('Skipping %s', child)
214
            else:
215
                descend_and_clean(child, file_types, dir_names)
216
217
            if child.name in dir_names:
218
                Runner.rmdir(child)
219
        else:
220
            log.debug('Ignoring %s (neither a file nor a folder)', child)
221
222
223
def remove_debris_for(topic, directory):
224
    """
225
    Clean up debris for a specific topic.
226
    """
227
    log.debug('Scanning for debris of %s ...', topic.title())
228
229
    for path_glob in DEBRIS_TOPICS[topic]:
230
        delete_filesystem_objects(directory, path_glob, recursive=True)
231
232
233
def remove_freeform_targets(glob_patterns, yes, directory):
234
    """
235
    Remove free-form targets using globbing.
236
237
    This is **potentially dangerous** since users can delete everything
238
    anywhere in their file system, including the entire project they're
239
    working on. For this reason, the implementation imposes the following
240
    (user experience-related) restrictions:
241
242
    - Deleting (directories) is not recursive, directory contents must be
243
      explicitly specified using globbing (e.g. ``dirname/**/*``).
244
    - The user is responsible for the deletion order, so that a directory
245
      is empty when it is attempted to be deleted.
246
    - A confirmation prompt for the deletion of every single file system
247
      object is shown (unless the ``--yes`` option is used, in addition).
248
    """
249
    for path_glob in glob_patterns:
250
        log.debug('Erase file system objects matching: %s', path_glob)
251
        delete_filesystem_objects(directory, path_glob, prompt=not yes)
252
253
254
def delete_filesystem_objects(directory, path_glob, prompt=False, recursive=False):
255
    """
256
    Identifies all pathnames matching a specific glob pattern, and attempts
257
    to delete them in the proper order, optionally asking for confirmation.
258
259
    Implementation Note: We sort the file system objects in *reverse order*
260
    and first delete *all files* before removing directories. This way we
261
    make sure that the directories that are deepest down in the hierarchy
262
    are empty (for both files & directories) when we attempt to remove them.
263
    """
264
    all_names = sorted(directory.glob(path_glob), reverse=True)
265
    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...
266
    files = (name for name in all_names if not name.is_dir() or name.is_symlink())
267
268
    for file_object in files:
269
        file_type = 'symlink' if file_object.is_symlink() else 'file'
270
        if prompt and not confirm('Delete %s %s' % (file_type, file_object)):
271
            Runner.unlink_failed += 1
272
            continue
273
        Runner.unlink(file_object)
274
275
    for dir_object in dirs:
276
        if prompt and not confirm('Remove empty directory %s' % dir_object):
277
            Runner.rmdir_failed += 1
278
            continue
279
        Runner.rmdir(dir_object)
280
281
    if recursive:
282
        subdirs = (Path(name.path) for name in os.scandir(directory) if name.is_dir())
283
        for subdir in subdirs:
284
            if should_ignore(subdir, Runner.ignore):
285
                log.debug('Skipping %s', subdir)
286
            else:
287
                delete_filesystem_objects(subdir, path_glob, prompt, recursive)
288
289
290
def confirm(message):
291
    """An interactive confirmation prompt."""
292
    try:
293
        answer = input('%s? ' % message)
294
        return answer.strip().lower() in ['y', 'yes']
295
    except KeyboardInterrupt:
296
        msg = 'Aborted by user.'
297
        raise SystemExit(msg)
298