#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Census of the PSP disc and download sets, from names and zip directories only. Reads (each optional): - ``$REDUMP_DIR/Sony - PlayStation Portable``: the Redump UMD set, one .zip per disc; - ``$NO_INTRO_DIR/`` for every No-Intro folder whose name contains "PlayStation Portable" (the PSN downloads, decrypted); - ``--curated DIR``: a loose folder (for example a frontend's library of .chd/.iso/.cso). Nothing is extracted, copied or hashed: only file names and each zip's central directory (member names and sizes) are read, so the run is cheap. Output is plain text, meant to be saved under tests/detectors/. source ; python -I scripts/census.py > tests/detectors/census-YYYY-MM-DD.txt python -I scripts/census.py --names-only # skip the zip directories """ from __future__ import annotations import argparse import collections import os import re import sys import zipfile from pathlib import Path REGIONS = ("USA", "Europe", "Japan", "Asia", "Korea", "Germany", "France", "Italy", "Spain", "UK", "Australia", "Brazil", "Netherlands", "Sweden", "Russia", "World", "Canada", "China", "Taiwan", "Hong Kong", "Greece", "Poland", "Portugal", "Scandinavia", "Latin America", "India", "Denmark", "Norway", "Finland", "Belgium", "Austria", "Switzerland", "New Zealand", "Mexico", "Argentina", "South Africa") TAGS = ("Demo", "Beta", "Proto", "Sample", "Unl", "Rev", "Kiosk", "Taikenban", "Promo", "Bonus Disc", "Alt", "UMD Video", "UMD Music", "Minis", "PSN", "Disc 1", "Disc 2", "Data Install Senyou Disc", "Game Disc", "Tech Demo", "Trial", "Update") def parens(name: str) -> list[str]: return re.findall(r"\(([^)]*)\)", name) def regions_of(groups: list[str]) -> list[str]: for group in groups: parts = [p.strip() for p in group.split(",")] if any(p in REGIONS for p in parts): return [p for p in parts if p in REGIONS] return ["(none)"] def tags_of(groups: list[str]) -> set[str]: found = set() for group in groups: lowered = group.lower() for tag in TAGS: if lowered == tag.lower() or lowered.startswith(tag.lower() + " "): found.add(tag) return found def survey(label: str, root: Path, read_zips: bool) -> None: files = sorted(p for p in root.iterdir() if p.is_file()) print(f"\n== {label}") print(f"files: {len(files)}") suffixes = collections.Counter(p.suffix.lower() for p in files) print("by extension: " + ", ".join(f"{k or '(none)'} {v}" for k, v in suffixes.most_common())) region_count: collections.Counter[str] = collections.Counter() tag_count: collections.Counter[str] = collections.Counter() titles = set() for path in files: stem = path.stem groups = parens(stem) for region in regions_of(groups): region_count[region] += 1 for tag in tags_of(groups): tag_count[tag] += 1 titles.add(re.sub(r"\s*\(.*$", "", stem)) print(f"distinct base titles (text before the first parenthesis): {len(titles)}") print("regions (a multi-region entry counts once per region): " + ", ".join( f"{k} {v}" for k, v in region_count.most_common(14))) print("tags: " + ", ".join(f"{k} {v}" for k, v in tag_count.most_common())) if not read_zips: return members: collections.Counter[str] = collections.Counter() per_zip: collections.Counter[int] = collections.Counter() total = 0 sizes: list[int] = [] errors = 0 odd: list[str] = [] for path in files: if path.suffix.lower() != ".zip": sizes.append(path.stat().st_size) continue try: with zipfile.ZipFile(path) as archive: infos = [i for i in archive.infolist() if not i.is_dir()] except (OSError, zipfile.BadZipFile): errors += 1 continue per_zip[len(infos)] += 1 for info in infos: members[Path(info.filename).suffix.lower() or "(none)"] += 1 total += info.file_size if info.filename.lower().endswith(".iso"): sizes.append(info.file_size) if info.file_size % 2048: odd.append(info.filename) print(f"zip directories read: {sum(per_zip.values())}, errors {errors}") print("members per zip: " + ", ".join(f"{k}: {v}" for k, v in sorted(per_zip.items()))) print("member extensions: " + ", ".join(f"{k} {v}" for k, v in members.most_common())) print(f"uncompressed bytes: {total} ({total / 2**40:.2f} TiB)") if sizes: sizes.sort() mib = 2**20 print(f"image sizes (MiB): min {sizes[0] / mib:.1f}, median {sizes[len(sizes) // 2] / mib:.1f}, " f"max {sizes[-1] / mib:.1f}") over = sum(1 for s in sizes if s > 1_000_000_000) print(f"images over 1 GB (dual-layer UMD, as a rule): {over}") print(f".iso members not a multiple of 2048 bytes: {len(odd)}") def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--names-only", action="store_true", help="do not open zip directories") parser.add_argument("--curated", type=Path, help="a loose library folder to count by extension") args = parser.parse_args() read_zips = not args.names_only redump = os.environ.get("REDUMP_DIR") no_intro = os.environ.get("NO_INTRO_DIR") if not redump and not no_intro and not args.curated: print("set REDUMP_DIR and/or NO_INTRO_DIR, or pass --curated", file=sys.stderr) return 2 if redump: umd = Path(redump) / "Sony - PlayStation Portable" if umd.is_dir(): survey("Redump: Sony - PlayStation Portable", umd, read_zips) else: print("\n== Redump: no 'Sony - PlayStation Portable' folder under $REDUMP_DIR") if no_intro: folders = sorted(p for p in Path(no_intro).iterdir() if p.is_dir() and "PlayStation Portable" in p.name) if not folders: print("\n== No-Intro: no PlayStation Portable folder under $NO_INTRO_DIR") for folder in folders: survey(f"No-Intro: {folder.name}", folder, read_zips) if args.curated: survey("curated library (--curated)", args.curated, False) return 0 if __name__ == "__main__": sys.exit(main())