Passed
Push — main ( a88161...fcda73 )
by Peter
01:16
created

pyclean.modern.delete_filesystem_objects()   C

Complexity

Conditions 10

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 31
rs 5.9999
c 0
b 0
f 0
cc 10
nop 4

How to fix   Complexity   

Complexity

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