-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckSammy.py
executable file
·810 lines (644 loc) · 33.5 KB
/
CheckSammy.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
#!/usr/bin/env python3
'''
Check Sammy is a GUI tool for checksumming stuff (using the md5 or xxHash algorithm).
It stores the hash values in separate json files (e.g. SomePath/InputFileName.md5).
The same files can be used for running integrity checks. In this case Sammy will look
for the json in whatever new path the file has been moved to (e.g. SomeOtherPath/InputFileName.md5).
Sammy was born at the Austrian Film Museum in 2019.
"Hoog Sammy, kijk omhoog Sammy
Anders is het vast te laat"
'''
from tkinter import *
from tkinter import filedialog
import multiprocessing.dummy as mp
import tkinter as tk
import os
import hashlib
import json
import threading
import datetime
import shutil
import subprocess
import xxhash
# To enable drag and drop:
#
# On Ubuntu 18.04:
# - tkdnd2.8 --> /usr/share/tcltk/ -
# - TkinterDnD2 --> /usr/lib/python3.6/ -
#
# On Windows 10:
# -tkdnd2.8 --> C:\Users\[Username]\AppData\Local\Programs\Python\Python[version]\tcl
# -TkinterDnD2 --> C:\Users\[Username]\AppData\Local\Programs\Python\Python[version]\Lib\site-packages
try:
import TkinterDnD2
from TkinterDnD2 import *
tkint = TkinterDnD2.TkinterDnD
print('Drag and drop enabled\n')
except:
tkint = tk
print('Drag and drop not enabled\n')
class SammyGUI(tkint.Tk):
# In this class all the widgets and methods of the GUI.
def __init__(self):
super().__init__()
self.checksummer = CheckSammy()
self.version = '0.11.1'
self.title('Check Sammy %s' % self.version)
if os.name == 'nt':
self.iconbitmap(os.path.abspath('./media/paw.ico'))
self.resizable(False,False)
self.check_this = {'F': [], 'D': []}
self.checked = {'Ok': [], 'Corrupted': [],
'No md5': [], 'Missing file': [], 'New file': []}
self.button_frame = tk.Frame(self)
self.button_frame.grid(row=1, column=0, pady=5, padx=5)
self.recursive_button = tk.Button(
self.button_frame, text=' All in one folder ', command=self.add_all_in_directory)
self.recursive_button.grid(
row=1, column=0, sticky='N', padx=2, pady=10)
self.folder_button = tk.Button(
self.button_frame, text='Single folder', command=self.add_directory)
self.folder_button.grid(row=2, column=0, sticky='N', padx=2, pady=2)
self.file_button = tk.Button(
self.button_frame, text='Single file', command=self.add_file)
self.file_button.grid(row=3, column=0, sticky='N', pady=2)
self.start_button = tk.Button(self.button_frame, text='Start', command=lambda: threading.Thread(
target=self.start, args=()).start())
self.start_button.grid(row=5, sticky='N', pady=2)
self.status_label = tk.Label(self.button_frame, text='Ready')
self.status_label.grid(row=6, sticky='N', pady=20)
try:
self.sammy = tk.PhotoImage(file='media/dog.png')
except:
pass
self.sammy_frame = tk.Frame(self.button_frame)
self.sammy_frame.grid(row=4, column=0)
try:
self.sammy_label = tk.Label(self.sammy_frame, image=self.sammy)
self.sammy_label.grid(pady=10)
except:
pass
self.batch_frame = tk.Frame(self, width=1700)
self.batch_frame.grid(row=0, column=1, rowspan=2)
self.batch_yscrollbar = tk.Scrollbar(
self.batch_frame, orient='vertical')
self.batch_yscrollbar.grid(row=1, column=4, rowspan=2, sticky='NS')
self.batch_xscrollbar = tk.Scrollbar(
self.batch_frame, orient='horizontal')
self.batch_xscrollbar.grid(row=2, column=0, columnspan=4, sticky='WE')
self.batch_remove_button = tk.Button(
self.batch_frame, text='Remove', command=self.remove_item)
self.batch_remove_button.grid(row=0, column=0, sticky='W', pady=5, padx=10)
self.batch_tranfer_button = tk.Button(
self.batch_frame, text='Safe transfer', command=self.open_safe_transfer)
self.batch_tranfer_button.grid(row=0, column=1, sticky='W', pady=5, padx=10)
self.batch_label = tk.Label(
self.batch_frame, text='Queue (0)', borderwidth=4, relief='groove', width=100)
self.batch_label.grid(row=0, column=2, sticky='W', pady=5)
self.ffmpeg_button = tk.Button(
self.batch_frame, text='FFmpeg options', command=self.open_ffmpeg_options)
self.ffmpeg_button.grid(row=0, column=3, sticky='W', pady=5, padx=10)
self.batch_listbox = tk.Listbox(self.batch_frame, yscrollcommand=self.batch_yscrollbar.set,
xscrollcommand=self.batch_xscrollbar.set, width=180, height=23,selectmode=EXTENDED)
self.batch_listbox.configure(activestyle='none', highlightthickness=0)
self.batch_yscrollbar.configure(command=self.batch_listbox.yview)
self.batch_xscrollbar.configure(command=self.batch_listbox.xview)
self.batch_listbox.grid(row=1, column=0, columnspan=4)
def drop(event):
if event.data:
ff = self.batch_listbox.tk.splitlist(event.data)
for f in ff:
if os.path.isfile(f):
if not os.path.abspath(f) in self.check_this['F'] and f != '':
self.check_this['F'].append(os.path.abspath(f))
elif os.path.isdir(f):
if not f in self.check_this['D'] and f != '':
self.check_this['D'].append(os.path.abspath(f))
self.update_batch()
try:
self.batch_listbox.drop_target_register(DND_FILES)
self.batch_listbox.dnd_bind('<<Drop>>', drop)
except:
pass
self.batch_frame.bind(
'<Leave>', lambda x: self.batch_listbox.selection_clear(0, END))
self.control_frame = tk.Frame(self)
self.control_frame.grid(row=0, column=0)
self.reset_button = tk.Button(
self.control_frame, text='Reset', command=self.reset)
self.reset_button.grid(sticky='N', pady=10)
self.operation_type = IntVar()
self.calculate_m = tk.Radiobutton(self.control_frame, text='Calculate md5', variable=self.operation_type,
value=0, tristatevalue=5, command=lambda: self.status_label.configure(text='Ready'))
self.calculate_m.grid(row=1, column=0, sticky='w')
self.calculate_x = tk.Radiobutton(self.control_frame, text='Calculate xxHash', variable=self.operation_type,
value=1, tristatevalue=5, command=lambda: self.status_label.configure(text='Ready'))
self.calculate_x.grid(row=2, column=0, sticky='w')
self.difference = tk.Radiobutton(self.control_frame, text='Check difference', variable=self.operation_type,
value=2, tristatevalue=5, command=lambda: self.status_label.configure(text='Ready'))
self.difference.grid(row=3, column=0, sticky='w')
self.streamhash = tk.Radiobutton(self.control_frame, text='FFmpeg', variable=self.operation_type,
value=3, tristatevalue=5, command=lambda: self.status_label.configure(text='Ready'))
self.streamhash.grid(row=4, column=0, sticky='w')
self.operation_type.set(0)
def open_safe_transfer(self):
x = self.winfo_x()
y = self.winfo_y()
self.transfer_window = tk.Toplevel()
self.transfer_window.geometry(f'650x160+{x+145}+{y+80}')
self.transfer_window.grab_set()
self.transfer_window.title('Safe Transfer')
if os.name == 'nt':
self.transfer_window.iconbitmap(os.path.abspath('./media/paw.ico'))
self.transfer_window.resizable(False, False)
self.transfer_dst_button = tk.Button(self.transfer_window, text='Select target folder:',command=self.select_target_directory)
self.transfer_dst_button.place(relx=0.3,y=20,relwidth=0.4)
self.transfer_dst_entry = tk.Entry(self.transfer_window,state=DISABLED,width=100)
self.transfer_dst_entry.place(x=15,y=70,width=620)
self.hash_type = IntVar()
self.md5_radiobutton = tk.Radiobutton(self.transfer_window, text='md5', variable=self.hash_type,
value=0, tristatevalue=4)
self.md5_radiobutton.place(x=15,y=100)
self.xxHash_radiobutton = tk.Radiobutton(self.transfer_window, text='xxHash', variable=self.hash_type,
value=1, tristatevalue=4)
self.xxHash_radiobutton.place(x=15,y=120)
self.transfer_start_button = tk.Button(self.transfer_window, text='Start transfer',command=lambda: threading.Thread(
target=self.safe_transfer, args=()).start())
self.transfer_start_button.place(relx=0.35,y=110,relwidth=0.3)
def open_ffmpeg_options(self):
x = self.winfo_x()
y = self.winfo_y()
self.ffmpeg_window = tk.Toplevel()
self.ffmpeg_window.geometry(f'600x160+{x+145}+{y+80}')
self.ffmpeg_window.grab_set()
self.ffmpeg_window.title('FFmpeg Options')
if os.name == 'nt':
self.ffmpeg_window.iconbitmap(os.path.abspath('./media/paw.ico'))
self.ffmpeg_window.resizable(False, False)
self.hash_label = tk.Label(
self.ffmpeg_window, text='Hash algorithm', borderwidth=4, relief='groove', width=15)
self.hash_label.grid(row=0, column=0, sticky='W', pady=5)
self.hash_alogorithm = IntVar()
self.md5_radiobutton = tk.Radiobutton(self.ffmpeg_window, text='md5', variable=self.hash_alogorithm,
value=0, tristatevalue=4)
self.md5_radiobutton.grid(row=1, column=0, sticky='W', pady=5)
self.crc32_radiobutton = tk.Radiobutton(self.ffmpeg_window, text='crc32', variable=self.hash_alogorithm,
value=1, tristatevalue=4,state=DISABLED)
self.crc32_radiobutton.grid(row=2, column=0, sticky='W', pady=5)
self.format_label = tk.Label(
self.ffmpeg_window, text='Format', borderwidth=4, relief='groove', width=15)
self.format_label.grid(row=0, column=1, sticky='W', pady=5)
self.ffmpeg_format = IntVar()
self.framemd5_radiobutton = tk.Radiobutton(self.ffmpeg_window, text='framemd5', variable=self.ffmpeg_format,
value=1, tristatevalue=4,state=DISABLED)
self.framemd5_radiobutton.grid(row=2, column=1, sticky='W', pady=5)
self.streamhash_radiobutton = tk.Radiobutton(self.ffmpeg_window, text='Streamhash', variable=self.ffmpeg_format,
value=0, tristatevalue=4)
self.streamhash_radiobutton.grid(row=1, column=1, sticky='W', pady=5)
self.codec_label = tk.Label(
self.ffmpeg_window, text='Codec', borderwidth=4, relief='groove', width=15)
self.codec_label.grid(row=0, column=2, sticky='W', pady=5)
self.ffmpeg_codec = IntVar()
self.raw_radiobutton = tk.Radiobutton(self.ffmpeg_window, text='Raw video', variable=self.ffmpeg_codec,
value=0, tristatevalue=4)
self.raw_radiobutton.grid(row=1, column=2, sticky='W', pady=5)
self.copy_radiobutton = tk.Radiobutton(self.ffmpeg_window, text='Copy (-c copy)', variable=self.ffmpeg_codec,
value=1, tristatevalue=4,state=DISABLED)
self.copy_radiobutton.grid(row=2, column=2, sticky='W', pady=5)
def select_target_directory(self):
dst = filedialog.askdirectory()
self.transfer_dst_entry.configure(state=NORMAL)
self.transfer_dst_entry.insert(0,dst)
self.transfer_dst_entry.configure(state=DISABLED)
def remove_item(self):
try:
items_list = []
for item in self.batch_listbox.curselection():
items_list.append(self.batch_listbox.get(item))
for item_text in items_list:
if item_text[2] == 'F':
self.check_this['F'].remove(item_text.split('"')[1])
elif item_text[2] == 'D':
self.check_this['D'].remove(item_text.split('"')[1])
self.update_batch()
self.batch_label.configure(
text='Queue (%s)' % str(self.batch_listbox.size()))
except:
pass
def add_file(self):
# Opens filedialog window for selecting files to add to the batch.
files = filedialog.askopenfilenames()
for file in files:
if not os.path.abspath(file) in self.check_this['F'] and file != '':
self.check_this['F'].append(os.path.abspath(file))
self.update_batch()
def add_directory(self):
# Opens filedialog window for selecting a folder to add to the batch.
# All the files in the folder (and recursively in the subfolders) will
# be checksummed individually.
dir = filedialog.askdirectory()
if not dir in self.check_this['D']:
if dir != '':
self.check_this['D'].append(os.path.abspath(dir))
self.update_batch()
def add_all_in_directory(self):
# Opens filedialog window for selecting a folder.
# Every file and/or folder inside the chosen directory will be added
# to the batch.
dir = filedialog.askdirectory()
if dir != '':
root, dirs, files = next(os.walk(dir))
for d in dirs:
self.check_this['D'].append(
os.path.abspath(self.checksummer.join_path(root, d)))
for f in files:
if not '.md5' in f and not '.xxh' in f:
self.check_this['F'].append(
os.path.abspath(self.checksummer.join_path(root, f)))
self.update_batch()
def update_batch(self):
# Updates the batch listbox.
self.batch_listbox.delete(0, END)
for file in self.check_this['F']:
self.batch_listbox.insert(END, ' -F - "%s"' % file)
for folder in self.check_this['D']:
self.batch_listbox.insert(END, ' -D - "%s"' % folder)
self.status_label.configure(text='Ready')
self.batch_label.configure(text='Queue (%s)' %
str(self.batch_listbox.size()))
def report(self,transfer=False):
# Generates the report of the fixity check.
ok_count = len(self.checked['Ok'])
corrupted_count = len(self.checked['Corrupted'])
no_checksum_count = len(self.checked['No md5'])
missing_count = len(self.checked['Missing file'])
new_file_count = len(self.checked['New file'])
total_count = ok_count + corrupted_count + no_checksum_count + missing_count + new_file_count
self.report_window = tk.Toplevel(self)
if os.name == 'nt':
self.report_window.iconbitmap(os.path.abspath('./media/paw.ico'))
self.report_window.title('Report')
self.report_yscrollbar = tk.Scrollbar(
self.report_window, orient='vertical')
self.report_yscrollbar.grid(row=0, column=1, sticky='NS')
self.report_text = tk.Text(self.report_window, width=100, height=30,
yscrollcommand=self.report_yscrollbar.set, state=DISABLED, wrap=WORD)
self.report_text.grid(row=0, column=0)
self.report_yscrollbar.configure(command=self.report_text.yview)
self.report_button = tk.Button(
self.report_window, text='Save report', command=self.save_report)
self.report_button.grid(row=1, column=0, pady=5)
self.report_text.configure(state=NORMAL)
self.report_text.delete(1.0, END)
operation = 'checked' if transfer == False else 'transferred'
self.report_text.insert(END, 'Fixity check summary:\n\n---\n%s files/folders %s.\n---\n\nOk: %s.\nCorrupted: %s.\nMissing: %s.\nNew/Unknown: %s.\n---\n\n' %
(str(total_count), operation, str(ok_count), str(corrupted_count), str(missing_count), str(new_file_count)))
if no_checksum_count > 0:
self.report_text.insert(
END, "Sammy couldn't find a valid checksum for %s files/folders.\n------------\n\n\n" % str(no_checksum_count))
else:
self.report_text.insert(
END, 'All the checksums were found.\n------------\n\n\n')
if corrupted_count > 0:
self.report_text.insert(
END, 'The following files/folders are corrupted:\n\n')
for obj in self.checked['Corrupted']:
self.report_text.insert(END, "'" + obj + "'\n")
self.report_text.insert(END, '---\n\n\n')
if missing_count > 0:
self.report_text.insert(
END, 'The following files/folders are missing:\n\n')
for obj in self.checked['Missing file']:
self.report_text.insert(END, "'" + obj + "'\n")
self.report_text.insert(END, '---\n\n\n')
if new_file_count > 0:
self.report_text.insert(
END, 'The following files/folders are new/unknown:\n\n')
for obj in self.checked['New file']:
self.report_text.insert(END, "'" + obj + "'\n")
self.report_text.insert(END, '---\n\n\n')
if no_checksum_count > 0:
self.report_text.insert(
END, "The following files don't have a valid hash:\n\n")
for obj in self.checked['No md5']:
self.report_text.insert(END, "'" + obj + "'\n")
self.report_text.insert(END, '---\n\n\n')
if ok_count > 0:
self.report_text.insert(
END, 'The following files/folders are ok:\n\n')
for obj in self.checked['Ok']:
self.report_text.insert(END, "'" + obj + "'\n")
self.report_text.insert(END, '---\n\n\n')
self.report_text.insert(END, 'Report generated by \'Check Sammy %s\' on %s' % (
self.version, str(datetime.date.today())))
self.report_text.configure(state=DISABLED)
self.status_label.configure(text='Ready')
self.checked = {'Ok': [], 'Corrupted': [],
'No md5': [], 'Missing file': [], 'New file': []}
def save_report(self,auto=False,path=None):
# Stores report in a .txt file.
try:
if auto == True:
date_report = datetime.datetime.now().strftime('%Y%m%d-%H.%M')
report_path = os.path.join(path,f'cs-report_{date_report}.txt')
with open(report_path, 'w+', encoding='utf8') as dot_txt:
dot_txt.write(self.report_text.get(1.0, END))
else:
with open(filedialog.asksaveasfilename(defaultextension='.txt', filetypes=(('Text file', '*.txt'), ('All files', '*.*'))), 'w+', encoding='utf8') as dot_txt:
dot_txt.write(self.report_text.get(1.0, END))
except FileNotFoundError:
pass
def reset(self):
# Empties the batch
self.check_this = {'F': [], 'D': []}
self.checked = {'Ok': [], 'Corrupted': [],
'No md5': [], 'Missing file': [], 'New file': []}
self.batch_listbox.delete(0, END)
self.update_batch()
self.status_label.configure(text='Ready')
def start(self):
# This method starts the selected operation (either create checksums or
# check their difference) for all of the files/folders in the batch listbox.
if self.check_this == {'F': [], 'D': []}:
self.status_label.configure(text='Batch empty')
else:
if self.operation_type.get() == 0:
self.create_md5()
if self.operation_type.get() == 1:
self.create_xxHash()
elif self.operation_type.get() == 2:
self.compare_checksums()
elif self.operation_type.get() == 3:
self.create_streamhash()
# else:
# print(self.operation_type.get())
# self.status_label.configure(text="Sammy's confused...")
def create_md5(self,transfer=False,dst=None):
self.status_label.configure(text='Working...')
workers = mp.Pool(8)
if transfer == False:
workers.map(self.checksummer.save_md5, self.check_this['F'])
else:
args = []
for file in self.check_this['F']:
args.append((file,dst))
workers.starmap(self.checksummer.transfer_save_md5, args)
for dir in self.check_this['D']:
self.hash_dict = {}
self.hash_dict['PARENT FOLDER'] = os.path.basename(dir)
for root, folders, files in os.walk(dir):
a = []
for file in files:
to_hash = self.checksummer.join_path(root, file)
a.append((dir, to_hash))
workers.starmap(self.checksummer.get_hash_dict, a)
js = json.dumps(self.hash_dict, indent=4)
if transfer == False:
with open(dir + '.md5', 'w+') as dot_md5:
dot_md5.write(js)
else:
path = self.checksummer.join_path(dst,dir.split(os.sep)[-1])
with open(path + '.md5', 'w+') as dot_md5:
dot_md5.write(js)
workers.close()
workers.join()
if transfer == False:
self.status_label.configure(text='Done')
def create_xxHash(self,transfer=False,dst=None):
self.status_label.configure(text='Working...')
workers = mp.Pool(8)
if transfer == False:
workers.map(self.checksummer.save_xxHash, self.check_this['F'])
else:
args = []
for file in self.check_this['F']:
args.append((file,dst))
workers.starmap(self.checksummer.transfer_save_xxHash, args)
for dir in self.check_this['D']:
self.hash_dict = {}
self.hash_dict['PARENT FOLDER'] = os.path.basename(dir)
for root, folders, files in os.walk(dir):
a = []
for file in files:
to_hash = self.checksummer.join_path(root, file)
a.append((dir, to_hash))
workers.starmap(self.checksummer.get_xxHash_dict, a)
js = json.dumps(self.hash_dict, indent=4)
if transfer == False:
with open(dir + '.xxh', 'w+') as dot_md5:
dot_md5.write(js)
else:
path = self.checksummer.join_path(dst,dir.split(os.sep)[-1])
with open(path + '.xxh', 'w+') as dot_md5:
dot_md5.write(js)
workers.close()
workers.join()
if transfer == False:
self.status_label.configure(text='Done')
def compare_checksums(self, transfer=False, check_transferred=None):
self.status_label.configure(text='Working...')
to_check = self.check_this if transfer == False else check_transferred
workers = mp.Pool(8)
workers.map(self.checksummer.check_md5, to_check['F'])
a = []
for dir in to_check['D']:
a.append((dir, 1))
workers.starmap(self.checksummer.check_md5, a)
workers.close()
workers.join()
self.report(transfer)
self.status_label.configure(text='Done')
def create_streamhash(self):
# This will ignore folders and only try to streamhash av-files
self.status_label.configure(text='Working...')
for file in self.check_this['F']:
try:
streamhash_dict = {}
streamhash_string = subprocess.check_output(['ffmpeg', '-i', file, '-map', '0', '-f', 'streamhash', '-hash', 'md5', '-']).decode("utf-8")
streamhash_dict['Filename: '] = os.path.basename(file)
for stream in streamhash_string.split('\n')[:-1]:
stream_number = stream.split('=')[0]
md5 = stream.split('=')[1]
streamhash_dict[stream_number] = md5
js = json.dumps(streamhash_dict, indent=4)
with open(file + '.streamhash', 'w+') as dot_streamhash:
dot_streamhash.write(js)
print('----------')
except subprocess.CalledProcessError:
print(f'\n\n----------\nFFmpeg didn\'t understand the input. "{file}" will be skipped.\n----------\n\n')
if len(self.check_this['F']) > 0:
print('\n\n')
else:
print('The FFmpeg option only works with files. Folders in queue have been skipped.\n----------')
self.status_label.configure(text='Done')
def safe_transfer(self):
if self.transfer_dst_entry.get() != '':
dst = self.transfer_dst_entry.get()
self.transfer_window.destroy()
try:
if self.check_this == {'F': [], 'D': []}:
self.status_label.configure(text='Batch empty')
else:
before = datetime.datetime.now()
check_transferred = {'F': [], 'D': []}
print('Calculating checksums...')
if self.hash_type.get() == 0:
self.create_md5(True,dst)
elif self.hash_type.get() == 1:
self.create_xxHash(True,dst)
print('Done\n')
print('Transferring files...')
for file in self.check_this['F']:
new_copy = self.checksummer.join_path(dst,file.split(os.sep)[-1])
if not os.path.isfile(self.checksummer.join_path(dst,file.split(os.sep)[-1])):
#shutil.copy2(file,dst)
if os.name == 'nt':
subprocess.call(['robocopy',os.path.abspath(os.path.dirname(file)),os.path.abspath(dst),os.path.basename(file)])
else:
subprocess.call(['rsync','--archive','--progress',file,dst])
else:
raise FileExistsError
check_transferred['F'].append(new_copy)
print('Done\n')
print('Transferring folders...')
for dir in self.check_this['D']:
new_copy = self.checksummer.join_path(dst,dir.split(os.sep)[-1])
if os.name == 'nt':
subprocess.call(['robocopy',os.path.abspath(dir),os.path.join(os.path.abspath(dst),os.path.basename(dir)),'/E'])
else:
os.mkdir(new_copy)
subprocess.call(['rsync','--archive','--progress',dir,dst])
check_transferred['D'].append(new_copy)
print('Done\n')
print('Comparing difference...')
self.compare_checksums(True,check_transferred)
self.save_report(auto=True,path=dst)
print(f'Done after {datetime.datetime.now()-before}')
print('----------')
except FileExistsError:
print('The destination directory is not empty')
class CheckSammy():
# This class is the actual 'checksum' handler.
def join_path(self,a,b):
return a + '/' + b
def calculate_md5(self, file):
# Calculates the md5 for the selected file.
h = hashlib.md5()
print(f'Calculating hash for {file}')
with open(file, 'rb') as file_data:
file_size = size_to_hash = os.path.getsize(file)
#percent = 0
for chunk in iter(lambda: file_data.read(4096), b''):
h.update(chunk)
return(h.hexdigest())
def calculate_xxHash(self, file):
h = xxhash.xxh64()
print(f'Calculating hash for {file}')
with open(file, 'rb') as file_data:
for chunk in iter(lambda: file_data.read(4096), b''):
h.update(chunk)
return(h.hexdigest())
def check_md5(self, path, switch=0):
# Calculates a new checksum and compares it with the one stored in the json.
target_name = os.path.basename(path) # File or directory
if switch == 0:
if os.path.isfile(path + '.md5'):
previous_checksum = json.load(open(path + '.md5'))[target_name]
new_checksum = self.calculate_md5(path)
if previous_checksum == new_checksum:
puppy.checked['Ok'].append(target_name)
else:
puppy.checked['Corrupted'].append(target_name)
elif os.path.isfile(path + '.xxh'):
previous_checksum = json.load(open(path + '.xxh'))[target_name]
new_checksum = self.calculate_xxHash(path)
if previous_checksum == new_checksum:
puppy.checked['Ok'].append(target_name)
else:
puppy.checked['Corrupted'].append(target_name)
else:
puppy.checked['No md5'].append(target_name)
elif switch == 1:
new_dict = {'PARENT FOLDER': target_name}
if os.path.isfile(path + '.md5'):
previous_dict = json.load(open(path + '.md5'))
for root, folders, files in os.walk(path):
for file in files:
to_hash = self.join_path(root, file)
new_dict[to_hash.replace(
path, '')[1:].replace(os.sep,'/')] = self.calculate_md5(to_hash)
for obj in previous_dict:
if not obj in new_dict:
puppy.checked['Missing file'].append(
self.join_path(new_dict['PARENT FOLDER'], obj))
for obj in new_dict:
if not obj in previous_dict:
puppy.checked['New file'].append(
self.join_path(new_dict['PARENT FOLDER'], obj))
else:
if new_dict[obj] == previous_dict[obj]:
if obj != 'PARENT FOLDER':
puppy.checked['Ok'].append(self.join_path(
new_dict['PARENT FOLDER'], obj))
else:
puppy.checked['Corrupted'].append(
self.join_path(new_dict['PARENT FOLDER'], obj))
elif os.path.isfile(path + '.xxh'):
previous_dict = json.load(open(path + '.xxh'))
for root, folders, files in os.walk(path):
for file in files:
file = file
to_hash = self.join_path(root, file)
new_dict[to_hash.replace(
path, '')[1:].replace(os.sep,'/')] = self.calculate_xxHash(to_hash)
for obj in previous_dict:
if not obj in new_dict:
puppy.checked['Missing file'].append(
self.join_path(new_dict['PARENT FOLDER'], obj))
for obj in new_dict:
if not obj in previous_dict:
puppy.checked['New file'].append(
self.join_path(new_dict['PARENT FOLDER'], obj))
else:
if new_dict[obj] == previous_dict[obj]:
if obj != 'PARENT FOLDER':
puppy.checked['Ok'].append(self.join_path(
new_dict['PARENT FOLDER'], obj))
else:
puppy.checked['Corrupted'].append(
self.join_path(new_dict['PARENT FOLDER'], obj))
else:
puppy.checked['No md5'].append(new_dict['PARENT FOLDER'])
def save_md5(self, file):
# Starts the calculate_md5 method and stores the results in a dictionary.
# Then dumps the dictionary into a json file.
checksum_data = {os.path.basename(file): self.calculate_md5(file)}
js = json.dumps(checksum_data, indent=4)
with open(file + '.md5', 'w+') as file_data:
file_data.write(js)
def save_xxHash(self, file):
checksum_data = {os.path.basename(file): self.calculate_xxHash(file)}
js = json.dumps(checksum_data, indent=4)
with open(file + '.xxh', 'w+') as file_data:
file_data.write(js)
def transfer_save_md5(self, file, dst):
# Starts the calculate_md5 method and stores the results in a dictionary.
# Then dumps the dictionary into a json file in the dst folder.
checksum_data = {os.path.basename(file): self.calculate_md5(file)}
js = json.dumps(checksum_data, indent=4)
path = self.join_path(dst,file.split(os.sep)[-1])
with open(path + '.md5', 'w+') as file_data:
file_data.write(js)
def transfer_save_xxHash(self, file, dst):
checksum_data = {os.path.basename(file): self.calculate_xxHash(file)}
js = json.dumps(checksum_data, indent=4)
path = self.join_path(dst,file.split(os.sep)[-1])
with open(path + '.xxh', 'w+') as file_data:
file_data.write(js)
def get_hash_dict(self, dir, to_hash):
# Starts the calculate_md5 method and stores the results in a dictionary.
puppy.hash_dict[to_hash.replace(dir, '')[1:].replace(os.sep,'/')] = self.calculate_md5(to_hash)
def get_xxHash_dict(self, dir, to_hash):
puppy.hash_dict[to_hash.replace(dir, '')[1:].replace(os.sep,'/')] = self.calculate_xxHash(to_hash)
puppy = SammyGUI()
puppy.mainloop()