Add helper function hash_file.

This commit is contained in:
voussoir 2018-03-18 20:56:29 -07:00
parent 2d73e59abf
commit 9a1caee412

View file

@ -4,6 +4,7 @@ codebase but don't deserve to be methods of any class.
'''
import datetime
import hashlib
import math
import mimetypes
import os
@ -161,6 +162,15 @@ def get_mimetype(filepath):
mimetype = mimetypes.guess_type(filepath)[0]
return mimetype
def hash_file(filepath, hasher):
bytestream = read_filebytes(filepath)
for chunk in bytestream:
hasher.update(chunk)
return hasher.hexdigest()
def hash_file_md5(filepath):
return hash_file(filepath, hasher=hashlib.md5())
def hyphen_range(s):
'''
Given a string like '1-3', return numbers (1, 3) representing lower