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

pyclean.main   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 41
dl 0
loc 66
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 remove_debris_for, suggest_debris_option
14
from .erase import remove_freeform_targets
15
from .folders import remove_empty_directories
16
from .gitclean import execute_git_clean
17
from .runner import Runner
18
from .traversal import descend_and_clean
19
20
log = logging.getLogger(__name__)
21
22
23
def pyclean(args):
24
    """Cross-platform cleaning of Python bytecode."""
25
    Runner.configure(args)
26
27
    for dir_name in args.directory:
28
        dir_path = Path(dir_name)
29
30
        log.info('Cleaning directory %s', dir_path)
31
        descend_and_clean(dir_path, BYTECODE_FILES, BYTECODE_DIRS)
32
33
        for topic in args.debris:
34
            remove_debris_for(topic, dir_path)
35
36
        remove_freeform_targets(dir_path, args.erase, args.yes, args.dry_run)
37
38
        if args.folders:
39
            log.debug('Removing empty directories...')
40
            remove_empty_directories(dir_path)
41
42
        if args.git_clean:
43
            execute_git_clean(dir_path, args)
44
45
    git_clean_note = ' (Not counting git clean)' if args.git_clean else ''
46
47
    log.info(
48
        'Total %d files, %d directories %s.%s',
49
        Runner.unlink_count,
50
        Runner.rmdir_count,
51
        'would be removed' if args.dry_run else 'removed',
52
        git_clean_note,
53
    )
54
55
    if Runner.unlink_failed or Runner.rmdir_failed:
56
        log.debug(
57
            '%d files, %d directories %s not be removed.%s',
58
            Runner.unlink_failed,
59
            Runner.rmdir_failed,
60
            'would' if args.dry_run else 'could',
61
            git_clean_note,
62
        )
63
64
    if not args.debris:
65
        suggest_debris_option(args)
66