Download release/engine/modules/common-fd7fce7509ed.py from AI45Research/ManipulationArena: direct link, hf CLI and curl.
- Browser
- Download file 3.07 kB
-
https://huggingface.co/datasets/AI45Research/ManipulationArena/resolve/main/release/engine/modules/common-fd7fce7509ed.py
- Command line
-
hf download hf://datasets/AI45Research/ManipulationArena/release/engine/modules/common-fd7fce7509ed.py
-
curl -L -o common-fd7fce7509ed.py https://huggingface.co/datasets/AI45Research/ManipulationArena/resolve/main/release/engine/modules/common-fd7fce7509ed.py
3.07 kB
| from __future__ import annotations | |
| import hashlib | |
| import json | |
| import os | |
| import tempfile | |
| from contextlib import contextmanager | |
| from datetime import datetime, timezone | |
| from pathlib import Path | |
| from typing import Any | |
| def load_json(path: Path) -> dict[str, Any]: | |
| value = json.loads(path.read_text(encoding="utf-8")) | |
| if not isinstance(value, dict): | |
| raise ValueError(f"expected a JSON object: {path}") | |
| return value | |
| def resolve_path(value: str, relative_to: Path) -> Path: | |
| expanded = Path(os.path.expandvars(value)).expanduser() | |
| return expanded if expanded.is_absolute() else (relative_to / expanded).resolve() | |
| def sha256_file(path: Path) -> str: | |
| digest = hashlib.sha256() | |
| with path.open("rb") as stream: | |
| for chunk in iter(lambda: stream.read(1024 * 1024), b""): | |
| digest.update(chunk) | |
| return digest.hexdigest() | |
| def json_hash(value: Any) -> str: | |
| data = json.dumps(value, sort_keys=True, ensure_ascii=False, separators=(",", ":")) | |
| return hashlib.sha256(data.encode("utf-8")).hexdigest() | |
| def write_json(path: Path, value: Any) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| descriptor, temporary_name = tempfile.mkstemp( | |
| dir=path.parent, | |
| prefix=f".{path.name}.", | |
| suffix=".tmp", | |
| ) | |
| temporary_path = Path(temporary_name) | |
| try: | |
| with os.fdopen(descriptor, "w", encoding="utf-8") as stream: | |
| stream.write(json.dumps(value, ensure_ascii=False, indent=2) + "\n") | |
| stream.flush() | |
| try: | |
| os.fsync(stream.fileno()) | |
| except OSError: | |
| pass | |
| os.replace(temporary_path, path) | |
| finally: | |
| temporary_path.unlink(missing_ok=True) | |
| def advisory_file_lock(path: Path): | |
| """Serialize cooperating writers on a shared filesystem lock file.""" | |
| import fcntl | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| with path.open("a+", encoding="utf-8") as stream: | |
| fcntl.flock(stream.fileno(), fcntl.LOCK_EX) | |
| try: | |
| yield | |
| finally: | |
| fcntl.flock(stream.fileno(), fcntl.LOCK_UN) | |
| def utc_now() -> str: | |
| return datetime.now(timezone.utc).isoformat() | |
| def progress(run_root: Path, stage: str, event: str, **fields: Any) -> None: | |
| path = run_root / "progress.jsonl" | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| entry = {"timestamp_utc": utc_now(), "stage": stage, "event": event, **fields} | |
| with advisory_file_lock(path.with_suffix(".lock")): | |
| with path.open("a", encoding="utf-8") as stream: | |
| stream.write(json.dumps(entry, ensure_ascii=False) + "\n") | |
| stream.flush() | |
| try: | |
| os.fsync(stream.fileno()) | |
| except OSError: | |
| pass | |
| def stage_record(run_root: Path, case_id: str, stage: str, status: str, **fields: Any) -> None: | |
| write_json(run_root / "cases" / case_id / stage / "stage_record.json", { | |
| "stage": stage, | |
| "status": status, | |
| "updated_utc": utc_now(), | |
| **fields, | |
| }) | |