-
Notifications
You must be signed in to change notification settings - Fork 0
/
synoFileCheck.py
145 lines (133 loc) · 5.28 KB
/
synoFileCheck.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
# synoFileCheck.py
#
# 26.02.2022 RB
# Compare folders after data migration of file servers
# Installation specific adjustments need to be entered at the
# beginning of the main program below
#
import os.path, json, time, filecmp
from datetime import datetime
#
# ------------------------------------------------------------
# Functions definitions
# ------------------------------------------------------------
#
def compareFolders(A, B):
result = {}
result['only_left_folders'] = []
result['only_left_files'] = []
result['only_left_errors'] = []
result['only_right_folders'] = []
result['only_right_files'] = []
result['only_right_errors'] = []
result['funny_names'] = []
result['diff_files'] = []
result['error_files'] = []
folderDifference(A, B, result)
return result
# ------------------------------------------------------------
def folderDifference(A, B, result):
dirs_cmp = filecmp.dircmp(A, B)
if len(dirs_cmp.left_only) > 0:
for x in dirs_cmp.left_only:
y = os.path.join(A, x)
if os.path.isdir(y):
result['only_left_folders'].append({'left': A, 'right': B, 'name': x})
elif os.path.isfile(y):
result['only_left_files'].append({'left': A, 'right': B, 'name': x})
else:
result['only_left_errors'].append({'left': A, 'right': B, 'name': x})
if len(dirs_cmp.right_only) > 0:
for x in dirs_cmp.right_only:
y = os.path.join(B, x)
if os.path.isdir(y):
result['only_right_folders'].append({'left': A, 'right': B, 'name': x})
elif os.path.isfile(y):
result['only_right_files'].append({'left': A, 'right': B, 'name': x})
else:
result['only_right_errors'].append({'left': A, 'right': B, 'name': x})
if len(dirs_cmp.common_funny) > 0:
for x in dirs_cmp.common_funny:
result['funny_names'].append({'left': A, 'right': B, 'name': x})
if len(dirs_cmp.diff_files) > 0:
for x in dirs_cmp.diff_files:
result['diff_files'].append({'left': A, 'right': B, 'name': x})
if len(dirs_cmp.funny_files) > 0:
for x in dirs_cmp.funny_files:
result['error_files'].append({'left': A, 'right': B, 'name': x})
for common_dir in dirs_cmp.common_dirs:
folderDifference(os.path.join(A, common_dir), os.path.join(B, common_dir), result)
# ------------------------------------------------------------
def cleanFileName(raw):
forbidden = "/\\"
out = ""
for c in raw:
if c in forbidden:
out += ""
else:
out += c
return out
# ------------------------------------------------------------
def shortenFilename(name, maxlen):
parts = name.rsplit('.', 1)
return parts[0][:maxlen-len(parts[1])-1] + '.' + parts[1]
# ------------------------------------------------------------
def singlecheck(folderA, folderB, R):
if not os.path.isdir(folderA):
print("Folder A not existing: " + folderA)
return R
if not os.path.isdir(folderB):
print("Folder B not existing: " + folderB)
return R
starttime = time.time()
curtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(curtime + " - singleCheck comparing " + folderA + " with " + folderB)
results = compareFolders(folderA, folderB)
endtime = time.time()
print('singleCheck ending after {:.0f} seconds'.format(endtime-starttime))
return mergeResults(R, results)
# ------------------------------------------------------------
def mergeResults(A, B):
keysA = A.keys()
keysB = B.keys()
out = {}
for a in keysA:
if a in keysB:
out[a] = A[a] + B[a]
else:
out[a] = A[a]
for b in keysB:
if not b in keysA:
out[b] = B[b]
return out
# ------------------------------------------------------------
def prepareResults(results, topA, topB, outFolder):
daytag = datetime.now().strftime("%y%m%d")
outFilename = cleanFileName(daytag + " compare " + topA + " " + topB + '.json')
with open(os.path.join(outFolder, outFilename), "w") as outfile:
json.dump(results, outfile)
#
# ============================================================
# Main program
# ============================================================
#
print("synoFileCheck starting ...")
# Customizing 1:
# Nodenames of old and new file server
topA = "\\\\oldSyno\\"
topB = "\\\\newSyno\\"
# Customizing 2:
# Shared folders with same name on old and new system
folders = ['Data1', 'Data3', 'homes', 'music', 'photo', 'video', 'web']
results = {}
# Process shared folders with same name
for folder in folders:
results = singlecheck(topA + folder, topB + folder, results)
# Customizing 3:
# Process folders with different names
results = singlecheck("\\\\oldSyno\\Data2\\special", "\\\\newSyno\\Data2\\special", results)
# Customizing 4:
# Change output folder for results (last parameter, optional)
# if kept empty string, output goes to the same folder as this script
prepareResults(results, topA, topB, '')
print("synoFileCheck ending")