Passed
Push — master ( ba6342...71f198 )
by P.R.
07:12
created

backuppc_clone.command.TraversePerformanceTestCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 119
Duplicated Lines 89.08 %

Importance

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