Advertisement
narthollis

Modaco Kitchen Checksum

Nov 19th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. import hashlib
  4. import os.path
  5.  
  6. def md5_for_file(f, block_size=2**20):
  7.     md5 = hashlib.md5()
  8.     while True:
  9.         data = f.read(block_size)
  10.         if not data:
  11.             break
  12.         md5.update(data)
  13.     print md5.hexdigest()
  14.     return md5
  15.  
  16. def checksumModacoFile(path):
  17.     sourceChecksum = os.path.basename(path)
  18.     sourceChecksum = sourceChecksum.lower()
  19.     sourceChecksum = sourceChecksum.split('md5',1)[1].strip('-')
  20.     sourceChecksum = sourceChecksum.split('.')[0]
  21.  
  22.     checksum = None
  23.     with open(os.path.abspath(path), 'rb') as f:
  24.         checksum = md5_for_file(f)
  25.  
  26.     return checksum.hexdigest() == sourceChecksum
  27.  
  28. if __name__ == "__main__":
  29.     import sys
  30.  
  31.     files = sys.argv
  32.     files.pop(0) # remove this file from list
  33.    
  34.     for file_ in files:
  35.         if checksumModacoFile(file_):
  36.             print '[PASS]', file_
  37.         else:
  38.             print '[FAIL]', file_
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement