# SPDX-License-Identifier: MIT """Find a family tool at its documented path and run it as a separate program. One variable per tool repository (Aubrey, 2026-11-24; emu-tools ``docs/contract.md``, section 5): ``EMU_TOOLS`` (the MIT emu-tools), ``EMU_TOOLS_GPL`` (emu-tools-gpl) and ``EMU_TOOLS_GPL2`` (emu-tools-gpl2). Each tool sits at a fixed path under its repository, listed in that repository's ``tools/README.md``; this module joins the variable and that path and nothing else. Nothing here imports a tool: they are GPL programs, run by path, and only their outputs (pictures, script logs, ``manifest.json``) are read. Values taken from them are OBSERVED, as each run's manifest says (Mesen2 b9fa69d via mesen2-runner 0.3.0). A tool that is missing is a missing input: :func:`require` prints which variable and path it expected and exits with status 3, as the contract says. """ from __future__ import annotations import os import subprocess import sys from pathlib import Path #: mesen2-runner (GPL-3.0-or-later): Mesen2 b9fa69d headless, machine ``snes``. Its README #: lists every option; it finds the library through ``--lib``, ``MESEN_CORE`` or its own build. MESEN2_RUN = ("EMU_TOOLS_GPL", "tools/mesen2/run.py") #: The S-DSP referee (snes-spc 0.1.0: snes_spc 0.9.0, LGPL-2.1, with its driver), built by #: that tool's own ``build.sh`` inside emu-tools-gpl. This repository neither fetches nor #: keeps snes_spc's source (Aubrey, 2026-11-24: no MIT repository keeps closed-box source). SPC_DUMP = ("EMU_TOOLS_GPL", "tools/snes-spc/build/spc_dump") #: What to do when a tool is there but not built yet, by tool. HINTS = {SPC_DUMP: 'run "$EMU_TOOLS_GPL/tools/snes-spc/build.sh" once to build it'} #: Exit status for a missing input (emu-tools docs/contract.md, section 1). MISSING = 3 def find(tool: tuple[str, str]) -> Path | None: """The tool's path, or None when its variable is unset or the file is absent.""" var, rel = tool root = os.environ.get(var) if not root: return None path = Path(root) / rel return path if path.exists() else None def why_missing(tool: tuple[str, str]) -> str: """The message (and a test's skip reason) naming the variable and the path expected.""" var, rel = tool state = "unset" if not os.environ.get(var) else f"set, but ${var}/{rel} does not exist" hint = f"; {HINTS[tool]}" if tool in HINTS else "" return ( f"${var}/{rel} is needed ({var} {state}): set {var} (emu-tools contract.md, section 5)" f"{hint}" ) def require(tool: tuple[str, str]) -> Path: """The tool's path; exit status 3 with a message naming the variable without it.""" path = find(tool) if path is None: print(why_missing(tool), file=sys.stderr) sys.exit(MISSING) return path def mesen2_frames( rom: Path, frames: list[int], work: Path, *, region: str = "auto", timeout: float = 300, ) -> dict[int, Path | None]: """Mesen2's last frame after each count in ``frames`` (from power-on, no input). One run of ``max(frames)`` frames that writes every count's picture on the way (``--frames-at``, mesen2-runner 0.3.0 on ``snes``: each the same picture a run of that many frames with ``--frames-out`` gives; checked here on the smoke list's 760 pictures, docs/validation.md "One run per game"). Up to 2026-11-24 this was one run per count in a ``--batch`` process. The run's directory is ``work/run/`` (its ``manifest.json`` and ``frame-NNNNNN.png``, 256x239 or 512x478, overscan included). ``region`` is ``--region`` (``auto`` is Mesen2's own choice from the dump, as the old driver left it); power-on RAM is the runner's default, zeros, recorded in the manifest. ``timeout`` is the run's ``--timeout``. Returns each count's PNG, or None where the run gave none. """ runner = require(MESEN2_RUN) rom, work = rom.resolve(), work.resolve() work.mkdir(parents=True, exist_ok=True) counts = sorted(set(frames)) out = work / "run" command = [ sys.executable, "-I", str(runner), "--machine", "snes", "--rom", str(rom), "--out", str(out), "--frames", str(counts[-1]), "--frames-at", ",".join(map(str, counts)), "--region", region, "--timeout", str(timeout), ] # fmt: skip try: subprocess.run(command, capture_output=True, check=False, timeout=timeout + 120) except subprocess.TimeoutExpired: pass # the run has its own --timeout; the pictures it wrote before then are kept found: dict[int, Path | None] = {} for n in frames: png = out / f"frame-{n:06d}.png" found[n] = png if png.exists() else None return found