# SPDX-License-Identifier: MIT """Census of the Macintosh firmware, disk images and MAME software lists, read in place. MAME_ROMPATH=... python -I scripts/census.py firmware MAC_DISK_DIRS=dir1:dir2 python -I scripts/census.py disks MAME_HASH_DIR=... python -I scripts/census.py softlists firmware: hashes, by SHA-1, every file inside the MAME sets of the compact Macintosh family (machines and keyboard devices) under MAME_ROMPATH, and inside any zip in the folders named in MAC_FIRMWARE_ZIPS (os.pathsep-separated paths of zips, e.g. a frontend or FPGA collection). Prints one row per file; nothing is copied. Firmware is identified by SHA-1 only, never committed. disks: walks every folder named in MAC_DISK_DIRS (os.pathsep-separated), opens each zip (and zips inside zips, in memory) and classifies every member by content: raw 400K/800K sector images (409,600 / 819,200 bytes), Disk Copy 4.2 (the 84-byte header, CiderPress II DiskCopy-notes), MOOF (its magic), MacBinary, and the volume signature at byte 1024 of the volume (MFS $D2D7, HFS $4244). Writes reference/census/disks.tsv (git-ignored) and prints totals. softlists: counts the entries of MAME's Macintosh software lists (CC0-1.0) under MAME_HASH_DIR (MAME's own hashpath), by list and by declared compatibility. """ from __future__ import annotations import collections import csv import hashlib import io import os import struct import sys import xml.etree.ElementTree as ET import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] OUT = ROOT / "reference" / "census" NESTED_LIMIT = 400 << 20 READ_LIMIT = 64 << 20 MACHINES = ["mac128k", "mac512k", "mac512ke", "macplus", "macplusj", "macse", "macsefd", "macclasc"] DEVICES = ["mackbd_m0110", "mackbd_m0110a", "mackbd_m0110a_f", "mackbd_m0110a_j", "mackbd_m0110b", "mackbd_m0110f", "mackbd_m0110j", "mackbd_m0110t", "mackbd_m0120", "mackbd_m0120p"] # fmt: skip SOFTLISTS = ["mac_flop", "mac_flop_orig", "mac_flop_clcracked", "mac_hdflop", "mac_hdd", "mac_cdrom"] def volume_kind(volume: bytes) -> str: if len(volume) < 1026: return "-" signature = struct.unpack_from(">H", volume, 1024)[0] return {0xD2D7: "MFS", 0x4244: "HFS", 0x482B: "HFS+"}.get(signature, f"sig {signature:04X}") def classify(name: str, data: bytes) -> dict: ext = os.path.splitext(name)[1].lower() row: dict = {"ext": ext, "size": len(data)} if data[:4] == b"MOOF": row["kind"] = "MOOF" if len(data) > 20 and data[12:16] == b"INFO": row["disk_type"] = {1: "SSDD GCR 400K", 2: "DSDD GCR 800K", 3: "DSHD MFM 1.44M"}.get( data[21], data[21] ) elif len(data) >= 84 and data[82:84] == b"\x01\x00" and 0 < data[0] <= 63: data_size = struct.unpack_from(">I", data, 64)[0] row["kind"] = "DiskCopy 4.2" row["dc42_data"] = data_size row["dc42_format"] = {0: "400K GCR", 1: "800K GCR", 2: "720K MFM", 3: "1440K MFM"}.get( data[80], data[80] ) row["volume"] = volume_kind(data[84 : 84 + 2048]) elif len(data) in (409600, 819200): row["kind"] = f"raw {len(data) // 1024}K" row["volume"] = volume_kind(data) elif len(data) in (737280, 1474560): row["kind"] = f"raw {len(data) // 1024}K MFM" row["volume"] = volume_kind(data) elif len(data) >= 128 and data[0] == 0 and 0 < data[1] <= 63 and data[74] == 0 and data[82] == 0: row["kind"] = "MacBinary?" elif len(data) >= 1 << 20: row["kind"] = "large (hard disk?)" row["volume"] = volume_kind(data[:2048]) if data[:2] != b"ER" else "partitioned (ER)" else: row["kind"] = "other" return row def walk_zip(label: str, archive: zipfile.ZipFile, rows: list, depth: int = 0) -> None: for info in archive.infolist(): if info.is_dir(): continue name = info.filename ext = os.path.splitext(name)[1].lower() try: if ext == ".zip" and depth < 3 and info.file_size <= NESTED_LIMIT: inner = zipfile.ZipFile(io.BytesIO(archive.read(info))) walk_zip(f"{label}/{name}", inner, rows, depth + 1) elif info.file_size <= READ_LIMIT: rows.append({"zip": label, "member": name, **classify(name, archive.read(info))}) else: with archive.open(info) as handle: head = handle.read(4096) rows.append({"zip": label, "member": name, "ext": ext, "size": info.file_size, "kind": "large (hard disk?)", "volume": volume_kind(head)}) # fmt: skip except (zipfile.BadZipFile, OSError, NotImplementedError, EOFError) as error: rows.append({"zip": label, "member": name, "error": str(error)}) def disks() -> int: dirs = [d for d in os.environ.get("MAC_DISK_DIRS", "").split(os.pathsep) if d] if not dirs: sys.exit("set MAC_DISK_DIRS to the disk-image folders or zips (os.pathsep-separated)") OUT.mkdir(parents=True, exist_ok=True) rows: list[dict] = [] for top in dirs: top_path = Path(top) paths = [top_path] if top_path.is_file() else sorted(p for p in top_path.rglob("*") if p.is_file()) for path in paths: label = f"{top_path.name}/{path.relative_to(top_path)}" if top_path.is_dir() else top_path.name if path.suffix.lower() == ".zip": try: walk_zip(label, zipfile.ZipFile(path), rows) except (zipfile.BadZipFile, OSError) as error: rows.append({"zip": label, "error": str(error)}) elif path.stat().st_size <= READ_LIMIT: rows.append({"zip": label, "member": path.name, **classify(path.name, path.read_bytes())}) print(f"scanned {top_path.name}: {len(rows)} rows so far", flush=True) fields = sorted({key for row in rows for key in row}) with (OUT / "disks.tsv").open("w", newline="", encoding="utf-8") as handle: writer = csv.DictWriter(handle, fields, delimiter="\t") writer.writeheader() writer.writerows(rows) print("kinds:", dict(collections.Counter(str(r.get("kind", "-")) for r in rows).most_common())) print("volumes:", dict(collections.Counter(str(r.get("volume", "-")) for r in rows).most_common())) print("DiskCopy formats:", dict(collections.Counter(str(r["dc42_format"]) for r in rows if "dc42_format" in r))) print("errors:", sum("error" in r for r in rows)) return 0 def firmware() -> int: rompath = os.environ.get("MAME_ROMPATH") if not rompath: sys.exit("set MAME_ROMPATH to the MAME ROM folder") places = [Path(rompath) / f"{name}.zip" for name in MACHINES + DEVICES] places += [Path(p) for p in os.environ.get("MAC_FIRMWARE_ZIPS", "").split(os.pathsep) if p] for place in places: if not place.exists(): print(f"MISSING\t{place.name}") continue with zipfile.ZipFile(place) as archive: for info in archive.infolist(): if info.is_dir() or info.file_size > 4 << 20: continue # firmware only; disk images in a collection zip are the disks census's digest = hashlib.sha1(archive.read(info)).hexdigest() print(f"{digest}\t{info.file_size}\t{place.name}\t{info.filename}") return 0 def softlists() -> int: hashdir = os.environ.get("MAME_HASH_DIR") if not hashdir: sys.exit("set MAME_HASH_DIR to MAME's hashpath (mame -showconfig | grep hashpath)") for name in SOFTLISTS: path = Path(hashdir) / f"{name}.xml" if not path.exists(): print(f"MISSING\t{name}") continue root = ET.parse(path).getroot() entries = root.findall("software") compat = collections.Counter() areas = collections.Counter() for software in entries: for share in software.findall("sharedfeat"): if share.get("name") == "compatibility": compat[share.get("value")] += 1 for part in software.findall("part"): for area in part.findall("dataarea"): for rom in area.findall("rom"): if rom.get("size"): areas[rom.get("size")] += 1 print(f"{name}\t{len(entries)} entries\tcompatibility {dict(compat.most_common(8))}" f"\tsizes {dict(areas.most_common(6))}") # fmt: skip return 0 if __name__ == "__main__": commands = {"disks": disks, "firmware": firmware, "softlists": softlists} if len(sys.argv) != 2 or sys.argv[1] not in commands: sys.exit(__doc__) sys.exit(commands[sys.argv[1]]())