Spaces:
Sleeping
Sleeping
Launch seven-model molecular playground with ZeroGPU, property controls, 3D viewing and downloads
3e3ee14 verified Download runtime/morph/utils/cache.py from maltefranke/morph-playground: direct link, hf CLI and curl.
- Browser
- Download file 1.12 kB
-
https://huggingface.co/spaces/maltefranke/morph-playground/resolve/main/runtime/morph/utils/cache.py
- Command line
-
hf download hf://spaces/maltefranke/morph-playground/runtime/morph/utils/cache.py
-
curl -L -o cache.py https://huggingface.co/spaces/maltefranke/morph-playground/resolve/main/runtime/morph/utils/cache.py
1.12 kB
| """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() | |
| 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) | |