#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Census of the Redump "Philips - CD-i" set: counts, regions, tags, track layouts. Reads each zip's central directory and its small .cue member (the cue sheet text, a few hundred bytes); no disc image is extracted, copied or hashed. The set is found through REDUMP_DIR (its "Philips - CD-i" folder, or the folder itself). Optionally cross-references MAME's CC0 software list (hash/cdi.xml, --softlist) by title to estimate how many discs need the Digital Video Cartridge (the list's ``compatibility=DVC`` shared feature). REDUMP_DIR=... python -I scripts/census_redump.py REDUMP_DIR=... python -I scripts/census_redump.py --softlist /usr/share/games/mame/hash/cdi.xml """ from __future__ import annotations import argparse import collections import os import re import sys import xml.etree.ElementTree as ET import zipfile from pathlib import Path SET_NAME = "Philips - CD-i" TAGS = ("Proto", "Beta", "Demo", "Sample", "Rev", "Alt", "Unl", "Program") def set_dir() -> Path: base = os.environ.get("REDUMP_DIR") if not base: sys.exit("set REDUMP_DIR (the Redump folder, or its 'Philips - CD-i' folder)") path = Path(base) return path / SET_NAME if (path / SET_NAME).is_dir() else path def base_title(name: str) -> str: return re.split(r"\s*\(", name, maxsplit=1)[0].strip().lower() def norm(title: str) -> str: return re.sub(r"[^a-z0-9]", "", title.lower()) def read_softlist(path: Path) -> dict[str, bool]: """Normalised base title -> needs DVC, from MAME's cdi.xml (CC0-1.0).""" titles: dict[str, bool] = {} for software in ET.parse(path).getroot().iter("software"): description = software.findtext("description") or "" dvc = any( feature.get("name") == "compatibility" and feature.get("value") == "DVC" for feature in software.iter("sharedfeat") ) key = norm(base_title(description)) titles[key] = titles.get(key, False) or dvc return titles def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--softlist", type=Path, help="MAME hash/cdi.xml to cross-reference") args = parser.parse_args() folder = set_dir() zips = sorted(folder.glob("*.zip")) counts: collections.Counter[str] = collections.Counter() regions: collections.Counter[str] = collections.Counter() tags: collections.Counter[str] = collections.Counter() tracks: collections.Counter[int] = collections.Counter() modes: collections.Counter[str] = collections.Counter() layouts: collections.Counter[str] = collections.Counter() titles: set[str] = set() total_bytes = 0 problems: list[str] = [] for path in zips: name = path.stem counts["discs"] += 1 titles.add(base_title(name)) fields = re.findall(r"\(([^)]*)\)", name) if fields: for region in fields[0].split(", "): regions[region] += 1 for field in fields[1:]: for tag in TAGS: if field.startswith(tag): tags[tag] += 1 if "(Disc " in name: counts["multi-disc members"] += 1 try: with zipfile.ZipFile(path) as archive: members = archive.infolist() bins = [m for m in members if m.filename.lower().endswith(".bin")] cues = [m for m in members if m.filename.lower().endswith(".cue")] total_bytes += sum(m.file_size for m in bins) tracks[len(bins)] += 1 if not cues: problems.append(f"{name}: no .cue") continue text = archive.read(cues[0]).decode("latin-1", "replace") except (OSError, zipfile.BadZipFile) as error: problems.append(f"{name}: {error}") continue kinds = re.findall(r"TRACK\s+\d+\s+(\S+)", text) for kind in kinds: modes[kind] += 1 # The track-type sequence, with runs of audio tracks collapsed: "MODE2/2352+AUDIOx12". first, rest = (kinds[0], kinds[1:]) if kinds else ("none", []) layout = first if rest: layout += "+" + "+".join(f"{k}x{rest.count(k)}" for k in sorted(set(rest))) layouts[layout] += 1 if kinds and all(k == "AUDIO" for k in kinds): # CD-i Ready: audio tracks only, the CD-i program hidden in track 1's pre-gap # (ICDIA application note "CD-i Ready disc"). counts["audio tracks only"] += 1 if "(Disc " not in name and len(bins) == 0: counts["cue sheet only, no image in the mirror"] += 1 print(f"Redump '{SET_NAME}': {counts['discs']} discs, {len(titles)} distinct base titles") print(f"multi-disc members: {counts['multi-disc members']}") print(f"uncompressed .bin total: {total_bytes} bytes ({total_bytes / 2**30:.1f} GiB), " f"{total_bytes // 2352} sectors of 2352 bytes") print("regions:", ", ".join(f"{k} {v}" for k, v in regions.most_common())) print("tags:", ", ".join(f"{k} {v}" for k, v in tags.most_common())) print("tracks per disc:", ", ".join(f"{k}:{v}" for k, v in sorted(tracks.items()))) print("track types (all tracks):", ", ".join(f"{k} {v}" for k, v in modes.most_common())) print("layouts:") for layout, number in layouts.most_common(12): print(f" {number:5d} {layout}") print(f"audio tracks only (CD-i Ready candidates): {counts['audio tracks only']}") print(f"cue sheet only, no image in the mirror: {counts['cue sheet only, no image in the mirror']}") if args.softlist: listed = read_softlist(args.softlist) dvc_titles = {t for t, d in listed.items() if d} matched = {norm(t) for t in titles} & set(listed) dvc_matched = {norm(t) for t in titles} & dvc_titles print(f"MAME cdi.xml: {len(listed)} base titles, {len(dvc_titles)} need DVC; " f"Redump base titles matched by name: {len(matched)}, of which DVC: {len(dvc_matched)}") for problem in problems: print("problem:", problem) return 0 if __name__ == "__main__": sys.exit(main())