Passed
Push — main ( 9b73b1...666399 )
by Peter
01:12
created

pyclean.main   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

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