#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Census the CoCo and Dragon software: the player's sets and MAME's software lists. 1. Looks under ``$NO_INTRO_DIR`` and the TOSEC tree beside it (``$TOSEC_DIR`` if set) for directories whose names name the Color Computer, the Dragon or the MC-10, and counts their files by extension. Nothing is opened, copied or written. 2. Counts the entries of the pinned MAME software lists in ``reference/softlists/`` (CC0-1.0): entries, parts by interface, and the file extensions they name. NO_INTRO_DIR=... python -I scripts/census_software.py > tests/detectors/census-software-.txt """ from __future__ import annotations import collections import os import re import sys import xml.etree.ElementTree as ET from pathlib import Path ROOT = Path(__file__).resolve().parents[1] NAMES = re.compile(r"color computer|coco|dragon data|dragon 32|dragon 64|\bdragon\b|tandy|trs-80 col|mc-10", re.I) LISTS = ["coco_cart", "coco_flop", "dragon_cart", "dragon_cass", "dragon_flop", "dragon_flex", "dragon_os9"] def scan(label: str, base: Path) -> None: print(f"\n## {label}: {'present' if base.is_dir() else 'not present'}") if not base.is_dir(): return hits = [d for d in sorted(base.iterdir()) if d.is_dir() and NAMES.search(d.name)] # TOSEC nests one level deeper (TOSEC///...). for maker in sorted(base.iterdir()): if maker.is_dir() and not NAMES.search(maker.name): hits += [d for d in sorted(maker.iterdir()) if d.is_dir() and NAMES.search(d.name)] if not hits: print(f"{label}: no CoCo, Dragon or MC-10 directory (searched {len(list(base.iterdir()))} entries)") for directory in hits: counts = collections.Counter(p.suffix.lower() for p in directory.rglob("*") if p.is_file()) total = sum(counts.values()) print(f"{directory.name}: {total} files, " + ", ".join(f"{k or '(none)'} {v}" for k, v in counts.most_common())) def softlists() -> None: print("\n## MAME software lists (reference/softlists, tag mame0285, CC0-1.0)") for name in LISTS: path = ROOT / "reference" / "softlists" / f"{name}.xml" if not path.exists(): print(f"{name}: not fetched (python -I scripts/fetch_references.py)") continue tree = ET.parse(path) entries = tree.getroot().findall("software") parts = collections.Counter(p.get("interface") for s in entries for p in s.findall("part")) sizes = collections.Counter() for software in entries: for rom in software.iter("rom"): if rom.get("name"): sizes[Path(rom.get("name")).suffix.lower() or "(none)"] += 1 clones = sum(1 for s in entries if s.get("cloneof")) print(f"{name}: {len(entries)} entries ({clones} clones); parts " + ", ".join(f"{k} {v}" for k, v in parts.most_common()) + "; files " + ", ".join(f"{k} {v}" for k, v in sizes.most_common(8))) def main() -> int: no_intro = os.environ.get("NO_INTRO_DIR") if not no_intro: print("NO_INTRO_DIR is not set", file=sys.stderr) return 2 tosec = Path(os.environ.get("TOSEC_DIR") or Path(no_intro).parent / "TOSEC") print("# software census; directories listed in place, nothing opened or copied") scan("No-Intro ($NO_INTRO_DIR)", Path(no_intro)) scan("TOSEC ($TOSEC_DIR, default: beside $NO_INTRO_DIR)", tosec) softlists() return 0 if __name__ == "__main__": sys.exit(main())