-
Notifications
You must be signed in to change notification settings - Fork 0
/
FolderSync.py
129 lines (102 loc) · 5.04 KB
/
FolderSync.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
SyncAndVerify
(C) Copyright 2024, Eric Bergman-Terrell
This file is part of SyncAndVerify.
SyncAndVerify is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SyncAndVerify is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
See the GNU General Public License: <http://www.gnu.org/licenses/>.
"""
import os
import stat
import shutil
from tenacity import RetryError
from FileSystemUtils import FileSystemUtils
from FolderQuickCompare import FolderQuickCompare
from VerifyPaths import VerifyPaths
from StringLiterals import StringLiterals
from Globals import app_globals
from AppException import AppException
class FolderSync:
@staticmethod
def sync(source_path, destination_path, exclusions, threads=1):
source_path, destination_path = VerifyPaths.verify(source_path, destination_path)
try:
if not os.path.exists(destination_path):
os.makedirs(destination_path)
comparison = FolderQuickCompare.compare(source_path, destination_path, exclusions, threads)
app_globals.log.print(f'\t{FolderQuickCompare.summary(comparison)}')
if comparison.differences > 0:
FolderSync._delete_folders(comparison, destination_path)
FolderSync._delete_files(comparison, destination_path)
FolderSync._create_folders(comparison, destination_path)
FolderSync._copy_files(comparison, source_path, destination_path)
# After backup is complete, re-run quick comparison to verify that everything was completed.
post_sync_comparison = FolderQuickCompare.compare(source_path, destination_path, exclusions, threads)
app_globals.log.print('\tSynced')
app_globals.log.print(f'\tChecking Sync: {FolderQuickCompare.summary(post_sync_comparison)}')
if post_sync_comparison.differences > 0:
error_message = f'{StringLiterals.ERROR_PREFIX}: Source folder is not fully synced'
app_globals.log.print(error_message)
FolderQuickCompare.display_results(post_sync_comparison)
raise AppException(error_message)
else:
app_globals.log.print('\tNo Need to Sync')
except (RetryError, IOError, OSError, BaseException) as exception:
raise AppException(f'{exception}', exception)
return comparison
@staticmethod
def _delete_readonly_file(action, path, exc):
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWRITE)
action(path)
else:
raise
@staticmethod
def _delete_folders(comparison, destination_path):
for folder in comparison.folders_to_delete:
full_path = os.path.join(destination_path, folder)
shutil.rmtree(full_path, onerror=FolderSync._delete_readonly_file)
@staticmethod
def _delete_files(comparison, destination_path):
for file in comparison.files_to_delete:
full_path = os.path.join(destination_path, file)
try:
os.remove(full_path)
except PermissionError:
os.chmod(full_path, stat.S_IWRITE)
os.remove(full_path)
@staticmethod
def _create_folders(comparison, destination_path):
for folder in comparison.folders_to_create:
full_path = os.path.join(destination_path, folder)
os.makedirs(full_path, exist_ok=False)
@staticmethod
def _copy_files(comparison, source_path, destination_path):
files_copied = 0
bytes_copied = 0
bytes_to_copy = sum(value.metadata.st_size for value in comparison.files_to_copy_metadata.values())
for file in comparison.files_to_copy:
file_source_path = os.path.join(source_path, file)
file_destination_path = os.path.join(destination_path, file)
app_globals.log.print(f'\t{file_destination_path}', standard_output=False)
FileSystemUtils.copy_file(file_source_path, file_destination_path)
files_copied += 1
bytes_copied += comparison.files_to_copy_metadata[file].metadata.st_size
if bytes_to_copy != 0:
bytes_percent = (bytes_copied * 100) / bytes_to_copy
else:
bytes_percent = 100.0
app_globals.log.print('\t\tFiles: ({:,}/{:,} {:.2f}%) Bytes: ({:,}/{:,} {:.2f}%)'.format(
files_copied,
len(comparison.files_to_copy_metadata),
(files_copied * 100) / len(comparison.files_to_copy_metadata),
bytes_copied,
bytes_to_copy,
bytes_percent
), standard_output=False)