renix1

md5 test

Jan 8th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. import hashlib
  2. import glob
  3. import os
  4. import sys
  5.  
  6. def md5(filename):
  7.     """ return a hash of file """
  8.     hash_md5 = hashlib.md5()
  9.     with open(filename, "rb") as file:
  10.         for ped in iter(lambda: file.read(4096), b""):
  11.             hash_md5.update(ped)
  12.     return hash_md5.hexdigest()
  13.  
  14. def list_files():
  15.     """ list files for compare after """
  16.     mds = {}
  17.     for f in glob.glob('*.sh'):
  18.         mds[f] = md5(f)
  19.     return mds
  20.  
  21. def create_changelog():
  22.     """ create a changelog, default filaname: AndroidStudio-0.1.sh, name, version and ext """
  23.     with open('CHANGELOG', 'w') as log:
  24.         for fname in glob.glob('*.sh'):
  25.             log.write("{NAME}{VERSION}/{HASH}\n" .format(NAME=fname[:fname.find('-'):], VERSION=fname[fname.find('-')::], HASH=md5(fname)))
  26.  
  27. def compare():
  28.     """ compare md5 of files with changelog official :d """
  29.     mds = list_files()
  30.     if os.path.isfile('CHANGELOG'):
  31.         with open('CHANGELOG') as log:
  32.             lines = log.readlines()
  33.             for l in lines:
  34.                 fname_current = l[:l.find('/'):]
  35.                 for key in mds:
  36.                     if key in fname_current:
  37.                         current_md5 = mds[key]
  38.                         if current_md5 in l[l.find('/')+1::]:
  39.                             return True
  40.                         else:
  41.                             return False
  42.     else:
  43.         sys.exit(0)
  44.  
  45. compare()
Advertisement
Add Comment
Please, Sign In to add comment