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

backuppc_clone.helper.PoolSync   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 116
Duplicated Lines 87.93 %

Importance

Changes 0
Metric Value
eloc 55
dl 102
loc 116
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A PoolSync.__scan_original_pool() 13 13 1
A PoolSync.__import_csv() 9 9 1
A PoolSync.synchronize() 12 12 1
A PoolSync.__update_database() 9 9 1
A PoolSync.__init__() 9 9 1
A PoolSync.__clean_clone_pool() 32 32 4

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