-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
102 lines (76 loc) · 3.28 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import argparse
import os
import shutil
from engine import utils
from engine import backup
def parse_arguments():
"""Parse arguments from console.
Parse two arguments from console, original directory and
backup directory paths and return them.
Returns:
Tuple(str): tuple consisting of original directory and
backup directory paths.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"original_directory",
type=str,
help="Path to directory with files to back up.")
parser.add_argument(
"backup_directory",
type=str,
help="Path to directory to which save backed up files.")
args = parser.parse_args()
return args.original_directory, args.backup_directory
def make_backup(original_dirpath, backup_dirpath):
"""Perform backup.
Main backup function. It lists all files to be copied, asks for
consent to perform backup, and copies all files to backup_dirpath
preserving the same file and directory tree.
Args:
original_dirpath (str): path to directory with files to back-up,
backup_dirpath (str): path to directory to save files.
Returns:
bool: True if backup was performed without errors, False if
there was not consent to perform back-up.
"""
relative_filepaths = utils.get_list_of_relative_filepaths(original_dirpath)
backups = backup.parse_backup_objects(
relative_filepaths, original_dirpath, backup_dirpath)
size_sum = sum([obj.get_size() for obj in backups])
utils.print_backup_objects(backups, size_sum=size_sum, files_limit=50)
utils.print_proceed_info()
answer = input()
if answer.lower() == 'y':
size_copied = 0
for obj in backups:
utils.clear_terminal()
size_state = utils.formatted_size_state(size_copied, size_sum)
print(f'{size_state}\nCopying:\n{obj}')
obj.make_backup()
size_copied += obj.get_size()
utils.clear_terminal()
print(utils.formatted_size_state(size_copied, size_sum))
print(utils.colored('Backup finished!', 'green'))
return True
else:
return False
if __name__ == "__main__":
original_dirpath, backup_dirpath = parse_arguments()
if not os.path.isdir(original_dirpath):
# Given original_dirpath argument is not a directory.
utils.quit_with_error('First argument is not a directory.')
if not os.access(original_dirpath, os.W_OK):
# User doesn't have permission to use given original_dirpath.
utils.quit_with_error(f'Permission denied for: {original_dirpath}')
if not os.path.isdir(backup_dirpath):
# Backup directory doesn't exist, create it.
try:
os.mkdir(backup_dirpath)
except PermissionError as e:
# User doesn't have permission to use given backup_dirpath.
utils.quit_with_error(f'Permission denied for: {backup_dirpath}')
if not os.access(backup_dirpath, os.W_OK):
# User doesn't have permission to use given backup_dirpath.
utils.quit_with_error(f'Permission denied for: {backup_dirpath}')
make_backup(original_dirpath, backup_dirpath)