INNER CODE UNIT · Python
atomic_write_json
i3T4AN/KADATH · kadath/engine.py:53
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)
def scoped_regular_bytes(root: Path, item: Path) -> bytes:
"""Read a regular file without following organism-created symlinks."""
root = root.absolute(); item = item.absolute(); relative = item.relative_to(root)
cursor = root
for part in relative.parts:
cursor = cursor / part
mode = cursor.lstat().st_mode
if stat.S_ISLNK(mode): raise ValueError(f"symbolic link is not admissible evidence: {relative}")
if not stat.S_ISREG(item.lstat().st_mode): raise ValueError(f"non-regular evidence file: {relative}")