diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py index d035949b3d..8d40bcdf83 100644 --- a/bitbake/lib/bb/utils.py +++ b/bitbake/lib/bb/utils.py @@ -520,22 +520,26 @@ def unlockfile(lf): fcntl.flock(lf.fileno(), fcntl.LOCK_UN) lf.close() +def _hasher(method, filename): + import mmap + + with open(filename, "rb") as f: + try: + with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mm: + for chunk in iter(lambda: mm.read(8192), b''): + method.update(chunk) + except ValueError: + # You can't mmap() an empty file so silence this exception + pass + return method.hexdigest() + + def md5_file(filename): """ Return the hex string representation of the MD5 checksum of filename. """ - import hashlib, mmap - - with open(filename, "rb") as f: - m = hashlib.md5() - try: - with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mm: - for chunk in iter(lambda: mm.read(8192), b''): - m.update(chunk) - except ValueError: - # You can't mmap() an empty file so silence this exception - pass - return m.hexdigest() + import hashlib + return _hasher(hashlib.md5(), filename) def sha256_file(filename): """ @@ -543,24 +547,14 @@ def sha256_file(filename): filename. """ import hashlib - - s = hashlib.sha256() - with open(filename, "rb") as f: - for line in f: - s.update(line) - return s.hexdigest() + return _hasher(hashlib.sha256(), filename) def sha1_file(filename): """ Return the hex string representation of the SHA1 checksum of the filename """ import hashlib - - s = hashlib.sha1() - with open(filename, "rb") as f: - for line in f: - s.update(line) - return s.hexdigest() + return _hasher(hashlib.sha1(), filename) def preserved_envvars_exported(): """Variables which are taken from the environment and placed in and exported