# SPDX-License-Identifier: MIT """Census a MAME driver family's romsets in $MAME_ROMPATH, read in place. MAME is a closed box: machine descriptions come from running `mame -listxml`, never from its source. Each set's zip is opened read-only and every member is checked against the description by CRC32 and by SHA-1 (computed here). Nothing is copied or extracted to disk. MAME_ROMPATH=... python -I scripts/census.py namco/namcos21.cpp namco/namcos21_c67.cpp \ namco/namcos21_de.cpp namco/gal3.cpp --device namcoc65 --device namcoc67 \ --device namcoc68 --firmware Prints one line per set (ROM count, regions, MAME status, complete or the bad members), totals, and with --firmware the SHA-1 of every ROM held by a device set or a device region (the MCU and DSP firmware the player supplies; never kept in this repository). """ from __future__ import annotations import argparse import hashlib import os import shutil import subprocess import sys import xml.etree.ElementTree as ET import zipfile import zlib def mame() -> str: return os.environ.get("MAME") or shutil.which("mame") or sys.exit("put mame on PATH or set MAME") def sets_for_sources(sources: set[str]) -> list[str]: out = subprocess.run([mame(), "-listsource"], capture_output=True, text=True, check=True).stdout return [p[0] for p in (line.split() for line in out.splitlines()) if len(p) == 2 and p[1] in sources] def listxml(names: list[str]) -> ET.Element: out = subprocess.run([mame(), "-listxml", *names], capture_output=True, check=True).stdout return ET.fromstring(out) def rompaths() -> list[str]: raw = os.environ.get("MAME_ROMPATH") if not raw: sys.exit("set MAME_ROMPATH (a non-merged MAME romset directory; ';' separates several)") return [p for p in raw.split(";") if p] def find_zip(name: str, paths: list[str]) -> str | None: for p in paths: z = os.path.join(p, name + ".zip") if os.path.exists(z): return z return None def members(zpath: str) -> tuple[dict, int]: found = {} with zipfile.ZipFile(zpath) as z: for info in z.infolist(): data = z.read(info) found[f"{zlib.crc32(data):08x}"] = (info.filename, hashlib.sha1(data).hexdigest()) return found, os.path.getsize(zpath) def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("sources", nargs="*", help="MAME source files, e.g. namco/namcos21.cpp") ap.add_argument("--device", action="append", default=[], help="a device set holding firmware (e.g. namcoc67)") ap.add_argument("--firmware", action="store_true", help="list device ROM SHA-1s") a = ap.parse_args() names = sets_for_sources(set(a.sources)) + a.device root = listxml(names) paths = rompaths() totals = {"sets": 0, "parents": 0, "present": 0, "complete": 0, "nodump": 0, "bytes": 0} firmware: dict[tuple[str, str], set[str]] = {} for m in root.iter("machine"): name = m.get("name") if name not in names: continue totals["sets"] += 1 totals["parents"] += m.get("cloneof") is None and m.get("isdevice") != "yes" allroms = m.findall("rom") roms = [r for r in allroms if r.get("status") != "nodump"] totals["nodump"] += len(allroms) - len(roms) drv = m.find("driver") status = drv.get("status") if drv is not None else "device" zpath = find_zip(name, paths) if zpath is None: print(f"{name:14} {len(roms):3} roms {status:12} MISSING") continue totals["present"] += 1 have, size = members(zpath) totals["bytes"] += size bad = [] for r in roms: hit = have.get(r.get("crc")) if hit is None or hit[1] != r.get("sha1"): # A ROM a device set supplies (merged from the device zip) may be absent. if ":" in (r.get("region") or "") and m.get("isdevice") != "yes": continue bad.append(r.get("name")) elif m.get("isdevice") == "yes": firmware.setdefault((r.get("sha1"), r.get("size")), set()).add( f"{name}:{r.get('region')}:{r.get('name')}") if not bad: totals["complete"] += 1 state = "complete" if not bad else "INCOMPLETE " + ",".join(bad) clone = m.get("cloneof") or "-" print(f"{name:14} {len(roms):3} roms {status:12} parent={clone:10} {m.findtext('year')} " f"{state} {m.findtext('description')}") print("totals", totals) if a.firmware: for (sha1, size), users in sorted(firmware.items(), key=lambda kv: sorted(kv[1])[0]): print(f"firmware sha1={sha1} size={size} " + " ".join(sorted(users))) return 0 if __name__ == "__main__": sys.exit(main())