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

pyclean.main   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 77
dl 0
loc 107
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
C pyclean() 0 43 11
1
# SPDX-FileCopyrightText: 2020 Peter Bittner <[email protected]>
2
#
3
# SPDX-License-Identifier: GPL-3.0-or-later
4
5
"""
6
Main orchestration of the pyclean cleanup process.
7
"""
8
9
import logging
10
from pathlib import Path
11
12
from .bytecode import BYTECODE_DIRS, BYTECODE_FILES
13
from .debris import (
14
    DEBRIS_TOPICS,
15
    detect_debris_in_directory,
16
    recursive_delete_debris,
17
    remove_debris_for,
18
    suggest_debris_option,
19
)
20
from .erase import confirm, delete_filesystem_objects, remove_freeform_targets
21
from .folders import remove_empty_directories
22
from .gitclean import GIT_FATAL_ERROR, build_git_clean_command, 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
33
log = logging.getLogger(__name__)
34
35
36
__all__ = [
37
    'BYTECODE_DIRS',
38
    'BYTECODE_FILES',
39
    'DEBRIS_TOPICS',
40
    'GIT_FATAL_ERROR',
41
    'CleanupRunner',
42
    'Runner',
43
    'build_git_clean_command',
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
def pyclean(args):
65
    """Cross-platform cleaning of Python bytecode."""
66
    Runner.configure(args)
67
68
    for dir_name in args.directory:
69
        dir_path = Path(dir_name)
70
71
        log.info('Cleaning directory %s', dir_path)
72
        descend_and_clean(dir_path, BYTECODE_FILES, BYTECODE_DIRS)
73
74
        for topic in args.debris:
75
            remove_debris_for(topic, dir_path)
76
77
        remove_freeform_targets(dir_path, args.erase, args.yes, args.dry_run)
78
79
        if args.folders:
80
            log.debug('Removing empty directories...')
81
            remove_empty_directories(dir_path)
82
83
        if args.git_clean:
84
            execute_git_clean(dir_path, args)
85
86
    git_clean_note = ' (Not counting git clean)' if args.git_clean else ''
87
88
    log.info(
89
        'Total %d files, %d directories %s.%s',
90
        Runner.unlink_count,
91
        Runner.rmdir_count,
92
        'would be removed' if args.dry_run else 'removed',
93
        git_clean_note,
94
    )
95
96
    if Runner.unlink_failed or Runner.rmdir_failed:
97
        log.debug(
98
            '%d files, %d directories %s not be removed.%s',
99
            Runner.unlink_failed,
100
            Runner.rmdir_failed,
101
            'would' if args.dry_run else 'could',
102
            git_clean_note,
103
        )
104
105
    if not args.debris:
106
        suggest_debris_option(args)
107