# SPDX-License-Identifier: MIT """Census of the MAME romsets this board family covers, read in place. MAME_ROMPATH=... python -I scripts/census.py # summary + per-set lines MAME_ROMPATH=... python -I scripts/census.py --json out.json The driver list comes from the installed MAME binary (`mame -listsource`, filtered by SOURCES below) and each set's expected files from `mame -listxml `; MAME is run as a closed box, its source is never read. Each set's zip under $MAME_ROMPATH (a non-merged collection: every file of a set in its own zip) is opened read-only and every expected file is checked by size, CRC32 and SHA-1. Nothing is copied or extracted to disk. """ from __future__ import annotations import argparse import collections import hashlib import json import os import subprocess import sys import xml.etree.ElementTree as ET import zipfile from pathlib import Path # The MAME source files whose machines this board covers (edit per repository). SOURCES = ("pacman/pacman.cpp", "pacman/pengo.cpp", "pacman/jrpacman.cpp") def mame(*args: str) -> str: return subprocess.run(["nice", "mame", *args], capture_output=True, text=True, check=False).stdout # fmt: skip def machines() -> list[str]: names = [] for line in mame("-listsource").splitlines(): parts = line.split() if len(parts) == 2 and parts[1] in SOURCES: names.append(parts[0]) return sorted(names) def describe(name: str) -> dict: root = ET.fromstring(mame("-listxml", name)) node = next(m for m in root.iter("machine") if m.get("name") == name) return { "name": name, "description": node.findtext("description"), "year": node.findtext("year"), "manufacturer": node.findtext("manufacturer"), "cloneof": node.get("cloneof"), "sampleof": node.get("sampleof"), "samples": [s.get("name") for s in node.iter("sample")], "chips": [f"{c.get('type')}:{c.get('name')}" for c in node.iter("chip")], "status": (node.find("driver").get("status") if node.find("driver") is not None else None), "roms": [ {k: r.get(k) for k in ("name", "size", "crc", "sha1", "status", "region")} for r in node.iter("rom") ], } def check_zip(path: Path, roms: list[dict]) -> tuple[int, int, list[str]]: """(files expected with a known dump, files verified, problems).""" wanted = [r for r in roms if r.get("status") != "nodump" and r.get("crc")] if not path.exists(): return len(wanted), 0, ["zip missing"] ok, problems = 0, [] with zipfile.ZipFile(path) as archive: by_crc = {f"{i.CRC:08x}": i for i in archive.infolist()} for rom in wanted: info = by_crc.get(rom["crc"]) if info is None or info.file_size != int(rom["size"]): problems.append(f"{rom['name']} absent") continue if rom.get("sha1") and hashlib.sha1(archive.read(info)).hexdigest() != rom["sha1"]: problems.append(f"{rom['name']} sha1 differs") continue ok += 1 return len(wanted), ok, problems def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--json", help="write the full census here") args = parser.parse_args() rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("set MAME_ROMPATH (a non-merged MAME collection)", file=sys.stderr) return 2 sets = [describe(n) for n in machines()] chips: collections.Counter[str] = collections.Counter() complete = 0 for entry in sets: expected, ok, problems = check_zip(Path(rompath) / f"{entry['name']}.zip", entry["roms"]) entry.update(expected=expected, verified=ok, problems=problems) complete += expected == ok chips.update(set(entry["chips"])) flag = "ok " if expected == ok else "BAD" clone = f" (clone of {entry['cloneof']})" if entry["cloneof"] else "" samples = f" samples:{entry['sampleof']}x{len(entry['samples'])}" if entry["samples"] else "" print(f"{flag} {entry['name']:12} {ok}/{expected} {entry['year']} {entry['status']:11}" f" {entry['description']}{clone}{samples} {'; '.join(problems[:2])}") # fmt: skip print(f"\n{len(sets)} machines in {', '.join(SOURCES)}; {complete} complete and verified " f"(size, CRC32, SHA-1) in MAME_ROMPATH") # fmt: skip print(f"parents {sum(1 for s in sets if not s['cloneof'])}, clones " f"{sum(1 for s in sets if s['cloneof'])}, needing samples " f"{sum(1 for s in sets if s['samples'])}") # fmt: skip print("chips (machines using each):") for chip, count in chips.most_common(): print(f" {count:4} {chip}") if args.json: Path(args.json).write_text(json.dumps(sets, indent=1), encoding="utf-8") return 0 if __name__ == "__main__": sys.exit(main())