INNER CODE UNIT · Python
hash_file
i3T4AN/KADATH · kadath/engine.py:43
def hash_file(path: Path) -> tuple[int, str]:
hasher = hashlib.sha256(); size = 0
with path.open("rb") as stream:
while True:
chunk = stream.read(1024 * 1024)
if not chunk: break
size += len(chunk); hasher.update(chunk)
return size, hasher.hexdigest()
def atomic_write_json(path: Path, payload: Any) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
temporary = path.with_name(path.name + ".tmp-" + uuid.uuid4().hex)
encoded = json.dumps(payload, indent=2, sort_keys=True, default=str)
with temporary.open("w") as stream:
stream.write(encoded); stream.flush(); os.fsync(stream.fileno())
temporary.replace(path)