#!/usr/bin/env python3 # SPDX-License-Identifier: MIT # Copies data from MAME's hash/a7800.xml (CC0-1.0); attribution in NOTICE, section 1. """Generate ``src/a7800_python/cartridge/mame_softlist.json`` from MAME's CC0 software list. MAME 0.285's ``hash/a7800.xml`` carries the licence line ``license:CC0-1.0`` in its own header (docs/cartridge.md "How many dumps can be typed"), so its facts may be committed as generated data. This script copies, per entry, only what the loader needs: the name and description, the slot feature (scheme and devices), the region (``compatibility``), the light-gun peripheral, the RAM and NVRAM areas, and each ROM chip's size, CRC-32 and SHA-1 in list order. Nothing else is read and no other table is consulted. python scripts/make_cart_table.py # rewrite the JSON python scripts/make_cart_table.py --check # fail if it would change """ from __future__ import annotations import argparse import json import re import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] HASH = Path("/usr/share/games/mame/hash/a7800.xml") OUT = ROOT / "src/a7800_python/cartridge/mame_softlist.json" def entries(text: str) -> list[dict]: rows = [] for block in re.findall(r"", text, flags=re.DOTALL): def one(pattern: str, default=None, block=block): match = re.search(pattern, block) return match.group(1) if match else default rom_area = re.search(r']*>(.*?)', block, flags=re.DOTALL) roms = [] if rom_area: for rom in re.finditer(r"]*)/>", rom_area.group(1)): attrs = dict(re.findall(r'(\w+)="([^"]*)"', rom.group(1))) if "sha1" in attrs: roms.append([int(attrs["size"], 0), attrs["crc"], attrs["sha1"]]) rows.append({ "name": one(r'(.*?)"), "year": one(r"(.*?)"), "publisher": one(r"(.*?)"), "compatibility": one(r'name="compatibility" value="([^"]+)"'), "slot": one(r'feature name="slot" value="([^"]+)"', "a78_rom"), "lightgun": 'name="peripheral" value="lightgun"' in block, "ram": int(one(r' int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--hash", type=Path, default=HASH) parser.add_argument("--check", action="store_true") args = parser.parse_args() text = args.hash.read_text(encoding="utf-8") if "license:CC0-1.0" not in text[:200]: raise SystemExit(f"{args.hash}: no CC0 licence line in its header; refusing") version = subprocess.run(["mame", "-version"], capture_output=True, text=True).stdout.strip() data = { "source": "MAME hash/a7800.xml (license:CC0-1.0), generated by scripts/make_cart_table.py", "mame_version": version, "entries": entries(text), } rendered = json.dumps(data, indent=1) + "\n" if args.check: same = OUT.exists() and OUT.read_text(encoding="utf-8") == rendered print("unchanged" if same else "would change") return 0 if same else 1 OUT.write_text(rendered, encoding="utf-8") print(f"{len(data['entries'])} entries -> {OUT}") return 0 if __name__ == "__main__": sys.exit(main())