backuppc_clone.command.InitOriginalCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 91.11 %

Importance

Changes 0
Metric Value
eloc 48
dl 82
loc 90
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B InitOriginalCommand._handle_command() 44 44 8
A InitOriginalCommand.__get_parameter() 21 21 1
A InitOriginalCommand._init_singletons() 5 5 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 configparser
2
import os
3
import subprocess
4
5
from backuppc_clone.command.BaseCommand import BaseCommand
6
7
8 View Code Duplication
class InitOriginalCommand(BaseCommand):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
9
    """
10
    Creates the configuration file for the original
11
12
    init-original
13
    """
14
15
    # ------------------------------------------------------------------------------------------------------------------
16
    @staticmethod
17
    def __get_parameter(perl: str, backuppc_config_filename: str, parameter_name: str) -> str:
18
        """
19
        Extracts a parameter for BackupPCs configuration file.
20
21
        @param str perl: Path to the perl executable.
22
        @param str backuppc_config_filename: Path to BackupPC config file.
23
        @param parameter_name: The name of the parameter.
24
25
        :rtype: str
26
        """
27
        file = open(backuppc_config_filename, 'rt')
28
        code = file.read()
29
30
        code += '\n'
31
        code += 'print $Conf{{{}}};'.format(parameter_name)
32
33
        pipe = subprocess.Popen([perl], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
34
        value = pipe.communicate(bytes(code, 'utf-8'))
35
36
        return str(value[0], 'utf-8')
37
38
    # ------------------------------------------------------------------------------------------------------------------
39
    def _init_singletons(self) -> None:
40
        """
41
        Omits the creating of singleton objects.
42
        """
43
        pass
44
45
    # ------------------------------------------------------------------------------------------------------------------
46
    def _handle_command(self) -> None:
47
        """
48
        Executes the command.
49
        """
50
        self._io.title('Creating Original Configuration File')
51
52
        while True:
53
            perl = self.ask('perl executable', '/usr/bin/perl')
54
            if os.path.isfile(perl):
55
                break
56
57
        while True:
58
            backuppc_config_filename = self.ask('Config file', '/etc/BackupPC/config.pl')
59
            if os.path.isfile(backuppc_config_filename):
60
                break
61
62
        name = self.__get_parameter(perl, backuppc_config_filename, 'ServerHost')
63
        top_dir = self.__get_parameter(perl, backuppc_config_filename, 'TopDir')
64
        conf_dir = self.__get_parameter(perl, backuppc_config_filename, 'ConfDir')
65
        log_dir = self.__get_parameter(perl, backuppc_config_filename, 'LogDir')
66
        pc_dir = os.path.join(top_dir, 'pc')
67
68
        config_filename_original = os.path.join(top_dir, 'original.cfg')
69
70
        if os.path.isfile(config_filename_original):
71
            create = self.confirm('Overwrite {}'.format(config_filename_original), False)
72
        else:
73
            create = True
74
75
        if create:
76
            self._io.writeln('Writing <fso>{}</fso>'.format(config_filename_original))
77
78
            config = configparser.ConfigParser()
79
            config['BackupPC Clone'] = {'role': 'original',
80
                                        'name': name}
81
            config['Original'] = {'top_dir':  top_dir,
82
                                  'conf_dir': conf_dir,
83
                                  'log_dir':  log_dir,
84
                                  'pc_dir':   pc_dir}
85
            with open(config_filename_original, 'w') as config_file:
86
                config.write(config_file)
87
88
        else:
89
            self._io.warning('No configuration file created')
90
91
# ----------------------------------------------------------------------------------------------------------------------
92