Issues (23)

backuppc_clone/helper/PoolSync.py (1 issue)

1
import os
2
import time
3
4
from backuppc_clone.Config import Config
5
from backuppc_clone.DataLayer import DataLayer
6
from backuppc_clone.ProgressBar import ProgressBar
7
from backuppc_clone.helper.PoolScanner import PoolScanner
8
from backuppc_clone.style.BackupPcCloneStyle import BackupPcCloneStyle
9
10
11 View Code Duplication
class PoolSync:
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
12
    """
13
    Inventories the original pool, prunes the clone pool and maintains the database.
14
    """
15
16
    # ------------------------------------------------------------------------------------------------------------------
17
    def __init__(self, io: BackupPcCloneStyle):
18
        """
19
        Object constructor.
20
21
        @param BackupPcCloneStyle io: The output style.
22
        """
23
24
        self.__io: BackupPcCloneStyle = io
25
        """
26
        The output style.
27
        """
28
29
    # ------------------------------------------------------------------------------------------------------------------
30
    def __clean_clone_pool(self) -> None:
31
        """
32
        Removes obsolete files from pool of clone.
33
        """
34
        self.__io.writeln('')
35
        self.__io.section('Clone pool')
36
        self.__io.writeln('')
37
38
        file_count = DataLayer.instance.pool_prepare_obsolete_clone_files()
39
        progress = ProgressBar(self.__io.output, file_count)
40
41
        top_dir_clone = Config.instance.top_dir_clone
42
        count = 0
43
        for rows in DataLayer.instance.pool_yield_obsolete_clone_files():
44
            for row in rows:
45
                try:
46
                    path = os.path.join(top_dir_clone, row['bpl_dir'], row['bpl_name'])
47
                    self.__io.log_very_verbose('Removing <fso>{}</fso>'.format(path))
48
                    os.remove(path)
49
                    count += 1
50
                except FileNotFoundError:
51
                    # Nothing to do.
52
                    pass
53
54
                DataLayer.instance.pool_delete_row(row['bpl_id'])
55
                progress.advance()
56
57
        progress.finish()
58
59
        self.__io.writeln('')
60
        self.__io.writeln(' Files removed: {}'.format(count))
61
        self.__io.writeln('')
62
63
    # ------------------------------------------------------------------------------------------------------------------
64
    def __scan_original_pool(self, csv_filename: str) -> None:
65
        """
66
        Scans the pool of the original and stores the data into a CSV file.
67
68
        @param str csv_filename: The name of the CSV file.
69
        """
70
        self.__io.section('Original pool')
71
72
        scanner = PoolScanner(self.__io)
73
        scanner.scan_directory(Config.instance.top_dir_original, ['pool', 'cpool'], csv_filename)
74
75
        self.__io.writeln(' Files found: {}'.format(scanner.count))
76
        self.__io.writeln('')
77
78
    # ------------------------------------------------------------------------------------------------------------------
79
    def __import_csv(self, csv_filename: str) -> None:
80
        """
81
        Imports to CSV file with entries of the original pool into the SQLite database.
82
83
        @param str csv_filename: The name of the CSV file.
84
        """
85
        self.__io.log_verbose(' Importing <fso>{}</fso> into <dbo>IMP_POOL</dbo>'.format(csv_filename))
86
87
        DataLayer.instance.import_csv('IMP_POOL', ['imp_inode', 'imp_dir', 'imp_name'], csv_filename)
88
89
    # ------------------------------------------------------------------------------------------------------------------
90
    def __update_database(self) -> None:
91
        """
92
        Updates the database.
93
        """
94
        self.__io.log_verbose(' Updating <dbo>BKC_POOL</dbo>')
95
        self.__io.writeln('')
96
97
        DataLayer.instance.pool_delete_obsolete_original_rows()
98
        DataLayer.instance.pool_insert_new_original()
99
100
    # ------------------------------------------------------------------------------------------------------------------
101
    def synchronize(self) -> None:
102
        """
103
        Inventories the original pool, prunes the clone pool and maintains the database.
104
        """
105
        Config.instance.last_pool_scan = int(time.time())
106
107
        csv_filename = os.path.join(Config.instance.tmp_dir_clone, 'pool.csv')
108
109
        self.__scan_original_pool(csv_filename)
110
        self.__import_csv(csv_filename)
111
        self.__clean_clone_pool()
112
        self.__update_database()
113
114
# ----------------------------------------------------------------------------------------------------------------------
115