1 | import os |
||
2 | import re |
||
3 | from typing import Optional, Dict, List |
||
4 | |||
5 | from backuppc_clone.Config import Config |
||
6 | from backuppc_clone.DataLayer import DataLayer |
||
7 | from backuppc_clone.style.BackupPcCloneStyle import BackupPcCloneStyle |
||
8 | |||
9 | |||
10 | View Code Duplication | class BackupInfoScanner: |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
11 | """ |
||
12 | Class for retrieving information about backups. |
||
13 | """ |
||
14 | |||
15 | # ------------------------------------------------------------------------------------------------------------------ |
||
16 | def __init__(self, io: BackupPcCloneStyle): |
||
17 | """ |
||
18 | Object constructor. |
||
19 | |||
20 | @param BackupPcCloneStyle io: The output style. |
||
21 | """ |
||
22 | |||
23 | self.__io: BackupPcCloneStyle = io |
||
24 | """ |
||
25 | The output style. |
||
26 | """ |
||
27 | |||
28 | # ------------------------------------------------------------------------------------------------------------------ |
||
29 | @staticmethod |
||
30 | def get_backup_info(backup_dir: str, param_name: str) -> Optional[str]: |
||
31 | """ |
||
32 | Extracts info about a backup from file backupInfo. |
||
33 | |||
34 | @param str backup_dir: The path to the host backup. |
||
35 | @param str param_name: The name of the info parameter. |
||
36 | |||
37 | :rtype: str|None |
||
38 | """ |
||
39 | ret = None |
||
40 | |||
41 | path = os.path.join(backup_dir, 'backupInfo') |
||
42 | if os.path.isfile(path): |
||
43 | with open(path) as file: |
||
44 | content = file.read() |
||
45 | result = re.search(r"'{}' => '(.*?)'".format(param_name), content) |
||
46 | if result: |
||
47 | ret = result.group(1) |
||
48 | |||
49 | if not ret: |
||
50 | ret = None |
||
51 | |||
52 | return ret |
||
53 | |||
54 | # ------------------------------------------------------------------------------------------------------------------ |
||
55 | def __scan_for_backups(self) -> List[Dict]: |
||
56 | """ |
||
57 | Scans for host backups and returns the metadata of the host backups. |
||
58 | |||
59 | :rtype: list[dict] |
||
60 | """ |
||
61 | pc_dir_original = Config.instance.pc_dir_original |
||
62 | |||
63 | self.__io.writeln(' Scanning <fso>{}</fso>'.format(pc_dir_original)) |
||
64 | |||
65 | backups = [] |
||
66 | for host in os.scandir(pc_dir_original): |
||
67 | if host.is_dir(): |
||
68 | host_dir = os.path.join(Config.instance.pc_dir_original, host.name) |
||
69 | for backup in os.scandir(host_dir): |
||
70 | if backup.is_dir() and re.match(r'^\d+$', backup.name): |
||
71 | backup_dir = os.path.join(host_dir, backup.name) |
||
72 | backups.append({'bob_host': host.name, |
||
73 | 'bob_number': int(backup.name), |
||
74 | 'bob_end_time': self.get_backup_info(backup_dir, 'endTime'), |
||
75 | 'bob_version': self.get_backup_info(backup_dir, 'version'), |
||
76 | 'bob_level': self.get_backup_info(backup_dir, 'level'), |
||
77 | 'bob_type': self.get_backup_info(backup_dir, 'type')}) |
||
78 | |||
79 | return backups |
||
80 | |||
81 | # ------------------------------------------------------------------------------------------------------------------ |
||
82 | def __import_backups(self, backups: List[Dict]) -> None: |
||
83 | """ |
||
84 | Imports the original host backups info into the SQLite database. |
||
85 | |||
86 | @param list[dict] backups: The metadata of the original backups. |
||
87 | """ |
||
88 | DataLayer.instance.original_backup_truncate() |
||
89 | |||
90 | for backup in backups: |
||
91 | DataLayer.instance.original_backup_insert(backup['bob_host'], |
||
92 | backup['bob_number'], |
||
93 | backup['bob_end_time'], |
||
94 | backup['bob_version'], |
||
95 | backup['bob_level'], |
||
96 | backup['bob_type']) |
||
97 | |||
98 | stats = DataLayer.instance.original_backup_get_stats() |
||
99 | |||
100 | self.__io.writeln('') |
||
101 | self.__io.writeln(' Found {} hosts and {} backups'.format(stats['#hosts'], stats['#backups'])) |
||
102 | self.__io.writeln('') |
||
103 | |||
104 | # ------------------------------------------------------------------------------------------------------------------ |
||
105 | def scan(self) -> None: |
||
106 | """ |
||
107 | Scans information about backups. |
||
108 | """ |
||
109 | backups = self.__scan_for_backups() |
||
110 | self.__import_backups(backups) |
||
111 | |||
112 | # ---------------------------------------------------------------------------------------------------------------------- |
||
113 |