#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Census of MAME's Taito F2 and F3 sets in $MAME_ROMPATH (listing and hashing only). MAME's own ``-listsource`` names the machines of ``taito/taito_f2.cpp`` and ``taito/taito_f3.cpp``; ``-listxml`` gives each one's ROMs (CRC, SHA-1, size, dump status), chips, display and driver status. Each set is looked up as ``.zip`` in every directory of $MAME_ROMPATH (``;``-separated, MAME's convention; a non-merged set holds its parent's and its devices' ROMs too). A ROM counts as present when a member with its CRC-32 and size is in the zip's central directory; ``--sha1`` also reads and hashes each member. No emulator runs over the library and nothing is copied. MAME_ROMPATH=... python -I scripts/census_mame.py # CRC from the directory MAME_ROMPATH=... python -I scripts/census_mame.py --sha1 # hash every member MAME_ROMPATH=... python -I scripts/census_mame.py --out tests/detectors/census.txt """ from __future__ import annotations import argparse import hashlib import os import subprocess import sys import xml.etree.ElementTree as ET import zipfile from collections import Counter from pathlib import Path SOURCES = ("taito/taito_f2.cpp", "taito/taito_f3.cpp") def machines(mame: str) -> list[str]: out = subprocess.run([mame, "-listsource"], capture_output=True, text=True, check=True) names = [] for line in out.stdout.splitlines(): parts = line.split() if len(parts) == 2 and parts[1] in SOURCES: names.append(parts[0]) return names def listxml(mame: str, names: list[str]) -> ET.Element: out = subprocess.run([mame, "-listxml", *names], capture_output=True, check=True) return ET.fromstring(out.stdout) def find_zip(rompath: list[Path], name: str) -> Path | None: for folder in rompath: candidate = folder / f"{name}.zip" if candidate.is_file(): return candidate return None def check_set(path: Path, roms: list[ET.Element], sha1: bool) -> tuple[int, int, list[str]]: """Return (present, wanted, missing names) for the good-dump ROMs of one machine.""" wanted = [r for r in roms if r.get("status", "good") != "nodump" and r.get("crc")] with zipfile.ZipFile(path) as archive: by_crc: dict[tuple[str, int], zipfile.ZipInfo] = {} for info in archive.infolist(): by_crc[(f"{info.CRC:08x}", info.file_size)] = info present, missing = 0, [] for rom in wanted: info = by_crc.get((rom.get("crc").lower(), int(rom.get("size")))) ok = info is not None if ok and sha1: digest = hashlib.sha1(archive.read(info)).hexdigest() ok = digest == rom.get("sha1") if ok: present += 1 else: missing.append(rom.get("name")) return present, len(wanted), missing def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--sha1", action="store_true", help="hash every member (slower)") parser.add_argument("--mame", default="mame") parser.add_argument("--out", type=Path, help="also write the report here") args = parser.parse_args() env = os.environ.get("MAME_ROMPATH") if not env: print("set MAME_ROMPATH", file=sys.stderr) return 2 rompath = [Path(p) for p in env.split(";") if p] root = listxml(args.mame, machines(args.mame)) lines: list[str] = [] totals: Counter[str] = Counter() status: Counter[tuple[str, str]] = Counter() for source in SOURCES: lines.append(f"== {source}") for m in root.findall("machine"): if m.get("sourcefile") != source: continue name = m.get("name") parent = m.get("cloneof") or "-" driver = m.find("driver") emul = driver.get("emulation") if driver is not None else "?" disp = m.find("display") screen = ( f"{disp.get('width')}x{disp.get('height')}@{float(disp.get('refresh')):.4f}" if disp is not None else "-" ) roms = m.findall("rom") nodump = sum(1 for r in roms if r.get("status") == "nodump") path = find_zip(rompath, name) if path is None: verdict = "MISSING" else: present, wanted, missing = check_set(path, roms, args.sha1) verdict = "complete" if present == wanted else f"incomplete {present}/{wanted}" if missing: verdict += " (" + ",".join(missing[:4]) + ("..." if len(missing) > 4 else "") + ")" totals[(source, verdict.split()[0])] += 1 status[(source, emul)] += 1 year = m.findtext("year") or "?" lines.append( f"{name:10} {parent:10} {year:5} {emul:11} {screen:22} roms={len(roms):3} " f"nodump={nodump} {verdict}" ) lines.append("== totals") for (source, verdict), count in sorted(totals.items()): lines.append(f"{source} {verdict}: {count}") for (source, emul), count in sorted(status.items()): lines.append(f"{source} driver emulation {emul}: {count}") lines.append(f"hash check: {'SHA-1 of every member' if args.sha1 else 'CRC-32 and size from the zip directory'}") report = "\n".join(lines) print(report) if args.out: args.out.write_text(report + "\n", encoding="utf-8") return 0 if __name__ == "__main__": sys.exit(main())