Spaces:
Sleeping
Sleeping
File size: 1,116 Bytes
3e3ee14 | 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 27 28 29 30 31 32 33 34 35 | """Small disk caches shared by independent Morph processes."""
import fcntl
import os
import tempfile
from contextlib import contextmanager
from pathlib import Path
def cache_root() -> Path:
"""Use MORPH_CACHE_DIR, otherwise the user's XDG cache directory."""
default = Path(os.environ.get("XDG_CACHE_HOME", Path.home() / ".cache")) / "morph"
return Path(os.environ.get("MORPH_CACHE_DIR", default)).expanduser()
@contextmanager
def file_lock(path):
"""Serialize writers and first-use readers of a shared cache file."""
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
with Path(f"{path}.lock").open("a") as lock:
fcntl.flock(lock, fcntl.LOCK_EX)
try:
yield
finally:
fcntl.flock(lock, fcntl.LOCK_UN)
def atomic_write(path: Path, data: bytes) -> None:
"""Replace a cache file only after its complete contents have been written."""
with tempfile.TemporaryDirectory(dir=path.parent) as staging:
temporary = Path(staging) / path.name
temporary.write_bytes(data)
os.replace(temporary, path)
|