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
![]() |
|||
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 |