File size: 2,637 Bytes
78a3b51 | 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | """Resource and renderer setup, before LIBERO or OpenGL is imported."""
import hashlib
import json
import os
from pathlib import Path
import sys
import tempfile
ROOT = Path(__file__).resolve().parent.parent
RESOURCES = Path(__file__).resolve().parent / "resources"
def configure(*, renderer=None, config_dir=None):
selected = renderer or os.environ.get("MUJOCO_GL", "osmesa")
if selected not in ("osmesa", "egl"):
raise ValueError("renderer must be osmesa or egl")
if "mujoco" in sys.modules and os.environ.get("MUJOCO_GL") != selected:
raise RuntimeError("renderer is already initialized; use a fresh process")
os.environ["MUJOCO_GL"] = os.environ["PYOPENGL_PLATFORM"] = selected
for name in ("OMP_NUM_THREADS", "OPENBLAS_NUM_THREADS", "MKL_NUM_THREADS"):
if not os.environ.get(name, '').isdigit() or int(os.environ[name]) < 1:
os.environ[name] = "1"
base = ROOT / "libero/libero"
extension = ROOT / "libero_rotation_planning"
paths = dict(benchmark_root=str(base), assets=str(base / "assets"),
bddl_files=str(extension / "bddl_files"),
init_states=str(extension / "init_files"),
datasets=str(RESOURCES / "expert_reference/dataset"))
cache = Path(os.environ.get("XDG_CACHE_HOME", Path.home() / ".cache"))
default = cache / "libero-geomotion" / hashlib.sha256(str(ROOT).encode()).hexdigest()[:16]
target = Path(config_dir or os.environ.get("LIBERO_CONFIG_PATH", default)).resolve()
existing_config = getattr(sys.modules.get("libero.libero"), "config_file", None)
if existing_config is not None:
existing = Path(existing_config).resolve()
if existing != target / "config.yaml":
raise RuntimeError("LIBERO config already initialized; use a fresh process")
target.mkdir(parents=True, exist_ok=True)
contents = json.dumps(paths, indent=2) + "\n"
path = target / "config.yaml"
if not path.exists() or path.read_text() != contents:
with tempfile.NamedTemporaryFile(mode="w", dir=target, delete=False) as stream:
stream.write(contents)
temporary = stream.name
os.replace(temporary, path)
os.environ["LIBERO_CONFIG_PATH"] = str(target)
return paths
def reference_manifest():
return json.loads((RESOURCES / "expert_reference/reference_manifest.json").read_text())
def evaluation_protocol():
return json.loads((RESOURCES / "evaluation_protocol.json").read_text())
def task_manifest():
path = ROOT / "libero_rotation_planning/benchmark/task_manifest.json"
return json.loads(path.read_text())
|