python/build/verify: move code to feed_file()

This commit is contained in:
Max Kellermann 2016-12-29 21:23:54 +01:00
parent e334b16aaa
commit 8bfabbe265

View File

@ -1,13 +1,24 @@
import hashlib import hashlib
def file_md5(path): def feed_file(h, f):
"""Calculate the MD5 checksum of a file and return it in hexadecimal notation.""" """Feed data read from an open file into the hashlib instance."""
with open(path, 'rb') as f:
m = hashlib.md5()
while True: while True:
data = f.read(65536) data = f.read(65536)
if len(data) == 0: if len(data) == 0:
# end of file # end of file
return m.hexdigest() break
m.update(data) h.update(data)
def feed_file_path(h, path):
"""Feed data read from a file (to be opened by this function) into the hashlib instance."""
with open(path, 'rb') as f:
feed_file(h, f)
def file_md5(path):
"""Calculate the MD5 checksum of a file and return it in hexadecimal notation."""
h = hashlib.md5()
feed_file_path(h, path)
return h.hexdigest()