-
Notifications
You must be signed in to change notification settings - Fork 0
/
FolderCompleteCompare.py
201 lines (151 loc) · 9.05 KB
/
FolderCompleteCompare.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""
SyncAndVerify
(C) Copyright 2021, 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
from Executor import Executor
from collections import namedtuple
from FileHash import FileHash, ERROR_MARKER
from VerifyPaths import VerifyPaths
from Globals import app_globals
from AppException import AppException
class FolderCompleteCompare:
@staticmethod
def compare(source_path, destination_path, threads=1):
try:
source_path, destination_path = VerifyPaths.verify(source_path, destination_path, True)
executor = Executor.create(threads)
future_source = executor.submit(FolderCompleteCompare._create_file_hashes, source_path)
future_destination = executor.submit(FolderCompleteCompare._create_file_hashes, destination_path)
executor.shutdown(wait=True)
(source_hashes, destination_hashes) = future_source.result(), future_destination.result()
executor = Executor.create(threads)
future_source = executor.submit(FolderCompleteCompare._get_folders, source_path)
future_destination = executor.submit(FolderCompleteCompare._get_folders, destination_path)
executor.shutdown(wait=True)
(source_folders, destination_folders) = future_source.result(), future_destination.result()
# in source only
files_in_source_folder_only = source_hashes.keys() - destination_hashes.keys()
# in destination only
files_in_destination_folder_only = destination_hashes.keys() - source_hashes.keys()
files_in_both_folders = source_hashes.keys() & destination_hashes.keys()
# source and destination files have different hashes
different_files = set()
identical_files = set()
# source and/or destination file could not be read to be hashed
could_not_read_files = set()
for file in files_in_both_folders:
source_hash = source_hashes[file]
destination_hash = destination_hashes[file]
if source_hash == ERROR_MARKER or destination_hash == ERROR_MARKER:
could_not_read_files.add(file)
else:
if source_hash != destination_hash:
different_files.add(file)
else:
identical_files.add(file)
differences = len(files_in_source_folder_only) + len(files_in_destination_folder_only) + len(
could_not_read_files) + len(different_files)
folders_in_source_folder_only = source_folders - destination_folders
folders_in_destination_folder_only = destination_folders - source_folders
folders_in_both_folders = source_folders.intersection(destination_folders)
CompleteComparison = namedtuple('CompleteComparison',
['folders_in_source_folder_only',
'folders_in_destination_folder_only',
'folders_in_both_folders',
'files_in_source_folder_only',
'files_in_destination_folder_only',
'files_in_both_folders',
'identical_files',
'different_files',
'could_not_read_files',
'source_hashes',
'destination_hashes',
'differences'])
return CompleteComparison(folders_in_source_folder_only,
folders_in_destination_folder_only,
folders_in_both_folders,
files_in_source_folder_only,
files_in_destination_folder_only,
files_in_both_folders,
identical_files,
different_files,
could_not_read_files,
source_hashes,
destination_hashes,
differences)
except (IOError, OSError, BaseException) as exception:
raise AppException(f'{exception}', exception)
@staticmethod
def _create_file_hashes(root_path):
file_hashes = {}
root_path_string = f'{root_path}'
walk = os.walk(root_path, True, FolderCompleteCompare._error)
file_hash = FileHash()
for dir_path, dir_names, file_names in walk:
for file_name in file_names:
file_full_path = os.path.join(dir_path, file_name)
file_full_path_string = f'{file_full_path}'
file_short_path = file_full_path_string[len(root_path_string):len(file_full_path_string)]
file_hashes[file_short_path] = file_hash.create_file_hash(file_full_path)
return file_hashes
@staticmethod
def _get_folders(root_path):
folders = set()
walk = os.walk(root_path, True, FolderCompleteCompare._error)
for dir_path, dir_names, file_names in walk:
if dir_path != root_path:
abbreviated_path = dir_path[len(root_path):len(dir_path)]
folders.add(abbreviated_path)
return folders
@staticmethod
def _error(self, exception):
raise AppException(f'FolderCompleteCompare Error: root: "{self.root}" exception: {exception}', exception)
@staticmethod
def display_results(comparison):
if len(comparison.folders_in_source_folder_only) > 0:
# folders in source only
app_globals.log.print(f'\tFolders in Source Folder Only: {len(comparison.folders_in_source_folder_only):,}')
FolderCompleteCompare._print_paths(comparison.folders_in_source_folder_only)
if len(comparison.folders_in_destination_folder_only) > 0:
# folders in source only
app_globals.log.print(f'\tFolders in Destination Folder Only: {len(comparison.folders_in_destination_folder_only):,}')
FolderCompleteCompare._print_paths(comparison.folders_in_destination_folder_only)
if len(comparison.files_in_source_folder_only) > 0:
# files in source only
app_globals.log.print(f'\tFiles in Source Folder Only: {len(comparison.files_in_source_folder_only):,}')
FolderCompleteCompare._print_paths(comparison.files_in_source_folder_only)
if len(comparison.files_in_destination_folder_only) > 0:
# files in destination only
app_globals.log.print(f'\n\tFiles in Destination Folder Only: {len(comparison.files_in_destination_folder_only):,}')
FolderCompleteCompare._print_paths(comparison.files_in_destination_folder_only)
if len(comparison.files_in_both_folders) != len(comparison.identical_files) or \
len(comparison.different_files) > 0 or len(comparison.could_not_read_files) > 0:
# in both source and destination
app_globals.log.print(
f'\n\tFiles in Source and Destination Folders: {len(comparison.files_in_both_folders):,} Identical: {len(comparison.identical_files):,} Different: {len(comparison.different_files):,} Read Errors: {len(comparison.could_not_read_files):,}')
for file in comparison.different_files:
source_hash = comparison.source_hashes[file]
destination_hash = comparison.destination_hashes[file]
if source_hash != destination_hash:
app_globals.log.print(f'\tFILES ARE DIFFERENT: file: "{file}" source hash: "{source_hash}" destination hash: "{destination_hash}"')
for file in comparison.could_not_read_files:
source_hash = comparison.source_hashes[file]
destination_hash = comparison.destination_hashes[file]
if source_hash != destination_hash:
app_globals.log.print(f'\tCOULD NOT READ FILE(S): file: "{file}" source hash: "{source_hash}" destination hash: "{destination_hash}"')
app_globals.log.print(f'\tDifferences (complete compare): {comparison.differences}')
@staticmethod
def _print_paths(path_set):
for path in path_set:
app_globals.log.print(f'\t\t"{path}"')