-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathanimecheck.py
executable file
·1630 lines (1266 loc) · 62.3 KB
/
animecheck.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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
'''
Version 1.2 2015.01.16
Copyright (c) 2009, Taoufik El Aoumari (v0.2)
Copyright (c) 2012-2015, OmegaPhil (v0.3-) - [email protected]
Copyright (c) 2012-2013, Ricardo Constantino (v0.5, 0.11) - [email protected]
Copyright (c) 2015, djcj (darealshinji) (v1.2) - [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 as
published by the Free Software Foundation.
This program 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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
# TODO: GPL3+? Original author hasn't responded to emails
# Preparing for Python 3
from __future__ import division, print_function, unicode_literals
GPL_NOTICE = '''
Copyright (C) 2012-2015 OmegaPhil
License GPLv3: GNU GPL version 3 <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
'''
import io
import os
import re
import shutil
import sys
import time
import traceback
import zlib
import hashlib
from datetime import datetime, timedelta
from optparse import OptionParser
# Initialising variables
addHashModeFiles = []
VERSION = '1.2' # Remember to update the notice at the top too
addHashFormat = '{name} [{hash}]'
done = 0
currentHashingTask = {}
listingError = False
# Fix Python 2.x input
try:
input = raw_input # pylint: disable=redefined-builtin,undefined-variable
except NameError:
pass
# In Python 2 on Debian, despite the fact that the locale is clearly UTF-8, the
# default encoding is set to ASCII. This is unacceptable - the requirement to
# deal with and print unicode filenames is trivial. In Python 3, the proper
# default of UTF-8 is already set. This was what essentially caused the
# original unicode errors on processing program arguments, and is also
# responsible for the encoding Python decides to use when it has the
# responsibility for writing stdout/err to file
if sys.getdefaultencoding() != 'utf-8':
# Non-UTF-8 encoding in use (probably ASCII) - fixing (the
# setdefaultencoding method is removed after it is set in a site
# customisation script during Python startup I think - hence the reload)
reload(sys) # pylint: disable=undefined-variable
sys.setdefaultencoding('utf-8') # pylint: disable=E1101
def crc32_checksum(filename):
'''CRC32 hashes the passed file, displaying the hashing progress'''
# Initialising variables
crc = 0
done = 0 # pylint: disable=W0621
# Opening file to hash, buffer is large presumably to ensure its read in
# fast
with io.open(filename, "rb") as fileToHash:
buff_size = 65536
size = os.path.getsize(filename)
try:
while True:
# Reading in a chunk of the data
data = fileToHash.read(buff_size)
done += len(data)
# Updating the hashing task status
if data:
currentHashingTask_update(hashedData=len(data),
fileSize=size,
hashedSoFar=done)
# Iteratively hashing the data
if not data:
break
crc = zlib.crc32(data, crc)
# Catching Cntrl+C and exiting
except KeyboardInterrupt:
sys.stdout.write(P_RESET)
sys.exit(1)
# Clearing up terminal and file
sys.stdout.write(P_RESET)
# If the crc hex value is negative, bitwise and it with the maximum 32bit
# value. Apparently this is a 'bit mask' resulting in a 32bit long value
# (rather than an infinitely long value, see
# http://stackoverflow.com/a/7825412). It is also guaranteed to return a
# positive number
if crc < 0:
crc &= 2 ** 32 - 1
# Return 8-digit precision hex integer in uppercase
return "%.8X" % (crc)
def md5_checksum(filename):
'''MD5 hashes the passed file, displaying the hashing progress'''
# Initialising variables
done = 0 # pylint: disable=W0621
# Opening file to hash, buffer is large presumably to ensure its read in
# fast
with io.open(filename, "rb") as fileToHash:
buff_size = 65536
size = os.path.getsize(filename)
# Preparing md5 hash object (disabling pylint error as it can't detect
# the md5 function)
md5Hash = hashlib.md5() # pylint: disable=E1101
try:
while True:
# Reading in a chunk of the data
data = fileToHash.read(buff_size)
done += len(data)
# Updating the hashing task status
if data:
currentHashingTask_update(hashedData=len(data),
fileSize=size,
hashedSoFar=done)
# Iteratively hashing the data
if not data:
break
md5Hash.update(data)
# Catching Cntrl+C and exiting
except KeyboardInterrupt:
sys.stdout.write(P_RESET)
sys.exit(1)
# Clearing up terminal
sys.stdout.write(P_RESET)
# Returning actual hash
return md5Hash.hexdigest()
def ed2k_link(filename):
'''Generates an eD2k link of the passed file, displaying the hashing
progress'''
# Based on radicand's code:
# http://www.radicand.org/edonkey2000-hash-in-python/
# eD2k links article: http://en.wikipedia.org/wiki/Ed2k_URI_scheme
# pylint considers this function to have too many branches?
# pylint: disable=R0912
# Initialising variables
# done is global as the md4_hash function needs to be able to update it
global done # pylint: disable=W0603
done = 0
# Obtaining file size
fileSize = os.path.getsize(filename)
try:
# Preparing md4 hash object. Obtaining a copy perhaps due to speed?
# hashlib does not include this algorithm, but the new method
# delegates to OpenSSL when the algorithm is not found
md4 = hashlib.new('md4').copy
except ValueError as e:
# OpenSSL is probably not available?
sys.stderr.write('eD2k link mode was requested, but an attempt to get '
'at md4 hashing failed - is OpenSSL installed?'
'\n\n%s\n' % (e))
sys.exit(1)
def gen(f):
'''Generator to return data in 9500KB blocks - these are the individual
blocks that are hashed to start with'''
# Initialising variables
# Ensuring a local variable is not created
global done # pylint: disable=W0603
currentBlockData = b''
# Defining a smaller read size that is a factor of 9500KB (9728000B),
# so that we get much finger grained feedback on the read progress
smallBufSize = 972800
while True:
try:
# Looping until a clean 9500KB block has been read
for _ in range(10):
# Reading data and breaking if nothing more has been read
data = f.read(smallBufSize)
if not data:
break
else:
currentBlockData += data
# Updating done
done += len(data)
# Updating the hashing task status
if data:
currentHashingTask_update(hashedData=len(data),
fileSize=fileSize,
hashedSoFar=done)
# Yielding or exiting based on whether the current block of
# data is empty. As this is a generator function and
# currentBlockData accrues data, unless the latter is cleared
# before yielding, its contents will persist
if currentBlockData:
dataToReturn = currentBlockData
currentBlockData = b''
yield dataToReturn
else:
return
# Catching Cntrl+C and exiting
except KeyboardInterrupt:
sys.stdout.write(P_RESET)
f.close()
sys.exit(1)
def md4_hash(data):
'''Returns md4 hash of passed data'''
try:
# Hashing passed block
m = md4()
m.update(data)
# Returning hash
return m
# Catching Cntrl+C and exiting
except KeyboardInterrupt:
sys.stdout.write(P_RESET)
f.close()
sys.exit(1)
with io.open(filename, 'rb') as f:
# Obtaining generator function
a = gen(f)
# Building up a list of md4 hashes associated with 9500KB blocks
hashes = [md4_hash(data).digest() for data in a]
# If only one chunk is present, the hash is already done, otherwise
# concatenate the hashes of all current blocks and hash this
if len(hashes) == 1:
ed2kHash = hashes[0].encode('hex')
else:
ed2kHash = md4_hash(b''.join(hashes)).hexdigest()
# Returning ed2k link
# E.g.: 'ed2k://|file|The_Two_Towers-The_Purist_Edit-Trailer.avi|14997504|965c013e991ee246d63d45ea71954c4d|/'
return ('ed2k://|file|%s|%d|%s|/' %
(os.path.basename(filename).replace(' ', '_'), fileSize,
ed2kHash))
def display_results(fileToHash, obtainedHash, displayMode,
checksumFileHash=None, hashRegex=None):
'''Displays results of a hashing operation'''
# Validating passed display mode
if (displayMode != 'colourHashWithHashInFilename'
and displayMode != 'colourHash'
and displayMode != 'hash'):
# Invalid display mode specified - erroring
sys.stderr.write('ERROR: display_results was called with an invalid '
'display mode (\'%s\')\n' % displayMode)
sys.exit()
if (displayMode == 'colourHashWithHashInFilename'
and hashRegex is None):
# Coloured filename hash was specified, but the regex necessary for
# detecting the hash was not specified - erroring
sys.stderr.write('ERROR: display_results was called with the '
'\'colourHashWithHashInFilename\' display mode, '
'however no regex was passed to detect the filename'
' hash with\n')
sys.exit()
# Dealing with different requested modes
if displayMode == 'colourHashWithHashInFilename':
try:
# Hashes are to be displayed coloured based on success, with an
# approriately-coloured hash in the filename too. Obtaining the
# hash from the filename (penultimate fragment) - remember that re
# does not support POSIX character classes
dest_sum = re.split(r'%s' % hashRegex, fileToHash,
flags=re.IGNORECASE)[-2]
# Setting colours and eventual message output destination
# depending on good/bad hash and registering a corrupt file as
# appropriate
if obtainedHash == dest_sum.upper():
h_in = H_GREEN
outputTo = sys.stdout
else:
h_in = H_RED
currentHashingTask_file_corrupt(fileToHash)
outputTo = sys.stderr
# Obtaining filename fragments before and after the hash
sfile = fileToHash.split(dest_sum)
# Printing results with coloured hash at the beginning and in
# the file path, to the relevant output depending on success
print("%s%s%s %s%s%s%s%s" % (h_in, obtainedHash, H_NULL,
sfile[0], h_in, dest_sum, H_NULL,
sfile[1]), file=outputTo)
except(IndexError, ValueError):
# No CRC32 has been found - outputting calculated value and file
# path
print("%s %s" % (obtainedHash, fileToHash))
# If hashes are to be added to filenames, adding to the list
if options.addHashMode != 'none':
hashedFile = [fileToHash, obtainedHash]
addHashModeFiles.append(hashedFile)
# Registering no hash file
currentHashingTask_file_no_hash(fileToHash)
elif displayMode == 'colourHash':
# Hashes are to be displayed coloured, but any hashes that may be
# present in the relevant filename are to be ignored. obtainedHash is
# uppercased here as md5 hashes are outputted lowercase. Registering a
# corrupt file as appropriate
if obtainedHash.upper() == checksumFileHash.upper():
h_in = H_GREEN
outputTo = sys.stdout
else:
h_in = H_RED
currentHashingTask_file_corrupt(fileToHash)
outputTo = sys.stderr
# Printing results with coloured hash at the beginning and in the
# file path, to the relevant output depending on success
print("%s%s%s %s" % (h_in, obtainedHash, H_NULL, fileToHash),
file=outputTo)
elif displayMode == 'hash':
# No colours are to be displayed - just the hash and file path
print("%s %s" % (obtainedHash, fileToHash))
def normalise_and_validate_files(files, checksumType):
'''Validates then returns a list of given files, ensuring they have
absolute paths'''
# Normalising the file paths to ensure all inputs are absolute paths
normalisedFiles = []
for fileToHash in files:
normalisedFiles.append(os.path.abspath(fileToHash))
# Debug code
#print(normalisedFiles)
# Obtaining common directory root
commonPrefix = os.path.commonprefix(normalisedFiles)
# Ensuring files share a common directory root
if commonPrefix == '':
# If the commonPrefix is blank there is a chance all files are in the
# current directory - making sure this is the case
for fileToHash in normalisedFiles:
if not os.path.isfile(fileToHash):
sys.stderr.write('%s\n%s create mode was requested, but the '
'passed files to hash do not share a common '
'root directory:\n\n%s\n' %
(parser.get_usage(), checksumType,
normalisedFiles))
sys.exit(1)
# It is - setting commonPrefix appropriately (curdir is just '.' -
# forcing it into a usable path)
commonPrefix = os.path.abspath(os.curdir)
# If the prefix is actually a file, fixing. The common prefix could also be
# a directory structure then a common part of the filename - attempting
# to resolve to the underlying directory
if not os.path.isdir(commonPrefix):
commonPrefix = os.path.dirname(commonPrefix)
# Ensuring common directory root is valid
if not os.path.isdir(commonPrefix):
sys.stderr.write('%s\n%s create mode was requested, but the '
'calculated common root directory (\'%s\') of '
'the passed files to hash is not valid:\n\n%s\n' %
(parser.get_usage(), checksumType, commonPrefix,
normalisedFiles))
sys.exit(1)
# Making sure that commonPrefix doesnt have a trailing slash
if commonPrefix[-1:] == os.sep:
commonPrefix = commonPrefix[:-1]
# Returning results
return normalisedFiles, commonPrefix
def recursive_file_search(pathsToSearch):
'''Recurses through all directories and files given to generate a complete
list of files'''
global listingError # pylint: disable=W0603
# Initialising variables
foundFiles = []
sanitisedFiles = []
directoryRecurseWarning = False
# Looping through all passed files and directories
for path in pathsToSearch:
if os.path.isdir(path):
# Warning user that directories are ignored in --no-recurse mode
# and skipping
if options.no_recurse:
if not directoryRecurseWarning:
sys.stderr.write('WARNING: Directory/ies have been '
'detected to hash but will be skipped due'
' to --no-recurse\n\n')
directoryRecurseWarning = True
else:
# Preparing the path to submit to os.walk - in Python 2, if a
# unicode string is passed and a subsequent file/directory has
# invalid bytes (invalid codepoint sequence) in its' name, any
# attempt by Python to manipulate the string will cause an
# unhandled UnicodeDecodeError. Bytes or strings don't have
# this issue as they are raw data and therefore not
# interpreted. In Python 3, strings are unicode by default, and
# the default behaviour for invalid bytes is to represent them
# by surrogate pairs rather than throwing an error
# Note that 'bytes()' in Python 2 is an alias to 'str()'
if sys.version_info.major < 3:
path = str(path)
# Recursively walking through directories discovered. If you
# don't pass an error handler, errors associated with listing
# the passed directory are silently ignored!!
for directory_path, _, directory_files in (
os.walk(path, onerror=walk_error_handler)):
# Adding all discovered files to the main list
for directory_file in directory_files:
foundFiles.append(os.path.join(directory_path,
directory_file))
elif os.path.isfile(path):
foundFiles.append(path)
else:
# Alerting user to invalid path and exiting
sys.stderr.write('Path \'%s\' is invalid\n' % path)
sys.exit(1)
# Looping through all found files
for foundFile in foundFiles:
# Ignoring the file if it is a symlink/hardlink when these are
# requested to be ignored
if options.ignore_links and os.path.islink(foundFile):
continue
# Checking the file paths for invalid bytes in filenames (this can
# happen due to extracting a zip that doesn't properly maintain or deal
# with the encoding the filenames were compressed in, e.g.)
try:
if sys.version_info.major < 3:
# File paths at this point should be bytes - converting to
# unicode (and subsequently catching invalid encoding). Note
# that replacing/escaping invalid characters at this stage
# merely causes a later stage to cock up
sanitisedFiles.append(unicode(foundFile)) # pylint: disable=undefined-variable
else:
# For Python 3, the string is already a unicode one, with
# invalid bytes represented by surrogate pairs. Therefore, to
# detect the invalid data, I need to encode it with UTF-8 to
# return bytes - the encoder will choke and throw an error. I
# don't keep the result as I want a unicode string, not bytes
_ = foundFile.encode('utf-8')
sanitisedFiles.append(foundFile)
except Exception: # pylint: disable=W0703
# Invalid encoding detected - warning user and recording the fact
# a listing error happened. In Python 2, foundFile is a string and
# therefore plain bytes that needs to be decoded to unicode, in
# Python 3 the string is already unicode with surrogate pairs used
# to mark the invalid bytes and is therefore safe to print and
# write
if sys.version_info.major < 3:
badPath = unicode(foundFile, errors='replace') # pylint: disable=undefined-variable
else:
badPath = foundFile
sys.stderr.write('\nERROR: The file \'%s\' has invalid encoding in'
' its path and will not be hashed!\n' % badPath)
listingError = True
# Returning sanitised files
return sanitisedFiles
def walk_error_handler(walkError):
'''Function called when recursive_file_search's os.walk call encounters
errors'''
global listingError # pylint: disable=W0603
# Detecting OSErrors (the only error that is supposed to be propagated)
# These occur during the initial directory listing of os.walk so should
# indicate a directory isn't hashable
if isinstance(walkError, OSError):
# Warning user
sys.stderr.write('\nERROR: Unable to list the directory \'%s\' - files'
' contained will not be hashed!\nReported error: %s\n'
% (walkError.filename, str(walkError)))
else:
# Unknown error detected - warning user
sys.stderr.write('\nERROR: Unexpected error whilst listing files in a '
'directory - please read the error message to see '
'which directory will not be hashed:\n%s' %
str(walkError))
# Recording the fact an error has occurred (this happens before the
# hashing task has been initialised)
listingError = True
def open_file(fileToOpen):
'''Opens files regardless of encoding...'''
# Python cannot Just Read files, even though it is 'unicode capable'. The
# following is basically babysitting the opening process with expected
# encodings
# Attempting to open the given file - tries UTF-16 (which has an implicit
# BOM) and then UTF-8 with BOM (Windowsism) - if the BOM isnt there this
# fact is ignored and the data is treated as proper UTF-8. If none of these
# work, the file is probably invalid data, since ASCII is valid UTF-8
# I have to read the entire file in (dont use read - returns string) as
# otherwise Python doesnt read any data on open and therefore doesn't know
# when the encoding is invalid, thus making this function pointless!
try:
return io.open(fileToOpen, encoding='utf-16').readlines()
except Exception: # pylint: disable=W0703
try:
return io.open(fileToOpen, encoding='utf-8-sig').readlines()
except Exception as e: # pylint: disable=W0703
# File is invalid - warning user and exiting
sys.stderr.write('\nERROR: Unable to open \'%s\' - invalid'
' encoding? Reported error:\n\n%s' %
(fileToOpen, str(e)))
sys.exit(1)
def humanise_bytes(byteCount, precision=2):
'''Return a humanised string representation of a number of bytes.
>>> humanise_bytes(1)
'1 byte'
>>> humanise_bytes(1024)
'1.0 kB'
>>> humanise_bytes(1024*123)
'123.0 kB'
>>> humanise_bytes(1024*12342)
'12.1 MB'
>>> humanise_bytes(1024*12342,2)
'12.05 MB'
>>> humanise_bytes(1024*1234,2)
'1.21 MB'
>>> humanise_bytes(1024*1234*1111,2)
'1.31 GB'
>>> humanise_bytes(1024*1234*1111,1)
'1.3 GB'
'''
# Modified from Version 2
# Source: http://code.activestate.com/recipes/577081-humanized-representation-of-a-number-of-bytes/
# License: See COPYING.MIT
# Bitshifting n digits to the left (more significant bits direction), every
# 10 = multiplying by 2^10 = 1024
abbrevs = (
(1 << 50, 'PB'),
(1 << 40, 'TB'),
(1 << 30, 'GB'),
(1 << 20, 'MB'),
(1 << 10, 'kB'),
(1, 'bytes')
)
if byteCount == 1:
return '1 byte'
for factor, suffix in abbrevs:
if byteCount >= factor:
break
# Obtaining initial formatted byte count (decimal of user-defined
# precision from the given float). This keeps insignificant
# trailing zeros which must be removed. g does this but takes in the data
# as a double, and therefore formats it with exponentiation which is not
# appropriate
formattedByteCount = '%.*f' % (precision, byteCount / factor) # pylint: disable=W0631
# Removing trailing zeros and decimal place then returning
return formattedByteCount.rstrip('0').rstrip('.') + suffix # pylint: disable=W0631
def get_date(ctime):
'''Function to return a local date in standard format'''
# Converting given ctime to struct_time based off local time
localTime = time.localtime(ctime)
# Generating date parts and ensuring they are correctly zero-padded as
# appropriate
timeDay = (str(localTime.tm_mday) if len(str(localTime.tm_mday)) == 2
else '0' + str(localTime.tm_mday))
timeMonth = (str(localTime.tm_mon) if len(str(localTime.tm_mon)) == 2
else '0' + str(localTime.tm_mon))
timeYear = str(localTime.tm_year)[-2:]
timeHour = (str(localTime.tm_hour) if len(str(localTime.tm_hour)) == 2
else '0' + str(localTime.tm_hour))
timeMinute = (str(localTime.tm_min) if len(str(localTime.tm_min)) == 2
else '0' + str(localTime.tm_min))
timeSecond = (str(localTime.tm_sec) if len(str(localTime.tm_sec)) == 2
else '0' + str(localTime.tm_sec))
# Returning standard format
return '%s/%s/%s %s:%s:%s' % (timeDay, timeMonth, timeYear, timeHour,
timeMinute, timeSecond)
def currentHashingTask_initialise(files):
'''Initialises current hashing task record'''
# Persisting currentHashingTask changes
global currentHashingTask # pylint: disable=W0603
# Defining here so it is in one place only
currentHashingTask = {
# Time task has started
'hashStartTime': 0,
'dataHashed': 0,
'dataToHash': 0,
'filesToHash': 0,
'filesHashed': 0,
'lastUpdatedSpeed': 0,
'lastUpdatedTime': 0,
# Records amount hashed on last terminal update
'lastUpdatedDataHashed': 0,
# Speeds are initialised as strings in case 0 is a
# valid recorded speed
'speed1': '',
'speed2': '',
'speed3': '',
'speed4': '',
# Cached speed value to avoid constantly changing
# output
'cachedAverageSpeed': '',
# Time at which the value was cached
'cachedAverageSpeedTime': 0,
'corruptFileCount': 0,
'noHashFileCount': 0,
'errorFileNotFoundCount': 0,
'errorOtherCount': 0}
# Looping for all passed files
for fileToHash in files:
try:
# Recording the scale of the task ahead
currentHashingTask['filesToHash'] += 1
currentHashingTask['dataToHash'] += os.path.getsize(fileToHash)
# Skipping all errors (probably caused by the above getsize) - the
# actual hashing code will raise the relevant errors
except: # pylint: disable=W0702
continue
# Setting start time
currentHashingTask['hashStartTime'] = time.time()
def currentHashingTask_update(hashedData=0, fileSize=0, hashedSoFar=0,
fileHashed=False):
'''Updates current hashing task record'''
# Persisting currentHashingTask changes
global currentHashingTask # pylint: disable=global-variable-not-assigned
# Updating task - splitting based on whether the update is more data
# hashed or a file has been completed
if hashedData != 0:
# Validating hashedData
if hashedData > currentHashingTask['dataToHash']:
raise Exception('currentHashingTask_update was informed that %dB '
'had been hashed, however only %dB remains to '
'hash' % (hashedData,
currentHashingTask['dataToHash']))
# TODO: Bug here in sfv creation mode - have had an instance of the
# above. Can't recreate though... 14.01.13: Another instance after
# hashing >400,000 files...
# Updating data amounts
currentHashingTask['dataHashed'] += hashedData
currentHashingTask['dataToHash'] -= hashedData
# Obtaining duration
lastUpdatedTime = (currentHashingTask['lastUpdatedTime']
if currentHashingTask['lastUpdatedTime']
else currentHashingTask['hashStartTime'])
duration = time.time() - lastUpdatedTime
# Debug code
#print('\n' + str(duration) + '\n')
# Checking if at least half a second has elapsed since the last
# average calculation. Running stats off the raw reads seems to
# result in largely overblown speeds?
if duration >= 0.5:
# It has - obtaining hashedData during the interval
hashedData = (currentHashingTask['dataHashed'] -
currentHashingTask['lastUpdatedDataHashed'])
# Updating recorded speeds
if (currentHashingTask['lastUpdatedSpeed'] == 0 or
currentHashingTask['lastUpdatedSpeed'] == 4):
# No speed yet recorded or the records have looped
currentHashingTask['speed1'] = hashedData / duration
currentHashingTask['lastUpdatedSpeed'] = 1
elif currentHashingTask['lastUpdatedSpeed'] == 1:
currentHashingTask['speed2'] = hashedData / duration
currentHashingTask['lastUpdatedSpeed'] = 2
elif currentHashingTask['lastUpdatedSpeed'] == 2:
currentHashingTask['speed3'] = hashedData / duration
currentHashingTask['lastUpdatedSpeed'] = 3
elif currentHashingTask['lastUpdatedSpeed'] == 3:
currentHashingTask['speed4'] = hashedData / duration
currentHashingTask['lastUpdatedSpeed'] = 4
# Updating lastUpdatedTime and lastUpdatedDataHashed
currentHashingTask['lastUpdatedTime'] = time.time()
currentHashingTask['lastUpdatedDataHashed'] = currentHashingTask['dataHashed']
# Determining average speed
# Creating a list of the speeds
speedList = [currentHashingTask['speed1'],
currentHashingTask['speed2'],
currentHashingTask['speed3'],
currentHashingTask['speed4']]
# Calculating the average speed. Denominator works as a speed has
# not been recorded if it is '' - cant use a truth test as 0 may
# be a valid result
speedSum = sum([speed for speed in speedList if speed != ''])
records = sum([1 for speed in speedList if speed != ''])
if records:
averageSpeed = humanise_bytes(speedSum / records) + "/Sec"
else:
averageSpeed = '0B/Sec'
# Updating terminal - print digit in 7 character field with right
# justification, but only if progress output is desired
if not options.no_progress:
if fileSize > 0:
sys.stdout.write('%7d%% %s%s' % (hashedSoFar * 100 /
fileSize, averageSpeed,
P_RESET))
else:
sys.stdout.write('%7d%% %s%s' % (100, averageSpeed,
P_RESET))
sys.stdout.flush()
elif fileHashed:
# Updating file counts
currentHashingTask['filesToHash'] -= 1
currentHashingTask['filesHashed'] += 1
# Resetting lastUpdatedTime
currentHashingTask['lastUpdatedTime'] = time.time()
else:
# Function has been called without valid parameters - raising error
raise Exception('currentHashingTask_update was called with either no '
'parameters or default parameters')
def currentHashingTask_error(e):
'''Registers an error with the current hashing task record'''
# Persisting currentHashingTask changes
global currentHashingTask # pylint: disable=global-variable-not-assigned
# Dealing with various errors
if isinstance(e, IOError) and e.errno == 2:
# File not found error
currentHashingTask['errorFileNotFoundCount'] += 1
else:
# Any other error - not aware of details yet
currentHashingTask['errorOtherCount'] += 1
def currentHashingTask_file_corrupt(corruptFile):
'''Registers a corrupt file with the current hashing task record'''
# Persisting currentHashingTask changes
global currentHashingTask # pylint: disable=global-variable-not-assigned
# Updating corrupt file count
currentHashingTask['corruptFileCount'] += 1
def currentHashingTask_file_no_hash(noHashFile):
'''Registers a file with no hash information with the current hashing task
record'''
# Persisting currentHashingTask changes
global currentHashingTask # pylint: disable=global-variable-not-assigned
# Updating no hash file count
currentHashingTask['noHashFileCount'] += 1
def currentHashingTask_summary():
'''Generates a summary of the completed current hashing task'''
# Exiting if the user has quashed summaries
if options.no_summary:
return
# Obtaining stats to display
notFoundCount = currentHashingTask['errorFileNotFoundCount']
otherErrorCount = currentHashingTask['errorOtherCount']
fileCount = (currentHashingTask['filesHashed'] + notFoundCount +
otherErrorCount)
filesDescription = 'files' if fileCount > 1 else 'file'
corruptCount = currentHashingTask['corruptFileCount']
noHashCount = currentHashingTask['noHashFileCount']
successCount = (currentHashingTask['filesHashed'] - corruptCount -
noHashCount)
started = currentHashingTask['hashStartTime']
finished = time.time()
elapsed = timedelta(seconds=(finished - started))
dataHashed = humanise_bytes(currentHashingTask['dataHashed'])
averageSpeed = humanise_bytes(currentHashingTask['dataHashed'] /
elapsed.total_seconds())
# Displaying summary of hashing task
print('\nHashing task complete: %d %s, %d not found, %d other error, %d'
' corrupt, %d no hash, %d hashed successfully' % (fileCount,
filesDescription,
notFoundCount,
otherErrorCount,
corruptCount,
noHashCount,
successCount))
print('Started %s, finished %s, elapsed %s, %s hashed (%s/Sec)\n'
% (get_date(started), get_date(finished), elapsed, dataHashed,
averageSpeed))
# Alerting user to the fact listing errors have happened - otherwise the
# potential 0 errors reported above is a lie
if listingError:
sys.stderr.write('WARNING: Some directories/files were not hashed due '
'to failures in the initial discovery stage - please '
'see errors noted before hashing started\n\n')
def hash_files(files, algorithm, hashInFilename=False):
'''Hashes passed files with the algorithm requested, displays results and
adds resulting hashes to filenames when requested'''
# Validating algorithm
if algorithm != 'crc32' and algorithm != 'md5':
# Invalid algorithm specified - erroring
sys.stderr.write('ERROR: hash_files was called with an invalid hashing'
' algorithm (\'%s\')\n' % algorithm)
sys.exit()
# Converting potential passed directories into their nested files
files = recursive_file_search(files)
# Initialising hashing task (files are the leftover arguments from the
# OptionParser processing)
currentHashingTask_initialise(files)
# Looping through files to process
for fileToHash in files:
try:
# Hashing file with appropriate algorithm
if algorithm == 'crc32':
checksum = crc32_checksum(fileToHash)
elif algorithm == 'md5':
checksum = md5_checksum(fileToHash)
# Updating hashing task
currentHashingTask_update(fileHashed=True)
# Displaying results with appropriate mode
if hashInFilename:
display_results(fileToHash, checksum,
'colourHashWithHashInFilename', None,
'([a-f0-9]{8})')
else:
display_results(fileToHash, checksum, 'hash')
except Exception as e: # pylint: disable=W0703
# Informing user
sys.stderr.write('\nERROR: Failed to %s hash the file \'%s\':\n\n'
'%s\n\n%s\n' % (algorithm, fileToHash, e,
traceback.format_exc()))
# Registering error and moving to next file
currentHashingTask_error(e)
continue
# Displaying a summary of the hashing task's progress - also adds a note
# about listing errors
currentHashingTask_summary()
# Exiting if hashes are not to be added to filenames or there are no files