"""Bench configuration: paths and runtime options. Everything is derived from --flags / environment so the same code runs from source (repo checkout) and from a PyInstaller bundle (Tauri sidecar). """ from __future__ import annotations import argparse import os import sys from dataclasses import dataclass, field from pathlib import Path def _bundle_root() -> Path: """Repo root / frozen asset root that contains bench/ assets.""" if getattr(sys, "frozen", False): # PyInstaller (onedir: assets in _internal) return Path(getattr(sys, "_MEIPASS", Path(sys.executable).resolve().parent)) return Path(__file__).resolve().parent.parent.parent @dataclass class BenchConfig: host: str = "127.0.0.1" port: int = 8787 backend: str = "auto" # auto | mock | comfy repo_root: Path = field(default_factory=_bundle_root) @property def bench_dir(self) -> Path: return self.repo_root / "bench" @property def base_workflow_path(self) -> Path: return self.bench_dir / "base_workflow.json" @property def ui_dist(self) -> Path: return self.bench_dir / "ui-dist" @property def work_dir(self) -> Path: default = ( # frozen sidecar: keep renders next to the binary, not in _internal Path(sys.executable).resolve().parent / "work" if getattr(sys, "frozen", False) else self.repo_root / "bench" / "work" ) d = Path(os.environ.get("MOXIE_BENCH_WORK", str(default))) d.mkdir(parents=True, exist_ok=True) return d @property def outputs_dir(self) -> Path: d = self.work_dir / "outputs" d.mkdir(parents=True, exist_ok=True) return d @property def inputs_dir(self) -> Path: d = self.work_dir / "inputs" d.mkdir(parents=True, exist_ok=True) return d @property def temp_dir(self) -> Path: d = self.work_dir / "temp" d.mkdir(parents=True, exist_ok=True) return d @property def pack_models_dir(self) -> Path: return self.repo_root / "models" # --- Comfy bridge backend --- comfyui_url: str = os.environ.get("MOXIE_BENCH_COMFYUI_URL", "http://127.0.0.1:8188") def resolve_backend(self) -> str: """auto: comfy when torch + ComfyUI reachable, else mock.""" if self.backend != "auto": return self.backend from .backends import comfy_bridge_available return "comfy" if comfy_bridge_available(self) else "mock" @classmethod def from_args(cls, argv: list[str] | None = None) -> "BenchConfig": p = argparse.ArgumentParser(prog="moxie-bench") p.add_argument("--host", default=cls.host) p.add_argument("--port", type=int, default=cls.port) p.add_argument( "--backend", choices=["auto", "mock", "comfy"], default=cls.backend, help="mock = self-contained synthetic renderer; comfy = drive a " "running ComfyUI at --comfyui-url with the base workflow", ) p.add_argument("--comfyui-url", default=cls.comfyui_url) p.add_argument( "--repo-root", default=None, help="Root containing bench/ and models/ (default: auto-detect)", ) args = p.parse_args(argv) cfg = cls( host=args.host, port=args.port, backend=args.backend, comfyui_url=args.comfyui_url, ) if args.repo_root: cfg.repo_root = Path(args.repo_root).resolve() return cfg