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

pyclean.runner   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 43
dl 0
loc 71
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A CleanupRunner.configure() 0 9 3
A CleanupRunner.__init__() 0 9 1

4 Functions

Rating   Name   Duplication   Size   Complexity  
A remove_file() 0 9 2
A print_dirname() 0 4 1
A print_filename() 0 4 1
A remove_directory() 0 9 2
1
# SPDX-FileCopyrightText: 2020 Peter Bittner <[email protected]>
2
#
3
# SPDX-License-Identifier: GPL-3.0-or-later
4
5
"""Cleanup runner with dry-run and file operations functionality."""
6
7
import logging
8
9
log = logging.getLogger(__name__)
10
11
12
class CleanupRunner:
13
    """Execution engine with object counting, logging and optional dry-run."""
14
15
    def __init__(self):
16
        """Cleanup runner with optional dry-run behavior."""
17
        self.unlink = None
18
        self.rmdir = None
19
        self.ignore = None
20
        self.unlink_count = None
21
        self.unlink_failed = None
22
        self.rmdir_count = None
23
        self.rmdir_failed = None
24
25
    def configure(self, args):
26
        """Set up runner according to command line options."""
27
        self.unlink = print_filename if args.dry_run else remove_file
28
        self.rmdir = print_dirname if args.dry_run else remove_directory
29
        self.ignore = args.ignore
30
        self.unlink_count = 0
31
        self.unlink_failed = 0
32
        self.rmdir_count = 0
33
        self.rmdir_failed = 0
34
35
36
Runner = CleanupRunner()
37
38
39
def remove_file(fileobj):
40
    """Attempt to delete a file object for real."""
41
    log.debug('Deleting file: %s', fileobj)
42
    try:
43
        fileobj.unlink()
44
        Runner.unlink_count += 1
45
    except OSError as err:
46
        log.debug('File not deleted. %s', err)
47
        Runner.unlink_failed += 1
48
49
50
def remove_directory(dirobj):
51
    """Attempt to remove a directory object for real."""
52
    log.debug('Removing directory: %s', dirobj)
53
    try:
54
        dirobj.rmdir()
55
        Runner.rmdir_count += 1
56
    except OSError as err:
57
        log.debug('Directory not removed. %s', err)
58
        Runner.rmdir_failed += 1
59
60
61
def print_filename(fileobj):
62
    """Only display the file name, used with --dry-run."""
63
    log.debug('Would delete file: %s', fileobj)
64
    Runner.unlink_count += 1
65
66
67
def print_dirname(dirobj):
68
    """Only display the directory name, used with --dry-run."""
69
    log.debug('Would delete directory: %s', dirobj)
70
    Runner.rmdir_count += 1
71