backuppc_clone.command.TraversePerformanceTestCommand   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 91.38 %

Importance

Changes 0
Metric Value
eloc 46
dl 106
loc 116
rs 10
c 0
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
B TraversePerformanceTestCommand.__traverse() 20 20 7
A TraversePerformanceTestCommand.__init__() 28 28 1
A TraversePerformanceTestCommand.execute() 11 11 1
A TraversePerformanceTestCommand.__report() 11 11 2
A TraversePerformanceTestCommand.handle() 16 16 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import os
2
import time
3
from typing import Optional
4
5
from cleo import Command, Input, Output
6
7
from backuppc_clone.style.BackupPcCloneStyle import BackupPcCloneStyle
8
9
10 View Code Duplication
class TraversePerformanceTestCommand(Command):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
11
    """
12
    Traversing recursively a directory performance test
13
14
    traverse-performance-test
15
        {--stat : Get status of each file}
16
        {dir    : The start directory}
17
    """
18
19
    # ------------------------------------------------------------------------------------------------------------------
20
    def __init__(self):
21
        """
22
        Object constructor.
23
        """
24
        Command.__init__(self)
25
26
        self.__stat: bool = False
27
        """
28
        If True stat must be called for each file.
29
        """
30
31
        self._io: Optional[BackupPcCloneStyle] = None
32
        """
33
        The output style.
34
        """
35
36
        self.__dir_count: int = 0
37
        """
38
        The number of directories counted.
39
        """
40
41
        self.__file_count: int = 0
42
        """
43
        The number of file counted.
44
        """
45
46
        self.__start_time: float = 0
47
        """
48
        The timestamp of the start of the performance test.
49
        """
50
51
    # ------------------------------------------------------------------------------------------------------------------
52
    def __traverse(self, path: str) -> None:
53
        """
54
        Traverse recursively a directory.
55
56
        @param str path: The path to the directory.
57
        """
58
        dirs = []
59
        for entry in os.scandir(path):
60
            if self.__stat and not entry.is_symlink():
61
                entry.stat()
62
63
            if entry.is_file():
64
                self.__file_count += 1
65
66
            elif entry.is_dir():
67
                dirs.append(entry.name)
68
                self.__dir_count += 1
69
70
        for name in dirs:
71
            self.__traverse(os.path.join(path, name))
72
73
    # ------------------------------------------------------------------------------------------------------------------
74
    def __report(self, end_time: float) -> None:
75
        """
76
        Prints the performance report.
77
78
        @param float end_time: The timestamp of the end of the performance test.
79
        """
80
        self._io.writeln('')
81
        self._io.writeln('number of directories: {}'.format(self.__dir_count))
82
        self._io.writeln('number of files      : {}'.format(self.__file_count))
83
        self._io.writeln('get status           : {}'.format('yes' if self.__stat else 'no'))
84
        self._io.writeln('duration             : {0:.1f}s'.format(end_time - self.__start_time))
85
86
    # ------------------------------------------------------------------------------------------------------------------
87
    def execute(self, input_object: Input, output_object: Output) -> None:
88
        """
89
        Executes the command.
90
91
        @param Input input_object: The input.
92
        @param Output output_object: The output.
93
        """
94
        self.input = input_object
95
        self.output = output_object
96
97
        self.handle()
98
99
    # ------------------------------------------------------------------------------------------------------------------
100
    def handle(self) -> None:
101
        """
102
        Executes the command.
103
        """
104
        self._io = BackupPcCloneStyle(self.input, self.output)
105
106
        self.__stat = self.option('stat')
107
        self.__dir_count = 0
108
        self.__file_count = 0
109
        self.__start_time = time.time()
110
111
        dir_name = self.argument('dir')
112
113
        self._io.writeln('Traversing <fso>{}</fso>'.format(dir_name))
114
        self.__traverse(dir_name)
115
        self.__report(time.time())
116
117
# ----------------------------------------------------------------------------------------------------------------------
118