# SPDX-License-Identifier: MIT """Count the firmware and software on hand for this board, in place (docs/census.md). MAME_ROMPATH=... python -I scripts/census.py firmware MAME_HASHPATH=... python -I scripts/census.py softlists NO_INTRO_DIR=... python -I scripts/census.py nointro REDUMP_DIR=... python -I scripts/census.py redump X1_MIRROR_DIRS=dir1:dir2 python -I scripts/census.py library 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). Firmware is reported by SHA-1 only and is never written anywhere. ``firmware`` asks the installed MAME (``mame -listxml``) which ROMs each driver needs and checks each member of ``.zip`` (and the device zips it names) under MAME_ROMPATH. ``library`` sorts the members of the zips in each folder by extension and, for D88 disk images, by the header's media byte (00h 2D, 10h 2DD, 20h 2HD) with the header's size field checked against the member's size. ``nointro`` and ``redump`` list the folders whose names match this machine; the mirror's No-Intro tree has none for either Japanese computer. """ 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 MACHINE = "Sharp X1" DRIVERS = ("x1", "x1turbo", "x1turbo40", "x1twin") SOFTLISTS = ("x1_flop", "x1_cass") FOLDER_PATTERNS = ("sharp - x1",) # lower-case substrings of a set folder name # Device sets whose ROMs belong to the board itself (printers and other slot options are skipped). BOARD_DEVICES = () MIRROR_VARIABLE = "X1_MIRROR_DIRS" D88_MEDIA = {0x00: "2D", 0x10: "2DD", 0x20: "2HD", 0x30: "1D", 0x40: "1DD"} 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 zip_sha1s(path: Path) -> dict[str, str]: if not path.exists(): return {} with zipfile.ZipFile(path) as handle: return {i.filename: hashlib.sha1(handle.read(i)).hexdigest() for i in handle.infolist()} 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 = list(machine.iter("rom")) if not roms or (name not in DRIVERS and name not in BOARD_DEVICES): continue held = zip_sha1s(Path(rompath) / f"{name}.zip") kind = "device" if machine.get("isdevice") == "yes" else "driver" print(f"{name} ({kind}, {machine.findtext('description')}): " f"{'present' if held else 'NO ZIP'}") # fmt: skip for rom in roms: sha1 = rom.get("sha1") status = rom.get("status", "good") if not sha1: print(f" {rom.get('name'):24s} {int(rom.get('size')):7d} (no dump known: {status})") continue state = "ok" if sha1 in held.values() else "MISSING" missing += state != "ok" flag = "" if status == "good" else f" [{status}]" print(f" {rom.get('name'):24s} {int(rom.get('size')):7d} {sha1} {state}{flag}") print(f"missing members: {missing}") return 0 def softlists() -> int: hashpath = os.environ.get("MAME_HASHPATH") if not hashpath: print("set MAME_HASHPATH (the installed MAME's hash/ folder)", file=sys.stderr) return 2 for name in SOFTLISTS: path = Path(hashpath) / f"{name}.xml" if not path.exists(): print(f"{name}: not found") continue root = ET.parse(path).getroot() entries = root.findall("software") supported = collections.Counter(e.get("supported", "yes") for e in entries) parts = collections.Counter(p.get("interface") for e in entries for p in e.iter("part")) print(f"{name}: {len(entries)} entries; supported {dict(supported)}; parts {dict(parts)}") return 0 def matching_folders(base: Path) -> list[Path]: return [c for c in sorted(base.iterdir()) if any(p in c.name.lower() for p in FOLDER_PATTERNS)] def tree(variable: str) -> int: base = os.environ.get(variable) if not base: print(f"set {variable}", file=sys.stderr) return 2 folders = matching_folders(Path(base)) for folder in folders: files = [p for p in folder.rglob("*") if p.is_file()] print(f"{folder.name}: {len(files)} files") if not folders: print(f"no folder for {MACHINE} under {variable}") return 0 def sniff(name: str, data: bytes) -> str: ext = name.rsplit(".", 1)[-1].lower() if "." in name else "" if ext == "d88" or (len(data) >= 0x2B0 and int.from_bytes(data[0x1C:0x20], "little") == len(data)): if len(data) < 0x2B0: return "d88 (short)" media = D88_MEDIA.get(data[0x1B], f"media {data[0x1B]:02X}") size_ok = int.from_bytes(data[0x1C:0x20], "little") == len(data) return f"d88 {media}{'' if size_ok else ' (size field differs)'}" if data[:4] == b"TAPE": return "tap (TAPE header)" if data.startswith(b"PC-8801 Tape Image(T88)"): return "t88" return ext or "(no extension)" def library() -> int: dirs = os.environ.get(MIRROR_VARIABLE) if not dirs: print(f"set {MIRROR_VARIABLE} (os.pathsep-separated folders of {MACHINE} collections)", file=sys.stderr) # fmt: skip return 2 for folder in dirs.split(os.pathsep): kinds: collections.Counter[str] = collections.Counter() zips = sorted(Path(folder).rglob("*.zip")) for archive in zips: try: with zipfile.ZipFile(archive) as handle: for info in handle.infolist(): if not info.is_dir(): kinds[sniff(info.filename, handle.read(info))] += 1 except zipfile.BadZipFile: kinds["(bad zip)"] += 1 print(f"{Path(folder).name}: {len(zips)} zips; members {dict(sorted(kinds.items()))}") return 0 def main() -> int: command = sys.argv[1] if len(sys.argv) > 1 else "" if command == "firmware": return firmware() if command == "softlists": return softlists() if command == "nointro": return tree("NO_INTRO_DIR") if command == "redump": return tree("REDUMP_DIR") if command == "library": return library() print(__doc__, file=sys.stderr) return 2 if __name__ == "__main__": sys.exit(main())