utils.py: add sha256sum(filepath)

This commit is contained in:
InsanePrawn 2022-09-09 20:49:18 +02:00
parent f77aa4f2a2
commit b31160146b

View file

@ -1,5 +1,6 @@
import atexit import atexit
import grp import grp
import hashlib
import logging import logging
import os import os
import pwd import pwd
@ -116,3 +117,14 @@ def read_files_from_tar(tar_file: str, files: Sequence[str]) -> Generator[tuple[
fd = index.extractfile(index.getmember(path)) fd = index.extractfile(index.getmember(path))
assert fd assert fd
yield path, fd yield path, fd
# stackoverflow magic from https://stackoverflow.com/a/44873382
def sha256sum(filename):
h = hashlib.sha256()
b = bytearray(128 * 1024)
mv = memoryview(b)
with open(filename, 'rb', buffering=0) as f:
while n := f.readinto(mv):
h.update(mv[:n])
return h.hexdigest()