-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirtool.py
More file actions
204 lines (172 loc) · 5.77 KB
/
dirtool.py
File metadata and controls
204 lines (172 loc) · 5.77 KB
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
202
import sys, os, hashlib
def usage():
print('Usage (1): python3 dirtool.py compare folder1 folder2', file=sys.stderr)
print('Usage (2): python3 dirtool.py hash folder [>output.txt]', file=sys.stderr)
print('Usage (2a): python3 dirtool.py hash folder -o output.txt # output is UTF-8 encoded', file=sys.stderr)
print('Usage (3): python3 dirtool.py dupfind folder', file=sys.stderr)
print('Usage (4): python3 dirtool.py hash-dupfind out.txt', file=sys.stderr)
print('Usage (4a): python3 dirtool.py hash-dupfind out.txt -o output.txt', file=sys.stderr)
print('Usage (5): python3 dirtool.py hash-compare out1.txt out2.txt', file=sys.stderr)
print('Usage (5a): python3 dirtool.py hash-compare out1.txt out2.txt -o output.txt # output is UTF-8 encoded', file=sys.stderr)
def check_argv(min):
if len(sys.argv) < min:
usage()
sys.exit(1)
def addentry(d, fname, checksum):
if checksum in d.keys():
d[checksum].append(fname)
else:
d[checksum] = [fname]
def traverse(folder, d):
for root, subdirs, files in os.walk(folder):
if root.endswith('/@eaDir'): # Synology DSM creates folders with derived data
continue
for f in files:
if f == '.DS_Store': # macOS creates hidden files
continue
fullname = os.path.join(root, f)
m = hashlib.sha256()
if os.path.islink(fullname):
m.update(os.readlink(fullname).encode('utf-8'))
else:
if len(os.path.abspath(fullname)) > 260 and sys.platform.startswith("win"):
fullname = '\\\\?\\' + os.path.abspath(fullname)
print('warning: length is not supported on Windows')
print(f'new name: {fullname}')
with open(fullname, 'rb') as fdata:
while True:
buf = fdata.read(1048576)
if not buf:
break
m.update(buf)
checksum = m.hexdigest()
addentry(d, fullname, checksum)
def compare(d1, d2):
print('Comparing {0} and {1} records'.format(len(d1), len(d2)))
found, notfound = 0, 0
for k1 in d1.keys():
if k1 in d2.keys():
found += 1
else:
notfound += 1
k2notfound = 0
notfoundlist = []
for k2 in d2.keys():
if k2 not in d1.keys():
k2notfound += 1
elem = d2[k2][0]
if elem[-1:] == '\n':
elem = elem[:-1]
notfoundlist += [elem]
print('{0} found, {1} d1 keys not found in d2'.format(found, notfound))
print('{0} keys in d2 not found in d1'.format(k2notfound))
print('\n'.join(notfoundlist))
# same as compare but outputs to a file
# under Windows redirecting output to a file may result in encoding error
# even if we change code page to 65001 (`chcp 65001`) the error persists
# feel free to refactor with compare if u have some time
def compare2(d1, d2, fname):
f = open(fname, 'w', encoding='utf-8')
f.write('Comparing {0} and {1} records'.format(len(d1), len(d2)))
found, notfound = 0, 0
for k1 in d1.keys():
if k1 in d2.keys():
found += 1
else:
notfound += 1
k2notfound = 0
notfoundlist = []
for k2 in d2.keys():
if k2 not in d1.keys():
k2notfound += 1
elem = d2[k2][0]
if elem[-1:] == '\n':
elem = elem[:-1]
notfoundlist += [elem]
f.write('{0} found, {1} d1 keys not found in d2\n'.format(found, notfound))
f.write('{0} keys in d2 not found in d1\n'.format(k2notfound))
f.write('\n'.join(notfoundlist))
f.write('\n')
f.close()
def dupfind(d):
for k in d.keys():
if len(d[k]) > 1:
for e in d[k]:
print(k, e)
def dupfind2(d, fname):
fout = open(fname, 'w', encoding='utf-8')
for k in d.keys():
if len(d[k]) > 1:
for e in d[k]:
fout.write(k)
fout.write(' ')
fout.write(e)
fout.close()
def output(d):
for k in d.keys():
for e in d[k]:
print(k, e)
# see comment in compare2
def output2(d, fname):
fout = open(fname, 'w', encoding='utf-8')
for k in d.keys():
for e in d[k]:
fout.write(k)
fout.write(' ')
fout.write(e)
fout.write('\n')
fout.close()
def load(fname):
d = {}
with open(fname, 'r', encoding='utf-8') as f:
lines = f.readlines()
for l in lines:
pos = l.find(' ')
checksum = l[:pos]
name = l[pos + 1:]
addentry(d, name, checksum)
return d
check_argv(2)
cmd = sys.argv[1]
if cmd == 'compare':
check_argv(4)
folder1, folder2 = sys.argv[2], sys.argv[3]
dict1, dict2 = {}, {}
traverse(folder1, dict1)
traverse(folder2, dict2)
compare(dict1, dict2)
elif cmd == 'hash':
check_argv(3)
folder = sys.argv[2]
dictf = {}
traverse(folder, dictf)
if len(sys.argv) >= 5 and sys.argv[3] == '-o':
output2(dictf, sys.argv[4])
else:
output(dictf)
elif cmd == 'dupfind':
check_argv(3)
folder = sys.argv[2]
dictf = {}
traverse(folder, dictf)
dupfind(dictf)
elif cmd == 'hash-dupfind':
check_argv(3)
file1 = sys.argv[2]
dict1 = load(file1)
if len(sys.argv) >= 5 and sys.argv[3] == '-o':
dupfind2(dict1, sys.argv[4])
else:
dupfind(dict1)
elif cmd == 'hash-compare':
check_argv(4)
file1, file2 = sys.argv[2], sys.argv[3]
dict1 = load(file1)
dict2 = load(file2)
if len(sys.argv) >= 6 and sys.argv[4] == '-o':
compare2(dict1, dict2, sys.argv[5])
else:
compare(dict1, dict2)
else:
usage()
sys.exit(1)