Passed
Pull Request — main (#107)
by
unknown
01:14
created

pyclean.modern.execute_git_clean()   A

Complexity

Conditions 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nop 2
dl 0
loc 17
rs 9.85
c 0
b 0
f 0
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 subprocess
11
from pathlib import Path
12
13
from .bytecode import BYTECODE_DIRS, BYTECODE_FILES
14
from .debris import (
15
    DEBRIS_TOPICS,
16
    detect_debris_in_directory,
17
    recursive_delete_debris,
18
    remove_debris_for,
19
    suggest_debris_option,
20
)
21
from .folders import remove_empty_directories
22
from .gitclean import GIT_FATAL_ERROR, execute_git_clean
23
from .runner import (
24
    CleanupRunner,
25
    Runner,
26
    print_dirname,
27
    print_filename,
28
    remove_directory,
29
    remove_file,
30
)
31
from .traversal import descend_and_clean, normalize, should_ignore
32
from .usability import confirm, delete_filesystem_objects, remove_freeform_targets
33
34
log = logging.getLogger(__name__)
35
36
37
__all__ = [
38
    'BYTECODE_DIRS',
39
    'BYTECODE_FILES',
40
    'CleanupRunner',
41
    'DEBRIS_TOPICS',
42
    'GIT_FATAL_ERROR',
43
    'Runner',
44
    'confirm',
45
    'delete_filesystem_objects',
46
    'descend_and_clean',
47
    'detect_debris_in_directory',
48
    'execute_git_clean',
49
    'normalize',
50
    'print_dirname',
51
    'print_filename',
52
    'pyclean',
53
    'recursive_delete_debris',
54
    'remove_debris_for',
55
    'remove_directory',
56
    'remove_empty_directories',
57
    'remove_file',
58
    'remove_freeform_targets',
59
    'should_ignore',
60
    'suggest_debris_option',
61
]
62
63
64
65
66
def pyclean(args):
67
    Runner.configure(args)
68
69
    for dir_name in args.directory:
70
        dir_path = Path(dir_name)
71
72
        log.info('Cleaning directory %s', dir_path)
73
        descend_and_clean(dir_path, BYTECODE_FILES, BYTECODE_DIRS)
74
75
        for topic in args.debris:
76
            remove_debris_for(topic, dir_path)
77
78
        remove_freeform_targets(dir_path, args.erase, args.yes, args.dry_run)
79
80
        if args.folders:
81
            log.debug('Removing empty directories...')
82
            remove_empty_directories(dir_path)
83
84
        if args.git_clean:
85
            execute_git_clean(dir_path, args)
86
87
    git_clean_note = ' (Not counting git clean)' if args.git_clean else ''
88
89
    log.info(
90
        'Total %d files, %d directories %s.%s',
91
        Runner.unlink_count,
92
        Runner.rmdir_count,
93
        'would be removed' if args.dry_run else 'removed',
94
        git_clean_note,
95
    )
96
97
    if Runner.unlink_failed or Runner.rmdir_failed:
98
        log.debug(
99
            '%d files, %d directories %s not be removed.%s',
100
            Runner.unlink_failed,
101
            Runner.rmdir_failed,
102
            'would' if args.dry_run else 'could',
103
            git_clean_note,
104
        )
105
106
    if not args.debris:
107
        suggest_debris_option(args)
108
109