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

backuppc_clone.helper.BackupDelete   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 84.21 %

Importance

Changes 0
Metric Value
eloc 26
dl 64
loc 76
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A BackupDelete.delete_backup() 12 12 1
A BackupDelete.__init__() 19 19 1
A BackupDelete.__delete_metadata() 10 10 1
A BackupDelete.__delete_files() 9 9 2

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 shutil
6
7
from backuppc_clone.Config import Config
8
from backuppc_clone.DataLayer import DataLayer
9
from backuppc_clone.style.BackupPcCloneStyle import BackupPcCloneStyle
10
11
12 View Code Duplication
class BackupDelete:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
13
    """
14
    Deletes a backup of a host
15
    """
16
17
    # ------------------------------------------------------------------------------------------------------------------
18
    def __init__(self, io: BackupPcCloneStyle):
19
        """
20
        Object constructor.
21
22
        :param BackupPcCloneStyle io: The output style.
23
        """
24
25
        self.__io: BackupPcCloneStyle = io
26
        """
27
        The output style.
28
        """
29
30
        self.__host: str = ''
31
        """
32
        The host of the backup.
33
        """
34
35
        self.__backup_no: int = 0
36
        """
37
        The number of the backup.
38
        """
39
40
    # ------------------------------------------------------------------------------------------------------------------
41
    def __delete_files(self) -> None:
42
        """
43
        Removes the backup from the cone file system.
44
        """
45
        self.__io.writeln(' Removing files')
46
47
        backup_dir_clone = Config.instance.backup_dir_clone(self.__host, self.__backup_no)
48
        if os.path.isdir(backup_dir_clone):
49
            shutil.rmtree(backup_dir_clone)
50
51
    # ------------------------------------------------------------------------------------------------------------------
52
    def __delete_metadata(self) -> None:
53
        """
54
        Removes the metadata from the database.
55
        """
56
        self.__io.writeln(' Removing metadata')
57
58
        hst_id = DataLayer.instance.get_host_id(self.__host)
59
        bck_id = DataLayer.instance.get_bck_id(hst_id, int(self.__backup_no))
60
61
        DataLayer.instance.backup_delete(bck_id)
62
63
    # ------------------------------------------------------------------------------------------------------------------
64
    def delete_backup(self, host: str, backup_no: int) -> None:
65
        """
66
        Deletes a backup of a host.
67
68
        :param str host: The host of the backup.
69
        :param int backup_no: The number of the backup.
70
        """
71
        self.__host = host
72
        self.__backup_no = backup_no
73
74
        self.__delete_metadata()
75
        self.__delete_files()
76
77
# ----------------------------------------------------------------------------------------------------------------------
78