30 lines
928 B
Python
30 lines
928 B
Python
# coding: utf-8
|
|
|
|
import hashlib
|
|
import os
|
|
|
|
|
|
def md5(file_path):
|
|
''' Generate md5 hash. '''
|
|
hash_md5 = hashlib.md5()
|
|
with open(file_path, "rb") as f:
|
|
for chunk in iter(lambda: f.read(4096), b""):
|
|
hash_md5.update(chunk)
|
|
return hash_md5.hexdigest()
|
|
|
|
|
|
def create_file_md5(file_path):
|
|
''' Get md5_file_hash of file and store to md5 file. '''
|
|
md5_file_path = f'{file_path}.md5'
|
|
md5_file_hash = md5(file_path)
|
|
# create md5_file
|
|
if not os.path.isfile(md5_file_path):
|
|
with open(md5_file_path, 'w', encoding='utf-8') as md5_file:
|
|
md5_file.write(md5_file_hash)
|
|
else:
|
|
with open(md5_file_path, 'r', encoding='utf-8') as md5_file:
|
|
md5_file_stored = md5_file.read()
|
|
# check md5_file
|
|
assert md5_file_hash == md5_file_stored, (
|
|
"Mosel's md5 don't matched with stored %s.md5 file", file_path)
|
|
return md5_file_hash
|