# SPDX-License-Identifier: MIT """Count the Amstrad CPC firmware and software on hand, in place (docs/census.md). MAME_ROMPATH=... python -I scripts/census.py firmware NO_INTRO_DIR=... python -I scripts/census.py nointro CPC_MIRROR_DIRS=dir1:dir2 python -I scripts/census.py library MAME_HASHPATH=... python -I scripts/census.py softlists Nothing is copied: archives are opened read-only and their members hashed or sniffed in memory. Paths come only from environment variables (the family's rule): MAME_ROMPATH for firmware, NO_INTRO_DIR for the No-Intro sets, and CPC_MIRROR_DIRS (a proposed new variable, os.pathsep-separated) for the folders that hold other CPC collections, since the mirror has no No-Intro or TOSEC CPC set. Firmware is reported by SHA-1 only. ``firmware`` asks the installed MAME (``mame -listxml``) which ROMs each CPC driver needs, then checks each member of ``.zip`` under MAME_ROMPATH against it. ``library`` counts the files inside each folder's zips by type, and sorts disc images by their header (standard "MV - CPC" against "EXTENDED CPC DSK"), tapes (CDT/TZX) by their header version, snapshots by their version byte, and cartridges by their RIFF form ("AMS!"). """ from __future__ import annotations import collections import hashlib import os import subprocess import sys import xml.etree.ElementTree as ET import zipfile from pathlib import Path DRIVERS = ("cpc464", "cpc664", "cpc6128", "cpc6128f", "cpc6128s", "cpc6128sp", "cpc464p", "cpc6128p", "gx4000", "kccomp") # fmt: skip SOFTLISTS = ("cpc_cass", "cpc_flop", "gx4000") def listxml(drivers: tuple[str, ...]) -> ET.Element: out = subprocess.run(["mame", "-listxml", *drivers], capture_output=True, text=True, check=True) return ET.fromstring(out.stdout) def firmware() -> int: rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("set MAME_ROMPATH", file=sys.stderr) return 2 root = listxml(DRIVERS) missing = 0 for machine in root.iter("machine"): name = machine.get("name") roms = [r for r in machine.iter("rom") if r.get("sha1")] if not roms: continue parent = machine.get("romof") description = machine.findtext("description") archive = Path(rompath) / f"{name}.zip" members: dict[str, str] = {} if archive.exists(): with zipfile.ZipFile(archive) as handle: for info in handle.infolist(): members[info.filename] = hashlib.sha1(handle.read(info)).hexdigest() kind = "device" if machine.get("isdevice") == "yes" else "bios" if machine.get("isbios") == "yes" else "driver" print(f"{name} ({kind}, {description}){' parent ' + parent if parent else ''}: " f"{'present' if archive.exists() else 'NO ZIP'}") # fmt: skip for rom in roms: sha1 = rom.get("sha1") state = "ok" if sha1 in members.values() else "MISSING" missing += state != "ok" print(f" {rom.get('name'):24s} {int(rom.get('size')):6d} {sha1} {state}") print(f"missing members: {missing}") return 0 def nointro() -> int: base = os.environ.get("NO_INTRO_DIR") if not base: print("set NO_INTRO_DIR", file=sys.stderr) return 2 hits = [p.name for p in Path(base).iterdir() if "amstrad" in p.name.lower() or "cpc" in p.name.lower()] print(f"No-Intro folders naming Amstrad or CPC: {len(hits)}") for name in sorted(hits): print(" ", name) return 0 def sniff(name: str, data: bytes) -> str: suffix = Path(name).suffix.lower() if data.startswith(b"EXTENDED CPC DSK"): return "disc: extended DSK" if data.startswith(b"MV - CPC"): return "disc: standard DSK" if data.startswith(b"ZXTape!\x1a"): return f"tape: TZX/CDT v{data[8]}.{data[9]:02d}" if data.startswith(b"MV - SNA"): return f"snapshot: SNA v{data[0x10]}" if data.startswith(b"RIFF") and data[8:12] == b"AMS!": return "cartridge: CPR (RIFF AMS!)" if data.startswith(b"CAPS"): return "disc: IPF" if data.startswith(b"HXCPICFE"): return "disc: HFE" return f"other: {suffix or '(none)'}" def library() -> int: dirs = [d for d in os.environ.get("CPC_MIRROR_DIRS", "").split(os.pathsep) if d] if not dirs: print("set CPC_MIRROR_DIRS (folders holding CPC collections)", file=sys.stderr) return 2 for folder in dirs: path = Path(folder) kinds: collections.Counter[str] = collections.Counter() archives = 0 for item in sorted(path.rglob("*")): if not item.is_file(): continue if item.suffix.lower() != ".zip": kinds[sniff(item.name, item.read_bytes()[:64])] += 1 continue archives += 1 try: with zipfile.ZipFile(item) as handle: for info in handle.infolist(): if info.is_dir(): continue with handle.open(info) as member: kinds[sniff(info.filename, member.read(64))] += 1 except zipfile.BadZipFile: kinds["unreadable zip"] += 1 print(f"{path.name}: {archives} zips") for kind, count in sorted(kinds.items()): print(f" {count:6d} {kind}") return 0 def softlists() -> int: hash_text = os.environ.get("MAME_HASHPATH") if not hash_text: print("set MAME_HASHPATH (the installed MAME's hash/ folder)", file=sys.stderr) return 2 hash_dir = Path(hash_text) for name in SOFTLISTS: tree = ET.parse(hash_dir / f"{name}.xml") entries = tree.getroot().findall("software") supported = collections.Counter(e.get("supported", "yes") for e in entries) print(f"{name}: {len(entries)} entries, supported {dict(supported)}") return 0 def main() -> int: commands = {"firmware": firmware, "nointro": nointro, "library": library, "softlists": softlists} if len(sys.argv) != 2 or sys.argv[1] not in commands: print(__doc__) return 2 return commands[sys.argv[1]]() if __name__ == "__main__": sys.exit(main())