#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Census for the Atari Portfolio: firmware by SHA-1, cartridge-like software, No-Intro. The Portfolio's software lives in its ROMs (DIP DOS, the BIOS and the built-in applications) and on memory cards. This script: 1. asks the installed MAME for the ROMs its ``pofo`` and ``pofo2`` machines want (``mame -listxml``: names, sizes, SHA-1s), then looks for each by SHA-1 inside ``.zip`` in every directory of ``MAME_ROMPATH`` (``;``-separated, as MAME takes it). Zip members are read in place and hashed, never extracted or copied; 2. lists MAME 0.285's CC0 software list for the memory-card slot (``reference/mame-hash/pofo.xml``) and whether a set for it is present; 3. looks for any Portfolio folder under ``NO_INTRO_DIR`` (none existed on 2026-11-26). MAME_ROMPATH=... NO_INTRO_DIR=... python -I scripts/census.py \\ > tests/detectors/census-.txt Firmware is identified by SHA-1 only: never copied, never committed. """ from __future__ import annotations import hashlib import os import subprocess import sys import xml.etree.ElementTree as ET import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] MACHINES = ("pofo", "pofo2") # Device ROMs MAME loads from the machine's zip: the HD61830 device's own character ROM. DEVICES = {"hd61830": "pofo"} SOFTLIST = ROOT / "reference" / "mame-hash" / "pofo.xml" MAME = os.environ.get("MAME", "mame") def wanted(machine: str) -> list[dict[str, str]]: xml = subprocess.run([MAME, "-listxml", machine], capture_output=True, text=True, check=True).stdout root = ET.fromstring(xml) node = next(m for m in root.findall("machine") if m.get("name") == machine) return [dict(rom.attrib) for rom in node.findall("rom")] def zip_hashes(dirs: list[Path], machine: str) -> dict[str, str]: found: dict[str, str] = {} for folder in dirs: path = folder / f"{machine}.zip" if not path.exists(): continue with zipfile.ZipFile(path) as archive: for member in archive.infolist(): found[hashlib.sha1(archive.read(member)).hexdigest()] = f"{path.name}:{member.filename}" return found def main() -> int: rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("set MAME_ROMPATH", file=sys.stderr) return 2 dirs = [Path(p) for p in rompath.split(";") if p] print(f"MAME: {subprocess.run([MAME, '-version'], capture_output=True, text=True).stdout.strip()}") have = need = 0 for machine, zip_name in [(m, m) for m in MACHINES] + list(DEVICES.items()): hashes = zip_hashes(dirs, zip_name) print(f"\n## {machine}" + (f" (device, from {zip_name}.zip)" if machine != zip_name else "")) for rom in wanted(machine): sha1 = rom.get("sha1", "") status = rom.get("status", "good") need += 1 where = hashes.get(sha1) have += bool(where) print(f"{int(rom['size']):7d} {sha1 or '(no dump)':40s} {rom['name']:28s} region={rom.get('region', '-'):8s} " f"bios={rom.get('bios', '-'):8s} status={status:8s} {'HAVE ' + where if where else 'MISSING'}") print(f"\nfirmware: {have} of {need} ROM entries found by SHA-1") print("\n## MAME software list pofo.xml (memory cards)") if SOFTLIST.exists(): for software in ET.parse(SOFTLIST).getroot().findall("software"): roms = [(r.get("name"), r.get("size"), r.get("sha1")) for r in software.iter("rom")] present = any((d / "pofo" / f"{software.get('name')}.zip").exists() for d in dirs) print(f"{software.get('name')}: {software.findtext('description')} {roms} set present: {present}") else: print("missing: run scripts/fetch_references.py") print("\n## No-Intro") nointro = os.environ.get("NO_INTRO_DIR") if nointro: folders = [p.name for p in Path(nointro).iterdir() if "portfolio" in p.name.lower()] print(f"Portfolio folders under NO_INTRO_DIR: {len(folders)} {folders}") else: print("NO_INTRO_DIR not set") return 0 if __name__ == "__main__": sys.exit(main())