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

BackupDelete.__delete_metadata()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 1
dl 10
loc 10
rs 10
c 0
b 0
f 0
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