Spaces:
Sleeping
Sleeping
File size: 811 Bytes
19583ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | """Filesystem helpers."""
from __future__ import annotations
import hashlib
import json
import os
from pathlib import Path
def sha256_file(path: str | os.PathLike) -> str:
"""SHA256 of a file, read incrementally so large checkpoints stay out of memory."""
digest = hashlib.sha256()
with Path(path).open('rb') as handle:
while chunk := handle.read(8 << 20):
digest.update(chunk)
return digest.hexdigest()
def write_json_atomic(path: str | os.PathLike, payload) -> None:
"""Write ``payload`` as JSON via a temporary file and an atomic rename."""
path = Path(path)
temporary = path.with_suffix(path.suffix + f'.tmp.{os.getpid()}')
temporary.write_text(json.dumps(payload, indent=2, sort_keys=True) + '\n', encoding='utf-8')
os.replace(temporary, path)
|