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

backuppc_clone.command.InitOriginalCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 88.17 %

Importance

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