#!/usr/bin/env python3 """Census of the Vectrex cartridge images and system ROMs on this machine. Reads every archive in place (zip with zipfile, 7z through the ``7z`` program's stdout), hashes each member in memory and writes nothing outside this repository. If MAME's software list has been fetched (``reference/src/mame/hash/vectrex.xml``), each image is looked up there by SHA-1 to get MAME's name and cartridge slot type. python -I scripts/census.py > tests/census/census-2026-11-11.txt Columns: size, CRC-32, SHA-1, header, list, file. ``header`` is ``gce`` when the image starts with the ``g GCE`` copyright string the Executive checks for, else ``-``. ``list`` is the MAME software-list name, or ``-`` if unlisted. """ from __future__ import annotations import hashlib import os import subprocess import sys import xml.etree.ElementTree as ET import zipfile import zlib from pathlib import Path def local(variable: str, *parts: str) -> Path: """A local ROM folder from an environment variable (no machine path lives here). Unset, it is a path that does not exist, so the folder counts as absent: NO_INTRO_DIR, MAME_ROMPATH (MAME non-merged), CURATED_ROMS (a frontend library with vectrex/), FRONTEND_BIOS (a frontend's BIOS folder). """ value = os.environ.get(variable) if not value: print(f"{variable} is unset: its folder counts as absent", file=sys.stderr) return Path(value or f"${variable}", *parts) ROOT = Path(__file__).resolve().parents[1] SETS = [ ("No-Intro", local("NO_INTRO_DIR", "GCE - Vectrex")), ("retrobat", local("CURATED_ROMS", "vectrex")), ] BIOS = [ local("MAME_ROMPATH", "vectrex.zip"), local("FRONTEND_BIOS", "vectrex.zip"), local("MAME_ROMPATH", "raaspec.zip"), ] SOFTLIST = ROOT / "reference" / "src" / "mame" / "hash" / "vectrex.xml" def members(path: Path) -> list[tuple[str, bytes]]: if path.suffix == ".zip": with zipfile.ZipFile(path) as archive: return [(name, archive.read(name)) for name in archive.namelist()] if path.suffix == ".7z": listing = subprocess.run( ["7z", "l", "-slt", "-ba", str(path)], capture_output=True, text=True, check=True ).stdout names = [line[7:] for line in listing.splitlines() if line.startswith("Path = ")] out = [] for name in names: data = subprocess.run( ["7z", "e", "-so", str(path), name], capture_output=True, check=True ).stdout out.append((name, data)) return out return [(path.name, path.read_bytes())] def softlist() -> dict[str, tuple[str, str]]: table: dict[str, tuple[str, str]] = {} if not SOFTLIST.exists(): return table for software in ET.parse(SOFTLIST).getroot().iter("software"): slot = "?" for feature in software.iter("feature"): if feature.get("name") == "slot": slot = feature.get("value", "?") for rom in software.iter("rom"): if rom.get("sha1"): table[rom.get("sha1")] = (software.get("name"), slot) return table def row(label: str, data: bytes, table: dict[str, tuple[str, str]]) -> str: sha1 = hashlib.sha1(data).hexdigest() crc = zlib.crc32(data) & 0xFFFFFFFF header = "gce" if data[:5] == b"g GCE" else "-" name, slot = table.get(sha1, ("-", "-")) return f"{len(data):6d} {crc:08x} {sha1} {header:3s} {name:10s} {slot:8s} {label}" def main() -> int: table = softlist() print(f"# MAME software list entries with a SHA-1: {len(table)}") for title, directory in SETS: if not directory.is_dir(): print(f"\n# {title}: {directory} absent, skipped") continue files = sorted(p for p in directory.iterdir() if p.suffix in {".zip", ".7z", ".bin", ".vec"}) print(f"\n# {title}: {directory} ({len(files)} archives)") seen: dict[str, str] = {} for path in files: for name, data in members(path): print(row(f"{path.name} :: {name}", data, table)) seen[hashlib.sha1(data).hexdigest()] = path.name print(f"# {title}: {len(seen)} distinct images") print("\n# System ROMs") for path in BIOS: if not path.exists(): print(f"# missing: {path}") continue for name, data in members(path): print(row(f"{path} :: {name}", data, table)) return 0 if __name__ == "__main__": sys.exit(main())