From 338d1cc7b90801aa4a683012b807f2906c06079b Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Tue, 17 Nov 2026 07:48:08 +0100 Subject: [PATCH 1/9] refactor: split the pack and manifest builders generate_pack, generate_emulator_pack, generate_manifest, _main and the other long functions of generate_pack.py are split by stage. A platform pack is planned once (_PlatformPlan) and written by _PlatformPackBuild, whose lookups generate_pack binds; the emulator pack and the manifest follow the same shape. The destinations a build has taken live in _Placed. Public builders keep their names and keyword callers: options past the eighth parameter are typed with Unpack[TypedDict], read once, and an unknown key still raises TypeError. The imports at the bottom of the module move to the top. The pack and manifest bytes are unchanged. generate_emulator_pack now takes zip_contents by keyword: pipeline.py:599 and test_verify_emulator_evidence.py:66 pass it by name. _data_directory_members, _select_variants and _manifest_core_entries take their options from the 9th argument by keyword. --- scripts/generate_pack.py | 5377 +++++++++++++++++++++----------------- 1 file changed, 3002 insertions(+), 2375 deletions(-) diff --git a/scripts/generate_pack.py b/scripts/generate_pack.py index be2a224f4..d1cd83397 100644 --- a/scripts/generate_pack.py +++ b/scripts/generate_pack.py @@ -19,7 +19,7 @@ import sys if sys.version_info < (3, 9): sys.exit(f"generate_pack.py needs Python 3.9 or newer, this is {sys.version.split()[0]}") -from collections.abc import Callable, Iterable, Iterator +from collections.abc import Callable, Iterable, Iterator, Mapping from artifacts import _build_timestamp, set_plain_mode import argparse @@ -34,7 +34,8 @@ import shutil import tempfile import zipfile from pathlib import Path, PurePosixPath -from typing import Any +from dataclasses import dataclass, field +from typing import TYPE_CHECKING, TypedDict sys.path.insert(0, os.path.dirname(__file__)) from alternatives import core_file_spares, settle_sets, slot_set_keys @@ -133,9 +134,84 @@ from validation import ( extra_alternatives, ) +from packpaths import ( + MEDIA_MARKER, + lands_on_android, + _archive_destination, + _has_path_conflict, + _register_path, + _flat, + _resolve_destination, + placement_block, + save_layout, +) +from packextras import ( + ExtraResolver, + extra_full_destination, + extra_region_key, + prefixed_destination, + save_entry_fields, + save_manifest_dest, + _detect_extras_prefix, + _collect_emulator_extras, + _extra_system_ids, + extras_for_systems as _extras_for_systems, + platform_region_groups, + emulator_region_drops, + emulator_required_spares, + _emulator_region_group as _emulator_region_group, + expand_agnostic_entries, + undeclared_report, +) +from packreadme import ( + _build_readme, + _build_agnostic_rename_readme, + save_place, +) +from packverify import ( + _check_member_hash as _check_member_hash, + _hash_matches as _hash_matches, + _intentional_hash_exclusion as _intentional_hash_exclusion, + verify_pack_against_platform, + verify_pack, + generate_sha256sums, +) +# _OFFLINE is deliberately not re-exported: importing a value copies it, so a +# facade name would keep saying False after set_offline() changed the flag. +from packresolve import ( + ACCEPTED_HASHES, + sha1_for_hash, + preferred_entries as _preferred_entries, + parse_hash_input, + parse_hash_file, + lookup_hashes, + resolve_declaration, + resolve_file, + download_external, +) + +if TYPE_CHECKING: + from typing_extensions import Unpack + + # How a manifest settles a platform declaration: the copy served, or None + # and why not. + _Settle = Callable[[dict, str, str, str | None], tuple[str | None, str]] + # What a destination came to: its entries, whether it was filled, and the + # report to make if no other member of its set fills it. + _Outcome = tuple[list[dict], bool, Callable[[], None] | None] + yaml = require_yaml() DEFAULT_PLATFORMS_DIR = "platforms" + + +def _reject_unknown(function: str, options: Mapping[str, object], known: frozenset[str]) -> None: + """Refuse a keyword the options do not declare, as a named parameter would.""" + for key in options: + if key not in known: + raise TypeError(f"{function}() got an unexpected keyword argument '{key}'") + + DEFAULT_DB_FILE = "database.json" DEFAULT_OUTPUT_DIR = "dist" DEFAULT_BIOS_DIR = "bios" @@ -536,6 +612,49 @@ def _tree_references( yield tuple(tree["source_systems"]), tree +class _TreeWalkOptions(TypedDict, total=False): + """The keyword options of _data_directory_members.""" + + refs_out: dict | None + required_only: bool + core_trees: Iterable[dict] + reclaim: dict[str, str] | None + + +def _carried_tree( + dd: dict, data_registry: dict | None, platform_name: str, required_only: bool, +) -> str | None: + """The cache folder of a tree reference the pack carries, or None.""" + ref_key = dd.get("ref", "") + if not ref_key or not data_registry or ref_key not in data_registry: + return None + if required_only and dd.get("required") is False: + return None + if ref_key in installed_data_refs(platform_name): + return None + entry = data_registry[ref_key] + allowed = entry.get("for_platforms") + if allowed and platform_name not in allowed: + return None + local_path = entry.get("local_cache", "") + if not local_path or not os.path.isdir(local_path): + print( + f" WARNING: data directory '{ref_key}' not cached at {local_path} (run refresh_data_dirs.py)" + ) + _MISSING_DATA_DIRS.add(ref_key) + return None + return local_path + + +def _tree_files(local_path: str, dd_prefix: str) -> Iterator[tuple[str, str]]: + """(source, destination) of every file of a cached tree.""" + for root, _dirs, filenames in os.walk(local_path): + for fname in filenames: + src = os.path.join(root, fname) + rel = os.path.relpath(src, local_path) + yield src, f"{dd_prefix}/{rel}" if dd_prefix else rel + + def _data_directory_members( pack_systems: dict, data_registry: dict | None, @@ -545,10 +664,7 @@ def _data_directory_members( seen_destinations: set, seen_lower: set, seen_parents: set, - refs_out: dict | None = None, - required_only: bool = False, - core_trees: Iterable[dict] = (), - reclaim: dict[str, str] | None = None, + **options: Unpack[_TreeWalkOptions], ) -> Iterator[tuple[str, str]]: """Yield (source, destination) for the cached data directories a platform's systems declare, then those its cores read (``core_trees``, @@ -570,91 +686,40 @@ def _data_directory_members( ``required_only`` leaves out a tree declared ``required: false``, one the core runs without, as it leaves out such a file. """ + _reject_unknown("_data_directory_members", options, _TreeWalkOptions.__optional_keys__) + refs_out = options.get("refs_out") + reclaim = options.get("reclaim") + required_only = options.get("required_only", False) + core_trees = options.get("core_trees", ()) # Data directories from _data_dirs.yml for systems, dd in _tree_references(pack_systems, core_trees): + local_path = _carried_tree(dd, data_registry, platform_name, required_only) + if local_path is None: + continue ref_key = dd.get("ref", "") - if not ref_key or not data_registry or ref_key not in data_registry: - continue needed = dd.get("required") is not False - if required_only and not needed: - continue - if ref_key in installed_data_refs(platform_name): - continue - entry = data_registry[ref_key] - allowed = entry.get("for_platforms") - if allowed and platform_name not in allowed: - continue - local_path = entry.get("local_cache", "") - if not local_path or not os.path.isdir(local_path): - print( - f" WARNING: data directory '{ref_key}' not cached at {local_path} (run refresh_data_dirs.py)" - ) - _MISSING_DATA_DIRS.add(ref_key) - continue dd_dest = dd.get("destination", "") - if base_dest and dd_dest: - dd_prefix = f"{base_dest}/{dd_dest}" - elif base_dest: - dd_prefix = base_dest - else: - dd_prefix = dd_dest + dd_prefix = f"{base_dest}/{dd_dest}" if base_dest and dd_dest else base_dest or dd_dest # Held while the caller writes what is yielded: a refresh swapping # the tree waits until the walk is done. with cache_lock(local_path, shared=True): - for root, _dirs, filenames in os.walk(local_path): - for fname in filenames: - src = os.path.join(root, fname) - rel = os.path.relpath(src, local_path) - full = f"{dd_prefix}/{rel}" if dd_prefix else rel - if reclaim and full in reclaim and ( - os.path.normpath(reclaim[full]) == os.path.normpath(src) - ): - del reclaim[full] - if refs_out is not None: - refs_out[full] = (ref_key, systems, dd_prefix, needed) - yield src, full - continue - if full in seen_destinations or ( - full.lower() in seen_lower and case_insensitive - ): - continue - if _has_path_conflict(full, seen_destinations, seen_parents): - continue + for src, full in _tree_files(local_path, dd_prefix): + if reclaim and full in reclaim and ( + os.path.normpath(reclaim[full]) == os.path.normpath(src) + ): + del reclaim[full] + elif full in seen_destinations or ( + full.lower() in seen_lower and case_insensitive + ) or _has_path_conflict(full, seen_destinations, seen_parents): + continue + else: seen_destinations.add(full) _register_path(full, seen_destinations, seen_parents) if case_insensitive: seen_lower.add(full.lower()) - if refs_out is not None: - refs_out[full] = (ref_key, systems, dd_prefix, needed) - yield src, full - - -def _pack_data_directories( - zf: zipfile.ZipFile | PackRecord, - pack_systems: dict, - data_registry: dict | None, - platform_name: str, - base_dest: str, - flatten: bool, - case_insensitive: bool, - seen_destinations: set, - seen_lower: set, - seen_parents: set, - required_only: bool = False, - core_trees: Iterable[dict] = (), -) -> int: - """Add the data directories to the pack. Returns how many files went in.""" - added = 0 - for src, full in _data_directory_members( - pack_systems, data_registry, platform_name, base_dest, - case_insensitive, seen_destinations, seen_lower, seen_parents, - required_only=required_only, core_trees=core_trees, - ): - _add_pack_member(zf, src, _flat(full, base_dest, flatten)) - if isinstance(zf, PackRecord): - zf.trees.append(_flat(full, base_dest, flatten)) - added += 1 - return added + if refs_out is not None: + refs_out[full] = (ref_key, systems, dd_prefix, needed) + yield src, full def _case_twin_pairs( @@ -714,6 +779,18 @@ def _report_pack_counts( f"{trees}{core_count} from cores), {', '.join(parts)} [{verification_mode}]" ) +class _VariantOptions(TypedDict, total=False): + """The keyword options of _select_variants.""" + + regions: list[str] | None + one_per_slot: bool + required_only: bool + undeclared: list[dict] | None + zip_contents: dict | None + data_registry: dict | None + slot_overrides: dict[str, str] | None + + def _select_variants( config: dict, pack_systems: dict, @@ -723,13 +800,7 @@ def _select_variants( emu_profiles: dict | None, target_cores: set[str] | None, source: str, - regions: list[str] | None, - one_per_slot: bool, - required_only: bool = False, - undeclared: list[dict] | None = None, - zip_contents: dict | None = None, - data_registry: dict | None = None, - slot_overrides: dict[str, str] | None = None, + **options: Unpack[_VariantOptions], ) -> tuple[set, list, list]: """Which regional and slot alternatives this pack leaves out. @@ -739,6 +810,9 @@ def _select_variants( Returns the destinations to drop, the groups kept only as a fallback, and the slots no profile puts in order. """ + _reject_unknown("_select_variants", options, _VariantOptions.__optional_keys__) + regions = options.get("regions") + one_per_slot = options.get("one_per_slot", False) region_drops: set[str] = set() region_fallbacks: list[str] = [] # One grouping for both passes: a file judged in its own system by one @@ -756,12 +830,12 @@ def _select_variants( target_cores=target_cores, include_extras=(source != "platform"), include_all=(source == "truth"), - required_only=required_only, + required_only=options.get("required_only", False), held_out=held, - undeclared=undeclared, - zip_contents=zip_contents, - data_registry=data_registry, - slot_overrides=slot_overrides, + undeclared=options.get("undeclared"), + zip_contents=options.get("zip_contents"), + data_registry=options.get("data_registry"), + slot_overrides=options.get("slot_overrides"), ) if regions: region_index = region_mod.platform_region_index( @@ -868,31 +942,829 @@ def _report_left_out(cut: GameDataCut, systems: set[str], cores: bool) -> None: print(f" {line}") +class PackNarrowing(TypedDict, total=False): + """How a platform pack is narrowed, shared by a pack and its split parts.""" + + emulators_dir: str + zip_contents: dict | None + data_registry: dict | None + emu_profiles: dict | None + target_cores: set[str] | None + required_only: bool + source: str + regions: list[str] | None + target_name: str | None + one_per_slot: bool + offline: bool | None + no_game_data: bool + + +class _PartScope(TypedDict, total=False): + """What one split part keeps of the platform.""" + + system_filter: list[str] | None + precomputed_extras: list[dict] | None + extras_only: bool + + +class PackOptions(PackNarrowing, _PartScope, total=False): + """The keyword options of generate_pack.""" + + flatten: bool + pack_name: str | None + record: PackRecord | None + within: dict[str, str] | None + + +@dataclass(frozen=True) +class _PackSettings: + """PackOptions with their defaults.""" + + emulators_dir: str = "emulators" + zip_contents: dict | None = None + data_registry: dict | None = None + emu_profiles: dict | None = None + target_cores: set[str] | None = None + required_only: bool = False + system_filter: list[str] | None = None + precomputed_extras: list[dict] | None = None + extras_only: bool = False + source: str = "full" + flatten: bool = True + regions: list[str] | None = None + target_name: str | None = None + one_per_slot: bool = False + offline: bool | None = None + pack_name: str | None = None + no_game_data: bool = False + record: PackRecord | None = None + within: dict[str, str] | None = None + + +@dataclass +class _Placed: + """The destinations a build has taken, and the folders they open. + + ``fold`` is whether a case twin of a taken path counts as taken. + """ + + fold: bool + destinations: set[str] = field(default_factory=set) + lower: set[str] = field(default_factory=set) # only used when fold=True + parents: set[str] = field(default_factory=set) # parent dirs of added files (path conflict detection) + + def take(self, dest: str) -> None: + self.destinations.add(dest) + _register_path(dest, self.destinations, self.parents) + if self.fold: + self.lower.add(dest.lower()) + + def holds(self, dest: str) -> bool: + return dest in self.destinations or (self.fold and dest.lower() in self.lower) + + def conflicts(self, dest: str) -> bool: + return _has_path_conflict(dest, self.destinations, self.parents) + + def block(self, dest: str) -> str | None: + return placement_block(dest, self.destinations, self.lower, self.parents, self.fold) + + +@dataclass +class _PlatformPlan: + """What a platform pack is built from, settled before its first member.""" + + platform_name: str + platforms_dir: str + db: dict + bios_dir: str + opts: _PackSettings + config: dict + emu_profiles: dict + zip_contents: dict + cut: GameDataCut | None + narrowings: list[tuple[str, str]] + zip_path: str + validation_index: dict + platform_profiles: dict[str, dict] + validation_owners: dict + slot_overrides: dict[str, str] + pack_systems: dict + preferred_entries: dict[str, int] = field(default_factory=dict) + undeclared: list[dict] | None = None + region_drops: set[str] = field(default_factory=set) + region_fallbacks: list = field(default_factory=list) + slot_undecidable: list[str] = field(default_factory=list) + + @property + def verification_mode(self) -> str: + return self.config.get("verification_mode", "existence") + + @property + def platform_display(self) -> str: + return self.config.get("platform", self.platform_name) + + @property + def base_dest(self) -> str: + return self.config.get("base_destination", "") + + @property + def case_insensitive(self) -> bool: + # Case-insensitive dedup only for platforms targeting Windows/macOS. + # Linux-only platforms (Batocera, Recalbox, RetroDECK, Lakka, RomM) + # are case-sensitive and may have distinct files like DISK.ROM vs disk.rom. + return self.config.get("case_insensitive_fs", False) + + +def _plan_platform_pack( + platform_name: str, + platforms_dir: str, + db: dict, + bios_dir: str, + output_dir: str, + opts: _PackSettings, +) -> _PlatformPlan | None: + """The configuration, scope and checks a platform pack reads, or None + when the systems asked for leave nothing to build.""" + config = load_platform_config(platform_name, platforms_dir) + if opts.target_name: + config = {**config, TARGET_KEY: opts.target_name} + zip_contents = {} if opts.zip_contents is None else opts.zip_contents + emu_profiles = opts.emu_profiles + if emu_profiles is None: + # The pipeline always passes them, but a caller that does not must get the + # same pack, not one built without validation, slots or agnostic cores. + emu_profiles = load_emulator_profiles(opts.emulators_dir) + # Cut from the full config and profiles: the category of a platform + # entry is read from the profiles' game data entries. + cut = ( + without_game_data(config, emu_profiles, opts.data_registry, opts.target_cores) + if opts.no_game_data else None + ) + if cut is not None: + config, emu_profiles = cut.config, cut.profiles + + narrowings = _narrowings( + opts.source, opts.regions, opts.target_name, opts.one_per_slot, opts.required_only, + no_game_data=opts.no_game_data, + ) + zip_path = os.path.join( + output_dir, + _platform_pack_name( + platform_name, platforms_dir, narrowings, opts.system_filter, opts.pack_name + ), + ) + if opts.record is None: + os.makedirs(output_dir, exist_ok=True) + + # Build emulator-level validation index from the platform's own + # emulators (same scope as verify.py) + from common import resolve_platform_cores + + validation_index = {} + platform_profiles: dict[str, dict] = {} + if emu_profiles: + platform_profiles = { + name: emu_profiles[name] + for name in resolve_platform_cores(config, emu_profiles) + } + validation_index = _build_validation_index(platform_profiles) + validation_owners = destination_owners(platform_profiles) + slot_overrides = slots.pack_overrides( + config, emu_profiles or {}, db, zip_contents, opts.data_registry, + target_cores=opts.target_cores, source=opts.source, + ) + + pack_systems = platform_scope_systems( + config, emu_profiles or {}, opts.target_cores, opts.system_filter + ) + if opts.system_filter and not pack_systems: + reason = system_scope_error(config, platform_name, opts.system_filter, opts.target_name) + print(f" WARNING: {reason}") + return None + if opts.extras_only: + # The part of a split that carries the core extras no platform system + # owns: the platform's own files and trees went to the other parts. + pack_systems = {} + + plan = _PlatformPlan( + platform_name, platforms_dir, db, bios_dir, opts, config, emu_profiles, + zip_contents, cut, narrowings, zip_path, validation_index, platform_profiles, + validation_owners, slot_overrides, pack_systems, + ) + _settle_pack_choices(plan) + return plan + + +def _settle_pack_choices(plan: _PlatformPlan) -> None: + """The declaration each destination takes and the variants left out.""" + opts = plan.opts + if opts.source != "truth": + # Decided among the declarations this pack keeps: a preferred entry + # that --required-only drops must not silence its required twin, or + # the destination leaves the pack without a trace (RetroDECK's four + # Atari 5200 ROMs, optional under atari-400-800 and required under + # atari-5200). No data directories: the choice is about the baseline. + plan.preferred_entries = _preferred_entries( + plan.pack_systems, plan.db, plan.bios_dir, plan.base_dest, opts.required_only, + plan.zip_contents, opts.offline, + ) + + # Region selection is decided once, over both the platform baseline and the + # core extras, so the two phases below consult a single set. Grouping uses a + # dedicated extras pass with an empty seen set: it only needs group + # membership, never destinations, and a superset of members is the safe + # direction. Runs only when --region is given. + # The gap report both extras passes read, computed once: the region + # grouping and the core complement below ask it the same question. + plan.undeclared = ( + undeclared_report( + plan.config, opts.emulators_dir, plan.db, plan.emu_profiles, opts.target_cores, + include_all=(opts.source == "truth"), + ) + if opts.source != "platform" and opts.precomputed_extras is None + else None + ) + plan.region_drops, plan.region_fallbacks, plan.slot_undecidable = _select_variants( + plan.config, plan.pack_systems, opts.emulators_dir, plan.db, plan.base_dest, + plan.emu_profiles, opts.target_cores, opts.source, regions=opts.regions, + one_per_slot=opts.one_per_slot, required_only=opts.required_only, + undeclared=plan.undeclared, zip_contents=plan.zip_contents, + data_registry=opts.data_registry, slot_overrides=plan.slot_overrides, + ) + + +_STATUS_LABELS = {"untested": "UNTESTED", "excluded": "EXCLUDED", "missing": "MISSING"} + + +@dataclass +class _PlatformPackBuild: + """One platform pack being written: its members and what each slot came to. + + The four lookups are bound by generate_pack, the one place that says how + a platform file and a core extra are found and judged. + """ + + plan: _PlatformPlan + zf: zipfile.ZipFile | PackRecord + resolve: Callable[[dict, str], tuple[str | None, str]] + verdict: Callable[[dict, str | None, str], tuple[str, str | None]] + judge: Callable[[dict, str | None, str], tuple[str | None, str | None]] + resolve_extra: Callable[[dict, str], tuple[str | None, str, str]] + placed: _Placed = field(init=False) + total_files: int = 0 + core_count: int = 0 + save_count: int = 0 + tree_count: int = 0 + # Per-destination status. ``excluded`` means a same-named local payload + # exists but contradicts the declaration and is intentionally not shipped. + # It is distinct from a genuine coverage gap (``missing``). + file_status: dict[str, str] = field(default_factory=dict) + file_reasons: dict[str, str] = field(default_factory=dict) + # Core extras whose local copy contradicts the profile hash: packed, + # reported, and never counted against the platform's own file total. + core_discrepancies: list[str] = field(default_factory=list) + # Spellings left out as case twins of a packed file, for the README. + case_twins: list[str] = field(default_factory=list) + + def __post_init__(self) -> None: + self.placed = _Placed(self.plan.case_insensitive) + + def _flat(self, path: str) -> str: + return _flat(path, self.plan.base_dest, self.plan.opts.flatten) + + def run(self) -> None: + if self.plan.opts.source != "truth": + for sys_id, system in sorted(self.plan.pack_systems.items()): + for file_entry in system.get("files", []): + self._pack_declared(sys_id, file_entry) + # Core requirements: files platform's cores need but YAML doesn't declare + self._pack_core_files() + self._pack_trees() + self._write_readme() + + def _pack_declared(self, sys_id: str, file_entry: dict) -> None: + plan, opts = self.plan, self.plan.opts + if opts.required_only and file_entry.get("required") is False: + return + dest = sanitize_pack_path(file_entry.get("destination", file_entry["name"])) + if plan.region_drops and dest in plan.region_drops: + return + if not dest: + self._judge_unnamed(sys_id, file_entry) + return + full_dest = f"{plan.base_dest}/{dest}" if plan.base_dest else dest + already_packed = self.placed.holds(full_dest) + + preferred = plan.preferred_entries.get(full_dest) + if preferred is not None and id(file_entry) != preferred: + return + if self.placed.conflicts(full_dest): + self._judge_conflict(file_entry, full_dest) + return + if file_entry.get("storage", "embedded") == "user_provided": + if not already_packed: + self._write_instructions(file_entry, full_dest) + return + + local_path, status = self.resolve(file_entry, full_dest) + if status == "external": + self._pack_external(file_entry, full_dest) + return + if status == "not_found": + local_path = self._agnostic_fallback(sys_id, file_entry, full_dest, already_packed) + if local_path is None: + return + status = "agnostic_fallback" + # Fall through to normal packing below + chosen = self._settle_declared(file_entry, full_dest, dest, local_path, status, already_packed) + if chosen is not None: + self._ship_declared(file_entry, full_dest, chosen, already_packed) + + def _judge_unnamed(self, sys_id: str, file_entry: dict) -> None: + # EmuDeck-style entries (system:md5 whitelist, no filename). + # Judged as verify judges it: resolved on disk, the md5 + # list parsed once (case, comma lists). + plan = self.plan + fkey = f"{sys_id}/{file_entry.get('name', '')}" + held, held_status = resolve_file( + file_entry, plan.db, plan.bios_dir, plan.zip_contents, + data_dir_registry=plan.opts.data_registry, offline=plan.opts.offline, + ) + if held_status == "not_found" or not held: + self.file_status[fkey] = "missing" + elif self.verdict(file_entry, held, held_status)[1]: + self.file_status[fkey] = "excluded" + else: + self.file_status.setdefault(fkey, "ok") + + def _judge_conflict(self, file_entry: dict, full_dest: str) -> None: + # Content ships under the conflicting shape (file vs dir, + # e.g. SGB1.sfc). verify.py resolves each declaration on + # its own, so this one is counted by what it resolves to: + # an unresolved one counted OK had the pack claim a file + # verify reports missing. + plan = self.plan + conflict_path, conflict_status = resolve_file( + file_entry, plan.db, plan.bios_dir, plan.zip_contents, + data_dir_registry=plan.opts.data_registry, offline=plan.opts.offline, + ) + # The verdict of the main path: a zipped_file archive is + # judged by the member inside, and a frontend that hashes + # one digest accepts a file another declared hash + # contradicts, as verify judges it. + conflict_status, refusal = self.verdict(file_entry, conflict_path, conflict_status) + if conflict_status == "not_found": + self.file_status[full_dest] = "missing" + self.file_reasons[full_dest] = "not found" + elif refusal: + self.file_status[full_dest] = "excluded" + self.file_reasons[full_dest] = refusal + else: + self.file_status.setdefault(full_dest, "ok") + + def _write_instructions(self, file_entry: dict, full_dest: str) -> None: + base_dest = self.plan.base_dest + self.placed.take(full_dest) + self.file_status.setdefault(full_dest, "ok") + instructions = file_entry.get( + "instructions", "Please provide this file manually." + ) + instr_name = f"INSTRUCTIONS_{file_entry['name']}.txt" + instr_path = ( + f"{base_dest}/{instr_name}" if base_dest else instr_name + ) + _write_generated_member( + self.zf, + self._flat(instr_path), + f"File needed: {file_entry['name']}\n\n{instructions}\n", + ) + self.total_files += 1 + + def _pack_external(self, file_entry: dict, full_dest: str) -> None: + file_ext = os.path.splitext(file_entry["name"])[1] or "" + with tempfile.NamedTemporaryFile( + delete=False, suffix=file_ext, dir=packresolve.scratch_dir() + ) as tmp: + tmp_path = tmp.name + + # The slot is the platform's file whether the download + # arrives or not: verify cannot fetch, and a core file + # filling it on a failed download made the two differ. + self.placed.take(full_dest) + try: + if self.plan.opts.record is None and download_external(file_entry, tmp_path): + extract = file_entry.get("extract", False) + if extract and tmp_path.endswith(".zip"): + _extract_zip_to_archive(tmp_path, self._flat(full_dest), self.zf) + else: + _add_pack_member(self.zf, tmp_path, self._flat(full_dest)) + self.file_status.setdefault(full_dest, "ok") + self.total_files += 1 + else: + self.file_status[full_dest] = "missing" + self.file_reasons[full_dest] = "external download failed" + finally: + if os.path.exists(tmp_path): + os.unlink(tmp_path) + + def _agnostic_fallback( + self, sys_id: str, file_entry: dict, full_dest: str, already_packed: bool, + ) -> str | None: + # A filename-agnostic core takes any image of its system, + # renamed. Only an existence frontend accepts that. + plan = self.plan + found = None + if not reads_file_contents(plan.verification_mode): + found = agnostic_substitute(file_entry, sys_id, plan.db, plan.platform_profiles) + if not found or not found[0]: + if not already_packed: + self.file_status[full_dest] = "missing" + self.file_reasons[full_dest] = "not found" + return None + local_path, agnostic_path = found + original_name = os.path.basename(local_path) + dest_name = file_entry.get("name", "") + if original_name != dest_name and agnostic_path: + alt_names = [ + entry.get("name", "") + for entry in plan.db.get("files", {}).values() + if entry.get("path", "").startswith(agnostic_path) + and entry.get("name", "") and entry.get("name", "") != original_name + ] + readme_text = _build_agnostic_rename_readme(dest_name, original_name, alt_names) + readme_name = f"RENAMED_{dest_name}.txt" + readme_full = f"{plan.base_dest}/{readme_name}" if plan.base_dest else readme_name + if readme_full not in self.placed.destinations: + _write_generated_member(self.zf, self._flat(readme_full), readme_text) + self.placed.destinations.add(readme_full) + return local_path + + def _settle_declared( + self, + file_entry: dict, + full_dest: str, + dest: str, + local_path: str | None, + status: str, + already_packed: bool, + ) -> str | None: + """The copy a declaration ships once judged, or None when refused.""" + plan = self.plan + # The verdict the install manifest reads too: a member missing + # from a zipped_file archive, or a hash the frontend rejects. + status, refusal = self.verdict(file_entry, local_path, status) + if refusal: + # An earlier declaration of this path shipped a file this + # one refuses: it is in the pack, untested as verify + # counts it, not excluded. + self.file_status[full_dest] = "untested" if already_packed else "excluded" + self.file_reasons[full_dest] = refusal + return None + if not hash_mismatch_excludes_file(plan.verification_mode) and ( + status == "hash_mismatch" and local_path + ): + # Existence platforms accept any file at the declared path: + # their code never reads the bytes. An upstream hash that + # contradicts a local dump is reported, not acted on. + self.file_reasons.setdefault( + full_dest, + existence_discrepancy(file_entry, local_path, plan.platform_display), + ) + if self.file_status.get(full_dest) == "missing": + # An earlier declaration of this path found nothing, and this + # one fills it, as verify counts it. + del self.file_status[full_dest] + self.file_reasons.pop(full_dest, None) + self.file_status.setdefault(full_dest, "ok") + + # Emulator-level validation: informational only for platform packs. + # Platform verification (existence/md5) is the authority for pack status. + # Emulator checks are supplementary: logged but don't downgrade. + # When a discrepancy is found, try to find a file satisfying both. + if self.file_status.get(full_dest) == "ok": + local_path, disagreement = self.judge(file_entry, local_path, dest) + if disagreement: + self.file_reasons.setdefault( + full_dest, f"{plan.platform_display} says OK but {disagreement}" + ) + return local_path + + def _ship_declared( + self, file_entry: dict, full_dest: str, local_path: str, already_packed: bool, + ) -> None: + within = self.plan.opts.within + if already_packed: + if full_dest not in self.placed.destinations: + self.case_twins.append(self._flat(full_dest)) + return + if within is not None and not _same_bytes(local_path, within.get(self._flat(full_dest))): + # The full pack holds another system's file here. + return + if self.file_status.get(full_dest) == "excluded": + # An earlier declaration refused the file this one ships. + self.file_status[full_dest] = "untested" + self.placed.take(full_dest) + + extract = file_entry.get("extract", False) + flat_dest = self._flat(full_dest) + if extract and local_path.endswith(".zip"): + _extract_zip_to_archive(local_path, flat_dest, self.zf) + elif local_path.endswith(".zip"): + _add_zip_to_pack(local_path, flat_dest, self.zf, file_entry) + else: + _add_pack_member(self.zf, local_path, flat_dest) + self.total_files += 1 + + def _region_dropped(self, fe: dict) -> bool: + drops = self.plan.region_drops + return bool(drops) and extra_region_key(fe, declared_root(self.plan.config)) in drops + + def _spares(self, files: list[dict]) -> set[int]: + if not self.plan.opts.required_only: + return set() + return core_file_spares(files, self._region_dropped) + + def _core_files(self) -> list[dict]: + plan, opts = self.plan, self.plan.opts + if opts.source == "platform": + return [] + if opts.precomputed_extras is not None: + return opts.precomputed_extras + if opts.system_filter: + # The systems asked for, with what their cores need: the split + # packs get exactly this, and a full pack of one system had none. + return _extras_for_systems( + _collect_emulator_extras( + plan.config, opts.emulators_dir, plan.db, set(), plan.base_dest, + plan.emu_profiles, target_cores=opts.target_cores, + include_all=(opts.source == "truth"), undeclared=plan.undeclared, + ), + opts.system_filter, + ) + return _collect_emulator_extras( + plan.config, + opts.emulators_dir, + plan.db, + self.placed.destinations, + plan.base_dest, + plan.emu_profiles, + target_cores=opts.target_cores, + include_all=(opts.source == "truth"), + undeclared=plan.undeclared, + ) + + def _pack_core_files(self) -> None: + core_files = self._core_files() + spares = self._spares(core_files) + for fe in core_files: + if self.plan.opts.required_only and (fe.get("required") is False or id(fe) in spares): + continue + self._pack_core_file(fe) + + def _pack_core_file(self, fe: dict) -> None: + plan = self.plan + dest = sanitize_pack_path(fe.get("destination", fe["name"])) + if self._region_dropped(fe): + return + if not dest: + return + if fe.get("root") in OUTER_ROOTS: + # Read below the roms directory or in an emulator folder: the + # pack is extracted into the BIOS folder and cannot reach + # them, the installer places these files. + return + # Core extras: _collect_emulator_extras already adjusted + # destinations for slug-based platforms. Apply the effective + # prefix (base_dest, or inferred from YAML when base_dest is + # empty, as RetroDECK infers "bios"). A file read below the save + # directory goes to the pack's save folder instead. + full_dest = extra_full_destination( + fe, _detect_extras_prefix(plan.config, plan.base_dest) + ) + if not full_dest: + return + # Taken, a case twin on a case-insensitive filesystem, or a + # file/directory conflict (SGB1.sfc vs SGB1.sfc/): verify replays + # the same rule (packextras.hidden_extras). + block = self.placed.block(full_dest) + if block: + if block == "case_twin" and self.resolve_extra(fe, dest)[0]: + self.case_twins.append(self._flat(full_dest)) + return + + # The emulator's own check picks the copy, as for a declared file + # and in the emulator pack: the name led RetroArch's GC/dsp_rom.bin + # to Dolphin's obsolete v0.3.1 free ROM, beside Nintendo's coef + # ROM, a pair Dolphin warns about at every start. + local_path, status, disagreement = self.resolve_extra(fe, dest) + if not local_path: + return + self._note_core_discrepancy(fe, full_dest, local_path, status, disagreement) + + flat_dest = self._flat(full_dest) + within = plan.opts.within + if within is not None and not _same_bytes(local_path, within.get(flat_dest)): + return + if local_path.endswith(".zip"): + _add_zip_to_pack(local_path, flat_dest, self.zf, fe) + else: + _add_pack_member(self.zf, local_path, flat_dest) + if self.file_status.get(full_dest) in ("missing", "excluded"): + self._core_fills_declaration(fe, full_dest) + self.placed.take(full_dest) + self.core_count += 1 + if fe.get("root") == "save": + self.save_count += 1 + self.total_files += 1 + + def _note_core_discrepancy( + self, fe: dict, full_dest: str, local_path: str, status: str, disagreement: str, + ) -> None: + if disagreement: + self.core_discrepancies.append(f"{full_dest}: {disagreement}") + if status == "hash_mismatch" and not _matches_an_alternative( + fe, local_path, self.plan.emu_profiles or {} + ): + # The core's declared hash comes from its source, the local dump + # is what the collection holds. Shipping it keeps the emulator + # working when the code never reads the hash, and the divergence is + # reported so it can be resolved at the profile or the dump. + # file_status tracks platform declarations only, so the report + # goes to its own list and never moves the pack's own count. + self.core_discrepancies.append( + f"{full_dest}: declared hash of " + f"{fe.get('source_profile') or fe.get('source_emulator', 'core profile')}" + " does not match the packed copy" + ) + + def _core_fills_declaration(self, fe: dict, full_dest: str) -> None: + source_name = fe.get("source_profile") or fe.get( + "source_emulator", "core profile" + ) + if reads_file_contents(self.plan.verification_mode): + # The frontend hashes what sits at the path, and the core's + # copy is not the hash it declares: the file ships for the + # core, the platform's own check still fails, as verify says. + self.file_reasons[full_dest] = ( + "platform-declared hash unavailable; " + f"packed the validated {source_name} requirement, " + "which the platform check does not accept" + ) + else: + # An existence frontend is satisfied by any file at the path. + self.file_status[full_dest] = "ok" + self.file_reasons.pop(full_dest, None) + + def _pack_trees(self) -> None: + plan, opts = self.plan, self.plan.opts + # The trees the cores read follow them as their extras do: by the + # systems asked for, and those of no system in the part of their own. + core_trees = ( + [] if opts.source == "platform" + else core_data_trees( + plan.config, plan.emu_profiles, plan.platform_name, opts.data_registry, + opts.target_cores, + ) + ) + if opts.extras_only: + owned = list(platform_scope_systems(plan.config, plan.emu_profiles, opts.target_cores)) + core_trees = [t for t in core_trees if not _extras_for_systems([t], owned)] + elif opts.system_filter: + core_trees = _extras_for_systems(core_trees, opts.system_filter) + # Counted apart: the trees a core reads are not on the platform's + # list. MyMinUI declares nothing and its line read "6944 baseline". + self.total_files += self._pack_data_directories(plan.pack_systems, ()) + self.tree_count = self._pack_data_directories({}, core_trees) + self.total_files += self.tree_count + + def _pack_data_directories(self, pack_systems: dict, core_trees: Iterable[dict]) -> int: + """Add the data directories to the pack. Returns how many files went in.""" + plan = self.plan + added = 0 + for src, full in _data_directory_members( + pack_systems, plan.opts.data_registry, plan.platform_name, plan.base_dest, + self.placed.fold, self.placed.destinations, self.placed.lower, + self.placed.parents, required_only=plan.opts.required_only, + core_trees=core_trees, + ): + _add_pack_member(self.zf, src, self._flat(full)) + if isinstance(self.zf, PackRecord): + self.zf.trees.append(self._flat(full)) + added += 1 + return added + + def _unfolded_twins(self) -> None: + # The spellings a folded platform counts as declared never reach + # the extras: collected as the filesystem that keeps case sees + # them, as the manifest's case_variants are (Mesen-CE's bios.col + # beside the declared BIOS.col). + plan, opts = self.plan, self.plan.opts + unfolded = _collect_emulator_extras( + {**plan.config, "case_insensitive_fs": False}, opts.emulators_dir, plan.db, set(), + plan.base_dest, plan.emu_profiles, target_cores=opts.target_cores, + include_all=(opts.source == "truth"), + ) + if opts.system_filter: + unfolded = _extras_for_systems(unfolded, opts.system_filter) + prefix = _detect_extras_prefix(plan.config, plan.base_dest) + unfolded_spares = self._spares(unfolded) + for fe in unfolded: + if opts.required_only and (fe.get("required") is False or id(fe) in unfolded_spares): + continue + if self._region_dropped(fe): + continue + full = extra_full_destination(fe, prefix) + if ( + full and full not in self.placed.destinations and full.lower() in self.placed.lower + and self.resolve_extra( + fe, sanitize_pack_path(fe.get("destination", fe["name"])) + )[0] + ): + self.case_twins.append(self._flat(full)) + + def _write_readme(self) -> None: + # README.txt for users: personalized step-by-step per platform + plan, opts = self.plan, self.plan.opts + registry = _platform_registry(plan.platforms_dir).get(plan.platform_name, {}) + install = registry.get("install", {}) or {} + keeps_case = plan.case_insensitive and any( + d.get("os") not in CASE_FOLDING_OS for d in install.get("detect", []) + ) + if keeps_case and opts.source != "platform" and opts.precomputed_extras is None: + self._unfolded_twins() + save_count = self.save_count + readme_text = _build_readme( + plan.platform_name, plan.platform_display, plan.base_dest, self.total_files, + len(plan.pack_systems), + source=opts.source, + contributors=registry.get("contributed_by", []), + regions=opts.regions, + fallback_systems=plan.region_fallbacks, + one_per_slot=opts.one_per_slot, + undecidable_slots=plan.slot_undecidable, + narrowings=plan.narrowings, + system_filter=opts.system_filter, + detect=install.get("detect", []), + standalone_copies=install.get("standalone_copies", []), + launch_folders=len(plan.config.get("core_system_dirs") or {}), + save_folder=(save_layout(plan.config).in_pack if save_count else ""), + save_where=( + save_place( + str(registry.get("extract_to") or ""), + save_layout(plan.config).folder, plan.base_dest, + ) if save_count else "" + ), + save_by_system=bool(save_count and save_layout(plan.config).by_system), + case_twins=( + _case_twin_pairs( + self.case_twins, self.placed.destinations, plan.base_dest, opts.flatten + ) + if keeps_case else [] + ), + ) + _write_generated_member(self.zf, "README.txt", readme_text) + for marker in pack_markers(plan.platform_name, opts.target_name, plan.platforms_dir): + _write_generated_member( + self.zf, + self._flat(f"{plan.base_dest}/{marker}" if plan.base_dest else marker), + "", + ) + + def report(self) -> None: + plan, opts = self.plan, self.plan.opts + _report_pack_counts( + plan.zip_path, self.file_status, self.total_files, self.core_count, opts.source, + plan.verification_mode, tree_count=self.tree_count, + ) + if plan.cut is not None: + _report_left_out( + plan.cut, + systems=set(plan.pack_systems) if opts.source != "truth" else set(), + cores=opts.source != "platform" and opts.precomputed_extras is None, + ) + for key, reason in sorted(self.file_reasons.items()): + label = _STATUS_LABELS.get(self.file_status.get(key, ""), "DISCREPANCY") + print(f" {label}: {key}: {reason}") + for note in sorted(self.core_discrepancies): + print(f" DISCREPANCY: {note}") + + +def _platform_registry(platforms_dir: str) -> dict: + """The platforms of _registry.yml, or nothing when the file is absent.""" + registry_path = Path(platforms_dir) / "_registry.yml" + if not registry_path.exists(): + return {} + with open(registry_path) as f: + return (yaml_load(f) or {}).get("platforms", {}) + + def generate_pack( platform_name: str, platforms_dir: str, db: dict, bios_dir: str, output_dir: str, - emulators_dir: str = "emulators", - zip_contents: dict | None = None, - data_registry: dict | None = None, - emu_profiles: dict | None = None, - target_cores: set[str] | None = None, - required_only: bool = False, - system_filter: list[str] | None = None, - precomputed_extras: list[dict] | None = None, - extras_only: bool = False, - source: str = "full", - flatten: bool = True, - regions: list[str] | None = None, - target_name: str | None = None, - one_per_slot: bool = False, - offline: bool | None = None, - pack_name: str | None = None, - no_game_data: bool = False, - record: PackRecord | None = None, - within: dict[str, str] | None = None, + **options: Unpack[PackOptions], ) -> str | None: """Generate a ZIP pack for a platform. @@ -914,657 +1786,51 @@ def generate_pack( Returns the path to the generated ZIP, or None on failure. """ - config = load_platform_config(platform_name, platforms_dir) - if target_name: - config = {**config, TARGET_KEY: target_name} - if zip_contents is None: - zip_contents = {} - if emu_profiles is None: - # The pipeline always passes them, but a caller that does not must get the - # same pack, not one built without validation, slots or agnostic cores. - emu_profiles = load_emulator_profiles(emulators_dir) - # Cut from the full config and profiles: the category of a platform - # entry is read from the profiles' game data entries. - cut = ( - without_game_data(config, emu_profiles, data_registry, target_cores) - if no_game_data else None - ) - if cut is not None: - config, emu_profiles = cut.config, cut.profiles - - verification_mode = config.get("verification_mode", "existence") - platform_display = config.get("platform", platform_name) - base_dest = config.get("base_destination", "") - - narrowings = _narrowings( - source, regions, target_name, one_per_slot, required_only, - no_game_data=no_game_data, - ) - zip_path = os.path.join( - output_dir, - _platform_pack_name( - platform_name, platforms_dir, narrowings, system_filter, pack_name - ), - ) - if record is None: - os.makedirs(output_dir, exist_ok=True) - - # Case-insensitive dedup only for platforms targeting Windows/macOS. - # Linux-only platforms (Batocera, Recalbox, RetroDECK, Lakka, RomM) - # are case-sensitive and may have distinct files like DISK.ROM vs disk.rom. - case_insensitive = config.get("case_insensitive_fs", False) - - total_files = 0 - missing_files = [] - # Core extras whose local copy contradicts the profile hash: packed, - # reported, and never counted against the platform's own file total. - core_discrepancies: list[str] = [] - user_provided = [] - seen_destinations: set[str] = set() - seen_lower: set[str] = set() # only used when case_insensitive=True - # Spellings left out as case twins of a packed file, for the README. - case_twins: list[str] = [] - seen_parents: set[str] = ( - set() - ) # parent dirs of added files (path conflict detection) - # Per-destination status. ``excluded`` means a same-named local payload - # exists but contradicts the declaration and is intentionally not shipped. - # It is distinct from a genuine coverage gap (``missing``). - file_status: dict[str, str] = {} - file_reasons: dict[str, str] = {} - - # Build emulator-level validation index from the platform's own - # emulators (same scope as verify.py) - from common import resolve_platform_cores - - validation_index = {} - platform_profiles: dict[str, dict] = {} - if emu_profiles: - platform_profiles = { - name: emu_profiles[name] - for name in resolve_platform_cores(config, emu_profiles) - } - validation_index = _build_validation_index(platform_profiles) - validation_owners = destination_owners(platform_profiles) - slot_overrides = slots.pack_overrides( - config, emu_profiles or {}, db, zip_contents, data_registry, - target_cores=target_cores, source=source, - ) - - pack_systems = platform_scope_systems( - config, emu_profiles or {}, target_cores, system_filter - ) - if system_filter and not pack_systems: - reason = system_scope_error(config, platform_name, system_filter, target_name) - print(f" WARNING: {reason}") + _reject_unknown("generate_pack", options, PackOptions.__optional_keys__) + opts = _PackSettings(**options) + plan = _plan_platform_pack(platform_name, platforms_dir, db, bios_dir, output_dir, opts) + if plan is None: return None - if extras_only: - # The part of a split that carries the core extras no platform system - # owns: the platform's own files and trees went to the other parts. - pack_systems = {} - preferred_entries: dict[str, int] = {} - if source != "truth": - # Decided among the declarations this pack keeps: a preferred entry - # that --required-only drops must not silence its required twin, or - # the destination leaves the pack without a trace (RetroDECK's four - # Atari 5200 ROMs, optional under atari-400-800 and required under - # atari-5200). No data directories: the choice is about the baseline. - preferred_entries = _preferred_entries( - pack_systems, db, bios_dir, base_dest, required_only, - zip_contents, offline, + def resolve(file_entry: dict, full_dest: str) -> tuple[str | None, str]: + return resolve_declaration( + file_entry, + db, + bios_dir, + plan.zip_contents, + opts.data_registry, + plan.slot_overrides.get(full_dest), + offline=opts.offline, ) - # Region selection is decided once, over both the platform baseline and the - # core extras, so the two phases below consult a single set. Grouping uses a - # dedicated extras pass with an empty seen set: it only needs group - # membership, never destinations, and a superset of members is the safe - # direction. Runs only when --region is given. - # The gap report both extras passes read, computed once: the region - # grouping and the core complement below ask it the same question. - undeclared = ( - undeclared_report( - config, emulators_dir, db, emu_profiles, target_cores, - include_all=(source == "truth"), + def verdict(file_entry: dict, local_path: str | None, status: str) -> tuple[str, str | None]: + return baseline_verdict( + file_entry, local_path, status, plan.verification_mode, + plan.config.get("hash_rule", ""), ) - if source != "platform" and precomputed_extras is None - else None - ) - region_drops, region_fallbacks, slot_undecidable = _select_variants( - config, pack_systems, emulators_dir, db, base_dest, emu_profiles, - target_cores, source, regions, one_per_slot, required_only, - undeclared=undeclared, zip_contents=zip_contents, - data_registry=data_registry, slot_overrides=slot_overrides, + + def judge(file_entry: dict, local_path: str | None, dest: str) -> tuple[str | None, str | None]: + return validated_choice( + file_entry, local_path, db, plan.validation_index, bios_dir, + digest_algorithm(plan.verification_mode), dest, plan.validation_owners, + ) + + extra_resolver = ExtraResolver( + db, bios_dir, plan.zip_contents, opts.data_registry, plan.validation_index, + plan.validation_owners, opts.offline, profiles=plan.emu_profiles, ) + def resolve_extra(fe: dict, dest: str) -> tuple[str | None, str, str]: + return extra_resolver.resolve(fe, dest) + with ( - contextlib.nullcontext(record) if record is not None - else _writing_pack(zip_path) + contextlib.nullcontext(opts.record) if opts.record is not None + else _writing_pack(plan.zip_path) ) as zf: - if source != "truth": - for sys_id, system in sorted(pack_systems.items()): - for file_entry in system.get("files", []): - if required_only and file_entry.get("required") is False: - continue - dest = sanitize_pack_path(file_entry.get("destination", file_entry["name"])) - if region_drops and dest in region_drops: - continue - if not dest: - # EmuDeck-style entries (system:md5 whitelist, no filename). - # Judged as verify judges it: resolved on disk, the md5 - # list parsed once (case, comma lists). - fkey = f"{sys_id}/{file_entry.get('name', '')}" - held, held_status = resolve_file( - file_entry, db, bios_dir, zip_contents, - data_dir_registry=data_registry, offline=offline, - ) - if held_status == "not_found" or not held: - file_status[fkey] = "missing" - elif baseline_verdict( - file_entry, held, held_status, verification_mode, - config.get("hash_rule", ""), - )[1]: - file_status[fkey] = "excluded" - else: - file_status.setdefault(fkey, "ok") - continue - if base_dest: - full_dest = f"{base_dest}/{dest}" - else: - full_dest = dest - - dedup_key = full_dest - already_packed = dedup_key in seen_destinations or ( - case_insensitive and dedup_key.lower() in seen_lower - ) - - preferred = preferred_entries.get(dedup_key) - if preferred is not None and id(file_entry) != preferred: - continue - - if _has_path_conflict(full_dest, seen_destinations, seen_parents): - # Content ships under the conflicting shape (file vs dir, - # e.g. SGB1.sfc). verify.py resolves each declaration on - # its own, so this one is counted by what it resolves to: - # an unresolved one counted OK had the pack claim a file - # verify reports missing. - conflict_path, conflict_status = resolve_file( - file_entry, db, bios_dir, zip_contents, - data_dir_registry=data_registry, offline=offline, - ) - # The verdict of the main path: a zipped_file archive is - # judged by the member inside, and a frontend that hashes - # one digest accepts a file another declared hash - # contradicts, as verify judges it. - conflict_status, refusal = baseline_verdict( - file_entry, conflict_path, conflict_status, verification_mode, - config.get("hash_rule", ""), - ) - if conflict_status == "not_found": - file_status[dedup_key] = "missing" - file_reasons[dedup_key] = "not found" - elif refusal: - file_status[dedup_key] = "excluded" - file_reasons[dedup_key] = refusal - else: - file_status.setdefault(dedup_key, "ok") - continue - - storage = file_entry.get("storage", "embedded") - - if storage == "user_provided": - if already_packed: - continue - seen_destinations.add(dedup_key) - _register_path(dedup_key, seen_destinations, seen_parents) - if case_insensitive: - seen_lower.add(dedup_key.lower()) - file_status.setdefault(dedup_key, "ok") - instructions = file_entry.get( - "instructions", "Please provide this file manually." - ) - instr_name = f"INSTRUCTIONS_{file_entry['name']}.txt" - instr_path = ( - f"{base_dest}/{instr_name}" if base_dest else instr_name - ) - _write_generated_member( - zf, - _flat(instr_path, base_dest, flatten), - f"File needed: {file_entry['name']}\n\n{instructions}\n", - ) - user_provided.append(file_entry["name"]) - total_files += 1 - continue - - local_path, status = resolve_declaration( - file_entry, - db, - bios_dir, - zip_contents, - data_registry, - slot_overrides.get(full_dest), - offline=offline, - ) - - if status == "external": - file_ext = os.path.splitext(file_entry["name"])[1] or "" - with tempfile.NamedTemporaryFile( - delete=False, suffix=file_ext, dir=packresolve.scratch_dir() - ) as tmp: - tmp_path = tmp.name - - # The slot is the platform's file whether the download - # arrives or not: verify cannot fetch, and a core file - # filling it on a failed download made the two differ. - seen_destinations.add(dedup_key) - _register_path(dedup_key, seen_destinations, seen_parents) - if case_insensitive: - seen_lower.add(dedup_key.lower()) - try: - if record is None and download_external(file_entry, tmp_path): - extract = file_entry.get("extract", False) - if extract and tmp_path.endswith(".zip"): - _extract_zip_to_archive(tmp_path, _flat(full_dest, base_dest, flatten), zf) - else: - _add_pack_member(zf, tmp_path, _flat(full_dest, base_dest, flatten)) - file_status.setdefault(dedup_key, "ok") - total_files += 1 - else: - file_status[dedup_key] = "missing" - file_reasons[dedup_key] = "external download failed" - finally: - if os.path.exists(tmp_path): - os.unlink(tmp_path) - continue - - if status == "not_found": - # A filename-agnostic core takes any image of its system, - # renamed. Only an existence frontend accepts that. - files_db = db.get("files", {}) - agnostic_path = None - agnostic_resolved = False - if not reads_file_contents(verification_mode): - found = agnostic_substitute(file_entry, sys_id, db, platform_profiles) - if found: - local_path, agnostic_path = found - agnostic_resolved = True - - if agnostic_resolved and local_path: - original_name = os.path.basename(local_path) - dest_name = file_entry.get("name", "") - if original_name != dest_name and agnostic_path: - alt_names = [] - for _s, _e in files_db.items(): - _p = _e.get("path", "") - if _p.startswith(agnostic_path): - _n = _e.get("name", "") - if _n and _n != original_name: - alt_names.append(_n) - readme_text = _build_agnostic_rename_readme( - dest_name, - original_name, - alt_names, - ) - readme_name = f"RENAMED_{dest_name}.txt" - readme_full = ( - f"{base_dest}/{readme_name}" - if base_dest - else readme_name - ) - if readme_full not in seen_destinations: - _write_generated_member( - zf, - _flat(readme_full, base_dest, flatten), - readme_text, - ) - seen_destinations.add(readme_full) - status = "agnostic_fallback" - # Fall through to normal packing below - else: - if not already_packed: - file_status[dedup_key] = "missing" - file_reasons[dedup_key] = "not found" - continue - - # The verdict the install manifest reads too: a member missing - # from a zipped_file archive, or a hash the frontend rejects. - status, refusal = baseline_verdict( - file_entry, local_path, status, verification_mode, - config.get("hash_rule", ""), - ) - if refusal: - # An earlier declaration of this path shipped a file this - # one refuses: it is in the pack, untested as verify - # counts it, not excluded. - file_status[dedup_key] = "untested" if already_packed else "excluded" - file_reasons[dedup_key] = refusal - continue - if not hash_mismatch_excludes_file(verification_mode) and ( - status == "hash_mismatch" and local_path - ): - # Existence platforms accept any file at the declared path: - # their code never reads the bytes. An upstream hash that - # contradicts a local dump is reported, not acted on. - file_reasons.setdefault( - dedup_key, - existence_discrepancy(file_entry, local_path, platform_display), - ) - if file_status.get(dedup_key) == "missing": - # An earlier declaration of this path found nothing, and this - # one fills it, as verify counts it. - del file_status[dedup_key] - file_reasons.pop(dedup_key, None) - file_status.setdefault(dedup_key, "ok") - - # Emulator-level validation: informational only for platform packs. - # Platform verification (existence/md5) is the authority for pack status. - # Emulator checks are supplementary: logged but don't downgrade. - # When a discrepancy is found, try to find a file satisfying both. - if file_status.get(dedup_key) == "ok": - local_path, disagreement = validated_choice( - file_entry, local_path, db, validation_index, bios_dir, - digest_algorithm(verification_mode), dest, validation_owners, - ) - if disagreement: - file_reasons.setdefault( - dedup_key, f"{platform_display} says OK but {disagreement}" - ) - - if already_packed: - if dedup_key not in seen_destinations: - case_twins.append(_flat(full_dest, base_dest, flatten)) - continue - if within is not None and not _same_bytes( - local_path, within.get(_flat(full_dest, base_dest, flatten)) - ): - # The full pack holds another system's file here. - continue - if file_status.get(dedup_key) == "excluded": - # An earlier declaration refused the file this one ships. - file_status[dedup_key] = "untested" - seen_destinations.add(dedup_key) - _register_path(dedup_key, seen_destinations, seen_parents) - if case_insensitive: - seen_lower.add(dedup_key.lower()) - - extract = file_entry.get("extract", False) - flat_dest = _flat(full_dest, base_dest, flatten) - if extract and local_path.endswith(".zip"): - _extract_zip_to_archive(local_path, flat_dest, zf) - elif local_path.endswith(".zip"): - _add_zip_to_pack(local_path, flat_dest, zf, file_entry) - else: - _add_pack_member(zf, local_path, flat_dest) - total_files += 1 - - # Core requirements: files platform's cores need but YAML doesn't declare - if emu_profiles is None: - emu_profiles = load_emulator_profiles(emulators_dir) - if source == "platform": - core_files = [] - elif precomputed_extras is not None: - core_files = precomputed_extras - elif system_filter: - # The systems asked for, with what their cores need: the split - # packs get exactly this, and a full pack of one system had none. - core_files = _extras_for_systems( - _collect_emulator_extras( - config, emulators_dir, db, set(), base_dest, emu_profiles, - target_cores=target_cores, include_all=(source == "truth"), - undeclared=undeclared, - ), - system_filter, - ) - else: - core_files = _collect_emulator_extras( - config, - emulators_dir, - db, - seen_destinations, - base_dest, - emu_profiles, - target_cores=target_cores, - include_all=(source == "truth"), - undeclared=undeclared, - ) - - core_count = 0 - save_count = 0 - extra_resolver = ExtraResolver( - db, bios_dir, zip_contents, data_registry, validation_index, - validation_owners, offline, profiles=emu_profiles, - ) - spares = core_file_spares( - core_files, - lambda fe: bool(region_drops) - and extra_region_key(fe, declared_root(config)) in region_drops, - ) if required_only else set() - for fe in core_files: - if required_only and (fe.get("required") is False or id(fe) in spares): - continue - dest = sanitize_pack_path(fe.get("destination", fe["name"])) - if region_drops and extra_region_key(fe, declared_root(config)) in region_drops: - continue - if not dest: - continue - if fe.get("root") in OUTER_ROOTS: - # Read below the roms directory or in an emulator folder: the - # pack is extracted into the BIOS folder and cannot reach - # them, the installer places these files. - continue - # Core extras: _collect_emulator_extras already adjusted - # destinations for slug-based platforms. Apply the effective - # prefix (base_dest, or inferred from YAML when base_dest is - # empty, as RetroDECK infers "bios"). A file read below the save - # directory goes to the pack's save folder instead. - full_dest = extra_full_destination( - fe, _detect_extras_prefix(config, base_dest) - ) - if not full_dest: - continue - # Taken, a case twin on a case-insensitive filesystem, or a - # file/directory conflict (SGB1.sfc vs SGB1.sfc/): verify replays - # the same rule (packextras.hidden_extras). - block = placement_block( - full_dest, seen_destinations, seen_lower, seen_parents, - case_insensitive, - ) - if block: - if block == "case_twin" and extra_resolver.resolve(fe, dest)[0]: - case_twins.append(_flat(full_dest, base_dest, flatten)) - continue - - # The emulator's own check picks the copy, as for a declared file - # and in the emulator pack: the name led RetroArch's GC/dsp_rom.bin - # to Dolphin's obsolete v0.3.1 free ROM, beside Nintendo's coef - # ROM, a pair Dolphin warns about at every start. - local_path, status, disagreement = extra_resolver.resolve(fe, dest) - if not local_path: - continue - if disagreement: - core_discrepancies.append(f"{full_dest}: {disagreement}") - if status == "hash_mismatch" and not _matches_an_alternative( - fe, local_path, emu_profiles or {} - ): - # The core's declared hash comes from its source, the local dump - # is what the collection holds. Shipping it keeps the emulator - # working when the code never reads the hash, and the divergence is - # reported so it can be resolved at the profile or the dump. - # file_status tracks platform declarations only, so the report - # goes to its own list and never moves the pack's own count. - core_discrepancies.append( - f"{full_dest}: declared hash of " - f"{fe.get('source_profile') or fe.get('source_emulator', 'core profile')}" - " does not match the packed copy" - ) - - flat_dest = _flat(full_dest, base_dest, flatten) - if within is not None and not _same_bytes(local_path, within.get(flat_dest)): - continue - if local_path.endswith(".zip"): - _add_zip_to_pack(local_path, flat_dest, zf, fe) - else: - _add_pack_member(zf, local_path, flat_dest) - if file_status.get(full_dest) in ("missing", "excluded"): - source_name = fe.get("source_profile") or fe.get( - "source_emulator", "core profile" - ) - if reads_file_contents(verification_mode): - # The frontend hashes what sits at the path, and the core's - # copy is not the hash it declares: the file ships for the - # core, the platform's own check still fails, as verify says. - file_reasons[full_dest] = ( - "platform-declared hash unavailable; " - f"packed the validated {source_name} requirement, " - "which the platform check does not accept" - ) - else: - # An existence frontend is satisfied by any file at the path. - file_status[full_dest] = "ok" - file_reasons.pop(full_dest, None) - seen_destinations.add(full_dest) - _register_path(full_dest, seen_destinations, seen_parents) - if case_insensitive: - seen_lower.add(full_dest.lower()) - core_count += 1 - if fe.get("root") == "save": - save_count += 1 - total_files += 1 - - # The trees the cores read follow them as their extras do: by the - # systems asked for, and those of no system in the part of their own. - core_trees = ( - [] if source == "platform" - else core_data_trees( - config, emu_profiles, platform_name, data_registry, target_cores - ) - ) - if extras_only: - owned = list(platform_scope_systems(config, emu_profiles, target_cores)) - core_trees = [t for t in core_trees if not _extras_for_systems([t], owned)] - elif system_filter: - core_trees = _extras_for_systems(core_trees, system_filter) - # Counted apart: the trees a core reads are not on the platform's - # list. MyMinUI declares nothing and its line read "6944 baseline". - total_files += _pack_data_directories( - zf, pack_systems, data_registry, platform_name, base_dest, - flatten, case_insensitive, seen_destinations, seen_lower, - seen_parents, required_only=required_only, - ) - tree_count = _pack_data_directories( - zf, {}, data_registry, platform_name, base_dest, - flatten, case_insensitive, seen_destinations, seen_lower, - seen_parents, required_only=required_only, core_trees=core_trees, - ) - total_files += tree_count - - # README.txt for users: personalized step-by-step per platform - num_systems = len(pack_systems) - _registry_path = Path(platforms_dir) / "_registry.yml" - _pack_registry: dict = {} - if _registry_path.exists(): - with open(_registry_path) as _rf: - _pack_registry = (yaml_load(_rf) or {}).get("platforms", {}) - keeps_case = case_insensitive and any( - d.get("os") not in CASE_FOLDING_OS - for d in (_pack_registry.get(platform_name, {}).get("install", {}) or {}) - .get("detect", []) - ) - if keeps_case and source != "platform" and precomputed_extras is None: - # The spellings a folded platform counts as declared never reach - # the extras: collected as the filesystem that keeps case sees - # them, as the manifest's case_variants are (Mesen-CE's bios.col - # beside the declared BIOS.col). - unfolded = _collect_emulator_extras( - {**config, "case_insensitive_fs": False}, emulators_dir, db, set(), - base_dest, emu_profiles, target_cores=target_cores, - include_all=(source == "truth"), - ) - if system_filter: - unfolded = _extras_for_systems(unfolded, system_filter) - prefix = _detect_extras_prefix(config, base_dest) - unfolded_spares = core_file_spares( - unfolded, - lambda fe: bool(region_drops) - and extra_region_key(fe, declared_root(config)) in region_drops, - ) if required_only else set() - for fe in unfolded: - if required_only and (fe.get("required") is False or id(fe) in unfolded_spares): - continue - if region_drops and extra_region_key(fe, declared_root(config)) in region_drops: - continue - full = extra_full_destination(fe, prefix) - if ( - full and full not in seen_destinations and full.lower() in seen_lower - and extra_resolver.resolve( - fe, sanitize_pack_path(fe.get("destination", fe["name"])) - )[0] - ): - case_twins.append(_flat(full, base_dest, flatten)) - readme_text = _build_readme( - platform_name, platform_display, base_dest, total_files, num_systems, - source=source, - contributors=_pack_registry.get(platform_name, {}).get("contributed_by", []), - regions=regions, - fallback_systems=region_fallbacks, - one_per_slot=one_per_slot, - undecidable_slots=slot_undecidable, - narrowings=narrowings, - system_filter=system_filter, - detect=( - _pack_registry.get(platform_name, {}).get("install", {}) or {} - ).get("detect", []), - standalone_copies=( - _pack_registry.get(platform_name, {}).get("install", {}) or {} - ).get("standalone_copies", []), - launch_folders=len(config.get("core_system_dirs") or {}), - save_folder=(save_layout(config).in_pack if save_count else ""), - save_where=( - save_place( - str(_pack_registry.get(platform_name, {}).get("extract_to") or ""), - save_layout(config).folder, base_dest, - ) if save_count else "" - ), - save_by_system=bool(save_count and save_layout(config).by_system), - case_twins=( - _case_twin_pairs(case_twins, seen_destinations, base_dest, flatten) - if keeps_case else [] - ), - ) - _write_generated_member(zf, "README.txt", readme_text) - for marker in pack_markers(platform_name, target_name, platforms_dir): - _write_generated_member( - zf, _flat(f"{base_dest}/{marker}" if base_dest else marker, base_dest, flatten), "" - ) - - _report_pack_counts( - zip_path, file_status, total_files, core_count, source, - verification_mode, tree_count=tree_count, - ) - if cut is not None: - _report_left_out( - cut, - systems=set(pack_systems) if source != "truth" else set(), - cores=source != "platform" and precomputed_extras is None, - ) - - - for key, reason in sorted(file_reasons.items()): - status = file_status.get(key, "") - if status == "untested": - label = "UNTESTED" - elif status == "excluded": - label = "EXCLUDED" - elif status == "missing": - label = "MISSING" - else: - label = "DISCREPANCY" - print(f" {label}: {key}: {reason}") - for name in missing_files: - print(f" MISSING: {name}") - for note in sorted(core_discrepancies): - print(f" DISCREPANCY: {note}") - return zip_path + build = _PlatformPackBuild(plan, zf, resolve, verdict, judge, resolve_extra) + build.run() + build.report() + return plan.zip_path def _extract_zip_to_archive( @@ -1712,6 +1978,385 @@ def _bounded_pack_name(display_names: list[str], keys: list[str], suffix: str) - return f"{display_names[0]}_and_{len(display_names) - 1}_more_{digest}{suffix}" +class EmulatorPackOptions(TypedDict, total=False): + """The keyword options of generate_emulator_pack.""" + + zip_contents: dict | None + offline: bool | None + platforms_dir: str | None + pack_name: str | None + system_ids: list[str] | None + final_dir: str | None + record: PackRecord | None + + +@dataclass +class _EmulatorPackBuild: + """One emulator pack being written, and what each destination came to.""" + + db: dict + bios_dir: str + standalone: bool + required_only: bool + zip_contents: dict + data_registry: dict + validation_index: dict + region_drops: set[str] + all_profiles: dict + zf: zipfile.ZipFile | PackRecord + offline: bool | None = None + system_ids: list[str] | None = None + record: PackRecord | None = None + total_files: int = 0 + data_files: int = 0 + missing_files: list[str] = field(default_factory=list) + rejected_files: list[str] = field(default_factory=list) + discrepancies: list[str] = field(default_factory=list) + unsourceable_files: list[str] = field(default_factory=list) + instruction_files: list[str] = field(default_factory=list) + placed: _Placed = field(default_factory=lambda: _Placed(True)) + seen_hashes: set[str] = field(default_factory=set) # SHA1 dedup for same file, different path + # Loose file packed at each folded destination, to tell a profile asking + # for the same file there from one asking for other bytes. + packed_at: dict[str, str] = field(default_factory=dict) + # A destination is missing once, however many profiles ask for it: + # verify --emulator counts by destination. + missing_at: set[str] = field(default_factory=set) + data_dir_notices: list[str] = field(default_factory=list) + + def report_missing(self, name: str, destination: str) -> None: + if destination not in self.missing_at: + self.missing_at.add(destination) + self.missing_files.append(name) + + def _add(self, local_path: str, dest: str, fe: dict | None) -> None: + """Write a loose file or an archive at dest and count it.""" + if fe is not None and local_path.endswith(".zip"): + _add_zip_to_pack(local_path, dest, self.zf, fe) + else: + _add_pack_member(self.zf, local_path, dest) + self.placed.take(dest) + self.total_files += 1 + if self.record is not None: + self.record.files.append(dest) + + def pack_profile(self, emu_name: str, profile: dict) -> None: + pack_structure = profile.get("pack_structure") + files = expand_agnostic_entries( + expand_directory_entries( + deliverable(profile.get("files", []), self.standalone), + self.db, + self.standalone, + ), + emu_name, profile, self.db, self.standalone, self.all_profiles, + ) + if self.system_ids: + files = [ + fe for fe in files if entry_serves_systems(fe, profile, self.system_ids) + ] + if not files and not profile.get("data_directories"): + print(f" No files needed for {profile.get('emulator', emu_name)}") + + # What each destination came to, settled by set once the + # profile is read: a member another member fills is not missing, + # and a set nobody holds is reported once (alternatives.py). An + # archive unit is a member like a loose file. + outcomes: list[_Outcome] = [] + spares = emulator_required_spares( + emu_name, profile, files, self.standalone, self.region_drops, self.db, + self.validation_index, self.zip_contents, data_registry=self.data_registry, + ) if self.required_only else set() + + # Pack archives as units + archive_prefix = profile.get("archive_prefix", "") + for archive_name in sorted({fe["archive"] for fe in files if fe.get("archive")}): + archive_dest = _archive_destination( + archive_name, archive_prefix, pack_structure, self.standalone + ) + own_entry = archive_entry(archive_name, files) + if self.required_only and archive_is_optional(own_entry, archive_name, files): + continue + if archive_dest in spares: + continue + outcomes.append(self._pack_archive(own_entry, archive_name, archive_dest)) + + for dest, alternatives in self._slots(files, spares).items(): + outcome = self._pack_slot(emu_name, files, dest, alternatives) + if outcome is not None: + outcomes.append(outcome) + + excused, _standing = settle_sets([ + (slot_set_keys(emu_name, entries), entries[0], fills) + for entries, fills, _report in outcomes + ]) + for position, (_entries, _fills, report) in enumerate(outcomes): + if report is not None and position not in excused: + report() + self._pack_trees(profile, pack_structure) + + def _pack_archive(self, own_entry: dict, archive_name: str, archive_dest: str) -> _Outcome: + local_path, status = resolve_file( + own_entry, + self.db, + self.bios_dir, + self.zip_contents, + data_dir_registry=self.data_registry, + offline=self.offline, + ) + taken = archive_dest.lower() in self.placed.lower + if taken or self.placed.conflicts(archive_dest): + # Another profile's file holds this path. The same archive + # asked for twice is served. Anything else leaves this + # profile without its archive, which used to pass in + # silence: a neocdz.zip lacking 000-lo.lo shipped as the + # one the second core needs. + if not (taken and local_path and _same_bytes( + local_path, self.packed_at.get(archive_dest.lower()) + )): + return ([own_entry], False, functools.partial( + self.report_missing, archive_name, archive_dest + )) + return ([own_entry], True, None) + if local_path and status not in ("not_found",): + self._add(local_path, archive_dest, own_entry) + self.packed_at[archive_dest.lower()] = local_path + return ([own_entry], True, None) + return ([own_entry], False, functools.partial( + self.report_missing, archive_name, archive_dest + )) + + def _slots(self, files: list[dict], spares: set) -> dict[str, list[dict]]: + # Pack individual files (skip archive members). The entries of one + # profile on one destination are the releases the code accepts + # there (raze's seven SW.GRP): the first one the core accepts + # fills the slot, as verify --emulator credits it, and the slot + # has one outcome however many alternatives it lists. + slots: dict[str, list[dict]] = {} + for fe in files: + if self.required_only and fe.get("required") is False: + continue + if fe.get("archive"): + continue + dest = _resolve_destination(fe, self.standalone) + if not dest or dest in spares: + continue + if self.region_drops and dest in self.region_drops: + continue + slots.setdefault(dest, []).append(fe) + return slots + + def _write_instructions(self, provided: dict, dest: str) -> None: + # Instructions, not a file: written beside the slot the + # user fills, and counted apart from what the pack holds. + self.placed.take(dest) + instr = provided.get("instructions", "Please provide this file manually.") + folder = dest.rsplit("/", 1)[0] + "/" if "/" in dest else "" + _write_generated_member( + self.zf, + f"{folder}INSTRUCTIONS_{provided['name']}.txt", + f"File needed: {provided['name']}\n\n{instr}\n", + ) + # verify --emulator counts an unsourceable one as such. + (self.unsourceable_files if provided.get("unsourceable") else self.instruction_files).append( + provided["name"] + ) + + def _choose( + self, emu_name: str, files: list[dict], alternatives: list[dict], + ) -> tuple[tuple[dict, str, str | None] | None, str | None, str | None]: + """The alternative the core accepts, with the refusal of the first + one it turned away and that copy.""" + fallback: tuple[dict, str, str | None] | None = None + refusal: str | None = None + refused_path: str | None = None + for fe in alternatives: + local_path, status = resolve_file( + {**with_archive_members(fe, files), "source_profile": emu_name}, + self.db, + self.bios_dir, + self.zip_contents, + dest_hint=fe.get("path", ""), + data_dir_registry=self.data_registry, + offline=self.offline, + ) + if status == "external": + return (fe, "", "external"), refusal, refused_path + if status in ("not_found", "user_provided") or not local_path: + continue + # The file verify --emulator credits is the one shipped: + # a dump the core's own check rejects gives way to a held + # one it accepts (azahar's otp.bin, dolphin's dsp_rom.bin). + better, disagreement = validated_choice( + fe, local_path, self.db, self.validation_index, self.bios_dir, None, + emulators={emu_name}, + ) + if disagreement: + reason = disagreement.split(" says ", 1)[-1] + refusal = refusal or f"{fe['name']}: {reason}" + refused_path = refused_path or local_path + continue + if better != local_path: + return (fe, better, None), refusal, refused_path + if status == "hash_mismatch": + # Not a check the core runs, so the file still serves, + # but it is not the dump the profile documents. + fallback = fallback or (fe, local_path, "mismatch") + continue + return (fe, local_path, None), refusal, refused_path + return fallback, refusal, refused_path + + def _pack_slot( + self, emu_name: str, files: list[dict], dest: str, alternatives: list[dict], + ) -> _Outcome | None: + # Name order, first file wins, case folded: verify replays + # the rule and reports a later profile's file as missing. + block = self.placed.block(dest) + + provided = next( + (fe for fe in alternatives if fe.get("storage") == "user_provided"), + None, + ) + if provided is not None: + if not block: + self._write_instructions(provided, dest) + return None + + chosen, refusal, refused_path = self._choose(emu_name, files, alternatives) + if block: + held = ( + chosen[1] if chosen is not None and chosen[2] != "external" + else None if chosen is not None else refused_path + ) + if chosen is not None or held is not None: + if block == "taken" and held and _same_bytes( + held, self.packed_at.get(dest.lower()) + ): + # The file already at this path, asked for twice. + return (alternatives, True, None) + return (alternatives, False, functools.partial( + self.report_missing, alternatives[0]["name"], dest + )) + + if chosen is None: + return self._unfilled(alternatives, dest, refusal) + fe, local_path, how = chosen + if how == "external": + return self._pack_external(fe, dest, alternatives) + if how == "mismatch": + self.discrepancies.append( + f"{fe['name']}: not the dump the {emu_name} profile declares" + ) + + # SHA1 dedup: skip if same physical file AND same destination + # (but allow same file to be packed under different destinations, + # e.g., IPL.bin in GC/USA/ and GC/EUR/ from same source) + dedup_key_hash = f"{os.path.realpath(local_path)}:{dest}" + if dedup_key_hash not in self.seen_hashes: + self.seen_hashes.add(dedup_key_hash) + self._add(local_path, dest, fe) + self.packed_at[dest.lower()] = local_path + return (alternatives, True, None) + + def _unfilled(self, alternatives: list[dict], dest: str, refusal: str | None) -> _Outcome: + if refusal: + self.rejected_files.append(refusal) + return (alternatives, False, None) + if all(fe.get("unsourceable") for fe in alternatives): + return (alternatives, False, functools.partial( + self.unsourceable_files.append, alternatives[0]["name"] + )) + return (alternatives, False, functools.partial( + self.report_missing, alternatives[0]["name"], dest + )) + + def _pack_external(self, fe: dict, dest: str, alternatives: list[dict]) -> _Outcome: + file_ext = os.path.splitext(fe["name"])[1] or "" + with tempfile.NamedTemporaryFile( + delete=False, suffix=file_ext, dir=packresolve.scratch_dir() + ) as tmp: + tmp_path = tmp.name + try: + # Built in memory, nothing is fetched: verify --emulator + # cannot fetch either and reports the file missing. + if self.record is None and download_external(fe, tmp_path): + self._add(tmp_path, dest, None) + return (alternatives, True, None) + return (alternatives, False, functools.partial( + self.missing_files.append, fe["name"] + )) + finally: + if os.path.exists(tmp_path): + os.unlink(tmp_path) + + def _pack_trees(self, profile: dict, pack_structure: dict | None) -> None: + # The trees after the files, as in a platform pack: a dump the + # profile declares wins over the copy a tree bundles at the same + # path (Dolphin's real dsp_rom.bin over Sys/GC's free one). + # A tree only one build reads follows that build, like a file. + for dd in deliverable( + profile.get("data_directories", []), self.standalone + ): + if self.required_only and dd.get("required") is False: + continue + ref_key = dd.get("ref", "") + if not ref_key or not self.data_registry or ref_key not in self.data_registry: + if ref_key: + self.data_dir_notices.append(ref_key) + continue + local_cache = self.data_registry[ref_key].get("local_cache", "") + if not local_cache or not os.path.isdir(local_cache): + self.data_dir_notices.append(ref_key) + continue + dd_dest = dd.get("destination", "") + if pack_structure: + mode_key = "standalone" if self.standalone else "libretro" + prefix = pack_structure.get(mode_key, "") + if prefix: + dd_dest = f"{prefix}/{dd_dest}" if dd_dest else prefix + self.data_files += _pack_data_tree( + self.zf, local_cache, dd_dest, self.placed.destinations, + self.placed.parents, self.placed.lower, self.packed_at, + ) + + def nothing_needed(self) -> bool: + return self.total_files + self.data_files == 0 and not ( + self.missing_files or self.rejected_files or self.instruction_files + or self.unsourceable_files + ) + + def report(self, shown: str) -> None: + # Report. The members of data trees are counted apart: verify --emulator + # counts the profile's files, and folding 3500 tree members into the + # same number made the two impossible to compare. + parts = [f"{self.total_files} files packed"] + if self.data_files: + parts.append(f"{self.data_files} from data directories") + if self.missing_files: + parts.append(f"{len(self.missing_files)} missing") + if self.rejected_files: + parts.append(f"{len(self.rejected_files)} refused by the core") + if self.unsourceable_files: + parts.append(f"{len(self.unsourceable_files)} unsourceable") + if self.instruction_files: + parts.append(f"{len(self.instruction_files)} to provide") + print(f" {shown}: {', '.join(parts)}") + for name in self.missing_files: + print(f" MISSING: {name}") + for name in self.unsourceable_files: + print(f" UNSOURCEABLE: {name}") + for name in self.instruction_files: + print(f" TO PROVIDE: {name} (instructions in the pack)") + for line in self.rejected_files: + print(f" REFUSED: {line}") + for line in self.discrepancies: + print(f" DISCREPANCY: {line}") + for ref in sorted(set(self.data_dir_notices)): + print( + f" Note: data directory '{ref}' required but not included (use refresh_data_dirs.py)" + ) + _MISSING_DATA_DIRS.update(self.data_dir_notices) + + def generate_emulator_pack( profile_names: list[str], emulators_dir: str, @@ -1719,15 +2364,10 @@ def generate_emulator_pack( bios_dir: str, output_dir: str, standalone: bool = False, - zip_contents: dict | None = None, + *, required_only: bool = False, regions: list[str] | None = None, - offline: bool | None = None, - platforms_dir: str | None = None, - pack_name: str | None = None, - system_ids: list[str] | None = None, - final_dir: str | None = None, - record: PackRecord | None = None, + **options: Unpack[EmulatorPackOptions], ) -> str | None: """Generate a ZIP pack for specific emulator profiles. @@ -1742,7 +2382,9 @@ def generate_emulator_pack( `record` builds the pack in memory instead (PackRecord): nothing is written or read, and the paths are kept on it. """ + _reject_unknown("generate_emulator_pack", options, EmulatorPackOptions.__optional_keys__) all_profiles = load_emulator_profiles(emulators_dir, skip_aliases=False) + zip_contents = options.get("zip_contents") if zip_contents is None: zip_contents = build_zip_contents_index(db) @@ -1752,6 +2394,7 @@ def generate_emulator_pack( print(f"Error: {exc}", file=sys.stderr) return None + record = options.get("record") # ZIP naming display_names = [_name_part(p.get("emulator", n)) for n, p in selected] validation_index = _build_validation_index(dict(selected)) @@ -1761,7 +2404,7 @@ def generate_emulator_pack( "full", regions, None, False, required_only, standalone ) ) - zip_name = pack_name or _bounded_pack_name( + zip_name = options.get("pack_name") or _bounded_pack_name( display_names, [n for n, _p in selected], f"{narrow_tags}_BIOS_Pack.zip" ) zip_path = os.path.join(output_dir, zip_name) @@ -1769,7 +2412,7 @@ def generate_emulator_pack( os.makedirs(output_dir, exist_ok=True) data_registry = load_data_dir_registry( - platforms_dir or os.path.join(os.path.dirname(__file__), "..", "platforms") + options.get("platforms_dir") or os.path.join(os.path.dirname(__file__), "..", "platforms") ) region_drops: set[str] = ( emulator_region_drops( @@ -1779,345 +2422,20 @@ def generate_emulator_pack( if regions else set() ) - - total_files = 0 - data_files = 0 - missing_files = [] - rejected_files: list[str] = [] - discrepancies: list[str] = [] - unsourceable_files: list[str] = [] - instruction_files: list[str] = [] - seen_destinations: set[str] = set() - seen_lower: set[str] = set() - seen_parents: set[str] = ( - set() - ) # parent dirs of added files (path conflict detection) - seen_hashes: set[str] = set() # SHA1 dedup for same file, different path - # Loose file packed at each folded destination, to tell a profile asking - # for the same file there from one asking for other bytes. - packed_at: dict[str, str] = {} - # A destination is missing once, however many profiles ask for it: - # verify --emulator counts by destination. - missing_at: set[str] = set() - - def report_missing(name: str, destination: str) -> None: - if destination not in missing_at: - missing_at.add(destination) - missing_files.append(name) - - data_dir_notices: list[str] = [] - with ( contextlib.nullcontext(record) if record is not None else _writing_pack(zip_path) ) as zf: + build = _EmulatorPackBuild( + db, bios_dir, standalone, required_only, zip_contents, data_registry, + validation_index, region_drops, all_profiles, zf, + offline=options.get("offline"), system_ids=options.get("system_ids"), record=record, + ) for emu_name, profile in sorted(selected): - pack_structure = profile.get("pack_structure") - files = expand_agnostic_entries( - expand_directory_entries( - deliverable(profile.get("files", []), standalone), - db, - standalone, - ), - emu_name, profile, db, standalone, all_profiles, - ) - if system_ids: - files = [ - fe for fe in files if entry_serves_systems(fe, profile, system_ids) - ] - if not files and not profile.get("data_directories"): - print(f" No files needed for {profile.get('emulator', emu_name)}") - - archives: set[str] = set() - for fe in files: - archive = fe.get("archive") - if archive: - archives.add(archive) - - # What each destination came to, settled by set once the - # profile is read: a member another member fills is not missing, - # and a set nobody holds is reported once (alternatives.py). An - # archive unit is a member like a loose file. - outcomes: list[tuple[list[dict], bool, Callable[[], None] | None]] = [] - spares = emulator_required_spares( - emu_name, profile, files, standalone, region_drops, db, - validation_index, zip_contents, data_registry, - ) if required_only else set() - - # Pack archives as units - archive_prefix = profile.get("archive_prefix", "") - for archive_name in sorted(archives): - archive_dest = _archive_destination( - archive_name, archive_prefix, pack_structure, standalone - ) - - own_entry = archive_entry(archive_name, files) - if required_only and archive_is_optional(own_entry, archive_name, files): - continue - if archive_dest in spares: - continue - local_path, status = resolve_file( - own_entry, - db, - bios_dir, - zip_contents, - data_dir_registry=data_registry, - offline=offline, - ) - taken = archive_dest.lower() in seen_lower - if taken or _has_path_conflict(archive_dest, seen_destinations, seen_parents): - # Another profile's file holds this path. The same archive - # asked for twice is served. Anything else leaves this - # profile without its archive, which used to pass in - # silence: a neocdz.zip lacking 000-lo.lo shipped as the - # one the second core needs. - if not (taken and local_path and _same_bytes( - local_path, packed_at.get(archive_dest.lower()) - )): - outcomes.append(([own_entry], False, functools.partial( - report_missing, archive_name, archive_dest - ))) - else: - outcomes.append(([own_entry], True, None)) - continue - if local_path and status not in ("not_found",): - if local_path.endswith(".zip"): - _add_zip_to_pack(local_path, archive_dest, zf, own_entry) - else: - _add_pack_member(zf, local_path, archive_dest) - seen_destinations.add(archive_dest) - _register_path(archive_dest, seen_destinations, seen_parents) - seen_lower.add(archive_dest.lower()) - packed_at[archive_dest.lower()] = local_path - total_files += 1 - if record is not None: - record.files.append(archive_dest) - outcomes.append(([own_entry], True, None)) - else: - outcomes.append(([own_entry], False, functools.partial( - report_missing, archive_name, archive_dest - ))) - - # Pack individual files (skip archive members). The entries of one - # profile on one destination are the releases the code accepts - # there (raze's seven SW.GRP): the first one the core accepts - # fills the slot, as verify --emulator credits it, and the slot - # has one outcome however many alternatives it lists. - slots: dict[str, list[dict]] = {} - for fe in files: - if required_only and fe.get("required") is False: - continue - if fe.get("archive"): - continue - dest = _resolve_destination(fe, standalone) - if not dest or dest in spares: - continue - if region_drops and dest in region_drops: - continue - slots.setdefault(dest, []).append(fe) - - for dest, alternatives in slots.items(): - # Name order, first file wins, case folded: verify replays - # the rule and reports a later profile's file as missing. - block = placement_block( - dest, seen_destinations, seen_lower, seen_parents, True - ) - - provided = next( - (fe for fe in alternatives if fe.get("storage") == "user_provided"), - None, - ) - if provided is not None and block: - continue - if provided is not None: - # Instructions, not a file: written beside the slot the - # user fills, and counted apart from what the pack holds. - seen_destinations.add(dest) - _register_path(dest, seen_destinations, seen_parents) - seen_lower.add(dest.lower()) - instr = provided.get("instructions", "Please provide this file manually.") - folder = dest.rsplit("/", 1)[0] + "/" if "/" in dest else "" - _write_generated_member( - zf, - f"{folder}INSTRUCTIONS_{provided['name']}.txt", - f"File needed: {provided['name']}\n\n{instr}\n", - ) - # verify --emulator counts an unsourceable one as such. - (unsourceable_files if provided.get("unsourceable") else instruction_files).append( - provided["name"] - ) - continue - - chosen: tuple[dict, str, str | None] | None = None - fallback: tuple[dict, str, str | None] | None = None - refusal: str | None = None - refused_path: str | None = None - for fe in alternatives: - local_path, status = resolve_file( - {**with_archive_members(fe, files), "source_profile": emu_name}, - db, - bios_dir, - zip_contents, - dest_hint=fe.get("path", ""), - data_dir_registry=data_registry, - offline=offline, - ) - if status == "external": - chosen = (fe, "", "external") - break - if status in ("not_found", "user_provided") or not local_path: - continue - # The file verify --emulator credits is the one shipped: - # a dump the core's own check rejects gives way to a held - # one it accepts (azahar's otp.bin, dolphin's dsp_rom.bin). - better, disagreement = validated_choice( - fe, local_path, db, validation_index, bios_dir, None, - emulators={emu_name}, - ) - if disagreement: - reason = disagreement.split(" says ", 1)[-1] - refusal = refusal or f"{fe['name']}: {reason}" - refused_path = refused_path or local_path - continue - if better != local_path: - chosen = (fe, better, None) - break - if status == "hash_mismatch": - # Not a check the core runs, so the file still serves, - # but it is not the dump the profile documents. - fallback = fallback or (fe, local_path, "mismatch") - continue - chosen = (fe, local_path, None) - break - chosen = chosen or fallback - - if block: - held = ( - chosen[1] if chosen is not None and chosen[2] != "external" - else None if chosen is not None else refused_path - ) - if chosen is not None or held is not None: - if block == "taken" and held and _same_bytes( - held, packed_at.get(dest.lower()) - ): - # The file already at this path, asked for twice. - outcomes.append((alternatives, True, None)) - continue - outcomes.append((alternatives, False, functools.partial( - report_missing, alternatives[0]["name"], dest - ))) - continue - - if chosen is None: - if refusal: - rejected_files.append(refusal) - outcomes.append((alternatives, False, None)) - elif all(fe.get("unsourceable") for fe in alternatives): - outcomes.append((alternatives, False, functools.partial( - unsourceable_files.append, alternatives[0]["name"] - ))) - else: - outcomes.append((alternatives, False, functools.partial( - report_missing, alternatives[0]["name"], dest - ))) - continue - - fe, local_path, how = chosen - if how == "external": - file_ext = os.path.splitext(fe["name"])[1] or "" - with tempfile.NamedTemporaryFile( - delete=False, suffix=file_ext, dir=packresolve.scratch_dir() - ) as tmp: - tmp_path = tmp.name - try: - # Built in memory, nothing is fetched: verify --emulator - # cannot fetch either and reports the file missing. - if record is None and download_external(fe, tmp_path): - _add_pack_member(zf, tmp_path, dest) - seen_destinations.add(dest) - _register_path(dest, seen_destinations, seen_parents) - seen_lower.add(dest.lower()) - total_files += 1 - if record is not None: - record.files.append(dest) - outcomes.append((alternatives, True, None)) - else: - outcomes.append((alternatives, False, functools.partial( - missing_files.append, fe["name"] - ))) - finally: - if os.path.exists(tmp_path): - os.unlink(tmp_path) - continue - if how == "mismatch": - discrepancies.append( - f"{fe['name']}: not the dump the {emu_name} profile declares" - ) - - # SHA1 dedup: skip if same physical file AND same destination - # (but allow same file to be packed under different destinations, - # e.g., IPL.bin in GC/USA/ and GC/EUR/ from same source) - dedup_key_hash = f"{os.path.realpath(local_path)}:{dest}" - outcomes.append((alternatives, True, None)) - if dedup_key_hash in seen_hashes: - continue - seen_hashes.add(dedup_key_hash) - - if local_path.endswith(".zip"): - _add_zip_to_pack(local_path, dest, zf, fe) - else: - _add_pack_member(zf, local_path, dest) - seen_destinations.add(dest) - _register_path(dest, seen_destinations, seen_parents) - seen_lower.add(dest.lower()) - packed_at[dest.lower()] = local_path - total_files += 1 - if record is not None: - record.files.append(dest) - - excused, _standing = settle_sets([ - (slot_set_keys(emu_name, entries), entries[0], fills) - for entries, fills, _report in outcomes - ]) - for position, (_entries, _fills, report) in enumerate(outcomes): - if report is not None and position not in excused: - report() - - # The trees after the files, as in a platform pack: a dump the - # profile declares wins over the copy a tree bundles at the same - # path (Dolphin's real dsp_rom.bin over Sys/GC's free one). - # A tree only one build reads follows that build, like a file. - for dd in deliverable( - profile.get("data_directories", []), standalone - ): - if required_only and dd.get("required") is False: - continue - ref_key = dd.get("ref", "") - if not ref_key or not data_registry or ref_key not in data_registry: - if ref_key: - data_dir_notices.append(ref_key) - continue - entry = data_registry[ref_key] - local_cache = entry.get("local_cache", "") - if not local_cache or not os.path.isdir(local_cache): - data_dir_notices.append(ref_key) - continue - dd_dest = dd.get("destination", "") - if pack_structure: - mode_key = "standalone" if standalone else "libretro" - prefix = pack_structure.get(mode_key, "") - if prefix: - dd_dest = f"{prefix}/{dd_dest}" if dd_dest else prefix - data_files += _pack_data_tree( - zf, local_cache, dd_dest, seen_destinations, seen_parents, seen_lower, - packed_at, - ) + build.pack_profile(emu_name, profile) # Remove empty ZIP (no files packed and no missing = nothing to ship) - nothing_needed = total_files + data_files == 0 and not ( - missing_files or rejected_files or instruction_files or unsourceable_files - ) - if record is None and nothing_needed: + if record is None and build.nothing_needed(): os.unlink(zip_path) # What verify --emulator reports as 0/0 OK: no file is needed, so # no pack is written and the run succeeds. Printed with the path, @@ -2125,46 +2443,18 @@ def generate_emulator_pack( print(f" {', '.join(display_names)}: no file needed, no pack written") return "" - # Report. The members of data trees are counted apart: verify --emulator - # counts the profile's files, and folding 3500 tree members into the - # same number made the two impossible to compare. - missing_count = len(missing_files) - ok_count = total_files - parts = [f"{ok_count} files packed"] - if data_files: - parts.append(f"{data_files} from data directories") - if missing_count: - parts.append(f"{missing_count} missing") - if rejected_files: - parts.append(f"{len(rejected_files)} refused by the core") - if unsourceable_files: - parts.append(f"{len(unsourceable_files)} unsourceable") - if instruction_files: - parts.append(f"{len(instruction_files)} to provide") - shown = os.path.join(final_dir, zip_name) if final_dir else zip_path - print(f" {shown}: {', '.join(parts)}") - for name in missing_files: - print(f" MISSING: {name}") - for name in unsourceable_files: - print(f" UNSOURCEABLE: {name}") - for name in instruction_files: - print(f" TO PROVIDE: {name} (instructions in the pack)") - for line in rejected_files: - print(f" REFUSED: {line}") - for line in discrepancies: - print(f" DISCREPANCY: {line}") - for ref in sorted(set(data_dir_notices)): - print( - f" Note: data directory '{ref}' required but not included (use refresh_data_dirs.py)" - ) - _MISSING_DATA_DIRS.update(data_dir_notices) + final_dir = options.get("final_dir") + build.report(os.path.join(final_dir, zip_name) if final_dir else zip_path) + return None if build.nothing_needed() else zip_path - return ( - zip_path - if total_files + data_files > 0 or missing_files or rejected_files or instruction_files - or unsourceable_files - else None - ) + +class SystemPackOptions(TypedDict, total=False): + """The keyword options of generate_system_pack.""" + + required_only: bool + regions: list[str] | None + offline: bool | None + platforms_dir: str | None def generate_system_pack( @@ -2175,15 +2465,17 @@ def generate_system_pack( output_dir: str, standalone: bool = False, zip_contents: dict | None = None, - required_only: bool = False, - regions: list[str] | None = None, - offline: bool | None = None, - platforms_dir: str | None = None, + **options: Unpack[SystemPackOptions], ) -> str | None: """Generate a ZIP pack for all emulators supporting given system IDs. Returns the path, "" when no file is needed, None on failure. """ + _reject_unknown("generate_system_pack", options, SystemPackOptions.__optional_keys__) + required_only = options.get("required_only", False) + regions = options.get("regions") + offline = options.get("offline") + platforms_dir = options.get("platforms_dir") profiles = load_emulator_profiles(emulators_dir) matching = profiles_for_systems(profiles, system_ids, standalone) @@ -2237,7 +2529,7 @@ def generate_system_pack( bios_dir, scratch, standalone, - zip_contents, + zip_contents=zip_contents, required_only=required_only, regions=regions, offline=offline, @@ -2281,6 +2573,40 @@ def _group_systems_by_manufacturer( return groups +class SplitOptions(PackNarrowing, total=False): + """The keyword options of generate_split_packs.""" + + records: dict[str, PackRecord] | None + + +def _split_extras(config: dict, db: dict, narrowing: PackNarrowing) -> list[dict]: + """The core extras of every part, collected once (expensive: scans 260+ + emulator profiles), then distributed per group by system overlap.""" + emu_profiles = narrowing["emu_profiles"] or {} + source = narrowing["source"] + target_cores = narrowing["target_cores"] + base_dest = config.get("base_destination", "") + # Each part cuts the platform's own entries from the full profiles. The + # extras are collected once here, already without the cores' game data. + cut = ( + without_game_data(config, emu_profiles, narrowing["data_registry"], target_cores) + if narrowing["no_game_data"] else None + ) + if source == "platform": + all_extras = [] + elif emu_profiles: + all_extras = _collect_emulator_extras( + cut.config if cut else config, narrowing["emulators_dir"], db, + set(), base_dest, cut.profiles if cut else emu_profiles, + target_cores=target_cores, include_all=(source == "truth"), + ) + else: + all_extras = [] + if cut is not None and source != "platform": + _report_left_out(cut, systems=set(), cores=True) + return all_extras + + def generate_split_packs( platform_name: str, platforms_dir: str, @@ -2288,19 +2614,7 @@ def generate_split_packs( bios_dir: str, output_dir: str, group_by: str = "system", - emulators_dir: str = "emulators", - zip_contents: dict | None = None, - data_registry: dict | None = None, - emu_profiles: dict | None = None, - target_cores: set[str] | None = None, - required_only: bool = False, - source: str = "full", - regions: list[str] | None = None, - target_name: str | None = None, - one_per_slot: bool = False, - offline: bool | None = None, - no_game_data: bool = False, - records: dict[str, PackRecord] | None = None, + **options: Unpack[SplitOptions], ) -> list[str]: """Generate split packs (one ZIP per system or manufacturer). @@ -2308,14 +2622,31 @@ def generate_split_packs( own placement (``within``). ``records`` builds the parts in memory, keyed by part name, with no archive written. """ + _reject_unknown("generate_split_packs", options, SplitOptions.__optional_keys__) config = load_platform_config(platform_name, platforms_dir) + records = options.get("records") + narrowing: PackNarrowing = { + "emulators_dir": options.get("emulators_dir", "emulators"), + "zip_contents": options.get("zip_contents"), + "data_registry": options.get("data_registry"), + "emu_profiles": options.get("emu_profiles"), + "target_cores": options.get("target_cores"), + "required_only": options.get("required_only", False), + "source": options.get("source", "full"), + "regions": options.get("regions"), + "target_name": options.get("target_name"), + "one_per_slot": options.get("one_per_slot", False), + "offline": options.get("offline"), + "no_game_data": options.get("no_game_data", False), + } # The split directory carries the same tags as the packs inside it, or two # differently narrowed runs write into one another. split_tags = "".join( tag for tag, _label in _narrowings( - source, regions, target_name, one_per_slot, required_only, - no_game_data=no_game_data, + narrowing["source"], narrowing["regions"], narrowing["target_name"], + narrowing["one_per_slot"], narrowing["required_only"], + no_game_data=narrowing["no_game_data"], ) ) # Two groupings write different files. Without the tag they accumulate in @@ -2333,16 +2664,10 @@ def generate_split_packs( if records is None: os.makedirs(split_dir, exist_ok=True) + emu_profiles = narrowing["emu_profiles"] if emu_profiles is None: - emu_profiles = load_emulator_profiles(emulators_dir) - narrowing = { - "emulators_dir": emulators_dir, "zip_contents": zip_contents, - "data_registry": data_registry, "emu_profiles": emu_profiles, - "target_cores": target_cores, "required_only": required_only, - "source": source, "regions": regions, "target_name": target_name, - "one_per_slot": one_per_slot, "offline": offline, - "no_game_data": no_game_data, - } + emu_profiles = load_emulator_profiles(narrowing["emulators_dir"]) + narrowing["emu_profiles"] = emu_profiles full = PackRecord() with contextlib.redirect_stdout(io.StringIO()): generate_pack( @@ -2350,52 +2675,32 @@ def generate_split_packs( record=full, **narrowing, ) - def part(pack_name: str, **scope: object) -> str | None: + def part(pack_name: str, scope: _PartScope) -> str | None: record = PackRecord() if records is not None else None zip_path = generate_pack( platform_name, platforms_dir, db, bios_dir, split_dir, pack_name=pack_name, record=record, within=full.sources, **scope, **narrowing, ) - if record is not None and zip_path: + if records is not None and record is not None and zip_path: records[pack_name] = record return zip_path # The systems a core of the target runs: a part per declared system sent # each one the target drops to a refusal naming it as unknown. - systems = platform_scope_systems(config, emu_profiles, target_cores) + systems = platform_scope_systems(config, emu_profiles, narrowing["target_cores"]) if group_by == "manufacturer": groups = _group_systems_by_manufacturer(systems, db, bios_dir) else: groups = {_system_display_name(sid): [sid] for sid in systems} - # Pre-compute core extras once (expensive: scans 260+ emulator profiles) - # then distribute per group based on emulator system overlap - base_dest = config.get("base_destination", "") - # Each part cuts the platform's own entries from the full profiles. The - # extras are collected once here, already without the cores' game data. - cut = ( - without_game_data(config, emu_profiles, data_registry, target_cores) - if no_game_data else None - ) - if source == "platform": - all_extras = [] - elif emu_profiles: - all_extras = _collect_emulator_extras( - cut.config if cut else config, emulators_dir, db, set(), base_dest, - cut.profiles if cut else emu_profiles, - target_cores=target_cores, include_all=(source == "truth"), - ) - else: - all_extras = [] - if cut is not None and source != "platform": - _report_left_out(cut, systems=set(), cores=True) + all_extras = _split_extras(config, db, narrowing) results = [] for group_name, group_system_ids in sorted(groups.items()): group_extras = _extras_for_systems(all_extras, group_system_ids) zip_path = part( f"{part_prefix}_{_name_part(group_name, '_')}_BIOS_Pack.zip", - system_filter=group_system_ids, precomputed_extras=group_extras, + {"system_filter": group_system_ids, "precomputed_extras": group_extras}, ) if zip_path: results.append(zip_path) @@ -2413,7 +2718,7 @@ def generate_split_packs( if undistributed: zip_path = part( f"{part_prefix}_Other_Cores_BIOS_Pack.zip", - precomputed_extras=undistributed, extras_only=True, + {"precomputed_extras": undistributed, "extras_only": True}, ) if zip_path: results.append(zip_path) @@ -2448,23 +2753,44 @@ def _entry_for_file(candidates: list[dict], held: dict) -> dict: return candidates[-1] -def generate_md5_pack( - hashes: list[tuple[str, str]], - db: dict, - bios_dir: str, - output_dir: str, - zip_contents: dict | None = None, - platform_name: str | None = None, - platforms_dir: str | None = None, - emulator_name: str | None = None, - emulators_dir: str | None = None, - standalone: bool = False, -) -> str | None: - """Build a pack from an explicit list of hashes with layout context.""" - files_db = db.get("files", {}) - if zip_contents is None: - zip_contents = {} +class Md5PackOptions(TypedDict, total=False): + """The keyword options of generate_md5_pack: the layout to follow.""" + platform_name: str | None + platforms_dir: str | None + emulator_name: str | None + emulators_dir: str | None + standalone: bool + + +@dataclass(frozen=True) +class _Md5Settings: + """Md5PackOptions with their defaults.""" + + platform_name: str | None = None + platforms_dir: str | None = None + emulator_name: str | None = None + emulators_dir: str | None = None + standalone: bool = False + + +@dataclass(frozen=True) +class _Md5Layout: + """Where a hash-selected pack puts each file: the declarations by + lowercased name, the platform's base destination, and the pack's name.""" + + index: dict[str, list[dict]] + base_dest: str + context_name: str + + +def _md5_layout(opts: _Md5Settings) -> _Md5Layout | None: + """The platform or emulator layout a hash list is packed in, or None + when the emulator named is refused.""" + platform_name = opts.platform_name + platforms_dir = opts.platforms_dir + emulator_name = opts.emulator_name + emulators_dir = opts.emulators_dir plat_file_index: dict[str, list[dict]] = {} base_dest = "" plat_display = "Custom" @@ -2472,7 +2798,7 @@ def generate_md5_pack( config = load_platform_config(platform_name, platforms_dir) base_dest = config.get("base_destination", "") plat_display = config.get("platform", platform_name) - for _sys_id, system in config.get("systems", {}).items(): + for system in config.get("systems", {}).values(): for fe in system.get("files", []): plat_file_index.setdefault(fe.get("name", "").lower(), []).append(fe) @@ -2484,7 +2810,7 @@ def generate_md5_pack( # core with no standalone build has no standalone layout either. try: (_name, profile), = select_emulator_profiles( - [emulator_name], profiles, standalone + [emulator_name], profiles, opts.standalone ) except ProfileSelectionError as exc: print(f"Error: {exc}", file=sys.stderr) @@ -2496,17 +2822,26 @@ def generate_md5_pack( plat_file_index.setdefault(alias.lower(), []).append(fe) context_name = plat_display if platform_name else (emu_display or "Custom") + return _Md5Layout(plat_file_index, base_dest, context_name) + + +def _md5_pack_name( + context_name: str, hashes: list[tuple[str, str]], opts: _Md5Settings, +) -> str: + """The file name of a hash-selected pack.""" # The selection mode is part of the name, as for --system: BizHawk is # both a platform and a profile display name, and the two layouts # (Firmware/ under the platform, the profile's paths under the core) # wrote one file, the second run replacing the first without a word. - mode_part = "_Emulator" if emulator_name and not platform_name else "" + mode_part = ( + "_Emulator" if opts.emulator_name and not opts.platform_name else "" + ) # --standalone changes the destination layout, so a run with it must not # take the name of a run without it. custom_tags = "".join( tag for tag, _label in _narrowings( - "full", None, None, False, False, standalone=standalone + "full", None, None, False, False, standalone=opts.standalone ) ) # The hash list is what chooses the content: two lists under one name @@ -2514,11 +2849,58 @@ def generate_md5_pack( selection = hashlib.sha1( "\n".join(sorted(f"{kind}:{value.lower()}" for kind, value in hashes)).encode() ).hexdigest()[:8] - zip_name = ( + return ( f"{_name_part(context_name, '_')}{mode_part}_Custom_{selection}{custom_tags}" "_BIOS_Pack.zip" ) - zip_path = os.path.join(output_dir, zip_name) + + +def _md5_destination(entry: dict, layout: _Md5Layout, opts: _Md5Settings) -> str: + """Where a held file goes in a hash-selected pack.""" + name = entry.get("name", "") + aliases = entry.get("aliases") or [] + paths = entry.get("paths") or [] + + dest = name + matched_fe = None + for lookup_name in [name] + aliases: + if lookup_name.lower() in layout.index: + matched_fe = _entry_for_file(layout.index[lookup_name.lower()], entry) + break + + if matched_fe: + if opts.emulator_name: + # Laid out as the emulator pack lays it out: its path and + # its standalone_path, which --standalone has to reach. + dest = _resolve_destination(matched_fe, opts.standalone) + else: + dest = matched_fe.get("destination", matched_fe.get("name", name)) + elif paths: + dest = paths[0] + + if layout.base_dest and not dest.startswith(layout.base_dest): + return f"{layout.base_dest}/{dest}" + return dest + + +def generate_md5_pack( + hashes: list[tuple[str, str]], + db: dict, + bios_dir: str, + output_dir: str, + zip_contents: dict | None = None, + **options: Unpack[Md5PackOptions], +) -> str | None: + """Build a pack from an explicit list of hashes with layout context.""" + files_db = db.get("files", {}) + if zip_contents is None: + zip_contents = {} + _reject_unknown("generate_md5_pack", options, Md5PackOptions.__optional_keys__) + opts = _Md5Settings(**options) + layout = _md5_layout(opts) + if layout is None: + return None + zip_path = os.path.join(output_dir, _md5_pack_name(layout.context_name, hashes, opts)) os.makedirs(output_dir, exist_ok=True) packed: list[tuple[str, str]] = [] @@ -2535,33 +2917,7 @@ def generate_md5_pack( entry = files_db[sha1] name = entry.get("name", "") - aliases = entry.get("aliases") or [] - paths = entry.get("paths") or [] - - dest = name - matched_fe = None - for lookup_name in [name] + aliases: - if lookup_name.lower() in plat_file_index: - matched_fe = _entry_for_file( - plat_file_index[lookup_name.lower()], entry - ) - break - - if matched_fe: - if emulator_name: - # Laid out as the emulator pack lays it out: its path and - # its standalone_path, which --standalone has to reach. - dest = _resolve_destination(matched_fe, standalone) - else: - dest = matched_fe.get("destination", matched_fe.get("name", name)) - elif paths: - dest = paths[0] - - if base_dest and not dest.startswith(base_dest): - full_dest = f"{base_dest}/{dest}" - else: - full_dest = dest - + full_dest = _md5_destination(entry, layout, opts) if full_dest in seen: continue seen.add(full_dest) @@ -2829,96 +3185,76 @@ def _refuse_for_verify_packs(args: argparse.Namespace, parser: argparse.Argument parser.error(f"{flag} is incompatible with --verify-packs") -def _validate_args(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None: - """Validate argument combinations and mutual exclusion rules.""" - _refuse_for_verify_packs(args, parser) +def _argument_conflicts(args: argparse.Namespace) -> list[tuple[bool, str]]: + """Each refused combination of arguments with its message, in the order + they are checked: the first one that applies is the error reported.""" has_platform = bool(args.platform) has_all = args.all has_emulator = bool(args.emulator) has_system = bool(args.system) - has_from_md5 = bool(args.from_md5 or getattr(args, "from_md5_file", None)) - - if args.from_md5 and getattr(args, "from_md5_file", None): - parser.error("--from-md5 and --from-md5-file are mutually exclusive") - if has_from_md5 and has_all: - parser.error("--from-md5 requires --platform or --emulator, not --all") - if has_from_md5 and has_system: - parser.error("--from-md5 and --system are mutually exclusive") - if has_from_md5 and args.split: - parser.error("--split and --from-md5 are mutually exclusive") - - # --platform/--all and --system can combine (system filters within platform) - # --emulator is exclusive with everything else - if has_emulator and (has_platform or has_all or has_system): - parser.error( - "--emulator is mutually exclusive with --platform, --all, and --system" - ) - if has_platform and has_all: - parser.error("--platform and --all are mutually exclusive") - if not (has_platform or has_all or has_emulator or has_system or has_from_md5): - parser.error("Specify --platform, --all, --emulator, --system, or --from-md5") - if args.standalone and not ( - has_emulator or (has_system and not has_platform and not has_all) - ): - parser.error( - "--standalone requires --emulator or --system (without --platform)" - ) - if args.split and not (has_platform or has_all): - parser.error("--split requires --platform or --all") - if args.split and has_emulator: - parser.error("--split is incompatible with --emulator") - if args.group_by is not None and not args.split: - parser.error("--group-by requires --split") - if args.target and not (has_platform or has_all): - parser.error("--target requires --platform or --all") - if args.target and has_emulator: - parser.error("--target is incompatible with --emulator") - if args.manifest and not (has_platform or has_all): - parser.error("--manifest requires --platform or --all") - if args.manifest and has_emulator: - parser.error("--manifest is incompatible with --emulator") - if args.manifest and args.split: - parser.error("--manifest is incompatible with --split") - if getattr(args, "region", None) and has_from_md5: - parser.error("--region and --from-md5 are mutually exclusive") + md5_file = getattr(args, "from_md5_file", None) + has_from_md5 = bool(args.from_md5 or md5_file) # A mode either applies a narrowing flag or refuses it. Swallowing one # answers about an artifact the caller did not ask for, and the name on # disk then asserts a narrowing the contents do not carry. # --source and --group-by default to None so a typed default value is # seen: compared with "full", `--emulator X --source full` went through. source_given = getattr(args, "source", None) is not None - if source_given and (has_emulator or (has_system and not has_platform)): - parser.error("--source requires --platform or --all") - if has_from_md5: - for flag, given in ( - ("--manifest", args.manifest), - ("--target", args.target), - ("--required-only", args.required_only), - ("--source", source_given), - ): - if given: - parser.error(f"{flag} is incompatible with --from-md5") - if has_system and (has_platform or has_all): - if args.manifest: - parser.error("--system is incompatible with --manifest") - if args.split: - parser.error("--system is incompatible with --split") - + with_platform = has_system and (has_platform or has_all) # Game data is categorized against a platform's list and the cores it # runs: a pack of one emulator or of a system's emulators has neither. - if getattr(args, "no_game_data", False) and not (has_platform or has_all): - parser.error("--no-game-data requires --platform or --all") - if getattr(args, "no_game_data", False) and has_from_md5: - parser.error("--no-game-data is incompatible with --from-md5") - if getattr(args, "one_per_slot", False): - if has_from_md5: - parser.error("--one-per-slot and --from-md5 are mutually exclusive") - if args.manifest: - parser.error("--one-per-slot is incompatible with --manifest") - if has_emulator: - parser.error("--one-per-slot is incompatible with --emulator") - if has_system: - parser.error("--one-per-slot is incompatible with --system") + no_game_data = getattr(args, "no_game_data", False) + one_per_slot = getattr(args, "one_per_slot", False) + return [ + (bool(args.from_md5 and md5_file), "--from-md5 and --from-md5-file are mutually exclusive"), + (has_from_md5 and has_all, "--from-md5 requires --platform or --emulator, not --all"), + (has_from_md5 and has_system, "--from-md5 and --system are mutually exclusive"), + (has_from_md5 and args.split, "--split and --from-md5 are mutually exclusive"), + # --platform/--all and --system can combine (system filters within platform) + # --emulator is exclusive with everything else + (has_emulator and (has_platform or has_all or has_system), + "--emulator is mutually exclusive with --platform, --all, and --system"), + (has_platform and has_all, "--platform and --all are mutually exclusive"), + (not (has_platform or has_all or has_emulator or has_system or has_from_md5), + "Specify --platform, --all, --emulator, --system, or --from-md5"), + (args.standalone and not ( + has_emulator or (has_system and not has_platform and not has_all) + ), "--standalone requires --emulator or --system (without --platform)"), + (args.split and not (has_platform or has_all), "--split requires --platform or --all"), + (args.split and has_emulator, "--split is incompatible with --emulator"), + (args.group_by is not None and not args.split, "--group-by requires --split"), + (args.target and not (has_platform or has_all), "--target requires --platform or --all"), + (args.target and has_emulator, "--target is incompatible with --emulator"), + (args.manifest and not (has_platform or has_all), + "--manifest requires --platform or --all"), + (args.manifest and has_emulator, "--manifest is incompatible with --emulator"), + (args.manifest and args.split, "--manifest is incompatible with --split"), + (getattr(args, "region", None) and has_from_md5, + "--region and --from-md5 are mutually exclusive"), + (source_given and (has_emulator or (has_system and not has_platform)), + "--source requires --platform or --all"), + (has_from_md5 and args.manifest, "--manifest is incompatible with --from-md5"), + (has_from_md5 and args.target, "--target is incompatible with --from-md5"), + (has_from_md5 and args.required_only, "--required-only is incompatible with --from-md5"), + (has_from_md5 and source_given, "--source is incompatible with --from-md5"), + (with_platform and args.manifest, "--system is incompatible with --manifest"), + (with_platform and args.split, "--system is incompatible with --split"), + (no_game_data and not (has_platform or has_all), + "--no-game-data requires --platform or --all"), + (no_game_data and has_from_md5, "--no-game-data is incompatible with --from-md5"), + (one_per_slot and has_from_md5, "--one-per-slot and --from-md5 are mutually exclusive"), + (one_per_slot and args.manifest, "--one-per-slot is incompatible with --manifest"), + (one_per_slot and has_emulator, "--one-per-slot is incompatible with --emulator"), + (one_per_slot and has_system, "--one-per-slot is incompatible with --system"), + ] + + +def _validate_args(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None: + """Validate argument combinations and mutual exclusion rules.""" + _refuse_for_verify_packs(args, parser) + for refused, message in _argument_conflicts(args): + if refused: + parser.error(message) def _write_manifest_if_changed(path: str, manifest: dict) -> None: @@ -3076,16 +3412,69 @@ def _baseline_shortfall(excluded: int, absent: int) -> list[str]: return parts -def _run_verify_packs(args: argparse.Namespace) -> None: - """Verify each pack in the output directory against its platform. +@dataclass(frozen=True) +class _PackCheck: + """What --verify-packs checks every pack against.""" - Delegates to verify_pack_against_platform so the command answers the same - question the pipeline asks: baseline files, core extras and structure. A - second implementation here answered a narrower one and reported OK while - the core extras went unchecked. - """ - verify_db = load_database(args.db) + db: dict + platforms_dir: str + emulators_dir: str + profiles: dict + regions: list[str] | None + data_registry: dict + no_game_data: bool + +def _verify_found_pack( + check: _PackCheck, platform_name: str, zip_path: str, target_cores: set[str] | None, +) -> bool: + """Check one pack of a platform and print its line; False when it fails.""" + pack_name = os.path.basename(zip_path) + variant = _pack_variant(pack_name) + if variant is None: + # One BIOS per slot: the slot choice is not replayed here. + print( + f" {platform_name} [{pack_name}]: hashes checked during the build; " + "one per slot by design, so not compared to the platform's full list" + ) + return True + source, required_only = variant + result = verify_pack_against_platform( + zip_path, + platform_name, + check.platforms_dir, + db=check.db, + emulators_dir=check.emulators_dir, + emu_profiles=check.profiles, + regions=check.regions, + data_registry=check.data_registry, + target_cores=target_cores, + source=source, + required_only=required_only, + no_game_data=check.no_game_data, + ) + (ok, _checked, _present, errors, bl_checked, bl_present, + core_checked, core_present, bl_excluded, bl_absent) = result + # These explain a baseline short of its total: a dump whose hash + # the frontend refuses is left out on purpose, and a file the + # collection does not hold cannot be packed (verify reports it). + shortfall = ", ".join(_baseline_shortfall(bl_excluded, bl_absent)) + excluded = f" ({shortfall})" if shortfall else "" + counts = ( + f"{bl_present}/{bl_checked} baseline{excluded}, " + f"{core_present}/{core_checked} cores" + ) + if ok: + print(f" {platform_name} [{pack_name}]: OK ({counts})") + return True + print(f" {platform_name} [{pack_name}]: FAIL ({counts}, {len(errors)} errors)") + for err in errors: + print(f" {err}") + return False + + +def _verify_packs_scope(args: argparse.Namespace) -> tuple[list[str], dict[str, set[str] | None]]: + """The platforms --verify-packs checks, and the cores of the target.""" if args.platform: platforms = [args.platform] elif args.all: @@ -3107,20 +3496,35 @@ def _run_verify_packs(args: argparse.Namespace) -> None: except (FileNotFoundError, ValueError) as e: print(f"ERROR: {e}", file=sys.stderr) sys.exit(1) + return platforms, target_cores_cache + + +def _run_verify_packs(args: argparse.Namespace) -> None: + """Verify each pack in the output directory against its platform. + + Delegates to verify_pack_against_platform so the command answers the same + question the pipeline asks: baseline files, core extras and structure. A + second implementation here answered a narrower one and reported OK while + the core extras went unchecked. + """ + verify_db = load_database(args.db) + platforms, target_cores_cache = _verify_packs_scope(args) all_ok = True - verify_regions = getattr(args, "regions", None) - verify_profiles = load_emulator_profiles(args.emulators_dir) - verify_data_registry = load_data_dir_registry(args.platforms_dir) + check = _PackCheck( + verify_db, args.platforms_dir, args.emulators_dir, + load_emulator_profiles(args.emulators_dir), getattr(args, "regions", None), + load_data_dir_registry(args.platforms_dir), getattr(args, "no_game_data", False), + ) # After an --all-variants build every variant is named, so none of the # six packs the run wrote passes unmentioned. expected = _expected_pack_names( platforms, args.platforms_dir, - verify_regions, + check.regions, args.target, ALL_VARIANTS if args.all_variants else (("full", False),), - no_game_data=getattr(args, "no_game_data", False), + no_game_data=check.no_game_data, ) on_disk = ( set(os.listdir(args.output_dir)) if os.path.isdir(args.output_dir) else set() @@ -3150,47 +3554,8 @@ def _run_verify_packs(args: argparse.Namespace) -> None: # A group pack and a pack built for the platform alone can both be # present, and each one is a pack of this platform. for pack_name in found: - variant = _pack_variant(pack_name) - if variant is None: - # One BIOS per slot: the slot choice is not replayed here. - print( - f" {platform_name} [{pack_name}]: hashes checked during the build; " - "one per slot by design, so not compared to the platform's full list" - ) - continue - source, required_only = variant zip_path = os.path.join(args.output_dir, pack_name) - result = verify_pack_against_platform( - zip_path, - platform_name, - args.platforms_dir, - db=verify_db, - emulators_dir=args.emulators_dir, - emu_profiles=verify_profiles, - regions=verify_regions, - data_registry=verify_data_registry, - target_cores=tc, - source=source, - required_only=required_only, - no_game_data=getattr(args, "no_game_data", False), - ) - (ok, _checked, _present, errors, bl_checked, bl_present, - core_checked, core_present, bl_excluded, bl_absent) = result - # These explain a baseline short of its total: a dump whose hash - # the frontend refuses is left out on purpose, and a file the - # collection does not hold cannot be packed (verify reports it). - shortfall = ", ".join(_baseline_shortfall(bl_excluded, bl_absent)) - excluded = f" ({shortfall})" if shortfall else "" - counts = ( - f"{bl_present}/{bl_checked} baseline{excluded}, " - f"{core_present}/{core_checked} cores" - ) - if ok: - print(f" {platform_name} [{pack_name}]: OK ({counts})") - else: - print(f" {platform_name} [{pack_name}]: FAIL ({counts}, {len(errors)} errors)") - for err in errors: - print(f" {err}") + if not _verify_found_pack(check, platform_name, zip_path, tc): all_ok = False failing.append(f"{platform_name} ({pack_name})") @@ -3335,26 +3700,13 @@ def _run_platform_packs( # renamed, it overwrote and carried away a # `--platform retroarch` pack already in the directory, # and os.rename refused an existing target on Windows. - group_name = None - if aliases: - narrow_tags = "".join( - tag - for tag, _label in _narrowings( - source, - getattr(args, "regions", None), - named_target, - args.one_per_slot, - required_only, - no_game_data=getattr(args, "no_game_data", False), - ) - ) - stem = _platform_pack_stem( - group_platforms, representative, args.platforms_dir - ) - group_name = ( - f"{stem}{narrow_tags}_BIOS_Pack" - f"{_system_tag(system_filter)}.zip" + group_name = ( + _group_pack_name( + args, group_platforms, representative, (source, required_only), + named_target, system_filter, ) + if aliases else None + ) zip_path = generate_pack( representative, args.platforms_dir, @@ -3406,6 +3758,55 @@ def _run_platform_packs( no_game_data=getattr(args, "no_game_data", False), ), target_list(board_list) if board_list.is_file() else None) + all_ok = _finalize_platform_packs(args, db, data_registry, built, systems_for, system_filter) + if failed: + # Verification only sees the ZIPs that exist, so a pack that was never + # written does not fail it. + print(f"ERROR: no pack for {', '.join(sorted(set(failed)))}", file=sys.stderr) + sys.exit(1) + if not all_ok: + sys.exit(1) + + +def _group_pack_name( + args: argparse.Namespace, + group_platforms: list[str], + representative: str, + variant: tuple[str, bool], + named_target: str | None, + system_filter: list[str] | None, +) -> str: + """The file name of a pack shared by a group of identical platforms.""" + source, required_only = variant + narrow_tags = "".join( + tag + for tag, _label in _narrowings( + source, + getattr(args, "regions", None), + named_target, + args.one_per_slot, + required_only, + no_game_data=getattr(args, "no_game_data", False), + ) + ) + stem = _platform_pack_stem( + group_platforms, representative, args.platforms_dir + ) + return ( + f"{stem}{narrow_tags}_BIOS_Pack" + f"{_system_tag(system_filter)}.zip" + ) + + +def _finalize_platform_packs( + args: argparse.Namespace, + db: dict, + data_registry: dict | None, + built: set[str], + systems_for: dict[str, dict], + system_filter: list[str] | None, +) -> bool: + """Verify the packs this run wrote and write their manifests.""" print("\nVerifying packs and generating manifests...") skip_conf = bool(system_filter or args.split) all_ok = verify_and_finalize_packs( @@ -3439,13 +3840,7 @@ def _run_platform_packs( regions=getattr(args, "regions", None), ) all_ok = all_ok and ok - if failed: - # Verification only sees the ZIPs that exist, so a pack that was never - # written does not fail it. - print(f"ERROR: no pack for {', '.join(sorted(set(failed)))}", file=sys.stderr) - sys.exit(1) - if not all_ok: - sys.exit(1) + return all_ok def _refuse_for_all_variants(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None: @@ -3608,7 +4003,8 @@ def main() -> None: sys.exit(1) -def _main(held: contextlib.ExitStack) -> None: +def _build_parser() -> argparse.ArgumentParser: + """The command line of generate_pack.py.""" parser = argparse.ArgumentParser(allow_abbrev=False, description="Generate platform BIOS ZIP packs") parser.add_argument( "--platform", "-p", type=platform_arg, @@ -3739,20 +4135,14 @@ def _main(held: contextlib.ExitStack) -> None: action="store_true", help="Extract and verify pack integrity (path + hash)", ) - args = parser.parse_args() - from verify import work_from_repo_root + return parser - work_from_repo_root( - args, parser, - ["db", "platforms_dir", "bios_dir", "output_dir", "emulators_dir", "from_md5_file"], - ) - packresolve.set_offline(bool(args.offline)) - if args.platform: - require_platform(args.platform, args.platforms_dir) - require_directory(args.bios_dir) - _refuse_unapplied_flags(args, parser) - _refuse_for_verify_packs(args, parser) +def _run_quick_mode( + args: argparse.Namespace, parser: argparse.ArgumentParser, held: contextlib.ExitStack, +) -> bool: + """Run a mode that builds nothing (a check of existing packs, the target + lists, a listing); True when one ran.""" # Quick-exit modes: --verify-packs alone = verify existing packs only # Combined with --all-variants, generation runs first then verify if args.verify_packs and not args.all_variants: @@ -3761,26 +4151,26 @@ def _main(held: contextlib.ExitStack) -> None: hold_data_dirs(held, load_data_dir_registry(args.platforms_dir)) with _pack_output_lock(args.output_dir, exclusive=False): _run_verify_packs(args) - return + return True if args.manifest_targets: _refuse_mixed_output(args.output_dir, "targets") with _pack_output_lock(args.output_dir): generate_target_manifests( os.path.join(args.platforms_dir, "targets"), args.output_dir ) - return + return True if args.list: print_platform_listing(args.platforms_dir) - return + return True if args.list_emulators: list_emulator_profiles(args.emulators_dir) - return + return True if args.list_systems: if args.platform: list_platform_system_ids(args.platform, args.platforms_dir) else: list_system_ids(args.emulators_dir) - return + return True if args.list_targets: if not args.platform: parser.error("--list-targets requires --platform") @@ -3789,71 +4179,61 @@ def _main(held: contextlib.ExitStack) -> None: targets = list_available_targets(args.platform, args.platforms_dir) if not targets: print(f"No targets configured for platform '{args.platform}'") - return + return True for t in targets: print(target_listing_line(t)) - return + return True + return False - _validate_args(args, parser) - _resolve_systems(args, parser) - # Unset until every refusal has run, so a typed default value is seen. - args.source = args.source or "full" - args.group_by = args.group_by or "system" - - # Hash lookup / pack mode - # A platform run may refresh the trees below, under the exclusive lock: - # it takes the shared ones after that. Every other mode reads them now. - if not (args.platform or args.all) or args.offline or args.no_refresh_data: - hold_data_dirs(held, load_data_dir_registry(args.platforms_dir)) - has_from_md5 = bool(args.from_md5 or getattr(args, "from_md5_file", None)) - if has_from_md5: - try: - hashes = ( - parse_hash_input(args.from_md5) - if args.from_md5 - else parse_hash_file(args.from_md5_file) - ) - except ValueError as exc: - print(f"ERROR: {exc}", file=sys.stderr) - sys.exit(1) - except OSError as exc: - print(f"ERROR: --from-md5-file {args.from_md5_file}: {exc.strerror}", file=sys.stderr) - sys.exit(1) - if not hashes: - print( - f"ERROR: no hash in the input. Accepted: {ACCEPTED_HASHES}", - file=sys.stderr, - ) - sys.exit(1) - db = load_database(args.db) - if not args.platform and not args.emulator: - # A lookup that found nothing is an answer a script must see. - if lookup_hashes( - hashes, db, args.bios_dir, args.emulators_dir, args.platforms_dir - ): - sys.exit(1) - return - zip_contents = build_zip_contents_index(db) - with _pack_output_lock(args.output_dir): - result = generate_md5_pack( - hashes=hashes, - db=db, - bios_dir=args.bios_dir, - output_dir=args.output_dir, - zip_contents=zip_contents, - platform_name=args.platform, - platforms_dir=args.platforms_dir, - emulator_name=args.emulator, - emulators_dir=args.emulators_dir, - standalone=getattr(args, "standalone", False), - ) - if not result: - sys.exit(1) - return +def _run_hash_mode(args: argparse.Namespace) -> None: + """--from-md5 and --from-md5-file: look the hashes up, or pack them.""" + try: + hashes = ( + parse_hash_input(args.from_md5) + if args.from_md5 + else parse_hash_file(args.from_md5_file) + ) + except ValueError as exc: + print(f"ERROR: {exc}", file=sys.stderr) + sys.exit(1) + except OSError as exc: + print(f"ERROR: --from-md5-file {args.from_md5_file}: {exc.strerror}", file=sys.stderr) + sys.exit(1) + if not hashes: + print( + f"ERROR: no hash in the input. Accepted: {ACCEPTED_HASHES}", + file=sys.stderr, + ) + sys.exit(1) db = load_database(args.db) + if not args.platform and not args.emulator: + # A lookup that found nothing is an answer a script must see. + if lookup_hashes( + hashes, db, args.bios_dir, args.emulators_dir, args.platforms_dir + ): + sys.exit(1) + return zip_contents = build_zip_contents_index(db) + with _pack_output_lock(args.output_dir): + result = generate_md5_pack( + hashes=hashes, + db=db, + bios_dir=args.bios_dir, + output_dir=args.output_dir, + zip_contents=zip_contents, + platform_name=args.platform, + platforms_dir=args.platforms_dir, + emulator_name=args.emulator, + emulators_dir=args.emulators_dir, + standalone=getattr(args, "standalone", False), + ) + if not result: + sys.exit(1) + +def _run_profile_mode(args: argparse.Namespace, db: dict, zip_contents: dict) -> None: + """--emulator, or --system without a platform: the emulators' own layout.""" # Emulator mode if args.emulator: names = [n.strip() for n in args.emulator.split(",") if n.strip()] @@ -3865,7 +4245,7 @@ def _main(held: contextlib.ExitStack) -> None: args.bios_dir, args.output_dir, args.standalone, - zip_contents, + zip_contents=zip_contents, required_only=args.required_only, regions=getattr(args, "regions", None), offline=args.offline, @@ -3876,44 +4256,69 @@ def _main(held: contextlib.ExitStack) -> None: return # System mode (standalone, without platform context) - if args.system and not args.platform and not args.all: - system_ids = [s.strip() for s in args.system.split(",") if s.strip()] - with _pack_output_lock(args.output_dir): - built = generate_system_pack( - system_ids, - args.emulators_dir, - db, - args.bios_dir, - args.output_dir, - args.standalone, - zip_contents, - required_only=args.required_only, - regions=getattr(args, "regions", None), - offline=args.offline, - platforms_dir=args.platforms_dir, - ) - if built is None: - sys.exit(1) - return + system_ids = [s.strip() for s in args.system.split(",") if s.strip()] + with _pack_output_lock(args.output_dir): + built = generate_system_pack( + system_ids, + args.emulators_dir, + db, + args.bios_dir, + args.output_dir, + args.standalone, + zip_contents=zip_contents, + required_only=args.required_only, + regions=getattr(args, "regions", None), + offline=args.offline, + platforms_dir=args.platforms_dir, + ) + if built is None: + sys.exit(1) - system_filter = getattr(args, "system_ids", None) - # Platform mode +def _requested_platforms(args: argparse.Namespace, parser: argparse.ArgumentParser) -> list[str]: + """The platforms a platform run builds for.""" if args.all and args.manifest: # Install manifests cover every registered platform: the installer # serves each one, with or without a release pack. - platforms = list_registered_platforms(args.platforms_dir) - elif args.all: + return list_registered_platforms(args.platforms_dir) + if args.all: # A release publishes the packs the registry marks release_pack and # nothing else: a pack built for another platform is one nobody uploads. platforms = list_release_pack_platforms(args.platforms_dir) if not platforms: parser.error("no registered platform has release_pack: true") - elif args.platform: - platforms = [args.platform] - else: + return platforms + if not args.platform: parser.error("Specify --platform or --all") - return + return [args.platform] + + +def _refresh_data_directories(args: argparse.Namespace, data_registry: dict) -> None: + """Bring the data directory caches up to date before a build reads them.""" + from refresh_data_dirs import load_registry, refresh_all + + registry = load_registry(os.path.join(args.platforms_dir, "_data_dirs.yml")) + results = refresh_all(registry, force=args.refresh_data) + updated = sum(1 for v in results.values() if v) + if updated: + print(f"Refreshed {updated} data director{'ies' if updated > 1 else 'y'}") + failed = sorted(k for k, v in results.items() if v is None) + if failed: + print(f"WARNING: data directory refresh failed: {', '.join(failed)}") + + +def _run_platform_mode( + args: argparse.Namespace, + parser: argparse.ArgumentParser, + held: contextlib.ExitStack, + db: dict, + zip_contents: dict, +) -> None: + """--platform or --all: packs or manifests in each platform's layout.""" + system_filter = getattr(args, "system_ids", None) + + # Platform mode + platforms = _requested_platforms(args, parser) # A mistyped target is refused before the output lock and the data # directory refresh: checked after them, `--target swich` waited on a @@ -3957,16 +4362,7 @@ def _main(held: contextlib.ExitStack) -> None: # during a pipeline sat silent for hours instead of being refused. with _pack_output_lock(args.output_dir): if data_registry and not args.offline and not args.no_refresh_data: - from refresh_data_dirs import load_registry, refresh_all - - registry = load_registry(os.path.join(args.platforms_dir, "_data_dirs.yml")) - results = refresh_all(registry, force=args.refresh_data) - updated = sum(1 for v in results.values() if v) - if updated: - print(f"Refreshed {updated} data director{'ies' if updated > 1 else 'y'}") - failed = sorted(k for k, v in results.items() if v is None) - if failed: - print(f"WARNING: data directory refresh failed: {', '.join(failed)}") + _refresh_data_directories(args, data_registry) if data_registry: hold_data_dirs(held, data_registry) @@ -3995,6 +4391,48 @@ def _main(held: contextlib.ExitStack) -> None: _run_verify_packs(args) +def _main(held: contextlib.ExitStack) -> None: + parser = _build_parser() + args = parser.parse_args() + from verify import work_from_repo_root + + work_from_repo_root( + args, parser, + ["db", "platforms_dir", "bios_dir", "output_dir", "emulators_dir", "from_md5_file"], + ) + packresolve.set_offline(bool(args.offline)) + if args.platform: + require_platform(args.platform, args.platforms_dir) + require_directory(args.bios_dir) + + _refuse_unapplied_flags(args, parser) + _refuse_for_verify_packs(args, parser) + if _run_quick_mode(args, parser, held): + return + + _validate_args(args, parser) + _resolve_systems(args, parser) + # Unset until every refusal has run, so a typed default value is seen. + args.source = args.source or "full" + args.group_by = args.group_by or "system" + + # Hash lookup / pack mode + # A platform run may refresh the trees below, under the exclusive lock: + # it takes the shared ones after that. Every other mode reads them now. + if not (args.platform or args.all) or args.offline or args.no_refresh_data: + hold_data_dirs(held, load_data_dir_registry(args.platforms_dir)) + if args.from_md5 or getattr(args, "from_md5_file", None): + _run_hash_mode(args) + return + + db = load_database(args.db) + zip_contents = build_zip_contents_index(db) + if args.emulator or (args.system and not args.platform and not args.all): + _run_profile_mode(args, db, zip_contents) + return + _run_platform_mode(args, parser, held, db, zip_contents) + + # Manifest generation (JSON inventory for install.py) @functools.cache @@ -4129,6 +4567,35 @@ def _one_content(held: list[str], entries: dict[str, dict], emulator: str) -> li return [min(own or held, key=lambda dest: (dest.count("/"), len(dest), dest))] +def _profile_roots(profile: dict) -> dict[str, str]: + """The data_root of each system the profile names a findable place for.""" + return { + _DATA_ROOT_SYSTEMS[key]: target + for key, value in (profile.get("data_root") or {}).items() + if key in _DATA_ROOT_SYSTEMS and isinstance(value, dict) + and (target := _data_root_target(_DATA_ROOT_SYSTEMS[key], value.get("path", ""))) + } + + +def _copy_source( + entry: dict, + by_tail: dict[str, list[str]], + entries: dict[str, dict] | None, + profile_name: str, +) -> str | None: + """The manifest dest a profile entry's standalone copy is taken from.""" + standalone = entry.get("standalone_path") + if (not standalone or standalone.endswith("/") or entry.get("mode") == "libretro" + or entry.get("type") == "directory" or entry.get("found_by") == "content"): + return None + held = by_tail.get(entry.get("path") or entry["name"], []) + if len(held) > 1 and entries is not None: + held = _one_content(held, entries, profile_name) + if len(held) != 1: + return None + return held[0] + + def _profile_copies( profiles: dict[str, dict], dests: list[str], declared: list[dict], entries: dict[str, dict] | None = None, @@ -4172,26 +4639,14 @@ def _profile_copies( copies: list[dict] = [] for profile_name in sorted(profiles): profile = profiles[profile_name] - roots = { - _DATA_ROOT_SYSTEMS[key]: target - for key, value in (profile.get("data_root") or {}).items() - if key in _DATA_ROOT_SYSTEMS and isinstance(value, dict) - and (target := _data_root_target(_DATA_ROOT_SYSTEMS[key], value.get("path", ""))) - } + roots = _profile_roots(profile) if not roots: continue for entry in profile.get("files", []): - standalone = entry.get("standalone_path") - if (not standalone or standalone.endswith("/") or entry.get("mode") == "libretro" - or entry.get("type") == "directory" or entry.get("found_by") == "content"): + source = _copy_source(entry, by_tail, entries, profile_name) + if source is None: continue - held = by_tail.get(entry.get("path") or entry["name"], []) - if len(held) > 1 and entries is not None: - held = _one_content(held, entries, profile_name) - if len(held) != 1: - continue - source = held[0] - where = PurePosixPath(standalone) + where = PurePosixPath(entry["standalone_path"]) placed: dict[str, list[str]] = {} for system, root in roots.items(): sep = "\\" if system == "windows" else "/" @@ -4296,7 +4751,7 @@ def _manifest_entry( "cores": cores, **selection_fields, } - if is_release_asset: + if local_path and is_release_asset: entry["storage"] = "release" entry["release_asset"] = _release_asset_name(local_path, repo_root) # The installer writes what it downloads with the umask's mode, but a program @@ -4323,20 +4778,27 @@ def _is_program(path: str) -> bool: return any(head.startswith(magic) for magic in _PROGRAM_MAGIC) +@dataclass(frozen=True) +class _ClaimContext: + """How a manifest resolves and judges the copy a core claim names.""" + + db: dict + bios_dir: str + zip_contents: dict | None + offline: bool | None + required_only: bool + validation_index: dict | None + owners: dict | None + repo_root: str + data_registry: dict | None = None + + def _attach_claims( entry: dict, claims: list[dict], - db: dict, - bios_dir: str, - zip_contents: dict | None, - offline: bool | None, - required_only: bool, - validation_index: dict | None, - owners: dict | None, + ctx: _ClaimContext, dest: str, manifest_dest: str, - repo_root: str, - data_registry: dict | None = None, ) -> None: """Every other core that reads this destination, in the order the builder meets them: a pack built without the earlier cores, or without the @@ -4355,35 +4817,26 @@ def _attach_claims( if not claim_emu or claim_emu in claimed: continue claimed.add(claim_emu) - if required_only and claim.get("required") is False: + if ctx.required_only and claim.get("required") is False: continue claim_path, claim_status = resolve_file( - claim, db, bios_dir, zip_contents, + claim, ctx.db, ctx.bios_dir, ctx.zip_contents, dest_hint=claim.get("destination", ""), - data_dir_registry=data_registry, offline=offline, + data_dir_registry=ctx.data_registry, offline=ctx.offline, ) if claim_status in ("not_found", "external", "user_provided") or not claim_path: # Nothing held for this core: narrowed to it without the cores # served above, the slot is a file left out (installer replays it). - systems = _extra_system_ids(claim) - entry.setdefault("absent_claims", []).append({ - "name": str(claim.get("name") or ""), - "system": systems[0] if systems else "", - "required": bool(claim.get("required", True)), - "reason": ( - claim_status - if claim_status in ("not_found", "external", "user_provided") - else "not_found" - ), - "cores": [claim_emu], - }) + entry.setdefault("absent_claims", []).append( + _absent_claim(claim, claim_emu, claim_status) + ) continue claim_path, _ = validated_choice( - claim, claim_path, db, validation_index or {}, bios_dir, None, - claim.get("layout_path") or dest, owners, + claim, claim_path, ctx.db, ctx.validation_index or {}, ctx.bios_dir, None, + claim.get("layout_path") or dest, ctx.owners, ) copy, _size = _manifest_entry( - claim_path, manifest_dest, [claim_emu], db, repo_root, {} + claim_path, manifest_dest, [claim_emu], ctx.db, ctx.repo_root, {} ) if copy is None: continue @@ -4399,6 +4852,22 @@ def _attach_claims( entry.pop("alternatives", None) +def _absent_claim(claim: dict, claim_emu: str, claim_status: str) -> dict: + """The omission a claim with nothing held stands for.""" + systems = _extra_system_ids(claim) + return { + "name": str(claim.get("name") or ""), + "system": systems[0] if systems else "", + "required": bool(claim.get("required", True)), + "reason": ( + claim_status + if claim_status in ("not_found", "external", "user_provided") + else "not_found" + ), + "cores": [claim_emu], + } + + def _record_slot_choices( entry: dict, choices: list[tuple[str, str | None]], @@ -4459,114 +4928,125 @@ def _record_slot_choices( entry["declared_choices"] = declared -def _manifest_core_entries( - core_files: list, - config: dict, - db: dict, - bios_dir: str, - base_dest: str, - repo_root: str, - zip_contents: dict | None, - offline: bool | None, - region_drops: set, - case_insensitive: bool, - seen_destinations: set, - seen_lower: set, - seen_parents: set, - manifest_files: list, - omitted_by_destination: dict, - record_omission: Callable[..., None], - pack_only_sizes: list[int], - selection: SelectionIndex, - required_only: bool = False, - validation_index: dict | None = None, - owners: dict | None = None, - data_registry: dict | None = None, - profiles: dict | None = None, -) -> int: - """Add the files a platform's cores need but its list does not name. +class _CoreEntryState(TypedDict): + """What _manifest_core_entries reads and fills of the manifest being listed.""" - The installer fetches by hash, so an entry records the copy this - repository holds rather than the value the profile declares: an - upstream hash carried by no local file leaves the entry with no URL. - Returns the bytes added. - """ - total_size = 0 - extras_pfx = _detect_extras_prefix(config, base_dest) - resolver = ExtraResolver( - db, bios_dir, zip_contents, data_registry, validation_index or {}, - owners, offline, profiles=profiles, - ) - spares = core_file_spares( - core_files, - lambda fe: bool(region_drops) - and extra_region_key(fe, declared_root(config)) in region_drops, - ) if required_only else set() - for fe in core_files: - if required_only and (fe.get("required") is False or id(fe) in spares): - continue + region_drops: set + case_insensitive: bool + seen_destinations: set + seen_lower: set + seen_parents: set + manifest_files: list + omitted_by_destination: dict + record_omission: Callable[..., None] + pack_only_sizes: list[int] + selection: SelectionIndex + + +class _CoreEntryParts(_CoreEntryState, total=False): + """_CoreEntryState and the optional keywords of _manifest_core_entries.""" + + required_only: bool + validation_index: dict | None + owners: dict | None + data_registry: dict | None + profiles: dict | None + + +@dataclass +class _CoreEntryLister: + """Lists a platform's core extras into the manifest, one at a time.""" + + config: dict + base_dest: str + ctx: _ClaimContext + state: _CoreEntryParts + resolver: ExtraResolver + total_size: int = 0 + extras_pfx: str = field(init=False) + + def __post_init__(self) -> None: + self.extras_pfx = _detect_extras_prefix(self.config, self.base_dest) + + def region_key(self, fe: dict) -> str: + return extra_region_key(fe, declared_root(self.config)) + + def region_dropped(self, fe: dict) -> bool: + drops = self.state["region_drops"] + return bool(drops) and self.region_key(fe) in drops + + def list_extra(self, fe: dict) -> None: + state = self.state dest = sanitize_pack_path(fe.get("destination", fe["name"])) if not dest: - continue - region_key = extra_region_key(fe, declared_root(config)) - if region_drops and region_key in region_drops: - continue - full_dest = extra_full_destination(fe, extras_pfx) + return + region_key = self.region_key(fe) + if state["region_drops"] and region_key in state["region_drops"]: + return + full_dest = extra_full_destination(fe, self.extras_pfx) if not full_dest: - continue - save_root = fe.get("root") == "save" + return if ( - case_insensitive - and full_dest not in seen_destinations - and full_dest.lower() in seen_lower + state["case_insensitive"] + and full_dest not in state["seen_destinations"] + and full_dest.lower() in state["seen_lower"] ): - # One file under two spellings: BlastEm opens 32X_M_BIOS.bin and - # PicoDrive 32X_M_BIOS.BIN. The second core's claim joins the - # entry kept, or a pack built without the first core loses the - # file the installer would still have to place. Collisions are - # rare, so the entry is looked up only when one happens. - folded = ( - save_manifest_dest(fe) if save_root - else _manifest_dest_of(full_dest, base_dest) - ).lower() - held = next( - (e for e in manifest_files - if e["dest"].lower() == folded - and (e.get("root") == "save") == save_root), - None, - ) - if held is not None and held.get("cores") is not None: - _attach_claims( - held, [fe], db, bios_dir, zip_contents, offline, required_only, - validation_index, owners, dest, held["dest"], repo_root, - data_registry, - ) - continue + self._join_spelling(fe, full_dest, dest) + return if placement_block( - full_dest, seen_destinations, seen_lower, seen_parents, - case_insensitive, + full_dest, state["seen_destinations"], state["seen_lower"], state["seen_parents"], + state["case_insensitive"], ): - continue + return # The builder's own lookup (ExtraResolver): the data directories, the # copy the emulator accepts, and the size its code checks. Without # the data directories blueMSX's MSX2P.rom named a different dump # than the one its pack carried. - local_path, status, _disagreement = resolver.resolve(fe, dest) + local_path, status, _disagreement = self.resolver.resolve(fe, dest) if not local_path: - source_emu = fe.get("source_profile") or fe.get("source_emulator", "") - if not _claim_platform_omission( - omitted_by_destination.get(full_dest), source_emu - ): - systems = _extra_system_ids(fe) - record_omission( - full_dest, fe, systems[0] if systems else "", status, _claim_cores(fe), - ) - continue + self._omit(fe, full_dest, status) + return + self._list_held(fe, dest, full_dest, region_key, local_path) + def _join_spelling(self, fe: dict, full_dest: str, dest: str) -> None: + # One file under two spellings: BlastEm opens 32X_M_BIOS.bin and + # PicoDrive 32X_M_BIOS.BIN. The second core's claim joins the + # entry kept, or a pack built without the first core loses the + # file the installer would still have to place. Collisions are + # rare, so the entry is looked up only when one happens. + save_root = fe.get("root") == "save" + folded = ( + save_manifest_dest(fe) if save_root + else _manifest_dest_of(full_dest, self.base_dest) + ).lower() + held = next( + (e for e in self.state["manifest_files"] + if e["dest"].lower() == folded + and (e.get("root") == "save") == save_root), + None, + ) + if held is not None and held.get("cores") is not None: + _attach_claims(held, [fe], self.ctx, dest, held["dest"]) + + def _omit(self, fe: dict, full_dest: str, reason: str) -> None: source_emu = fe.get("source_profile") or fe.get("source_emulator", "") + if not _claim_platform_omission( + self.state["omitted_by_destination"].get(full_dest), source_emu + ): + systems = _extra_system_ids(fe) + self.state["record_omission"]( + full_dest, fe, systems[0] if systems else "", reason, _claim_cores(fe), + ) + + def _list_held( + self, fe: dict, dest: str, full_dest: str, region_key: str, local_path: str, + ) -> None: + state = self.state + source_emu = fe.get("source_profile") or fe.get("source_emulator", "") + save_root = fe.get("root") == "save" # Manifest dests are relative to base_destination. Keep the inferred # extras prefix when it is an internal layout dir (RetroDECK bios/). @@ -4579,42 +5059,31 @@ def _manifest_core_entries( # Relative to the roms or emulators folder the entry names. manifest_dest = dest else: - manifest_dest = _manifest_dest_of(full_dest, base_dest) + manifest_dest = _manifest_dest_of(full_dest, self.base_dest) entry, file_size = _manifest_entry( local_path, manifest_dest, [source_emu] if source_emu else [], - db, repo_root, - selection.fields(region_key, fe.get("name", "")), + self.ctx.db, self.ctx.repo_root, + state["selection"].fields(region_key, fe.get("name", "")), ) if entry is not None and save_root: entry.update(save_entry_fields(fe)) elif entry is not None and outer_root: entry["root"] = outer_root if entry is None: - if not _claim_platform_omission( - omitted_by_destination.get(full_dest), source_emu - ): - systems = _extra_system_ids(fe) - record_omission( - full_dest, fe, systems[0] if systems else "", "not_found", - _claim_cores(fe), - ) + self._omit(fe, full_dest, "not_found") if file_size: - pack_only_sizes.append(file_size) - continue + state["pack_only_sizes"].append(file_size) + return if is_game_data(fe) and all( is_game_data(claim) for claim in fe.get("other_claims", []) ): entry["category"] = GAME_DATA - _attach_claims( - entry, fe.get("other_claims", []), db, bios_dir, zip_contents, - offline, required_only, validation_index, owners, dest, - manifest_dest, repo_root, data_registry, - ) + _attach_claims(entry, fe.get("other_claims", []), self.ctx, dest, manifest_dest) - manifest_files.append(entry) - displaced = omitted_by_destination.pop(full_dest, None) + state["manifest_files"].append(entry) + displaced = state["omitted_by_destination"].pop(full_dest, None) if displaced and displaced.get("cores"): # A core that wants this slot and has nothing held: a pack built # without the cores served here leaves the slot to it, missing, @@ -4622,13 +5091,54 @@ def _manifest_core_entries( entry.setdefault("absent_claims", []).append( {k: v for k, v in displaced.items() if k not in ("dest", "held")} ) - total_size += file_size - seen_destinations.add(full_dest) - _register_path(full_dest, seen_destinations, seen_parents) - if case_insensitive: - seen_lower.add(full_dest.lower()) + self.total_size += file_size + state["seen_destinations"].add(full_dest) + _register_path(full_dest, state["seen_destinations"], state["seen_parents"]) + if state["case_insensitive"]: + state["seen_lower"].add(full_dest.lower()) - return total_size + +def _manifest_core_entries( + core_files: list, + config: dict, + db: dict, + bios_dir: str, + base_dest: str, + repo_root: str, + zip_contents: dict | None, + offline: bool | None, + **state: Unpack[_CoreEntryParts], +) -> int: + """Add the files a platform's cores need but its list does not name. + + The installer fetches by hash, so an entry records the copy this + repository holds rather than the value the profile declares: an + upstream hash carried by no local file leaves the entry with no URL. + Returns the bytes added. + """ + _reject_unknown( + "_manifest_core_entries", state, + _CoreEntryParts.__required_keys__ | _CoreEntryParts.__optional_keys__, + ) + required_only = state.get("required_only", False) + ctx = _ClaimContext( + db, bios_dir, zip_contents, offline, required_only, + state.get("validation_index"), state.get("owners"), repo_root, + state.get("data_registry"), + ) + lister = _CoreEntryLister( + config, base_dest, ctx, state, + ExtraResolver( + db, bios_dir, zip_contents, ctx.data_registry, ctx.validation_index or {}, + ctx.owners, offline, profiles=state.get("profiles"), + ), + ) + spares = core_file_spares(core_files, lister.region_dropped) if required_only else set() + for fe in core_files: + if required_only and (fe.get("required") is False or id(fe) in spares): + continue + lister.list_extra(fe) + return lister.total_size def _manifest_dest_of(full_dest: str, base_dest: str) -> str: @@ -4655,6 +5165,18 @@ def _claim_platform_omission(held: dict | None, emulator: str) -> bool: return True +class _SelectionOptions(TypedDict, total=False): + """The keyword options of _manifest_selection.""" + + required_only: bool + regions: list[str] | None + undeclared: list[dict] | None + zip_contents: dict | None + data_registry: dict | None + slot_overrides: dict[str, str] | None + one_per_slot: bool + + def _manifest_selection( config: dict, pack_systems: dict, @@ -4664,13 +5186,7 @@ def _manifest_selection( emu_profiles: dict, target_cores: set[str] | None, source: str, - required_only: bool, - regions: list[str] | None, - undeclared: list[dict] | None = None, - zip_contents: dict | None = None, - data_registry: dict | None = None, - slot_overrides: dict[str, str] | None = None, - one_per_slot: bool = False, + **options: Unpack[_SelectionOptions], ) -> tuple[set[str], SelectionIndex, set[str]]: """Destinations a region list and --one-per-slot remove, the groups the installer replays, and the destinations the builder holds. @@ -4679,6 +5195,10 @@ def _manifest_selection( decides on the full manifest withdraws what `--region` and `--one-per-slot` withdraw from the pack. """ + _reject_unknown("_manifest_selection", options, _SelectionOptions.__optional_keys__) + required_only = options.get("required_only", False) + regions = options.get("regions") + one_per_slot = options.get("one_per_slot", False) extras: list[dict] = [] held: set[str] = set() region_groups, _extra_dests = platform_region_groups( @@ -4694,10 +5214,10 @@ def _manifest_selection( required_only=required_only, extras_out=extras, held_out=held, - undeclared=undeclared, - zip_contents=zip_contents, - data_registry=data_registry, - slot_overrides=slot_overrides, + undeclared=options.get("undeclared"), + zip_contents=options.get("zip_contents"), + data_registry=options.get("data_registry"), + slot_overrides=options.get("slot_overrides"), ) region_index = region_mod.platform_region_index( config, emu_profiles, target_cores @@ -4863,27 +5383,622 @@ def system_titles(*entry_lists: Iterable[dict]) -> dict[str, str]: return titles +class InstallListOptions(TypedDict, total=False): + """The keyword options of generate_install_list, which it hands to + generate_manifest.""" + + emulators_dir: str + zip_contents: dict | None + emu_profiles: dict | None + target_cores: set[str] | None + source: str + regions: list[str] | None + target_name: str | None + offline: bool | None + required_only: bool + data_registry: dict | None + fold_case: bool | None + no_game_data: bool + one_per_slot: bool + + +class ManifestOptions(InstallListOptions, total=False): + """The keyword options of generate_manifest.""" + + known_slot_choices: dict | None + build_out: dict | None + + +@dataclass(frozen=True) +class _ManifestSettings: + """ManifestOptions with their defaults.""" + + emulators_dir: str = "emulators" + zip_contents: dict | None = None + emu_profiles: dict | None = None + target_cores: set[str] | None = None + source: str = "full" + regions: list[str] | None = None + target_name: str | None = None + offline: bool | None = None + required_only: bool = False + data_registry: dict | None = None + fold_case: bool | None = None + no_game_data: bool = False + one_per_slot: bool = False + known_slot_choices: dict | None = None + build_out: dict | None = None + + +@dataclass +class _ManifestBuild: + """One install manifest being listed: its files, omissions and the slots + they take, settled by the rules the pack builder follows.""" + + platform_name: str + platforms_dir: str + db: dict + bios_dir: str + opts: _ManifestSettings + config: dict + emu_profiles: dict + zip_contents: dict + data_registry: dict + game_entries: set[int] + install_section: dict + repo_root: str + pack_systems: dict + manifest_profiles: dict = field(default_factory=dict) + manifest_validation: dict = field(default_factory=dict) + manifest_owners: dict = field(default_factory=dict) + game_destinations: set[str] = field(default_factory=set) + placed: _Placed = field(init=False) + # Destination -> (entry, full destination, manifest destination) of each + # platform file, for the core claims a target build would fall back on. + platform_entries: dict[str, tuple[dict, str, str]] = field(default_factory=dict) + # Full destination -> the platform systems declaring it. A file's + # `systems` also names the systems whose cores read it there. A pack built + # for a target keeps the file only while a declaring system stays. + declared_by: dict[str, set[str]] = field(default_factory=dict) + manifest_files: list[dict] = field(default_factory=list) + omitted_by_destination: dict[str, dict] = field(default_factory=dict) + total_size: int = 0 + # Sizes of the files the pack carries and the installer cannot fetch: the + # collection does not index them, a data directory cache answered. + pack_only_sizes: list[int] = field(default_factory=list) + # Platform entries resolved into a data directory cache: destination -> + # (cache file, size). Settled once the trees are walked. + cache_held: dict[str, tuple[str, int]] = field(default_factory=dict) + rename_notes: set[str] = field(default_factory=set) + slot_choices: dict = field(default_factory=dict) + slot_overrides: dict[str, str] = field(default_factory=dict) + undeclared: list[dict] | None = None + region_drops: set[str] = field(default_factory=set) + selection: SelectionIndex | None = None + builder_held: set[str] = field(default_factory=set) + declarations: dict[str, dict[str, list[dict]]] = field(default_factory=dict) + data_members: list[tuple[str, str]] = field(default_factory=list) + data_directories: list[dict] = field(default_factory=list) + + def __post_init__(self) -> None: + self.placed = _Placed(self.case_insensitive) + + @property + def base_dest(self) -> str: + return self.config.get("base_destination", "") + + @property + def case_insensitive(self) -> bool: + return self.config.get("case_insensitive_fs", False) + + @property + def verification_mode(self) -> str: + return self.config.get("verification_mode", "existence") + + @property + def fields(self) -> SelectionIndex: + assert self.selection is not None + return self.selection + + def manifest_destination(self, full_destination: str) -> str: + return _manifest_dest_of(full_destination, self.base_dest) + + def record_omission( + self, + full_destination: str, + file_entry: dict, + system_id: str, + reason: str, + cores: list[str] | None, + ) -> None: + # A core extra (cores given) is keyed as the region pass keys it. + bare = ( + sanitize_pack_path(file_entry.get("destination", file_entry.get("name", ""))) + if cores is None else extra_region_key(file_entry, declared_root(self.config)) + ) + self.omitted_by_destination[full_destination] = { + **self._omission_dest(full_destination, file_entry), + "name": str(file_entry.get("name") or ""), + "system": system_id, + "required": bool(file_entry.get("required", True)), + "reason": reason, + "cores": cores, + # An omission keeps its region groups, as the builder groups it: + # a requested file the collection lacks answers the request + # without displacing a held fallback (resolve_region_drops, + # held). One the builder holds and the installer cannot fetch + # does displace it in the pack, so it says so, and the installer + # drops the same fallback. + **self.fields.fields(bare, str(file_entry.get("name") or "")), + **({"held": True} if bare in self.builder_held else {}), + **( + {"category": GAME_DATA} + if (bare in self.game_destinations if cores is None else is_game_data(file_entry)) + else {} + ), + } + + def _omission_dest(self, full_destination: str, file_entry: dict) -> dict: + # Read in the emulator's own folder, outside the BIOS tree: dest is + # the path the emulator reads, relative to that folder. + if file_entry.get("unplaced"): + return {"dest": sanitize_pack_path(file_entry.get("path") or ""), + "root": "emulator"} + if file_entry.get("root") in OUTER_ROOTS: + return {"dest": sanitize_pack_path( + file_entry.get("destination") or file_entry.get("path") or "" + ), "root": file_entry["root"]} + if file_entry.get("root") == "save": + return {"dest": save_manifest_dest(file_entry), **save_entry_fields(file_entry)} + return {"dest": self.manifest_destination(full_destination)} + + def kept_declarations(self) -> Iterator[tuple[str, dict, str, str]]: + """(system, entry, destination, full destination) of each platform + declaration this manifest keeps, in the builder's order.""" + opts = self.opts + for sys_id, system in sorted(self.pack_systems.items()): + for file_entry in system.get("files", []): + if opts.required_only and file_entry.get("required") is False: + continue + dest = sanitize_pack_path(file_entry.get("destination", file_entry["name"])) + if not dest: + continue + if self.region_drops and dest in self.region_drops: + continue + full_dest = f"{self.base_dest}/{dest}" if self.base_dest else dest + yield sys_id, file_entry, dest, full_dest + + def declaration_order(self, full_dest: str) -> list[tuple[list[str], dict, str]]: + """The declarations answering a destination as its systems drop. + + A pack built for a target loses the systems no available core + reads, and the next declaration takes the destination by the + same rule: RetroArch's bare nintendo-ds dsi_bios9.bin once the + pinned nintendo-dsi one is gone. + """ + remaining = dict(self.declarations.get(full_dest, {})) + order: list[tuple[list[str], dict, str]] = [] + while remaining: + winner = _preferred_entries( + {sid: {"files": fes} for sid, fes in remaining.items()}, + self.db, self.bios_dir, self.base_dest, self.opts.required_only, + self.zip_contents, self.opts.offline, + ).get(full_dest) + first_sys, first = next( + (sid, fe) for sid, fes in sorted(remaining.items()) for fe in fes + if winner is None or id(fe) == winner + ) + systems = sorted( + sid for sid, fes in remaining.items() + if any(fe is first for fe in fes) + ) + order.append((systems, first, first_sys)) + for sid in systems: + del remaining[sid] + return order + + def list_baseline(self, settle: _Settle) -> None: + """Phase 1: the platform's own files.""" + # The same destination can be declared by more than one system, bare + # in one and hash-constrained in another. The builder lets the + # constrained sibling claim it. Without the same rule here the + # manifest named whatever answered to the name, so the pack and the + # installer disagreed about which file a destination means. + # Decided among the declarations this manifest keeps, as the pack + # does. A manifest carries no data directories. + preferred_entries = _preferred_entries( + self.pack_systems, self.db, self.bios_dir, self.base_dest, + self.opts.required_only, self.zip_contents, self.opts.offline, + ) + for sys_id, file_entry, _dest, full_dest in self.kept_declarations(): + self.declarations.setdefault(full_dest, {}).setdefault(sys_id, []).append( + file_entry + ) + for sys_id, file_entry, dest, full_dest in self.kept_declarations(): + self.declared_by.setdefault(full_dest, set()).add(sys_id) + preferred = preferred_entries.get(full_dest) + if preferred is not None and id(file_entry) != preferred: + continue + if self.placed.holds(full_dest) or self.placed.conflicts(full_dest): + continue + if file_entry.get("storage", "embedded") == "user_provided": + self._list_instructions(file_entry, sys_id, full_dest) + continue + local_path, status = settle( + file_entry, sys_id, dest, self.slot_overrides.get(full_dest) + ) + if local_path is None: + self.record_omission(full_dest, file_entry, sys_id, status, None) + continue + self._list_declared(file_entry, sys_id, dest, full_dest, local_path, settle) + + def _list_instructions(self, file_entry: dict, sys_id: str, full_dest: str) -> None: + self.record_omission( + full_dest, + file_entry, + sys_id, + "user_provided", + None, + ) + # The pack writes the instructions in the slot's place + # and holds it: one more member than the files listed. + instructions = file_entry.get( + "instructions", "Please provide this file manually." + ) + self.pack_only_sizes.append(len( + f"File needed: {file_entry['name']}\n\n{instructions}\n".encode() + )) + self.placed.take(full_dest) + + def _list_declared( + self, + file_entry: dict, + sys_id: str, + dest: str, + full_dest: str, + local_path: str, + settle: _Settle, + ) -> None: + entry, file_size = _manifest_entry( + local_path, dest, None, self.db, self.repo_root, + self.fields.fields(dest, file_entry["name"]), + ) + if entry is not None and dest in self.game_destinations: + entry["category"] = GAME_DATA + if entry is not None and self.opts.target_cores is None: + order = self.declaration_order(full_dest) + if full_dest in self.slot_choices or len(order) > 1: + _record_slot_choices( + entry, self.slot_choices.get(full_dest, []), order, + settle, dest, self.db, self.repo_root, + ) + if entry is None: + self.record_omission(full_dest, file_entry, sys_id, "not_found", None) + if file_size and full_dest not in self.placed.destinations: + if _in_data_cache(local_path, self.data_registry): + self.cache_held[full_dest] = (local_path, file_size) + else: + self.pack_only_sizes.append(file_size) + self.placed.take(full_dest) + return + + self.manifest_files.append(entry) + self.platform_entries[full_dest] = (entry, full_dest, dest) + self.omitted_by_destination.pop(full_dest, None) + self.total_size += file_size + self.placed.take(full_dest) + + def claim_context(self) -> _ClaimContext: + return _ClaimContext( + self.db, self.bios_dir, self.zip_contents, self.opts.offline, + self.opts.required_only, self.manifest_validation, self.manifest_owners, + self.repo_root, self.data_registry, + ) + + def list_core_files(self) -> None: + """Phase 2: core complement (emulator extras).""" + opts = self.opts + unplaceable: list[dict] = [] + baseline_claims: dict[str, dict] = {} + absent: list[dict] = [] + if opts.source != "platform": + core_files = _collect_emulator_extras( + self.config, + opts.emulators_dir, + self.db, + self.placed.destinations, + self.base_dest, + self.emu_profiles, + target_cores=opts.target_cores, + include_all=(opts.source == "truth"), + unplaceable=unplaceable, + baseline_claims=baseline_claims, + absent=absent, + undeclared=self.undeclared, + ) + else: + core_files = [] + _record_unplaceable(unplaceable, self.record_omission, self.config, self.emu_profiles) + _record_absent( + absent, self.config, self.base_dest, self.region_drops, opts.required_only, + self.placed.destinations, self.omitted_by_destination, self.record_omission, + ) + self.total_size += _manifest_core_entries( + core_files, self.config, self.db, self.bios_dir, self.base_dest, self.repo_root, + self.zip_contents, opts.offline, + region_drops=self.region_drops, case_insensitive=self.case_insensitive, + seen_destinations=self.placed.destinations, seen_lower=self.placed.lower, + seen_parents=self.placed.parents, manifest_files=self.manifest_files, + omitted_by_destination=self.omitted_by_destination, + record_omission=self.record_omission, pack_only_sizes=self.pack_only_sizes, + selection=self.fields, required_only=opts.required_only, + validation_index=self.manifest_validation, owners=self.manifest_owners, + data_registry=self.data_registry, profiles=self.emu_profiles, + ) + self._settle_claims(baseline_claims) + + def _settle_claims(self, baseline_claims: dict[str, dict]) -> None: + ctx = self.claim_context() + for key, (entry, full_dest, dest) in self.platform_entries.items(): + declared = self.declared_by.get(full_dest, set()) + if declared and declared != set(entry.get("systems") or []): + entry["declared_in"] = sorted(declared) + claims = baseline_claims.get(key, {}).get("other_claims") + if claims and any(not is_game_data(claim) for claim in claims): + # A core reads this path as firmware: a pack without game data + # still fills it. + entry.pop("category", None) + if claims: + _attach_claims(entry, claims, ctx, dest, dest) + # A platform omission narrows like a platform file: by the systems + # declaring it, which can be fewer than the ones whose cores read it. + for full_dest, omission in self.omitted_by_destination.items(): + declared = self.declared_by.get(full_dest, set()) + if ( + omission.get("cores") is None + and declared + and declared != set(omission.get("systems") or []) + ): + omission["declared_in"] = sorted(declared) + + def list_data_directories(self) -> list[int]: + """Phase 3: data directories, and the sizes the pack alone holds. + + They are not files of the collection: the installer fetches each + from its source archive and keeps the members whose hash the list + below records, the same tree the pack carries. + """ + opts = self.opts + data_refs: dict[str, tuple[str, tuple[str, ...], str, bool]] = {} + reclaim = {full: path for full, (path, _size) in self.cache_held.items()} + core_trees = ([] if opts.source == "platform" + else core_data_trees(self.config, self.emu_profiles, self.platform_name, + self.data_registry, opts.target_cores)) + self.data_members = list(_data_directory_members( + self.pack_systems, self.data_registry, self.platform_name, self.base_dest, + self.case_insensitive, self.placed.destinations, self.placed.lower, + self.placed.parents, refs_out=data_refs, required_only=opts.required_only, + core_trees=core_trees, + reclaim=reclaim, + )) + # A platform entry found in a tree the pack carries is that tree's member: + # the installer rebuilds it from the tree's archive. One found in a tree + # the pack does not carry stays omitted, held by the pack alone. + for full_dest, (_path, size) in self.cache_held.items(): + if full_dest in reclaim: + self.pack_only_sizes.append(size) + else: + self.omitted_by_destination.pop(full_dest, None) + self.data_directories = _manifest_data_directories( + self.data_members, data_refs, self.data_registry or {}, self.base_dest, + tree_readers(self.pack_systems, core_trees), self.pack_systems, + ) + return self.pack_only_sizes + [os.path.getsize(src) for src, _dest in self.data_members] + + def result(self, data_sizes: list[int]) -> dict: + opts, config, pack_systems = self.opts, self.config, self.pack_systems + manifest_files, platform_name = self.manifest_files, self.platform_name + omitted = self.omitted_by_destination + # The pack is unzipped into the BIOS folder: an entry the installer puts + # in the roms, emulators or configs folder is in its list, not in the ZIP. + outer = [e for e in manifest_files if e.get("root") in OUTER_ROOTS] + core_names, system_cores = _manifest_core_index( + config, self.emu_profiles, manifest_files, list(omitted.values()) + ) + result: dict = { + "manifest_version": 2, + "source": opts.source, + "regions": list(opts.regions or []), + # A target-filtered manifest is only distinguishable from the full one + # by its filename, so it records the filter the way regions does. + # Absent on an unfiltered manifest rather than empty, to leave the + # twelve default files byte-identical. + **({"target": opts.target_name} if opts.target_name else {}), + # Every game data file below says so: the installer's --no-game-data + # relies on it, and refuses a list that does not record categories. + "categorized": True, + # The entries a slot decides carry `slot`, which --one-per-slot + # reads: without this mark the installer refuses it. + "slot_fields": True, + **({"no_game_data": True} if opts.no_game_data else {}), + "platform": platform_name, + "display_name": config.get("platform", platform_name), + "version": platform_list_version(config, platform_name), + "generated": _build_timestamp(self.db), + "base_destination": self.base_dest, + "detect": self.install_section.get("detect", []), + "standalone_copies": _standalone_copies( + self.install_section.get("standalone_copies", []), self.manifest_profiles, + manifest_files, + ), + "total_files": len(manifest_files), + "total_size": self.total_size, + "pack_files": ( + len(manifest_files) - len(outer) + len(data_sizes) + len(PACK_DOCUMENTS) + + len(self.rename_notes) + + len(pack_markers(platform_name, opts.target_name, self.platforms_dir)) + ), + "pack_size": self.total_size - sum(int(e.get("size") or 0) for e in outer) + + sum(data_sizes), + "total_omitted": len(omitted), + "omitted_files": sorted( + omitted.values(), key=lambda entry: entry["dest"] + ), + "core_names": core_names, + **({"downloaded_cores": downloaded} if (downloaded := _downloaded_cores( + config, self.emu_profiles, core_names, self.platforms_dir, + )) else {}), + **({"offered_boards": offered} if (offered := _offered_boards( + config, self.emu_profiles, core_names, + [*manifest_files, *omitted.values()], + )) else {}), + "system_cores": system_cores, + # The names the platform itself gives its systems (Batocera's psx), + # which --system accepts beside the ids. + "system_names": platform_system_names(config, list(pack_systems)), + "system_titles": system_titles(manifest_files, omitted.values()), + "files": manifest_files, + } + if self.data_directories: + result["data_directories"] = self.data_directories + self._outer_directories(result) + return result + + def _outer_directories(self, result: dict) -> None: + for root in OUTER_ROOTS: + layout = outer_layout(self.config, root) or {} + if layout and any(entry.get("root") == root + for entry in [*self.manifest_files, *result["omitted_files"]]): + # Where the installer finds the folder: relative to the folder + # holding the BIOS folder, which the platform names bios_folder. + result[f"{root}_directory"] = { + key: layout[key] for key in MANIFEST_OUTER_FIELDS if key in layout + } + saved = [entry for entry in self.manifest_files if entry.get("root") == "save"] + if saved: + save = save_layout(self.config) + result["save_directory"] = {"folder": save.folder, "by_core": save.by_core} + if save.by_system: + # The launcher writes the directory at each launch, one folder + # per system: each dest starts with that folder. + result["save_directory"]["by_system"] = True + + +def _start_manifest( + platform_name: str, + platforms_dir: str, + db: dict, + bios_dir: str, + registry_path: str, + opts: _ManifestSettings, +) -> tuple[_ManifestBuild, dict]: + """The manifest's configuration and scope, and the profiles before the + game data cut.""" + config = load_platform_config(platform_name, platforms_dir) + if opts.fold_case is not None: + config = {**config, "case_insensitive_fs": opts.fold_case} + if opts.target_name: + config = {**config, TARGET_KEY: opts.target_name} + zip_contents = {} if opts.zip_contents is None else opts.zip_contents + emu_profiles = opts.emu_profiles + if emu_profiles is None: + emu_profiles = load_emulator_profiles(opts.emulators_dir) + data_registry = opts.data_registry + if data_registry is None: + data_registry = load_data_dir_registry(platforms_dir) + full_profiles = emu_profiles + game_entries: set[int] = set() + if opts.no_game_data: + cut = without_game_data(config, emu_profiles, data_registry, opts.target_cores) + config, emu_profiles = cut.config, cut.profiles + else: + game_entries = platform_game_data(config, emu_profiles, data_registry) + + # Load registry for install metadata + registry: dict = {} + if os.path.exists(registry_path): + with open(registry_path) as f: + registry = yaml_load(f) or {} + plat_registry = registry.get("platforms", {}).get(platform_name, {}) + + plat_cores = _platform_cores(config, emu_profiles) if opts.target_cores else None + pack_systems = filter_systems_by_target( + config.get("systems", {}), + emu_profiles, + opts.target_cores, + platform_cores=plat_cores, + ) + build = _ManifestBuild( + platform_name, platforms_dir, db, bios_dir, opts, config, emu_profiles, + zip_contents, data_registry, game_entries, plat_registry.get("install", {}), + os.path.dirname(os.path.dirname(os.path.abspath(__file__))), pack_systems, + ) + build.manifest_profiles = { + name: emu_profiles[name] + for name in _platform_cores(config, emu_profiles) + } + build.manifest_validation = _build_validation_index(build.manifest_profiles) + build.manifest_owners = destination_owners(build.manifest_profiles) + _settle_manifest_scope(build) + return build, full_profiles + + +def _settle_manifest_scope(build: _ManifestBuild) -> None: + """Game data destinations, slot choices and the region selection.""" + opts, config, emu_profiles = build.opts, build.config, build.emu_profiles + # A destination is game data when every declaration of it is: a firmware + # declaration of the same path keeps it in a pack built without game data. + declared_kinds: dict[str, set[bool]] = {} + for system in build.pack_systems.values(): + for fe in system.get("files", []): + bare = sanitize_pack_path(fe.get("destination", fe.get("name", ""))) + if bare: + declared_kinds.setdefault(bare, set()).add(id(fe) in build.game_entries) + build.game_destinations = {d for d, kinds in declared_kinds.items() if kinds == {True}} + + # The full manifest records every core's answer at a contested slot, so a + # target narrowed from it gets what that target's own pack serves. + whole = opts.target_cores is None and opts.source != "platform" and bool(emu_profiles) + # The truth list read inside this run takes the choices already made. + build.slot_choices = ( + opts.known_slot_choices if opts.known_slot_choices is not None and whole + else slots.slot_choices(config, emu_profiles, build.db, build.zip_contents, + build.data_registry) + if whole else {} + ) + # Derived from the same choices: computed twice, it was 8% of a run. + build.slot_overrides = slots.pack_overrides( + config, emu_profiles, build.db, build.zip_contents, build.data_registry, + target_cores=opts.target_cores, source=opts.source, + choices=build.slot_choices if whole else None, + ) + + # Read by the region grouping and by phase 2 with the same arguments. + build.undeclared = ( + undeclared_report( + config, opts.emulators_dir, build.db, emu_profiles, opts.target_cores, + include_all=(opts.source == "truth"), + ) + if opts.source != "platform" + else None + ) + build.region_drops, build.selection, build.builder_held = _manifest_selection( + config, build.pack_systems, opts.emulators_dir, build.db, build.base_dest, + emu_profiles, opts.target_cores, opts.source, + required_only=opts.required_only, regions=opts.regions, + undeclared=build.undeclared, zip_contents=build.zip_contents, + data_registry=build.data_registry, slot_overrides=build.slot_overrides, + one_per_slot=opts.one_per_slot, + ) + + def generate_manifest( platform_name: str, platforms_dir: str, db: dict, bios_dir: str, registry_path: str, - emulators_dir: str = "emulators", - zip_contents: dict | None = None, - emu_profiles: dict | None = None, - target_cores: set[str] | None = None, - source: str = "full", - regions: list[str] | None = None, - target_name: str | None = None, - offline: bool | None = None, - required_only: bool = False, - data_registry: dict | None = None, - fold_case: bool | None = None, - no_game_data: bool = False, - one_per_slot: bool = False, - known_slot_choices: dict | None = None, - build_out: dict | None = None, + **options: Unpack[ManifestOptions], ) -> dict: """Generate a JSON manifest for a platform (same resolution as generate_pack). @@ -4898,520 +6013,57 @@ def generate_manifest( --standalone read. `build_out` receives the slot choices and the profiles this build read, which that second build takes over. """ - config = load_platform_config(platform_name, platforms_dir) - if fold_case is not None: - config = {**config, "case_insensitive_fs": fold_case} - if target_name: - config = {**config, TARGET_KEY: target_name} - if zip_contents is None: - zip_contents = {} - if emu_profiles is None: - emu_profiles = load_emulator_profiles(emulators_dir) - if data_registry is None: - data_registry = load_data_dir_registry(platforms_dir) - full_profiles = emu_profiles - game_entries: set[int] = set() - if no_game_data: - cut = without_game_data(config, emu_profiles, data_registry, target_cores) - config, emu_profiles = cut.config, cut.profiles - else: - game_entries = platform_game_data(config, emu_profiles, data_registry) - - platform_display = config.get("platform", platform_name) - base_dest = config.get("base_destination", "") - case_insensitive = config.get("case_insensitive_fs", False) - verification_mode = config.get("verification_mode", "existence") - manifest_profiles = { - name: emu_profiles[name] - for name in _platform_cores(config, emu_profiles) - } - manifest_validation = _build_validation_index(manifest_profiles) - manifest_owners = destination_owners(manifest_profiles) - - # Load registry for install metadata - registry: dict = {} - if os.path.exists(registry_path): - with open(registry_path) as f: - registry = yaml_load(f) or {} - plat_registry = registry.get("platforms", {}).get(platform_name, {}) - install_section = plat_registry.get("install", {}) - detect = install_section.get("detect", []) - standalone_copies = install_section.get("standalone_copies", []) - - repo_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - - plat_cores = _platform_cores(config, emu_profiles) if target_cores else None - pack_systems = filter_systems_by_target( - config.get("systems", {}), - emu_profiles, - target_cores, - platform_cores=plat_cores, + _reject_unknown("generate_manifest", options, ManifestOptions.__optional_keys__) + opts = _ManifestSettings(**options) + build, full_profiles = _start_manifest( + platform_name, platforms_dir, db, bios_dir, registry_path, opts ) + config, verification_mode = build.config, build.verification_mode - # A destination is game data when every declaration of it is: a firmware - # declaration of the same path keeps it in a pack built without game data. - declared_kinds: dict[str, set[bool]] = {} - for system in pack_systems.values(): - for fe in system.get("files", []): - bare = sanitize_pack_path(fe.get("destination", fe.get("name", ""))) - if bare: - declared_kinds.setdefault(bare, set()).add(id(fe) in game_entries) - game_destinations = {d for d, kinds in declared_kinds.items() if kinds == {True}} - - seen_destinations: set[str] = set() - # Destination -> (entry, full destination, manifest destination) of each - # platform file, for the core claims a target build would fall back on. - platform_entries: dict[str, tuple[dict, str, str]] = {} - # Full destination -> the platform systems declaring it. A file's - # `systems` also names the systems whose cores read it there. A pack built - # for a target keeps the file only while a declaring system stays. - declared_by: dict[str, set[str]] = {} - seen_lower: set[str] = set() - seen_parents: set[str] = set() - manifest_files: list[dict] = [] - omitted_by_destination: dict[str, dict] = {} - total_size = 0 - # Sizes of the files the pack carries and the installer cannot fetch: the - # collection does not index them, a data directory cache answered. - pack_only_sizes: list[int] = [] - # Platform entries resolved into a data directory cache: destination -> - # (cache file, size). Settled once the trees are walked. - cache_held: dict[str, tuple[str, int]] = {} - rename_notes: set[str] = set() - # The full manifest records every core's answer at a contested slot, so a - # target narrowed from it gets what that target's own pack serves. - whole = target_cores is None and source != "platform" and bool(emu_profiles) - # The truth list read inside this run takes the choices already made. - slot_choices = ( - known_slot_choices if known_slot_choices is not None and whole - else slots.slot_choices(config, emu_profiles, db, zip_contents, data_registry) - if whole else {} - ) - # Derived from the same choices: computed twice, it was 8% of a run. - slot_overrides = slots.pack_overrides( - config, emu_profiles, db, zip_contents, data_registry, - target_cores=target_cores, source=source, - choices=slot_choices if whole else None, - ) - - def manifest_destination(full_destination: str) -> str: - if base_dest and full_destination.startswith(f"{base_dest}/"): - return full_destination[len(base_dest) + 1:] - return full_destination - - def record_omission( - full_destination: str, - file_entry: dict, - system_id: str, - reason: str, - cores: list[str] | None, - ) -> None: - # A core extra (cores given) is keyed as the region pass keys it. - bare = ( - sanitize_pack_path(file_entry.get("destination", file_entry.get("name", ""))) - if cores is None else extra_region_key(file_entry, declared_root(config)) - ) - save_root = file_entry.get("root") == "save" - # Read in the emulator's own folder, outside the BIOS tree: dest is - # the path the emulator reads, relative to that folder. - outside = bool(file_entry.get("unplaced")) - if outside: - dest_fields: dict = {"dest": sanitize_pack_path(file_entry.get("path") or ""), - "root": "emulator"} - elif file_entry.get("root") in OUTER_ROOTS: - dest_fields = {"dest": sanitize_pack_path( - file_entry.get("destination") or file_entry.get("path") or "" - ), "root": file_entry["root"]} - elif save_root: - dest_fields = {"dest": save_manifest_dest(file_entry), **save_entry_fields(file_entry)} - else: - dest_fields = {"dest": manifest_destination(full_destination)} - omitted_by_destination[full_destination] = { - **dest_fields, - "name": str(file_entry.get("name") or ""), - "system": system_id, - "required": bool(file_entry.get("required", True)), - "reason": reason, - "cores": cores, - # An omission keeps its region groups, as the builder groups it: - # a requested file the collection lacks answers the request - # without displacing a held fallback (resolve_region_drops, - # held). One the builder holds and the installer cannot fetch - # does displace it in the pack, so it says so, and the installer - # drops the same fallback. - **selection.fields(bare, str(file_entry.get("name") or "")), - **({"held": True} if bare in builder_held else {}), - **( - {"category": GAME_DATA} - if (bare in game_destinations if cores is None else is_game_data(file_entry)) - else {} - ), - } - - # Read by the region grouping and by phase 2 with the same arguments. - undeclared = ( - undeclared_report( - config, emulators_dir, db, emu_profiles, target_cores, - include_all=(source == "truth"), - ) - if source != "platform" - else None - ) - region_drops, selection, builder_held = _manifest_selection( - config, pack_systems, emulators_dir, db, base_dest, emu_profiles, - target_cores, source, required_only, regions, undeclared=undeclared, - zip_contents=zip_contents, data_registry=data_registry, - slot_overrides=slot_overrides, one_per_slot=one_per_slot, - ) - - # Phase 1: baseline files - if source != "truth": - # The same destination can be declared by more than one system, bare - # in one and hash-constrained in another. The builder lets the - # constrained sibling claim it. Without the same rule here the - # manifest named whatever answered to the name, so the pack and the - # installer disagreed about which file a destination means. - # Decided among the declarations this manifest keeps, as the pack - # does. A manifest carries no data directories. - preferred_entries = _preferred_entries( - pack_systems, db, bios_dir, base_dest, required_only, - zip_contents, offline, - ) - declarations: dict[str, dict[str, list[dict]]] = {} - for sys_id, system in sorted(pack_systems.items()): - for file_entry in system.get("files", []): - if required_only and file_entry.get("required") is False: - continue - dest = sanitize_pack_path(file_entry.get("destination", file_entry["name"])) - if dest and not (region_drops and dest in region_drops): - full_dest = f"{base_dest}/{dest}" if base_dest else dest - declarations.setdefault(full_dest, {}).setdefault(sys_id, []).append( - file_entry - ) - - def declaration_order(full_dest: str) -> list[tuple[list[str], dict, str]]: - """The declarations answering a destination as its systems drop. - - A pack built for a target loses the systems no available core - reads, and the next declaration takes the destination by the - same rule: RetroArch's bare nintendo-ds dsi_bios9.bin once the - pinned nintendo-dsi one is gone. - """ - remaining = dict(declarations.get(full_dest, {})) - order: list[tuple[list[str], dict, str]] = [] - while remaining: - winner = _preferred_entries( - {sid: {"files": fes} for sid, fes in remaining.items()}, - db, bios_dir, base_dest, required_only, zip_contents, offline, - ).get(full_dest) - first_sys, first = next( - (sid, fe) for sid, fes in sorted(remaining.items()) for fe in fes - if winner is None or id(fe) == winner - ) - systems = sorted( - sid for sid, fes in remaining.items() - if any(fe is first for fe in fes) - ) - order.append((systems, first, first_sys)) - for sid in systems: - del remaining[sid] - return order - - def settle( - file_entry: dict, sys_id: str, dest: str, override: str | None - ) -> tuple[str | None, str]: - """The file served once the slot is settled, or None and why not.""" - local_path, status = resolve_declaration( - file_entry, - db, - bios_dir, - zip_contents, - data_registry, - override, - offline=offline, - ) - if status == "not_found" and not reads_file_contents(verification_mode): - found = agnostic_substitute(file_entry, sys_id, db, manifest_profiles) - if found: - local_path, status = found[0], "agnostic_fallback" - if os.path.basename(local_path) != file_entry.get("name", ""): - # The pack explains the rename in a note of its own. - rename_notes.add(file_entry.get("name", "")) - # The builder's verdict: a hash platform omits what its frontend - # would reject, a zipped_file archive included when the member is - # missing whatever the outer resolution said. An existence - # platform never reads the bytes. - status, refusal = baseline_verdict( - file_entry, local_path, status, verification_mode, - config.get("hash_rule", ""), - ) - if refusal or status in ("not_found", "external"): - return None, "hash_mismatch" if refusal else status - # The variant the pack ships when an emulator rejects the - # resolved file: the installer must place the same bytes. - local_path, _disagreement = validated_choice( - file_entry, local_path, db, manifest_validation, bios_dir, - digest_algorithm(verification_mode), dest, manifest_owners, - ) - return local_path, status - - for sys_id, system in sorted(pack_systems.items()): - for file_entry in system.get("files", []): - if required_only and file_entry.get("required") is False: - continue - dest = sanitize_pack_path(file_entry.get("destination", file_entry["name"])) - if not dest: - continue - if region_drops and dest in region_drops: - continue - full_dest = f"{base_dest}/{dest}" if base_dest else dest - declared_by.setdefault(full_dest, set()).add(sys_id) - - preferred = preferred_entries.get(full_dest) - if preferred is not None and id(file_entry) != preferred: - continue - - dedup_key = full_dest - if dedup_key in seen_destinations: - continue - if case_insensitive and dedup_key.lower() in seen_lower: - continue - if _has_path_conflict(full_dest, seen_destinations, seen_parents): - continue - - storage = file_entry.get("storage", "embedded") - if storage == "user_provided": - record_omission( - full_dest, - file_entry, - sys_id, - "user_provided", - None, - ) - # The pack writes the instructions in the slot's place - # and holds it: one more member than the files listed. - instructions = file_entry.get( - "instructions", "Please provide this file manually." - ) - pack_only_sizes.append(len( - f"File needed: {file_entry['name']}\n\n{instructions}\n".encode() - )) - seen_destinations.add(dedup_key) - _register_path(dedup_key, seen_destinations, seen_parents) - if case_insensitive: - seen_lower.add(dedup_key.lower()) - continue - - local_path, status = settle( - file_entry, sys_id, dest, slot_overrides.get(full_dest) - ) - if local_path is None: - record_omission(full_dest, file_entry, sys_id, status, None) - continue - - entry, file_size = _manifest_entry( - local_path, dest, None, db, repo_root, - selection.fields(dest, file_entry["name"]), - ) - if entry is not None and dest in game_destinations: - entry["category"] = GAME_DATA - if entry is not None and target_cores is None: - order = declaration_order(full_dest) - if full_dest in slot_choices or len(order) > 1: - _record_slot_choices( - entry, slot_choices.get(full_dest, []), order, - settle, dest, db, repo_root, - ) - if entry is None: - record_omission(full_dest, file_entry, sys_id, "not_found", None) - if file_size and full_dest not in seen_destinations: - if _in_data_cache(local_path, data_registry): - cache_held[full_dest] = (local_path, file_size) - else: - pack_only_sizes.append(file_size) - seen_destinations.add(dedup_key) - _register_path(dedup_key, seen_destinations, seen_parents) - if case_insensitive: - seen_lower.add(dedup_key.lower()) - continue - - manifest_files.append(entry) - platform_entries[dedup_key] = (entry, full_dest, dest) - omitted_by_destination.pop(full_dest, None) - total_size += file_size - seen_destinations.add(dedup_key) - _register_path(dedup_key, seen_destinations, seen_parents) - if case_insensitive: - seen_lower.add(dedup_key.lower()) - - # Phase 2: core complement (emulator extras) - unplaceable: list[dict] = [] - baseline_claims: dict[str, dict] = {} - absent: list[dict] = [] - if source != "platform": - core_files = _collect_emulator_extras( - config, - emulators_dir, + def settle( + file_entry: dict, sys_id: str, dest: str, override: str | None + ) -> tuple[str | None, str]: + """The file served once the slot is settled, or None and why not.""" + local_path, status = resolve_declaration( + file_entry, db, - seen_destinations, - base_dest, - emu_profiles, - target_cores=target_cores, - include_all=(source == "truth"), - unplaceable=unplaceable, - baseline_claims=baseline_claims, - absent=absent, - undeclared=undeclared, + bios_dir, + build.zip_contents, + build.data_registry, + override, + offline=opts.offline, ) - else: - core_files = [] - _record_unplaceable(unplaceable, record_omission, config, emu_profiles) - _record_absent( - absent, config, base_dest, region_drops, required_only, - seen_destinations, omitted_by_destination, record_omission, - ) - total_size += _manifest_core_entries( - core_files, config, db, bios_dir, base_dest, repo_root, - zip_contents, offline, region_drops, case_insensitive, - seen_destinations, seen_lower, seen_parents, manifest_files, - omitted_by_destination, record_omission, pack_only_sizes, selection, - required_only, manifest_validation, manifest_owners, data_registry, - emu_profiles, - ) - for key, (entry, full_dest, dest) in platform_entries.items(): - declared = declared_by.get(full_dest, set()) - if declared and declared != set(entry.get("systems") or []): - entry["declared_in"] = sorted(declared) - claims = baseline_claims.get(key, {}).get("other_claims") - if claims and any(not is_game_data(claim) for claim in claims): - # A core reads this path as firmware: a pack without game data - # still fills it. - entry.pop("category", None) - if claims: - _attach_claims( - entry, claims, db, bios_dir, zip_contents, offline, required_only, - manifest_validation, manifest_owners, dest, dest, repo_root, - data_registry, - ) - # A platform omission narrows like a platform file: by the systems - # declaring it, which can be fewer than the ones whose cores read it. - for full_dest, omission in omitted_by_destination.items(): - declared = declared_by.get(full_dest, set()) - if ( - omission.get("cores") is None - and declared - and declared != set(omission.get("systems") or []) - ): - omission["declared_in"] = sorted(declared) + if status == "not_found" and not reads_file_contents(verification_mode): + found = agnostic_substitute(file_entry, sys_id, db, build.manifest_profiles) + if found: + local_path, status = found[0], "agnostic_fallback" + if os.path.basename(local_path) != file_entry.get("name", ""): + # The pack explains the rename in a note of its own. + build.rename_notes.add(file_entry.get("name", "")) + # The builder's verdict: a hash platform omits what its frontend + # would reject, a zipped_file archive included when the member is + # missing whatever the outer resolution said. An existence + # platform never reads the bytes. + status, refusal = baseline_verdict( + file_entry, local_path, status, verification_mode, + config.get("hash_rule", ""), + ) + if refusal or status in ("not_found", "external"): + return None, "hash_mismatch" if refusal else status + # The variant the pack ships when an emulator rejects the + # resolved file: the installer must place the same bytes. + local_path, _disagreement = validated_choice( + file_entry, local_path, db, build.manifest_validation, bios_dir, + digest_algorithm(verification_mode), dest, build.manifest_owners, + ) + return local_path, status - # Phase 3: data directories. They are not files of the collection: the - # installer fetches each from its source archive and keeps the members - # whose hash the list below records, the same tree the pack carries. - data_refs: dict[str, tuple[str, tuple[str, ...], str, bool]] = {} - reclaim = {full: path for full, (path, _size) in cache_held.items()} - core_trees = ([] if source == "platform" - else core_data_trees(config, emu_profiles, platform_name, data_registry, - target_cores)) - data_members = list(_data_directory_members( - pack_systems, data_registry, platform_name, base_dest, - case_insensitive, seen_destinations, seen_lower, seen_parents, - data_refs, required_only=required_only, - core_trees=core_trees, - reclaim=reclaim, - )) - # A platform entry found in a tree the pack carries is that tree's member: - # the installer rebuilds it from the tree's archive. One found in a tree - # the pack does not carry stays omitted, held by the pack alone. - for full_dest, (_path, size) in cache_held.items(): - if full_dest in reclaim: - pack_only_sizes.append(size) - else: - omitted_by_destination.pop(full_dest, None) - data_sizes = pack_only_sizes + [os.path.getsize(src) for src, _dest in data_members] - data_directories = _manifest_data_directories( - data_members, data_refs, data_registry or {}, base_dest, - tree_readers(pack_systems, core_trees), pack_systems, - ) - - # The pack is unzipped into the BIOS folder: an entry the installer puts - # in the roms, emulators or configs folder is in its list, not in the ZIP. - outer = [e for e in manifest_files if e.get("root") in OUTER_ROOTS] - now = _build_timestamp(db) - core_names, system_cores = _manifest_core_index( - config, emu_profiles, manifest_files, omitted_by_destination.values() - ) - - result: dict = { - "manifest_version": 2, - "source": source, - "regions": list(regions or []), - # A target-filtered manifest is only distinguishable from the full one - # by its filename, so it records the filter the way regions does. - # Absent on an unfiltered manifest rather than empty, to leave the - # twelve default files byte-identical. - **({"target": target_name} if target_name else {}), - # Every game data file below says so: the installer's --no-game-data - # relies on it, and refuses a list that does not record categories. - "categorized": True, - # The entries a slot decides carry `slot`, which --one-per-slot - # reads: without this mark the installer refuses it. - "slot_fields": True, - **({"no_game_data": True} if no_game_data else {}), - "platform": platform_name, - "display_name": platform_display, - "version": platform_list_version(config, platform_name), - "generated": now, - "base_destination": base_dest, - "detect": detect, - "standalone_copies": _standalone_copies( - standalone_copies, manifest_profiles, manifest_files, - ), - "total_files": len(manifest_files), - "total_size": total_size, - "pack_files": ( - len(manifest_files) - len(outer) + len(data_sizes) + len(PACK_DOCUMENTS) - + len(rename_notes) + len(pack_markers(platform_name, target_name, platforms_dir)) - ), - "pack_size": total_size - sum(int(e.get("size") or 0) for e in outer) + sum(data_sizes), - "total_omitted": len(omitted_by_destination), - "omitted_files": sorted( - omitted_by_destination.values(), key=lambda entry: entry["dest"] - ), - "core_names": core_names, - **({"downloaded_cores": downloaded} if (downloaded := _downloaded_cores( - config, emu_profiles, core_names, platforms_dir, - )) else {}), - **({"offered_boards": offered} if (offered := _offered_boards( - config, emu_profiles, core_names, - [*manifest_files, *omitted_by_destination.values()], - )) else {}), - "system_cores": system_cores, - # The names the platform itself gives its systems (Batocera's psx), - # which --system accepts beside the ids. - "system_names": platform_system_names(config, list(pack_systems)), - "system_titles": system_titles(manifest_files, omitted_by_destination.values()), - "files": manifest_files, - } - if data_directories: - result["data_directories"] = data_directories - for root in OUTER_ROOTS: - layout = outer_layout(config, root) or {} - if layout and any(entry.get("root") == root - for entry in [*manifest_files, *result["omitted_files"]]): - # Where the installer finds the folder: relative to the folder - # holding the BIOS folder, which the platform names bios_folder. - result[f"{root}_directory"] = { - key: layout[key] for key in MANIFEST_OUTER_FIELDS if key in layout - } - saved = [entry for entry in manifest_files if entry.get("root") == "save"] - if saved: - layout = save_layout(config) - result["save_directory"] = {"folder": layout.folder, "by_core": layout.by_core} - if layout.by_system: - # The launcher writes the directory at each launch, one folder - # per system: each dest starts with that folder. - result["save_directory"]["by_system"] = True - if case_insensitive and fold_case is None and any( + if opts.source != "truth": + build.list_baseline(settle) + build.list_core_files() + result = build.result(build.list_data_directories()) + detect = build.install_section.get("detect", []) + if build.case_insensitive and opts.fold_case is None and any( d.get("os") not in CASE_FOLDING_OS for d in detect ): # RetroArch also runs where case is kept: there Mesen-CE opens @@ -5421,11 +6073,13 @@ def generate_manifest( # or macOS too, stays folded. kept = generate_manifest( platform_name, platforms_dir, db, bios_dir, registry_path, - emulators_dir, zip_contents, full_profiles, target_cores, source, - regions, target_name, offline, required_only, data_registry, - fold_case=False, no_game_data=no_game_data, + emulators_dir=opts.emulators_dir, zip_contents=build.zip_contents, + emu_profiles=full_profiles, target_cores=opts.target_cores, source=opts.source, + regions=opts.regions, target_name=opts.target_name, offline=opts.offline, + required_only=opts.required_only, data_registry=build.data_registry, + fold_case=False, no_game_data=opts.no_game_data, ) - folded = {entry["dest"] for entry in manifest_files} + folded = {entry["dest"] for entry in build.manifest_files} folded_lower = {dest.lower() for dest in folded} variants = [ entry for entry in kept["files"] @@ -5433,8 +6087,8 @@ def generate_manifest( ] if variants: result["case_variants"] = variants - if build_out is not None: - build_out.update(slot_choices=slot_choices, profiles=manifest_profiles) + if opts.build_out is not None: + opts.build_out.update(slot_choices=build.slot_choices, profiles=build.manifest_profiles) return result @@ -5445,7 +6099,7 @@ def generate_install_list( bios_dir: str, registry_path: str, standalone_out: dict | None = None, - **options: Any, + **options: Unpack[InstallListOptions], ) -> dict: """The list install.py reads: the full manifest, the files and omissions the --source truth build holds apart from it, and the emulators with a @@ -5457,22 +6111,23 @@ def generate_install_list( A manifest narrowed by a target, a case fold or --one-per-slot is no list install.py reads by default and gets neither. """ + settings = _ManifestSettings(**options) built: dict = {} result = generate_manifest(platform_name, platforms_dir, db, bios_dir, registry_path, build_out=built, **options) - if (options.get("source", "full") != "full" or options.get("target_cores") is not None - or options.get("fold_case") is not None or options.get("one_per_slot")): + if (settings.source != "full" or settings.target_cores is not None + or settings.fold_case is not None or settings.one_per_slot): return result + truth_options: InstallListOptions = {**options, "source": "truth"} with contextlib.redirect_stdout(io.StringIO()): truth = generate_manifest( platform_name, platforms_dir, db, bios_dir, registry_path, - **{**options, "source": "truth"}, known_slot_choices=built["slot_choices"], + **truth_options, known_slot_choices=built["slot_choices"], ) _mark_truth(result, truth) layouts = _standalone_layouts( - built["profiles"], options.get("emulators_dir", "emulators"), db, bios_dir, - options.get("zip_contents") or {}, options.get("offline"), platforms_dir, - os.path.dirname(os.path.dirname(os.path.abspath(__file__))), result, + built["profiles"], settings.emulators_dir, db, bios_dir, + settings.zip_contents or {}, settings.offline, platforms_dir, result, ) if layouts: result["standalone_builds"] = sorted(layouts) @@ -5539,7 +6194,6 @@ def _standalone_layouts( zip_contents: dict, offline: bool | None, platforms_dir: str, - repo_root: str, result: dict, ) -> dict[str, list]: """Per emulator with a standalone build, the files `--emulator X @@ -5550,6 +6204,7 @@ def _standalone_layouts( that entry. Another is written whole. A data tree member is left to the list's data directories. """ + repo_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) known = { copy["sha1"] for entry in [*result["files"], *result.get("truth_files", [])] @@ -5596,6 +6251,45 @@ def tree_readers(pack_systems: dict, core_trees: Iterable[dict]) -> dict[str, li return {ref: sorted(names) for ref, names in cores.items() if ref not in declared} +def _tree_group( + ref: str, prefix: str, entry: dict, base_dest: str, versions: dict[str, dict[str, dict]], +) -> dict: + """The manifest record of one data directory, before its files. + + ``versions`` caches each .versions.json read, by path. + """ + version = str(entry.get("version", "master")) + dest = prefix + if base_dest and (dest == base_dest or dest.startswith(base_dest + "/")): + dest = dest[len(base_dest):].lstrip("/") + group: dict = { + "ref": ref, + "dest": dest, + "url": str(entry.get("source_url", "")).format(version=version), + "type": "zip" if entry.get("source_type") == "zip" else "tarball", + "systems": [], + "files": {}, + } + if group["type"] == "zip": + group["strip_components"] = int(entry.get("strip_components", 0)) + else: + group["source_path"] = str(entry.get("source_path", "")).format(version=version) + if entry.get("exclude"): + group["exclude"] = [str(e) for e in entry["exclude"]] + cache = entry.get("local_cache") + if cache: + recorded = Path(cache).parent / ".versions.json" + if str(recorded) not in versions: + try: + versions[str(recorded)] = json.loads(recorded.read_text()) + except FileNotFoundError: + versions[str(recorded)] = {} + size = versions[str(recorded)].get(ref, {}).get("archive_size") + if isinstance(size, int) and size > 0: + group["archive_size"] = size + return group + + def _manifest_data_directories( members: list[tuple[str, str]], refs: dict[str, tuple[str, tuple[str, ...], str, bool]], @@ -5633,39 +6327,12 @@ def _manifest_data_directories( ref, systems, prefix, needed = refs[full] if platform_systems is not None: systems = tuple(own for sid in systems for own in by_norm.get(_norm_system_id(sid), ())) - entry = registry.get(ref, {}) key = (ref, prefix) group = grouped.get(key) if group is None: - version = str(entry.get("version", "master")) - dest = prefix - if base_dest and (dest == base_dest or dest.startswith(base_dest + "/")): - dest = dest[len(base_dest):].lstrip("/") - group = grouped[key] = { - "ref": ref, - "dest": dest, - "url": str(entry.get("source_url", "")).format(version=version), - "type": "zip" if entry.get("source_type") == "zip" else "tarball", - "systems": [], - "files": {}, - } - if group["type"] == "zip": - group["strip_components"] = int(entry.get("strip_components", 0)) - else: - group["source_path"] = str(entry.get("source_path", "")).format(version=version) - if entry.get("exclude"): - group["exclude"] = [str(e) for e in entry["exclude"]] - cache = entry.get("local_cache") - if cache: - recorded = Path(cache).parent / ".versions.json" - if str(recorded) not in versions: - try: - versions[str(recorded)] = json.loads(recorded.read_text()) - except FileNotFoundError: - versions[str(recorded)] = {} - size = versions[str(recorded)].get(ref, {}).get("archive_size") - if isinstance(size, int) and size > 0: - group["archive_size"] = size + group = grouped[key] = _tree_group( + ref, prefix, registry.get(ref, {}), base_dest, versions + ) if needed: group["needed"] = True group["systems"].extend(s for s in systems if s not in group["systems"]) @@ -6006,23 +6673,18 @@ def _content_narrowing_tags() -> tuple[str, ...]: Region and target are absent on purpose: both are handed to the check itself, which narrows its expectation instead of skipping it. """ + # (source, one_per_slot, required_only), one dimension at a time. dimensions = ( - {"source": "platform"}, - {"source": "truth"}, - {"one_per_slot": True}, - {"required_only": True}, + ("platform", False, False), + ("truth", False, False), + ("full", True, False), + ("full", False, True), ) tags: list[str] = [] - for dimension in dimensions: + for source, one_per_slot, required_only in dimensions: tags.extend( tag - for tag, _label in _narrowings( - dimension.get("source", "full"), - None, - None, - dimension.get("one_per_slot", False), - dimension.get("required_only", False), - ) + for tag, _label in _narrowings(source, None, None, one_per_slot, required_only) ) return tuple(tags) @@ -6087,18 +6749,79 @@ def _pack_archives(output_dir: str) -> list[str]: _PACK_NAME = re.compile(r"_BIOS_Pack(?:_[^/]+)?\.zip$") +class FinalizeOptions(TypedDict, total=False): + """The keyword options of verify_and_finalize_packs.""" + + platforms_dir: str + skip_conformance: bool + data_registry: dict | None + regions: list[str] | None + target_name: str | None + emulators_dir: str + no_game_data: bool + pack_systems_by_name: dict[str, dict] | None + built: Iterable[str] | None + + +@dataclass(frozen=True) +class _FinalizeSettings: + """FinalizeOptions with their defaults.""" + + platforms_dir: str = "platforms" + skip_conformance: bool = False + data_registry: dict | None = None + regions: list[str] | None = None + target_name: str | None = None + emulators_dir: str = "emulators" + no_game_data: bool = False + pack_systems_by_name: dict[str, dict] | None = None + built: Iterable[str] | None = None + + +def _conforms(zip_path: str, pname: str, db: dict, opts: _FinalizeSettings) -> bool: + """Stage 2 for one platform of a pack: extract and verify against its + config. Prints the platform's line and its errors.""" + ( + p_ok, + _total, + _matched, + p_errors, + bl_checked, + bl_present, + core_checked, + core_present, + bl_excluded, + bl_absent, + ) = verify_pack_against_platform( + zip_path, + pname, + opts.platforms_dir, + db=db, + emulators_dir=opts.emulators_dir, + regions=opts.regions, + data_registry=opts.data_registry, + target_cores=_target_cores_for(pname, opts.target_name, opts.platforms_dir), + no_game_data=opts.no_game_data, + ) + status = "OK" if p_ok else "FAILED" + exclusion_note = "".join( + f", {part}" for part in _baseline_shortfall(bl_excluded, bl_absent) + ) + print( + f" platform {pname}: {bl_present}/{bl_checked} baseline present" + f"{exclusion_note}, " + f"{core_present}/{core_checked} cores present, {status}" + ) + if not p_ok: + for err in p_errors: + print(f" {err}") + return p_ok + + def verify_and_finalize_packs( output_dir: str, db: dict, - platforms_dir: str = "platforms", - skip_conformance: bool = False, - data_registry: dict | None = None, - regions: list[str] | None = None, - target_name: str | None = None, - emulators_dir: str = "emulators", - no_game_data: bool = False, - pack_systems_by_name: dict[str, dict] | None = None, - built: Iterable[str] | None = None, + **options: Unpack[FinalizeOptions], ) -> bool: """Verify all packs, inject manifests, generate SHA256SUMS. @@ -6116,19 +6839,22 @@ def verify_and_finalize_packs( Returns True if all packs pass verification. """ + _reject_unknown("verify_and_finalize_packs", options, FinalizeOptions.__optional_keys__) + opts = _FinalizeSettings(**options) all_ok = True # Map ZIP names to platform names # By the exact name this run's reduction gives a platform's pack, never by # substring: a _Custom or regional pack left by another run was judged # against this run's expectation and failed it. - registered = list_registered_platforms(platforms_dir) + registered = list_registered_platforms(opts.platforms_dir) expected = _expected_pack_names( - registered, platforms_dir, regions, target_name, no_game_data=no_game_data + registered, opts.platforms_dir, opts.regions, opts.target_name, + no_game_data=opts.no_game_data, ) packs = _pack_archives(output_dir) - if built is not None: - packs = [name for name in packs if name in set(built)] + if opts.built is not None: + packs = [name for name in packs if name in set(opts.built)] pack_to_platform: dict[str, list[str]] = {} for name in packs: for pname in registered: @@ -6140,7 +6866,7 @@ def verify_and_finalize_packs( zip_path = os.path.join(output_dir, name) # Stage 1: database integrity - ok, manifest = verify_pack(zip_path, db, data_registry=data_registry) + ok, manifest = verify_pack(zip_path, db, data_registry=opts.data_registry) summary = manifest["summary"] status = "OK" if ok else "ERRORS" print( @@ -6152,51 +6878,17 @@ def verify_and_finalize_packs( print(f" ERROR: {err}") all_ok = False failing.append(name) - annotate_pack_systems(manifest, (pack_systems_by_name or {}).get(name)) + annotate_pack_systems(manifest, (opts.pack_systems_by_name or {}).get(name)) inject_manifest(zip_path, manifest) # Stage 2: platform conformance (extract + verify) # Skipped for filtered/split/custom packs (intentionally partial) - if skip_conformance: + if opts.skip_conformance: continue if _narrows_contents(name): continue - platforms = pack_to_platform.get(name, []) - for pname in platforms: - ( - p_ok, - total, - matched, - p_errors, - bl_checked, - bl_present, - core_checked, - core_present, - bl_excluded, - bl_absent, - ) = verify_pack_against_platform( - zip_path, - pname, - platforms_dir, - db=db, - emulators_dir=emulators_dir, - regions=regions, - data_registry=data_registry, - target_cores=_target_cores_for(pname, target_name, platforms_dir), - no_game_data=no_game_data, - ) - status = "OK" if p_ok else "FAILED" - exclusion_note = "".join( - f", {part}" for part in _baseline_shortfall(bl_excluded, bl_absent) - ) - print( - f" platform {pname}: {bl_present}/{bl_checked} baseline present" - f"{exclusion_note}, " - f"{core_present}/{core_checked} cores present, {status}" - ) - if not p_ok: - for err in p_errors: - print(f" {err}") + for pname in pack_to_platform.get(name, []): + if not _conforms(zip_path, pname, db, opts): all_ok = False failing.append(f"{name} ({pname})") @@ -6206,71 +6898,6 @@ def verify_and_finalize_packs( print(f"ERROR: packs failed verification: {', '.join(failing)}", file=sys.stderr) return all_ok -from packpaths import ( # noqa: E402,F401 - MEDIA_MARKER, - lands_on_android, - _archive_destination, - _path_parents, - _has_path_conflict, - _register_path, - _flat, - _resolve_destination, - placement_block, - save_layout, -) -from packextras import ( # noqa: E402,F401 - ExtraResolver, - extra_full_destination, - extra_region_key, - prefixed_destination, - save_entry_fields, - save_manifest_dest, - _detect_extras_prefix, - _detect_slug_structure, - _map_emulator_to_slug, - _collect_emulator_extras, - _extra_system_ids, - extras_for_systems as _extras_for_systems, - platform_region_groups, - emulator_region_drops, - emulator_required_spares, - _emulator_region_group, - expand_agnostic_entries, - undeclared_report, -) - - -from packreadme import ( # noqa: E402 - _build_readme, - _build_agnostic_rename_readme, - save_place, -) -from packverify import ( # noqa: E402,F401 - _check_member_hash, - _hash_matches, - _repo_satisfies_declaration, - _intentional_hash_exclusion, - verify_pack_against_platform, - verify_pack, - generate_sha256sums, -) - - -# _OFFLINE is deliberately not re-exported: importing a value copies it, so a -# facade name would keep saying False after set_offline() changed the flag. -from packresolve import ( # noqa: E402,F401 - ACCEPTED_HASHES, - _detect_hash_type, - sha1_for_hash, - preferred_entries as _preferred_entries, - parse_hash_input, - parse_hash_file, - lookup_hashes, - resolve_declaration, - resolve_file, - download_external, -) - if __name__ == "__main__": from common import run_cli From de6b01eb7311500b31e58446b30edaaed67de298 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Tue, 17 Nov 2026 07:56:08 +0100 Subject: [PATCH 2/9] refactor: split pack verification by stage --- scripts/packverify.py | 847 +++++++++++++++++++++++++----------------- 1 file changed, 509 insertions(+), 338 deletions(-) diff --git a/scripts/packverify.py b/scripts/packverify.py index e42ab4c32..3f7e191cb 100644 --- a/scripts/packverify.py +++ b/scripts/packverify.py @@ -7,6 +7,8 @@ coverage report that disagree describe different collections.""" from __future__ import annotations from collections import Counter +from dataclasses import dataclass, field +from typing import TYPE_CHECKING, TypedDict from auto_fetch import DEFAULT_BIOS_DIR from artifacts import _build_timestamp @@ -37,7 +39,14 @@ from gamedata import without_game_data from common import resolve_platform_cores from common import sanitize_pack_path import zipfile -from collections.abc import Callable +from collections.abc import Callable, Mapping + +# Imported at run time where typing has it: a test reads the signature of +# verify_pack_against_platform with its annotations evaluated. +if sys.version_info >= (3, 11): + from typing import Unpack +elif TYPE_CHECKING: + from typing_extensions import Unpack @functools.cache @@ -121,6 +130,100 @@ def _same_members(pack: zipfile.ZipFile, name: str, source: str) -> bool: return False +def _data_indexes(data_registry: dict | None) -> tuple[dict[str, list[str]], dict[str, str]]: + """The cached data directory files by name and by relative path.""" + _data_index: dict[str, list[str]] = {} + _data_path_index: dict[str, str] = {} + if data_registry: + for _de in data_registry.values(): + cache = _de.get("local_cache", "") + if not cache or not os.path.isdir(cache): + continue + for _r, _d, _fns in os.walk(cache): + for _fn in _fns: + _fp = os.path.join(_r, _fn) + _rel = os.path.relpath(_fp, cache) + _data_path_index[_rel] = _fp + _data_index.setdefault(_fn, []).append(_fp) + return _data_index, _data_path_index + + +def _is_pack_note(name: str) -> bool: + """Whether a member is a note the builder composes, not a collected file.""" + # The builder writes its notes beside the slot they explain + # (bios/INSTRUCTIONS_x.txt, games/keen6/INSTRUCTIONS_y.txt), so + # they are told by their own name, not by a root prefix. + base = name.rsplit("/", 1)[-1] + return base.startswith(("INSTRUCTIONS_", "RENAMED_")) or base == MEDIA_MARKER or name in ( + "manifest.json", + "README.txt", + ) + + +def _member_digests(zf: zipfile.ZipFile, info: zipfile.ZipInfo) -> tuple[str, str, int]: + """(sha1, md5, size) of a pack member.""" + with zf.open(info) as f: + sha1_h = hashlib.sha1() + md5_h = hashlib.md5() + size = 0 + for chunk in iter(lambda: f.read(65536), b""): + sha1_h.update(chunk) + md5_h.update(chunk) + size += len(chunk) + return sha1_h.hexdigest(), md5_h.hexdigest(), size + + +def _rebuilt_from(zf: zipfile.ZipFile, name: str, by_name: dict, files_db: dict) -> str | None: + """The name of a held archive a rebuilt ZIP carries the members of.""" + _bn = os.path.basename(name) + for _src_sha1 in by_name.get(_bn, []): + if _src_sha1 not in files_db: + continue + _src_path = files_db[_src_sha1]["path"] + if not os.path.exists(_src_path): + continue + if _same_members(zf, name, _src_path): + return _bn + return None + + +def _cached_as( + zf: zipfile.ZipFile, + name: str, + size: int, + sha1: str, + data_indexes: tuple[dict[str, list[str]], dict[str, str]], +) -> str | None: + """The name of the data directory file a member is, or None.""" + _data_index, _data_path_index = data_indexes + _bn = os.path.basename(name) + _pr = name + for _known_prefix in ("system/", "bios/", "BIOS/", "Firmware/"): + if name.startswith(_known_prefix): + _pr = name[len(_known_prefix):] + break + _cands = [] + if _pr in _data_path_index: + _cands.append(_data_path_index[_pr]) + for _dp in _data_index.get(_bn, []): + if _dp not in _cands: + _cands.append(_dp) + for _dp in _cands: + if not os.path.exists(_dp): + continue + # The content, not the size: a member written corrupt + # keeps its length, and a same-sized homonym from another + # cache is another file. + if os.path.getsize(_dp) == size and _file_sha1(_dp) == sha1: + return _bn + if ( + name.endswith(".zip") and _dp.endswith(".zip") + and _same_members(zf, name, _dp) + ): + return _bn + return None + + def verify_pack( zip_path: str, db: dict, data_registry: dict | None = None ) -> tuple[bool, dict]: @@ -138,53 +241,23 @@ def verify_pack( by_name = db.get("indexes", {}).get("by_name", {}) # name -> [SHA1] # Data directory file index - _data_index: dict[str, list[str]] = {} - _data_path_index: dict[str, str] = {} - if data_registry: - for _dk, _de in data_registry.items(): - cache = _de.get("local_cache", "") - if not cache or not os.path.isdir(cache): - continue - for _r, _d, _fns in os.walk(cache): - for _fn in _fns: - _fp = os.path.join(_r, _fn) - _rel = os.path.relpath(_fp, cache) - _data_path_index[_rel] = _fp - _data_index.setdefault(_fn, []).append(_fp) + data_indexes = _data_indexes(data_registry) - manifest = { + manifest: dict = { "schema_version": 1, "version": 1, "generator": "retrobios generate_pack.py", "generated": _build_timestamp(db), "files": [], } - errors = [] + errors: list[str] = [] with zipfile.ZipFile(zip_path, "r") as zf: for info in zf.infolist(): - if info.is_dir(): + if info.is_dir() or _is_pack_note(info.filename): continue name = info.filename - # The builder writes its notes beside the slot they explain - # (bios/INSTRUCTIONS_x.txt, games/keen6/INSTRUCTIONS_y.txt), so - # they are told by their own name, not by a root prefix. - base = name.rsplit("/", 1)[-1] - if base.startswith(("INSTRUCTIONS_", "RENAMED_")) or base == MEDIA_MARKER or name in ( - "manifest.json", - "README.txt", - ): - continue - with zf.open(info) as f: - sha1_h = hashlib.sha1() - md5_h = hashlib.md5() - size = 0 - for chunk in iter(lambda: f.read(65536), b""): - sha1_h.update(chunk) - md5_h.update(chunk) - size += len(chunk) - sha1 = sha1_h.hexdigest() - md5 = md5_h.hexdigest() + sha1, md5, size = _member_digests(zf, info) # Look up in database: files_db keyed by SHA1 db_entry = files_db.get(sha1) @@ -195,61 +268,26 @@ def verify_pack( else: # Try MD5 -> SHA1 lookup ref_sha1 = by_md5.get(md5) - if ref_sha1: - db_entry = files_db.get(ref_sha1) - if db_entry: - file_name = db_entry.get("name", "") - status = "verified_md5" - else: - status = "untracked" + db_entry = files_db.get(ref_sha1) if ref_sha1 else None + if db_entry: + file_name = db_entry.get("name", "") + status = "verified_md5" else: status = "untracked" # Rebuilt ZIP: verify inner ROM CRC32s match source if status == "untracked" and name.endswith(".zip"): - _bn = os.path.basename(name) - for _src_sha1 in by_name.get(_bn, []): - if _src_sha1 not in files_db: - continue - _src_path = files_db[_src_sha1]["path"] - if not os.path.exists(_src_path): - continue - if _same_members(zf, name, _src_path): - status = "verified_rebuild" - file_name = _bn - break + rebuilt = _rebuilt_from(zf, name, by_name, files_db) + if rebuilt: + status = "verified_rebuild" + file_name = rebuilt # Data directory: check against cached files - if status == "untracked" and _data_index: - _bn = os.path.basename(name) - _pr = name - for _known_prefix in ("system/", "bios/", "BIOS/", "Firmware/"): - if name.startswith(_known_prefix): - _pr = name[len(_known_prefix):] - break - _cands = [] - if _pr in _data_path_index: - _cands.append(_data_path_index[_pr]) - for _dp in _data_index.get(_bn, []): - if _dp not in _cands: - _cands.append(_dp) - for _dp in _cands: - if not os.path.exists(_dp): - continue - # The content, not the size: a member written corrupt - # keeps its length, and a same-sized homonym from another - # cache is another file. - if os.path.getsize(_dp) == size and _file_sha1(_dp) == sha1: - status = "verified_data" - file_name = _bn - break - if ( - name.endswith(".zip") and _dp.endswith(".zip") - and _same_members(zf, name, _dp) - ): - status = "verified_data" - file_name = _bn - break + if status == "untracked" and data_indexes[0]: + cached = _cached_as(zf, name, size, sha1, data_indexes) + if cached: + status = "verified_data" + file_name = cached status, file_name = _settle_untracked( status, name, zf, by_md5, held_inside, errors, file_name @@ -374,8 +412,8 @@ def _check_member_hash( for h in digests.values(): h.update(chunk) crc = zlib.crc32(chunk, crc) - actual = {name: h.hexdigest() for name, h in digests.items()} - actual["crc32"] = format(crc & 0xFFFFFFFF, "08x") + computed = {name: h.hexdigest() for name, h in digests.items()} + computed["crc32"] = format(crc & 0xFFFFFFFF, "08x") def composite() -> str | None: # Recalbox's Md5Composite, over the sorted members of a ZIP. @@ -390,10 +428,10 @@ def _check_member_hash( return None if digest_verdict( - file_entry, zf.getinfo(member).file_size, lambda: actual, mode, rule, composite + file_entry, zf.getinfo(member).file_size, lambda: computed, mode, rule, composite ): return None - return f"{member}: {mode} {actual[mode]} != declared {declared}" + return f"{member}: {mode} {computed[mode]} != declared {declared}" def _repo_satisfies_declaration( entries: list[dict], db: dict, mode: str @@ -534,13 +572,14 @@ def _structural_errors(zf: zipfile.ZipFile, zip_set: set) -> list[str]: errors.append(f"path traversal: {n}") # Zero-byte check (exclude Dolphin GraphicMods markers) - for info in zf.infolist(): - if info.file_size == 0 and not info.is_dir(): - if "GraphicMods" not in info.filename and info.filename not in ( - "manifest.json", - "README.txt", - ) and info.filename.rsplit("/", 1)[-1] != MEDIA_MARKER: - errors.append(f"zero-byte: {info.filename}") + errors.extend( + f"zero-byte: {info.filename}" + for info in zf.infolist() + if info.file_size == 0 and not info.is_dir() + and "GraphicMods" not in info.filename + and info.filename not in ("manifest.json", "README.txt") + and info.filename.rsplit("/", 1)[-1] != MEDIA_MARKER + ) return errors @@ -561,20 +600,11 @@ def _shipped_copy( def _builder_extras( - config: dict, - emulators_dir: str, + expect: _Expectation, db: dict, - emu_profiles: dict, baseline_groups: dict[str, list[dict]], - base_dest: str, - is_flat: bool, - zip_set: set[str], - zip_lower: dict[str, str], - target_cores: set[str] | None, - region_drops: set[str], - data_registry: dict | None, + view: _PackView, include_all: bool = False, - required_only: bool = False, ) -> dict[str, str]: """{full destination: local copy} of each core extra the builder writes. @@ -588,15 +618,18 @@ def _builder_extras( from packextras import ExtraResolver, hidden_extras from validation import _build_validation_index, destination_owners + config, emu_profiles = expect.config, expect.emu_profiles + data_registry = expect.opts.data_registry + base_dest = expect.base_dest mode = config.get("verification_mode", "existence") hash_rule = config.get("hash_rule", "") shipped: dict[str, str | None] = {} for expected, declarations in baseline_groups.items(): - if expected not in zip_set and expected.lower() not in zip_lower: + if not view.holds(expected): continue unflat = ( f"{base_dest}/{expected}" - if is_flat and base_dest and not expected.startswith(f"{base_dest}/") + if view.is_flat and base_dest and not expected.startswith(f"{base_dest}/") else expected ) if any(fe.get("storage") in ("user_provided", "external") for fe in declarations): @@ -616,8 +649,8 @@ def _builder_extras( } placed_local: dict[str, str] = {} hidden_extras( - config, emulators_dir, db, emu_profiles, shipped, target_cores, - region_drops or None, + config, expect.opts.emulators_dir, db, emu_profiles, shipped, expect.target_cores, + expect.region_drops or None, resolver=ExtraResolver( db, DEFAULT_BIOS_DIR, build_zip_contents_index(db), data_registry, _build_validation_index(platform_profiles), @@ -625,7 +658,7 @@ def _builder_extras( ), placed_local=placed_local, include_all=include_all, - required_only=required_only, + required_only=expect.opts.required_only, ) return placed_local @@ -639,19 +672,351 @@ def _same_content(zf: zipfile.ZipFile, member: str, local: str) -> bool: return local.lower().endswith(".zip") and _same_members(zf, member, local) +class PlatformCheckOptions(TypedDict, total=False): + """The keyword options of verify_pack_against_platform.""" + + emulators_dir: str + emu_profiles: dict | None + regions: list[str] | None + data_registry: dict | None + source: str + required_only: bool + no_game_data: bool + + +@dataclass(frozen=True) +class _PlatformCheckSettings: + """PlatformCheckOptions with their defaults.""" + + emulators_dir: str = "emulators" + emu_profiles: dict | None = None + regions: list[str] | None = None + data_registry: dict | None = None + source: str = "full" + required_only: bool = False + no_game_data: bool = False + + +@dataclass(frozen=True) +class _Expectation: + """What the builder would have packed for a platform, before the pack is read.""" + + config: dict + emu_profiles: dict + expected_systems: dict + region_drops: set[str] + db: dict | None + target_cores: set[str] | None + opts: _PlatformCheckSettings + + @property + def base_dest(self) -> str: + return self.config.get("base_destination", "") + + +@dataclass(frozen=True) +class _PackView: + """A pack's member names as the checks read them.""" + + zf: zipfile.ZipFile + zip_set: set[str] + zip_lower: dict[str, str] + zip_parents: set[str] + is_flat: bool + + def holds(self, expected: str) -> bool: + return expected in self.zip_set or expected.lower() in self.zip_lower + + +@dataclass +class _BaselineCount: + """The baseline destinations checked, and what each came to.""" + + checked: int = 0 + present: int = 0 + excluded: int = 0 + absent: int = 0 + decl_by_member: dict[str, list[dict]] = field(default_factory=dict) + + +def _reject_unknown(function: str, options: Mapping[str, object], known: frozenset[str]) -> None: + """Refuse a keyword the options do not declare, as a named parameter would.""" + for key in options: + if key not in known: + raise TypeError(f"{function}() got an unexpected keyword argument '{key}'") + + +def _cut_config( + platform_name: str, + platforms_dir: str, + target_cores: set[str] | None, + opts: _PlatformCheckSettings, +) -> tuple[dict, dict]: + """The platform's config and the profiles, without game data when asked.""" + config = load_platform_config(platform_name, platforms_dir) + emu_profiles = opts.emu_profiles + if emu_profiles is None: + emu_profiles = load_emulator_profiles(opts.emulators_dir) + if opts.no_game_data: + cut = without_game_data(config, emu_profiles, opts.data_registry, target_cores) + config, emu_profiles = cut.config, cut.profiles + return config, emu_profiles + + +def _region_drops( + config: dict, + emu_profiles: dict, + expected_systems: dict, + db: dict | None, + *, + target_cores: set[str] | None, + opts: _PlatformCheckSettings, +) -> set[str]: + """The regional variants the builder leaves out, by the same selection.""" + region_drops: set[str] = set() + if not opts.regions: + return region_drops + region_index = region_mod.platform_region_index( + config, emu_profiles, target_cores + ) + held: set[str] = set() + held_zip_contents = build_zip_contents_index(db) if db is not None else {} + region_groups, _extra_dests = platform_region_groups( + config, + expected_systems, + opts.emulators_dir, + db, + config.get("base_destination", ""), + emu_profiles, + target_cores=target_cores, + include_extras=(opts.source != "platform"), + include_all=(opts.source == "truth"), + required_only=opts.required_only, + held_out=held, + zip_contents=held_zip_contents, + data_registry=opts.data_registry, + slot_overrides=slots.pack_overrides( + config, emu_profiles, db, held_zip_contents, opts.data_registry, + target_cores=target_cores, source=opts.source, + ) if db is not None else None, + ) + return region_mod.resolve_region_drops( + region_groups, region_index, opts.regions, held + ) + + +def _pack_view(zf: zipfile.ZipFile, config: dict, base_dest: str) -> _PackView: + zip_set = set(zf.namelist()) + # A case twin stands in for the file only where the platform's + # filesystem folds case, as the builder decides: on Lakka bios.col + # and BIOS.col are two files two cores open, and either one missing + # was counted present through the other. + zip_lower = ( + {n.lower(): n for n in zip_set} + if config.get("case_insensitive_fs", False) else {} + ) + + # Auto-detect flat vs nested ZIP + is_flat = bool(base_dest) and not any( + n.startswith(base_dest + "/") + for n in zip_set + if n not in ("README.txt", "manifest.json", MEDIA_MARKER) and not n.endswith("/") + ) + # Mirror the builder's path-conflict logic: a declaration whose path + # collides file-vs-directory with a packed member was skipped by the + # builder (upstream declares e.g. both SGB1.sfc and SGB1.sfc/program.rom) + zip_parents: set[str] = set() + for n in zip_set: + parts = n.split("/") + for i in range(1, len(parts)): + zip_parents.add("/".join(parts[:i])) + return _PackView(zf, zip_set, zip_lower, zip_parents, is_flat) + + +def _baseline_groups( + expect: _Expectation, is_flat: bool, +) -> tuple[dict[str, list[dict]], dict[int, str]]: + """The platform's declarations by the path they expect in the pack, and + the system of each declaration.""" + opts = expect.opts + base_dest = expect.base_dest + baseline_groups: dict[str, list[dict]] = {} + system_of: dict[int, str] = {} + for sys_id, system in (expect.expected_systems if opts.source != "truth" else {}).items(): + for fe in system.get("files", []): + system_of[id(fe)] = sys_id + if opts.required_only and fe.get("required") is False: + continue + dest = sanitize_pack_path(fe.get("destination", fe.get("name", ""))) + if not dest: + continue + if expect.region_drops and dest in expect.region_drops: + continue + expected = f"{base_dest}/{dest}" if base_dest and not is_flat else dest + baseline_groups.setdefault(expected, []).append(fe) + return baseline_groups, system_of + + +def _count_baseline( + expect: _Expectation, + view: _PackView, + baseline_groups: dict[str, list[dict]], + system_of: dict[int, str], + errors: list[str], +) -> _BaselineCount: + """Each baseline destination present, accounted for, or missing.""" + db = expect.db + data_registry = expect.opts.data_registry + count = _BaselineCount(checked=len(baseline_groups)) + # RetroBat's group: one packed file of it is enough, as verify counts it. + packed_groups = { + (system_of[id(fe)], fe["group"]) + for expected, declarations in baseline_groups.items() + if view.holds(expected) + for fe in declarations + if fe.get("group") + } + exclusion_index = build_zip_contents_index(db) if db is not None else {} + categories = EntryCategories(expect.config, expect.emu_profiles, data_registry) + for expected, declarations in baseline_groups.items(): + if expected in view.zip_set: + member = expected + elif expected.lower() in view.zip_lower: + member = view.zip_lower[expected.lower()] + elif _has_path_conflict(expected, view.zip_set, view.zip_parents): + # Skipped by the builder for the same reason: not an error + count.present += 1 + continue + elif db is not None and _intentional_hash_exclusion( + declarations, + db, + zip_contents=exclusion_index, + data_dir_registry=data_registry, + verification_mode=expect.config.get("verification_mode", "existence"), + hash_rule=expect.config.get("hash_rule", ""), + ): + count.excluded += 1 + continue + elif all( + fe.get("group") and (system_of[id(fe)], fe["group"]) in packed_groups + for fe in declarations + ): + count.absent += 1 + continue + elif db is not None and all( + categories.category(fe, fe.get("destination", fe.get("name", "")), + system_of[id(fe)]) != BIOS + for fe in declarations + ) and _held_nowhere(declarations, db, exclusion_index, data_registry): + # A game's data or an entry no core opens that the collection + # lacks: verify counts it apart. Missing firmware stays an error. + count.absent += 1 + continue + else: + errors.append(f"baseline missing: {expected}") + continue + count.present += 1 + count.decl_by_member.setdefault(member, []).extend(declarations) + return count + + +def _native_hash_errors( + zf: zipfile.ZipFile, decl_by_member: dict[str, list[dict]], config: dict, db: dict | None, +) -> list[str]: + """The platform's native hash check replayed on the pack's bytes.""" + # Reproduce the platform's native hash check on pack bytes. + # A destination declared by several entries passes when the packed + # member satisfies any of them. A failure only counts as a pack + # error when the repo holds a file matching a declaration: without + # one, the pack ships its best effort and the gap is a data issue + # reported by verify.py, not a generation bug. + verification_mode = config.get("verification_mode", "existence") + digest = digest_algorithm(verification_mode) + hash_rule = config.get("hash_rule", "") + errors: list[str] = [] + if not (reads_file_contents(verification_mode) and digest): + return errors + for member, decl_entries in decl_by_member.items(): + checkable = [ + fe + for fe in decl_entries + if str(fe.get(digest) or "").strip() + ] + if not checkable: + continue + # An archive declared once per ROM it must hold is checked + # once per ROM by the frontend: each such declaration has to + # pass on its own, where the repository holds an archive + # that would. + for fe in checkable: + if not fe.get("zipped_file"): + continue + err = _check_member_hash(zf, member, fe, digest, hash_rule) + if err is not None and (db is None or _repo_holds_member(fe, db)): + errors.append(err) + member_errors = [] + satisfied = False + for fe in checkable: + err = _check_member_hash(zf, member, fe, digest, hash_rule) + if err is None: + satisfied = True + break + member_errors.append(err) + if satisfied: + continue + if db is None or _repo_satisfies_declaration( + checkable, db, verification_mode + ): + errors.append(member_errors[0]) + return errors + + +def _check_core_extras( + expect: _Expectation, + view: _PackView, + baseline_groups: dict[str, list[dict]], + errors: list[str], +) -> tuple[int, int]: + """(checked, present) of the core extras the builder places.""" + # 2. Core extras: the builder's own placement replayed + # (packextras.hidden_extras, which verify reads too), after the + # platform files it wrote. Each placed extra must sit at its path + # with the bytes the builder resolved: an empty seen set and a + # presence test credited a core file at a path where the pack + # holds the platform's other dump. + core_checked = 0 + core_present = 0 + opts = expect.opts + db = expect.db + if db is None or opts.source == "platform": + return core_checked, core_present + placed_local = _builder_extras( + expect, db, baseline_groups, view, include_all=(opts.source == "truth"), + ) + for full, local in sorted(placed_local.items()): + core_checked += 1 + flat = _flat(full, expect.base_dest, view.is_flat) + member = flat if flat in view.zip_set else view.zip_lower.get(flat.lower()) + if member is None: + errors.append(f"core missing: {full}") + elif not _same_content(view.zf, member, local): + errors.append( + f"core file differs: {member} is not the {os.path.basename(local)}" + " the builder places there" + ) + else: + core_present += 1 + return core_checked, core_present + + def verify_pack_against_platform( zip_path: str, platform_name: str, platforms_dir: str, db: dict | None = None, - emulators_dir: str = "emulators", - emu_profiles: dict | None = None, - regions: list[str] | None = None, - data_registry: dict | None = None, + *, target_cores: set[str] | None = None, - source: str = "full", - required_only: bool = False, - no_game_data: bool = False, + **options: Unpack[PlatformCheckOptions], ) -> tuple[bool, int, int, list[str], int, int, int, int, int, int]: """Verify a pack ZIP against its platform config and core requirements. @@ -679,19 +1044,11 @@ def verify_pack_against_platform( holds no file for that are a game's data or that no core the platform runs opens. verify.py counts them apart from firmware. """ - - config = load_platform_config(platform_name, platforms_dir) - base_dest = config.get("base_destination", "") - errors: list[str] = [] - checked = 0 - present = 0 - - if emu_profiles is None: - emu_profiles = load_emulator_profiles(emulators_dir) - if no_game_data: - cut = without_game_data(config, emu_profiles, data_registry, target_cores) - config, emu_profiles = cut.config, cut.profiles - + _reject_unknown( + "verify_pack_against_platform", options, PlatformCheckOptions.__optional_keys__ + ) + opts = _PlatformCheckSettings(**options) + config, emu_profiles = _cut_config(platform_name, platforms_dir, target_cores, opts) expected_systems = config.get("systems", {}) if target_cores is not None: expected_systems = filter_systems_by_target( @@ -700,221 +1057,35 @@ def verify_pack_against_platform( target_cores, resolve_platform_cores(config, emu_profiles), ) - - region_drops: set[str] = set() - if regions: - region_index = region_mod.platform_region_index( - config, emu_profiles, target_cores - ) - held: set[str] = set() - held_zip_contents = build_zip_contents_index(db) if db is not None else {} - region_groups, _extra_dests = platform_region_groups( - config, - expected_systems, - emulators_dir, - db, - base_dest, - emu_profiles, - target_cores=target_cores, - include_extras=(source != "platform"), - include_all=(source == "truth"), - required_only=required_only, - held_out=held, - zip_contents=held_zip_contents, - data_registry=data_registry, - slot_overrides=slots.pack_overrides( - config, emu_profiles, db, held_zip_contents, data_registry, - target_cores=target_cores, source=source, - ) if db is not None else None, - ) - region_drops = region_mod.resolve_region_drops( - region_groups, region_index, regions, held - ) + expect = _Expectation( + config, emu_profiles, expected_systems, + _region_drops( + config, emu_profiles, expected_systems, db, + target_cores=target_cores, opts=opts, + ), + db, target_cores, opts, + ) + errors: list[str] = [] with zipfile.ZipFile(zip_path, "r") as zf: - zip_set = set(zf.namelist()) - # A case twin stands in for the file only where the platform's - # filesystem folds case, as the builder decides: on Lakka bios.col - # and BIOS.col are two files two cores open, and either one missing - # was counted present through the other. - zip_lower = ( - {n.lower(): n for n in zip_set} - if config.get("case_insensitive_fs", False) else {} - ) - - # Auto-detect flat vs nested ZIP - is_flat = bool(base_dest) and not any( - n.startswith(base_dest + "/") - for n in zip_set - if n not in ("README.txt", "manifest.json", MEDIA_MARKER) and not n.endswith("/") - ) - - errors.extend(_structural_errors(zf, zip_set)) + view = _pack_view(zf, expect.config, expect.base_dest) + errors.extend(_structural_errors(zf, view.zip_set)) # 1. Baseline file presence + native hash check - verification_mode = config.get("verification_mode", "existence") - baseline_checked = 0 - baseline_present = 0 - baseline_excluded = 0 - baseline_absent = 0 - decl_by_member: dict[str, list[dict]] = {} - # Mirror the builder's path-conflict logic: a declaration whose path - # collides file-vs-directory with a packed member was skipped by the - # builder (upstream declares e.g. both SGB1.sfc and SGB1.sfc/program.rom) - zip_parents: set[str] = set() - for n in zip_set: - parts = n.split("/") - for i in range(1, len(parts)): - zip_parents.add("/".join(parts[:i])) - baseline_groups: dict[str, list[dict]] = {} - system_of: dict[int, str] = {} - for sys_id, system in (expected_systems if source != "truth" else {}).items(): - for fe in system.get("files", []): - system_of[id(fe)] = sys_id - if required_only and fe.get("required") is False: - continue - dest = sanitize_pack_path(fe.get("destination", fe.get("name", ""))) - if not dest: - continue - if region_drops and dest in region_drops: - continue - expected = f"{base_dest}/{dest}" if base_dest and not is_flat else dest - baseline_groups.setdefault(expected, []).append(fe) - - baseline_checked = len(baseline_groups) - # RetroBat's group: one packed file of it is enough, as verify counts it. - packed_groups = { - (system_of[id(fe)], fe["group"]) - for expected, declarations in baseline_groups.items() - if expected in zip_set or expected.lower() in zip_lower - for fe in declarations - if fe.get("group") - } - exclusion_index = build_zip_contents_index(db) if db is not None else {} - categories = EntryCategories(config, emu_profiles, data_registry) - for expected, declarations in baseline_groups.items(): - if expected in zip_set: - member = expected - elif expected.lower() in zip_lower: - member = zip_lower[expected.lower()] - elif _has_path_conflict(expected, zip_set, zip_parents): - # Skipped by the builder for the same reason: not an error - baseline_present += 1 - continue - elif db is not None and _intentional_hash_exclusion( - declarations, - db, - zip_contents=exclusion_index, - data_dir_registry=data_registry, - verification_mode=verification_mode, - hash_rule=config.get("hash_rule", ""), - ): - baseline_excluded += 1 - continue - elif all( - fe.get("group") and (system_of[id(fe)], fe["group"]) in packed_groups - for fe in declarations - ): - baseline_absent += 1 - continue - elif db is not None and all( - categories.category(fe, fe.get("destination", fe.get("name", "")), - system_of[id(fe)]) != BIOS - for fe in declarations - ) and _held_nowhere(declarations, db, exclusion_index, data_registry): - # A game's data or an entry no core opens that the collection - # lacks: verify counts it apart. Missing firmware stays an error. - baseline_absent += 1 - continue - else: - errors.append(f"baseline missing: {expected}") - continue - baseline_present += 1 - decl_by_member.setdefault(member, []).extend(declarations) - - # Reproduce the platform's native hash check on pack bytes. - # A destination declared by several entries passes when the packed - # member satisfies any of them. A failure only counts as a pack - # error when the repo holds a file matching a declaration: without - # one, the pack ships its best effort and the gap is a data issue - # reported by verify.py, not a generation bug. - digest = digest_algorithm(verification_mode) - hash_rule = config.get("hash_rule", "") - if reads_file_contents(verification_mode) and digest: - for member, decl_entries in decl_by_member.items(): - checkable = [ - fe - for fe in decl_entries - if str(fe.get(digest) or "").strip() - ] - if not checkable: - continue - # An archive declared once per ROM it must hold is checked - # once per ROM by the frontend: each such declaration has to - # pass on its own, where the repository holds an archive - # that would. - for fe in checkable: - if not fe.get("zipped_file"): - continue - err = _check_member_hash(zf, member, fe, digest, hash_rule) - if err is not None and (db is None or _repo_holds_member(fe, db)): - errors.append(err) - member_errors = [] - satisfied = False - for fe in checkable: - err = _check_member_hash(zf, member, fe, digest, hash_rule) - if err is None: - satisfied = True - break - member_errors.append(err) - if satisfied: - continue - if db is None or _repo_satisfies_declaration( - checkable, db, verification_mode - ): - errors.append(member_errors[0]) - - # 2. Core extras: the builder's own placement replayed - # (packextras.hidden_extras, which verify reads too), after the - # platform files it wrote. Each placed extra must sit at its path - # with the bytes the builder resolved: an empty seen set and a - # presence test credited a core file at a path where the pack - # holds the platform's other dump. - core_checked = 0 - core_present = 0 - if db is not None and source != "platform": - placed_local = _builder_extras( - config, emulators_dir, db, emu_profiles, baseline_groups, - base_dest, is_flat, zip_set, zip_lower, target_cores, - region_drops, data_registry, - include_all=(source == "truth"), required_only=required_only, - ) - for full, local in sorted(placed_local.items()): - core_checked += 1 - member = _flat(full, base_dest, is_flat) - member = member if member in zip_set else zip_lower.get(member.lower()) - if member is None: - errors.append(f"core missing: {full}") - elif not _same_content(zf, member, local): - errors.append( - f"core file differs: {member} is not the {os.path.basename(local)}" - " the builder places there" - ) - else: - core_present += 1 - - checked = baseline_checked + core_checked - present = baseline_present + core_present + baseline_groups, system_of = _baseline_groups(expect, view.is_flat) + baseline = _count_baseline(expect, view, baseline_groups, system_of, errors) + errors.extend(_native_hash_errors(zf, baseline.decl_by_member, expect.config, db)) + core_checked, core_present = _check_core_extras(expect, view, baseline_groups, errors) return ( len(errors) == 0, - checked, - present, + baseline.checked + core_checked, + baseline.present + core_present, errors, - baseline_checked, - baseline_present, + baseline.checked, + baseline.present, core_checked, core_present, - baseline_excluded, - baseline_absent, + baseline.excluded, + baseline.absent, ) From 2f2d390c34b48db8656ed9fe7c9f178ef75f1a03 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Tue, 17 Nov 2026 07:56:08 +0100 Subject: [PATCH 3/9] refactor: split safe_extract_zip by check --- scripts/ziptools.py | 183 +++++++++++++++++++++++++++----------------- 1 file changed, 111 insertions(+), 72 deletions(-) diff --git a/scripts/ziptools.py b/scripts/ziptools.py index 4fcf14e06..4567e2ef3 100644 --- a/scripts/ziptools.py +++ b/scripts/ziptools.py @@ -5,6 +5,8 @@ capping a real romset: the largest pack is already several gigabytes.""" from __future__ import annotations +from dataclasses import dataclass + import functools import hashlib import os @@ -235,6 +237,109 @@ MAX_ZIP_COMPRESSION_RATIO = 1_100 _BOUNDED_RATIO_METHODS = (zipfile.ZIP_STORED, zipfile.ZIP_DEFLATED) +@dataclass(frozen=True) +class _ZipLimits: + """The resource bounds safe_extract_zip enforces.""" + + members: int + member_size: int + total_size: int + compression_ratio: int + + +def _member_path(member: zipfile.ZipInfo, seen: set[str]) -> tuple[str, list[str], int]: + """(name, path components, mode) of a member whose path and type are safe.""" + # Archives written on Windows store a backslash separator. It is a + # separator, not a filename character, so it is normalized before + # the component checks rather than rejected. + name = member.filename.replace("\\", "/") + if not name or "\x00" in name: + raise ValueError(f"Unsafe ZIP member name: {member.filename!r}") + if name.startswith("/") or re.match(r"^[A-Za-z]:", name): + raise ValueError(f"Absolute ZIP member path: {name}") + parts = [part for part in name.split("/") if part] + if any(part in (".", "..") for part in parts): + raise ValueError(f"ZIP traversal detected: {name}") + normalized = "/".join(parts) + if normalized in seen: + raise ValueError(f"Duplicate ZIP member path: {name}") + seen.add(normalized) + + mode = (member.external_attr >> 16) & 0xFFFF + file_type = stat.S_IFMT(mode) + if file_type not in (0, stat.S_IFREG, stat.S_IFDIR): + raise ValueError(f"ZIP link or special file rejected: {name}") + if member.flag_bits & 0x1: + raise ValueError(f"Encrypted ZIP member rejected: {name}") + return name, parts, mode + + +def _check_member_size( + member: zipfile.ZipInfo, name: str, declared_total: int, limits: _ZipLimits, +) -> int: + """The declared total with this member, after its size limits are checked.""" + if member.file_size > limits.member_size: + raise ValueError( + f"ZIP member {name} is {member.file_size} bytes; " + f"limit is {limits.member_size}" + ) + declared_total += member.file_size + if declared_total > limits.total_size: + raise ValueError( + f"ZIP expands to {declared_total} bytes, limit is {limits.total_size}" + ) + if member.file_size and member.compress_type in _BOUNDED_RATIO_METHODS: + if member.compress_size == 0: + raise ValueError(f"Invalid compression size for ZIP member: {name}") + if member.file_size / member.compress_size > limits.compression_ratio: + raise ValueError(f"Suspicious compression ratio for ZIP member: {name}") + return declared_total + + +def _install_member( + zf: zipfile.ZipFile, + member: zipfile.ZipInfo, + name: str, + target: str, + mode: int, + umask: int, + max_member_size: int, +) -> None: + """Stream a member to a temporary sibling, check it, then move it in place.""" + os.makedirs(os.path.dirname(target), exist_ok=True) + tmp_path = "" + try: + with tempfile.NamedTemporaryFile( + mode="wb", dir=os.path.dirname(target), delete=False + ) as tmp_file: + tmp_path = tmp_file.name + actual_size = 0 + with zf.open(member, "r") as source: + while True: + chunk = source.read(1024 * 1024) + if not chunk: + break + actual_size += len(chunk) + if actual_size > member.file_size or actual_size > max_member_size: + raise ValueError( + f"ZIP member exceeded declared or configured size: {name}" + ) + tmp_file.write(chunk) + if actual_size != member.file_size: + raise ValueError( + f"ZIP member size mismatch for {name}: " + f"{actual_size} != {member.file_size}" + ) + # The member's own mode, else what a plain open() gives. The + # temporary file is private and would keep 0600 otherwise. + os.chmod(tmp_path, ((mode & 0o777) or 0o666) & ~umask) + os.replace(tmp_path, target) + tmp_path = "" + finally: + if tmp_path and os.path.exists(tmp_path): + os.unlink(tmp_path) + + def safe_extract_zip( zip_path: str, dest_dir: str, @@ -249,56 +354,22 @@ def safe_extract_zip( Files are streamed to a temporary sibling and atomically installed only after their declared length and CRC have been checked by ``zipfile``. """ + limits = _ZipLimits(max_members, max_member_size, max_total_size, max_compression_ratio) dest = os.path.realpath(dest_dir) os.makedirs(dest, exist_ok=True) umask = process_umask() with zipfile.ZipFile(zip_path, "r") as zf: members = zf.infolist() - if len(members) > max_members: + if len(members) > limits.members: raise ValueError( - f"ZIP has {len(members)} members, limit is {max_members}" + f"ZIP has {len(members)} members, limit is {limits.members}" ) declared_total = 0 seen: set[str] = set() for member in members: - # Archives written on Windows store a backslash separator. It is a - # separator, not a filename character, so it is normalized before - # the component checks rather than rejected. - name = member.filename.replace("\\", "/") - if not name or "\x00" in name: - raise ValueError(f"Unsafe ZIP member name: {member.filename!r}") - if name.startswith("/") or re.match(r"^[A-Za-z]:", name): - raise ValueError(f"Absolute ZIP member path: {name}") - parts = [part for part in name.split("/") if part] - if any(part in (".", "..") for part in parts): - raise ValueError(f"ZIP traversal detected: {name}") - normalized = "/".join(parts) - if normalized in seen: - raise ValueError(f"Duplicate ZIP member path: {name}") - seen.add(normalized) - - mode = (member.external_attr >> 16) & 0xFFFF - file_type = stat.S_IFMT(mode) - if file_type not in (0, stat.S_IFREG, stat.S_IFDIR): - raise ValueError(f"ZIP link or special file rejected: {name}") - if member.flag_bits & 0x1: - raise ValueError(f"Encrypted ZIP member rejected: {name}") - if member.file_size > max_member_size: - raise ValueError( - f"ZIP member {name} is {member.file_size} bytes; " - f"limit is {max_member_size}" - ) - declared_total += member.file_size - if declared_total > max_total_size: - raise ValueError( - f"ZIP expands to {declared_total} bytes, limit is {max_total_size}" - ) - if member.file_size and member.compress_type in _BOUNDED_RATIO_METHODS: - if member.compress_size == 0: - raise ValueError(f"Invalid compression size for ZIP member: {name}") - if member.file_size / member.compress_size > max_compression_ratio: - raise ValueError(f"Suspicious compression ratio for ZIP member: {name}") + name, parts, mode = _member_path(member, seen) + declared_total = _check_member_size(member, name, declared_total, limits) target = os.path.realpath(os.path.join(dest, *parts)) if not target.startswith(dest + os.sep) and target != dest: @@ -306,36 +377,4 @@ def safe_extract_zip( if member.is_dir() or name.endswith("/"): os.makedirs(target, exist_ok=True) continue - - os.makedirs(os.path.dirname(target), exist_ok=True) - tmp_path = "" - try: - with tempfile.NamedTemporaryFile( - mode="wb", dir=os.path.dirname(target), delete=False - ) as tmp_file: - tmp_path = tmp_file.name - actual_size = 0 - with zf.open(member, "r") as source: - while True: - chunk = source.read(1024 * 1024) - if not chunk: - break - actual_size += len(chunk) - if actual_size > member.file_size or actual_size > max_member_size: - raise ValueError( - f"ZIP member exceeded declared or configured size: {name}" - ) - tmp_file.write(chunk) - if actual_size != member.file_size: - raise ValueError( - f"ZIP member size mismatch for {name}: " - f"{actual_size} != {member.file_size}" - ) - # The member's own mode, else what a plain open() gives. The - # temporary file is private and would keep 0600 otherwise. - os.chmod(tmp_path, ((mode & 0o777) or 0o666) & ~umask) - os.replace(tmp_path, target) - tmp_path = "" - finally: - if tmp_path and os.path.exists(tmp_path): - os.unlink(tmp_path) + _install_member(zf, member, name, target, mode, umask, limits.member_size) From 59941727ee5e064297a72f03d35668212d4d2895 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Tue, 17 Nov 2026 07:56:08 +0100 Subject: [PATCH 4/9] refactor: split preferred_entries by choice --- scripts/packresolve.py | 84 ++++++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 31 deletions(-) diff --git a/scripts/packresolve.py b/scripts/packresolve.py index 45552b46a..8f64b55b9 100644 --- a/scripts/packresolve.py +++ b/scripts/packresolve.py @@ -283,6 +283,7 @@ def sha1_for_hash(hash_type: str, hash_val: str, db: dict) -> str | None: """The database key a looked-up hash names, or None.""" files_db = db.get("files", {}) indexes = db.get("indexes", {}) + sha1: str | None if hash_type == "sha1": sha1 = hash_val elif hash_type == "crc32": @@ -450,6 +451,53 @@ def resolve_declaration( )) +def _declarations_by_destination( + pack_systems: dict, base_dest: str, required_only: bool, +) -> dict[str, list[dict]]: + """The declarations of each full destination, in system order.""" + dest_entries: dict[str, list[dict]] = {} + for _sys_id, system in sorted(pack_systems.items()): + for file_entry in system.get("files", []): + if required_only and file_entry.get("required") is False: + continue + dest = sanitize_pack_path( + file_entry.get("destination", file_entry.get("name", "")) + ) + if not dest: + continue + full = f"{base_dest}/{dest}" if base_dest else dest + dest_entries.setdefault(full, []).append(file_entry) + return dest_entries + + +def _archive_choice( + resolved: list[tuple[dict, str | None, str]], members: list[dict], +) -> dict | None: + """The declaration whose archive the most per-ROM declarations accept.""" + # One archive declared once per ROM it must hold: the archive + # to ship is the one the most declarations accept, not the first + # that answers one of them (adam_fdc.zip: a one-member MAME set + # answered first, the eight-member set sat beside it). + def accepted(path: str | None) -> int: + if not path or not path.endswith(".zip"): + return -1 + return sum(inner_rom_check(fe, path) == "ok" for fe in members) + + fe, path, _st = max(resolved, key=lambda item: accepted(item[1])) + return fe if accepted(path) > 0 else None + + +def _hash_choice(resolved: list[tuple[dict, str | None, str]]) -> dict | None: + """The first declaration resolved by md5, else the first by any hash.""" + best = None + for fe, _lp, _st in resolved: + if _lp and _st == "md5_exact": + return fe + if best is None and _lp and resolution_is_hash_exact(_st): + best = fe + return best + + def preferred_entries( pack_systems: dict, db: dict, @@ -470,18 +518,7 @@ def preferred_entries( and could crown a declaration only a data/ cache satisfies. """ preferred_entries: dict[str, int] = {} - dest_entries: dict[str, list[dict]] = {} - for sys_id, system in sorted(pack_systems.items()): - for file_entry in system.get("files", []): - if required_only and file_entry.get("required") is False: - continue - dest = sanitize_pack_path( - file_entry.get("destination", file_entry.get("name", "")) - ) - if not dest: - continue - full = f"{base_dest}/{dest}" if base_dest else dest - dest_entries.setdefault(full, []).append(file_entry) + dest_entries = _declarations_by_destination(pack_systems, base_dest, required_only) for full, entries in dest_entries.items(): if len(entries) < 2: continue @@ -504,26 +541,11 @@ def preferred_entries( ] members = [fe for fe in entries if fe.get("zipped_file")] if members: - # One archive declared once per ROM it must hold: the archive - # to ship is the one the most declarations accept, not the first - # that answers one of them (adam_fdc.zip: a one-member MAME set - # answered first, the eight-member set sat beside it). - def accepted(path: str | None) -> int: - if not path or not path.endswith(".zip"): - return -1 - return sum(inner_rom_check(fe, path) == "ok" for fe in members) - - fe, path, _st = max(resolved, key=lambda item: accepted(item[1])) - if accepted(path) > 0: - preferred_entries[full] = id(fe) + chosen = _archive_choice(resolved, members) + if chosen is not None: + preferred_entries[full] = id(chosen) continue - best = None - for fe, _lp, _st in resolved: - if _lp and _st == "md5_exact": - best = fe - break - if best is None and _lp and resolution_is_hash_exact(_st): - best = fe + best = _hash_choice(resolved) if best is not None: preferred_entries[full] = id(best) From fe25a7caa0304fadece60f304f54609045e24098 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Tue, 17 Nov 2026 07:56:09 +0100 Subject: [PATCH 5/9] refactor: tidy packpaths and torrentzip returns --- scripts/packpaths.py | 4 +--- scripts/torrentzip.py | 15 +++++++-------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/scripts/packpaths.py b/scripts/packpaths.py index c7b953432..045417a45 100644 --- a/scripts/packpaths.py +++ b/scripts/packpaths.py @@ -161,9 +161,7 @@ def _has_path_conflict(dest: str, seen_files: set[str], seen_parents: set[str]) for parent in _path_parents(dest): if parent in seen_files: return True - if dest in seen_parents: - return True - return False + return dest in seen_parents def placement_block( dest: str, diff --git a/scripts/torrentzip.py b/scripts/torrentzip.py index 5e2faff66..41f1588fb 100644 --- a/scripts/torrentzip.py +++ b/scripts/torrentzip.py @@ -217,6 +217,8 @@ def main() -> int: parser.add_argument("--rebuild", metavar="ZIP", help="normalize an archive") parser.add_argument("--output", "-o", help="output path for --rebuild") args = parser.parse_args() + if not (args.check or args.rebuild): + parser.error("nothing to do: pass --check or --rebuild") if args.check: data = Path(args.check).read_bytes() @@ -228,14 +230,11 @@ def main() -> int: print(f" conform: {data == rebuilt}") return 0 if data == rebuilt else 1 - if args.rebuild: - data = rebuild_torrentzip(args.rebuild) - dest = args.output or args.rebuild - Path(dest).write_bytes(data) - print(f"{dest}: {len(data)} bytes, md5 {hashlib.md5(data).hexdigest()}") - return 0 - - parser.error("nothing to do: pass --check or --rebuild") + data = rebuild_torrentzip(args.rebuild) + dest = args.output or args.rebuild + Path(dest).write_bytes(data) + print(f"{dest}: {len(data)} bytes, md5 {hashlib.md5(data).hexdigest()}") + return 0 if __name__ == "__main__": From add67da611db0b5e2b7cea997d8a0e2f69174cee Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Tue, 17 Nov 2026 07:57:59 +0100 Subject: [PATCH 6/9] refactor: keep the anchors the source tests read The conflict branch calls baseline_verdict before the storage test, _run_verify_packs unpacks the verdict itself, and the tree walks spell required_only and seen_parents as test_audit_regressions reads them. --- scripts/generate_pack.py | 180 ++++++++++++++++++++------------------- 1 file changed, 91 insertions(+), 89 deletions(-) diff --git a/scripts/generate_pack.py b/scripts/generate_pack.py index d1cd83397..9ba5304c7 100644 --- a/scripts/generate_pack.py +++ b/scripts/generate_pack.py @@ -621,15 +621,11 @@ class _TreeWalkOptions(TypedDict, total=False): reclaim: dict[str, str] | None -def _carried_tree( - dd: dict, data_registry: dict | None, platform_name: str, required_only: bool, -) -> str | None: +def _carried_tree(dd: dict, data_registry: dict | None, platform_name: str) -> str | None: """The cache folder of a tree reference the pack carries, or None.""" ref_key = dd.get("ref", "") if not ref_key or not data_registry or ref_key not in data_registry: return None - if required_only and dd.get("required") is False: - return None if ref_key in installed_data_refs(platform_name): return None entry = data_registry[ref_key] @@ -693,11 +689,13 @@ def _data_directory_members( core_trees = options.get("core_trees", ()) # Data directories from _data_dirs.yml for systems, dd in _tree_references(pack_systems, core_trees): - local_path = _carried_tree(dd, data_registry, platform_name, required_only) + needed = dd.get("required") is not False + if required_only and not needed: + continue + local_path = _carried_tree(dd, data_registry, platform_name) if local_path is None: continue ref_key = dd.get("ref", "") - needed = dd.get("required") is not False dd_dest = dd.get("destination", "") dd_prefix = f"{base_dest}/{dd_dest}" if base_dest and dd_dest else base_dest or dd_dest # Held while the caller writes what is yielded: a refresh swapping @@ -1242,6 +1240,34 @@ class _PlatformPackBuild: self._pack_trees() self._write_readme() + def _judge_conflict(self, file_entry: dict, full_dest: str) -> None: + # Content ships under the conflicting shape (file vs dir, + # e.g. SGB1.sfc). verify.py resolves each declaration on + # its own, so this one is counted by what it resolves to: + # an unresolved one counted OK had the pack claim a file + # verify reports missing. + plan = self.plan + conflict_path, conflict_status = resolve_file( + file_entry, plan.db, plan.bios_dir, plan.zip_contents, + data_dir_registry=plan.opts.data_registry, offline=plan.opts.offline, + ) + # The verdict of the main path: a zipped_file archive is + # judged by the member inside, and a frontend that hashes + # one digest accepts a file another declared hash + # contradicts, as verify judges it. + conflict_status, refusal = baseline_verdict( + file_entry, conflict_path, conflict_status, plan.verification_mode, + plan.config.get("hash_rule", ""), + ) + if conflict_status == "not_found": + self.file_status[full_dest] = "missing" + self.file_reasons[full_dest] = "not found" + elif refusal: + self.file_status[full_dest] = "excluded" + self.file_reasons[full_dest] = refusal + else: + self.file_status.setdefault(full_dest, "ok") + def _pack_declared(self, sys_id: str, file_entry: dict) -> None: plan, opts = self.plan, self.plan.opts if opts.required_only and file_entry.get("required") is False: @@ -1261,7 +1287,8 @@ class _PlatformPackBuild: if self.placed.conflicts(full_dest): self._judge_conflict(file_entry, full_dest) return - if file_entry.get("storage", "embedded") == "user_provided": + storage = file_entry.get("storage", "embedded") + if storage == "user_provided": if not already_packed: self._write_instructions(file_entry, full_dest) return @@ -1297,31 +1324,6 @@ class _PlatformPackBuild: else: self.file_status.setdefault(fkey, "ok") - def _judge_conflict(self, file_entry: dict, full_dest: str) -> None: - # Content ships under the conflicting shape (file vs dir, - # e.g. SGB1.sfc). verify.py resolves each declaration on - # its own, so this one is counted by what it resolves to: - # an unresolved one counted OK had the pack claim a file - # verify reports missing. - plan = self.plan - conflict_path, conflict_status = resolve_file( - file_entry, plan.db, plan.bios_dir, plan.zip_contents, - data_dir_registry=plan.opts.data_registry, offline=plan.opts.offline, - ) - # The verdict of the main path: a zipped_file archive is - # judged by the member inside, and a frontend that hashes - # one digest accepts a file another declared hash - # contradicts, as verify judges it. - conflict_status, refusal = self.verdict(file_entry, conflict_path, conflict_status) - if conflict_status == "not_found": - self.file_status[full_dest] = "missing" - self.file_reasons[full_dest] = "not found" - elif refusal: - self.file_status[full_dest] = "excluded" - self.file_reasons[full_dest] = refusal - else: - self.file_status.setdefault(full_dest, "ok") - def _write_instructions(self, file_entry: dict, full_dest: str) -> None: base_dest = self.plan.base_dest self.placed.take(full_dest) @@ -1640,11 +1642,13 @@ class _PlatformPackBuild: def _pack_data_directories(self, pack_systems: dict, core_trees: Iterable[dict]) -> int: """Add the data directories to the pack. Returns how many files went in.""" plan = self.plan + required_only = plan.opts.required_only + seen_parents = self.placed.parents added = 0 for src, full in _data_directory_members( pack_systems, plan.opts.data_registry, plan.platform_name, plan.base_dest, self.placed.fold, self.placed.destinations, self.placed.lower, - self.placed.parents, required_only=plan.opts.required_only, + seen_parents, required_only=required_only, core_trees=core_trees, ): _add_pack_member(self.zf, src, self._flat(full)) @@ -2293,10 +2297,11 @@ class _EmulatorPackBuild: # profile declares wins over the copy a tree bundles at the same # path (Dolphin's real dsp_rom.bin over Sys/GC's free one). # A tree only one build reads follows that build, like a file. + required_only = self.required_only for dd in deliverable( profile.get("data_directories", []), self.standalone ): - if self.required_only and dd.get("required") is False: + if required_only and dd.get("required") is False: continue ref_key = dd.get("ref", "") if not ref_key or not self.data_registry or ref_key not in self.data_registry: @@ -3425,54 +3430,6 @@ class _PackCheck: no_game_data: bool -def _verify_found_pack( - check: _PackCheck, platform_name: str, zip_path: str, target_cores: set[str] | None, -) -> bool: - """Check one pack of a platform and print its line; False when it fails.""" - pack_name = os.path.basename(zip_path) - variant = _pack_variant(pack_name) - if variant is None: - # One BIOS per slot: the slot choice is not replayed here. - print( - f" {platform_name} [{pack_name}]: hashes checked during the build; " - "one per slot by design, so not compared to the platform's full list" - ) - return True - source, required_only = variant - result = verify_pack_against_platform( - zip_path, - platform_name, - check.platforms_dir, - db=check.db, - emulators_dir=check.emulators_dir, - emu_profiles=check.profiles, - regions=check.regions, - data_registry=check.data_registry, - target_cores=target_cores, - source=source, - required_only=required_only, - no_game_data=check.no_game_data, - ) - (ok, _checked, _present, errors, bl_checked, bl_present, - core_checked, core_present, bl_excluded, bl_absent) = result - # These explain a baseline short of its total: a dump whose hash - # the frontend refuses is left out on purpose, and a file the - # collection does not hold cannot be packed (verify reports it). - shortfall = ", ".join(_baseline_shortfall(bl_excluded, bl_absent)) - excluded = f" ({shortfall})" if shortfall else "" - counts = ( - f"{bl_present}/{bl_checked} baseline{excluded}, " - f"{core_present}/{core_checked} cores" - ) - if ok: - print(f" {platform_name} [{pack_name}]: OK ({counts})") - return True - print(f" {platform_name} [{pack_name}]: FAIL ({counts}, {len(errors)} errors)") - for err in errors: - print(f" {err}") - return False - - def _verify_packs_scope(args: argparse.Namespace) -> tuple[list[str], dict[str, set[str] | None]]: """The platforms --verify-packs checks, and the cores of the target.""" if args.platform: @@ -3554,11 +3511,56 @@ def _run_verify_packs(args: argparse.Namespace) -> None: # A group pack and a pack built for the platform alone can both be # present, and each one is a pack of this platform. for pack_name in found: + variant = _pack_variant(pack_name) + if variant is None: + # One BIOS per slot: the slot choice is not replayed here. + print( + f" {platform_name} [{pack_name}]: hashes checked during the build; " + "one per slot by design, so not compared to the platform's full list" + ) + continue + source, required_only = variant zip_path = os.path.join(args.output_dir, pack_name) - if not _verify_found_pack(check, platform_name, zip_path, tc): + result = verify_pack_against_platform( + zip_path, + platform_name, + check.platforms_dir, + db=check.db, + emulators_dir=check.emulators_dir, + emu_profiles=check.profiles, + regions=check.regions, + data_registry=check.data_registry, + target_cores=tc, + source=source, + required_only=required_only, + no_game_data=check.no_game_data, + ) + (ok, _checked, _present, errors, bl_checked, bl_present, + core_checked, core_present, bl_excluded, bl_absent) = result + # These explain a baseline short of its total: a dump whose hash + # the frontend refuses is left out on purpose, and a file the + # collection does not hold cannot be packed (verify reports it). + shortfall = ", ".join(_baseline_shortfall(bl_excluded, bl_absent)) + excluded = f" ({shortfall})" if shortfall else "" + counts = ( + f"{bl_present}/{bl_checked} baseline{excluded}, " + f"{core_present}/{core_checked} cores" + ) + if ok: + print(f" {platform_name} [{pack_name}]: OK ({counts})") + else: + print(f" {platform_name} [{pack_name}]: FAIL ({counts}, {len(errors)} errors)") + for err in errors: + print(f" {err}") all_ok = False failing.append(f"{platform_name} ({pack_name})") + if not _verification_summary(failing, verified, args.output_dir) or not all_ok: + sys.exit(1) + + +def _verification_summary(failing: list[str], verified: int, output_dir: str) -> bool: + """Print how --verify-packs ended; False when nothing was verified.""" if failing: # Named at the end, where the log is read, and on stderr, as the # build path does: a failing pack sat among its own error lines. @@ -3571,10 +3573,9 @@ def _run_verify_packs(args: argparse.Namespace) -> None: if not verified: # Every platform skipped is no check at all: a mistyped or purged # output directory read as a passed integrity check. - print(f"ERROR: no pack to verify in {args.output_dir}", file=sys.stderr) - all_ok = False - if not all_ok: - sys.exit(1) + print(f"ERROR: no pack to verify in {output_dir}", file=sys.stderr) + return False + return True # The source x required combinations --all-variants builds, in build order. @@ -5769,6 +5770,7 @@ class _ManifestBuild: below records, the same tree the pack carries. """ opts = self.opts + required_only = opts.required_only data_refs: dict[str, tuple[str, tuple[str, ...], str, bool]] = {} reclaim = {full: path for full, (path, _size) in self.cache_held.items()} core_trees = ([] if opts.source == "platform" @@ -5777,7 +5779,7 @@ class _ManifestBuild: self.data_members = list(_data_directory_members( self.pack_systems, self.data_registry, self.platform_name, self.base_dest, self.case_insensitive, self.placed.destinations, self.placed.lower, - self.placed.parents, refs_out=data_refs, required_only=opts.required_only, + self.placed.parents, refs_out=data_refs, required_only=required_only, core_trees=core_trees, reclaim=reclaim, )) From de2cd60583893f8b6eba980c93b3e80aae37953a Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:02:39 +0100 Subject: [PATCH 7/9] refactor: split the pack readme by section --- scripts/packreadme.py | 302 +++++++++++++++++++++++++----------------- 1 file changed, 179 insertions(+), 123 deletions(-) diff --git a/scripts/packreadme.py b/scripts/packreadme.py index 7b3aea729..7a0716a09 100644 --- a/scripts/packreadme.py +++ b/scripts/packreadme.py @@ -5,9 +5,16 @@ goes, and which ones the user has to rename.""" from __future__ import annotations +from typing import TYPE_CHECKING, TypedDict + +from packextras import reject_unknown from region import label as region_label from siteguides import INSTALL_PS1, INSTALL_SH, pack_manual +if TYPE_CHECKING: + from typing_extensions import Unpack + + def _detect_systems(detect: list[dict]) -> list[str]: """The operating systems the registry's detection rules cover, in order.""" order = ("linux", "darwin", "windows") @@ -187,27 +194,33 @@ def _save_section( return "\n".join(lines) + "\n\n" +class ReadmeOptions(TypedDict, total=False): + """What a pack's README states besides its platform and its counts.""" + + source: str + contributors: list[dict] | None + regions: list[str] | None + fallback_systems: list[str] | None + one_per_slot: bool + undecidable_slots: list[str] | None + narrowings: list[tuple[str, str]] | None + system_filter: list[str] | None + detect: list[dict] | None + standalone_copies: list[dict] | None + launch_folders: int + save_folder: str + save_where: str + save_by_system: bool + case_twins: list[tuple[str, str]] | None + + def _build_readme( platform_name: str, platform_display: str, base_dest: str, total_files: int, num_systems: int, - source: str = "full", - contributors: list[dict] | None = None, - regions: list[str] | None = None, - fallback_systems: list[str] | None = None, - one_per_slot: bool = False, - undecidable_slots: list[str] | None = None, - narrowings: list[tuple[str, str]] | None = None, - system_filter: list[str] | None = None, - detect: list[dict] | None = None, - standalone_copies: list[dict] | None = None, - launch_folders: int = 0, - save_folder: str = "", - save_where: str = "", - save_by_system: bool = False, - case_twins: list[tuple[str, str]] | None = None, + **options: Unpack[ReadmeOptions], ) -> str: """Build a personalized step-by-step README for each platform pack. @@ -222,7 +235,9 @@ def _build_readme( where names keep their case (EmuDeck and RetroArch on Linux), where the core asking for the other spelling finds nothing. """ - narrowings = narrowings or [] + reject_unknown("_build_readme", options, ReadmeOptions.__optional_keys__) + regions = options.get("regions") + launch_folders = options.get("launch_folders", 0) sep = "=" * 50 scope = ( f"{launch_folders} launch folders" if launch_folders and not num_systems @@ -235,40 +250,7 @@ def _build_readme( f"{sep}\n\n" ) - detect = list(detect or []) - bios_paths = [str(rule.get("bios_path", "")) for rule in detect] - if any(rule.get("method") == "card_marker" for rule in detect): - guide = ( - "INSTALLATION GUIDE\n\n" - + _card_section(platform_name, platform_display) - + _card_manual(platform_display) - ) - else: - guide = ( - "INSTALLATION GUIDE\n\n" - + _automatic_section(platform_name, platform_display, detect, standalone_copies) - + (pack_manual(platform_name) or _generic_manual(bios_paths)) - ) - - if case_twins: - listed = "".join( - f" {missing} (the pack has {held})\n" for missing, held in case_twins - ) - guide += ( - "NAMES THAT DIFFER ONLY BY CASE\n\n" - " Some cores open the same file under another spelling. This\n" - " pack holds one spelling of each, all Windows and macOS can\n" - " store in one folder. Linux, SteamOS and Android keep both:\n" - " there, run the installer, which adds these spellings, or\n" - " copy each file under the name below.\n\n" - f"{listed}\n" - ) - - if save_folder: - guide += _save_section( - platform_display, save_folder, save_where, save_by_system, - in_frontend_settings=not save_by_system, in_place=not base_dest, - ) + guide = _install_guide(platform_name, platform_display, base_dest, options) if regions: region_help = ( @@ -292,83 +274,158 @@ def _build_readme( f"{sep}\n" ) - source_info = "" - if source == "platform": - source_info = ( - "PACK TYPE: Platform Only\n\n" - f" This pack contains only files declared by {platform_display}.\n" - " Core extras from emulator profiles are not included.\n" - " Use the Full pack for maximum coverage.\n\n" - ) - elif source == "truth": - source_info = ( - "PACK TYPE: Ground Truth\n\n" - " This pack contains files that emulators actually load,\n" - " based on source code analysis of emulator profiles.\n" - " Independent of platform scraper accuracy.\n\n" - ) - - region_info = "" - if regions: - pretty = ", ".join(region_label(slug) for slug in regions) - region_info = ( - "PACK TYPE: Region Filtered\n\n" - f" Region priority: {pretty}\n\n" - " Only the best-matching BIOS was kept for each system.\n" - ) - if fallback_systems: - listed = "\n".join(f" {_group_label(s)}" for s in fallback_systems) - region_info += ( - "\n These systems have no BIOS in those regions, so every\n" - " BIOS file for them was kept:\n" - f"{listed}\n" - ) - region_info += ( - "\n This shrinks the pack. It does not change how cores pick\n" - " a BIOS: most already select per region from fixed filename\n" - " lists driven by the game's region. Loading imports from\n" - " another region may need the unfiltered pack.\n\n" - ) - - slot_info = "" - if one_per_slot: - slot_info = ( - " Where a core declares which BIOS it prefers, only that one was\n" - " kept for each system and region. Systems whose cores declare no\n" - " order keep every BIOS file.\n" - ) - if undecidable_slots: - slot_info += ( - f" {len(undecidable_slots)} slot(s) had no declared order.\n" - ) - slot_info += "\n" - - narrowed = "" - labels = [label for _tag, label in narrowings] - if system_filter: - labels.append(f"systems {', '.join(system_filter)}") - if labels: - listed = "".join(f" {label}\n" for label in labels) - narrowed = ( - "PACK TYPE: Narrowed\n\n" - " This pack holds fewer files than the full one:\n" - f"{listed}" - "\n The unfiltered pack is the one to use when in doubt.\n\n" - ) - - credits = "" - if contributors: - credits = "\nCONTRIBUTORS\n\n" - for cb in contributors: - username = cb.get("username", "") - credits += f" @{username}\n" - credits += "\n" + source_info = _source_info(options.get("source", "full"), platform_display) + region_info = _region_info(regions, options.get("fallback_systems")) + slot_info = _slot_info( + options.get("one_per_slot", False), options.get("undecidable_slots") + ) + narrowed = _narrowed_info( + options.get("narrowings") or [], options.get("system_filter") + ) + credits = _credits(options.get("contributors")) return ( header + narrowed + source_info + region_info + slot_info + guide + credits + footer ) + +def _install_guide( + platform_name: str, platform_display: str, base_dest: str, options: ReadmeOptions +) -> str: + """The installation steps, the case twins and the save folder.""" + detect = list(options.get("detect") or []) + bios_paths = [str(rule.get("bios_path", "")) for rule in detect] + if any(rule.get("method") == "card_marker" for rule in detect): + guide = ( + "INSTALLATION GUIDE\n\n" + + _card_section(platform_name, platform_display) + + _card_manual(platform_display) + ) + else: + guide = ( + "INSTALLATION GUIDE\n\n" + + _automatic_section( + platform_name, platform_display, detect, options.get("standalone_copies") + ) + + (pack_manual(platform_name) or _generic_manual(bios_paths)) + ) + + case_twins = options.get("case_twins") + if case_twins: + listed = "".join( + f" {missing} (the pack has {held})\n" for missing, held in case_twins + ) + guide += ( + "NAMES THAT DIFFER ONLY BY CASE\n\n" + " Some cores open the same file under another spelling. This\n" + " pack holds one spelling of each, all Windows and macOS can\n" + " store in one folder. Linux, SteamOS and Android keep both:\n" + " there, run the installer, which adds these spellings, or\n" + " copy each file under the name below.\n\n" + f"{listed}\n" + ) + + save_folder = options.get("save_folder", "") + if save_folder: + save_by_system = options.get("save_by_system", False) + guide += _save_section( + platform_display, save_folder, options.get("save_where", ""), save_by_system, + in_frontend_settings=not save_by_system, in_place=not base_dest, + ) + return guide + + +def _source_info(source: str, platform_display: str) -> str: + """The PACK TYPE block of a platform-only or ground-truth pack.""" + if source == "platform": + return ( + "PACK TYPE: Platform Only\n\n" + f" This pack contains only files declared by {platform_display}.\n" + " Core extras from emulator profiles are not included.\n" + " Use the Full pack for maximum coverage.\n\n" + ) + if source == "truth": + return ( + "PACK TYPE: Ground Truth\n\n" + " This pack contains files that emulators actually load,\n" + " based on source code analysis of emulator profiles.\n" + " Independent of platform scraper accuracy.\n\n" + ) + return "" + + +def _region_info(regions: list[str] | None, fallback_systems: list[str] | None) -> str: + """The PACK TYPE block of a region-filtered pack.""" + if not regions: + return "" + pretty = ", ".join(region_label(slug) for slug in regions) + region_info = ( + "PACK TYPE: Region Filtered\n\n" + f" Region priority: {pretty}\n\n" + " Only the best-matching BIOS was kept for each system.\n" + ) + if fallback_systems: + listed = "\n".join(f" {_group_label(s)}" for s in fallback_systems) + region_info += ( + "\n These systems have no BIOS in those regions, so every\n" + " BIOS file for them was kept:\n" + f"{listed}\n" + ) + region_info += ( + "\n This shrinks the pack. It does not change how cores pick\n" + " a BIOS: most already select per region from fixed filename\n" + " lists driven by the game's region. Loading imports from\n" + " another region may need the unfiltered pack.\n\n" + ) + return region_info + + +def _slot_info(one_per_slot: bool, undecidable_slots: list[str] | None) -> str: + """What --one-per-slot kept.""" + if not one_per_slot: + return "" + slot_info = ( + " Where a core declares which BIOS it prefers, only that one was\n" + " kept for each system and region. Systems whose cores declare no\n" + " order keep every BIOS file.\n" + ) + if undecidable_slots: + slot_info += ( + f" {len(undecidable_slots)} slot(s) had no declared order.\n" + ) + return slot_info + "\n" + + +def _narrowed_info( + narrowings: list[tuple[str, str]], system_filter: list[str] | None +) -> str: + """The PACK TYPE block of a pack holding fewer files than the full one.""" + labels = [label for _tag, label in narrowings] + if system_filter: + labels.append(f"systems {', '.join(system_filter)}") + if not labels: + return "" + listed = "".join(f" {label}\n" for label in labels) + return ( + "PACK TYPE: Narrowed\n\n" + " This pack holds fewer files than the full one:\n" + f"{listed}" + "\n The unfiltered pack is the one to use when in doubt.\n\n" + ) + + +def _credits(contributors: list[dict] | None) -> str: + """The CONTRIBUTORS block.""" + if not contributors: + return "" + credits = "\nCONTRIBUTORS\n\n" + for cb in contributors: + username = cb.get("username", "") + credits += f" @{username}\n" + return credits + "\n" + + def _build_agnostic_rename_readme( destination: str, original: str, @@ -382,8 +439,7 @@ def _build_agnostic_rename_readme( ] if alternatives: lines.append("All variants included in this pack:") - for alt in sorted(alternatives): - lines.append(f" {alt}") + lines.extend(f" {alt}" for alt in sorted(alternatives)) lines.append("") lines.append(f"To use a different variant, rename it to: {destination}") return "\n".join(lines) + "\n" From 2a92089d9285f0a2c955085da3ba677528530228 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:02:39 +0100 Subject: [PATCH 8/9] refactor: split packextras by stage --- scripts/packextras.py | 1080 +++++++++++++++++++++++++---------------- 1 file changed, 664 insertions(+), 416 deletions(-) diff --git a/scripts/packextras.py b/scripts/packextras.py index e713b229e..464150244 100644 --- a/scripts/packextras.py +++ b/scripts/packextras.py @@ -6,7 +6,9 @@ this module builds so the report and the pack withdraw the same files.""" from __future__ import annotations -from collections.abc import Iterable +from collections.abc import Iterable, Mapping +from dataclasses import dataclass, field +from typing import TYPE_CHECKING, TypedDict from common import PROFILE_IDENTITY_FIELDS, _norm_system_id, entry_system_ids from alternatives import core_file_spares, required_only_spares, set_fields @@ -24,6 +26,16 @@ from validation import deliverable, read_from_save_dir, read_from_system_dir from packpaths import libretro_systems, save_destination, save_layout from region import FILE_GROUP +if TYPE_CHECKING: + from typing_extensions import Unpack + + +def reject_unknown(function: str, options: Mapping[str, object], known: frozenset[str]) -> None: + """Refuse a keyword the options do not declare, as a named parameter would.""" + for key in options: + if key not in known: + raise TypeError(f"{function}() got an unexpected keyword argument '{key}'") + def _detect_extras_prefix(config: dict, base_dest: str) -> str: """The prefix core-extra destinations take (may be empty). @@ -256,6 +268,10 @@ def _file_sha1(path: str) -> str | None: return None +class ResolverOptions(TypedDict, total=False): + pending_assets: bool + + class ExtraResolver: """How the builder finds the copy of a core's file it packs. @@ -277,7 +293,7 @@ class ExtraResolver: offline: bool | None = True, *, profiles: dict, - pending_assets: bool = False, + **options: Unpack[ResolverOptions], ) -> None: self.profiles = profiles self.db = db @@ -289,7 +305,8 @@ class ExtraResolver: self.offline = offline # verify's replay: never downloads, and counts the release asset an # online build fetches, as it does for a platform entry. - self.pending_assets = pending_assets + reject_unknown("ExtraResolver", options, ResolverOptions.__optional_keys__) + self.pending_assets = options.get("pending_assets", False) # Each profile's entries by slot, built on first use: every extra # scanned its whole profile, 33 million entries in one verify --all. self._slots: dict[str, dict[tuple[str, str], list[dict]]] = {} @@ -341,6 +358,18 @@ class ExtraResolver: return local_path, status, disagreement +class HiddenOptions(TypedDict, total=False): + """How hidden_extras replays the builder, and what else it fills.""" + + report: list[dict] | None + resolver: ExtraResolver | None + placed_kind: dict[str, str] | None + placed_local: dict[str, str] | None + system_ids: list[str] | None + include_all: bool + required_only: bool + + def hidden_extras( config: dict, emulators_dir: str, @@ -350,13 +379,7 @@ def hidden_extras( target_cores: set[str] | None = None, region_drops: set[str] | None = None, placed: set[str] | None = None, - report: list[dict] | None = None, - resolver: ExtraResolver | None = None, - placed_kind: dict[str, str] | None = None, - placed_local: dict[str, str] | None = None, - system_ids: list[str] | None = None, - include_all: bool = False, - required_only: bool = False, + **options: Unpack[HiddenOptions], ) -> dict[tuple[str, str, str], str]: """Report entries the builder holds and still leaves out, with the reason. @@ -386,6 +409,14 @@ def hidden_extras( """ from packpaths import _register_path, placement_block + reject_unknown("hidden_extras", options, HiddenOptions.__optional_keys__) + report = options.get("report") + resolver = options.get("resolver") + placed_kind = options.get("placed_kind") + placed_local = options.get("placed_local") + system_ids = options.get("system_ids") + include_all = options.get("include_all", False) + required_only = options.get("required_only", False) sha1_by_path = { entry.get("path"): sha1 for sha1, entry in db.get("files", {}).items() } @@ -416,13 +447,8 @@ def hidden_extras( for fe in extras: if required_only and (fe.get("required") is False or id(fe) in spares): continue - dest = sanitize_pack_path(fe.get("destination", fe.get("name", ""))) - if not dest or (region_drops and extra_region_key(fe, region_prefix) in region_drops): - continue - if fe.get("root") in OUTER_ROOTS: - # As the builder does: the pack is extracted into the BIOS folder - # and cannot reach the roms, emulator or configs folder, the - # installer places these files. + dest = _packable_dest(fe, region_drops, region_prefix) + if not dest: continue full = extra_full_destination(fe, prefix) if not full: @@ -455,17 +481,43 @@ def hidden_extras( content[full.lower()] = held if case_insensitive: lower.add(full.lower()) - if placed is not None and local: - placed.add(full) - if placed_kind is not None and local: - placed_kind[full] = fe.get("category", "bios") - if placed_local is not None and local: - placed_local[full] = local + if local: + _record_placed(full, local, fe, placed, placed_kind, placed_local) # A file read below a save directory kept per system is packed once per # system: one copy left out does not leave the file out. return {key: reason for key, reason in hidden.items() if key not in packed_keys} +def _packable_dest(fe: dict, region_drops: set[str] | None, region_prefix: str) -> str: + """The extra's destination, empty when the builder never places it.""" + dest = sanitize_pack_path(fe.get("destination", fe.get("name", ""))) + if not dest or (region_drops and extra_region_key(fe, region_prefix) in region_drops): + return "" + if fe.get("root") in OUTER_ROOTS: + # As the builder does: the pack is extracted into the BIOS folder + # and cannot reach the roms, emulator or configs folder, the + # installer places these files. + return "" + return dest + + +def _record_placed( + full: str, + local: str, + fe: dict, + placed: set[str] | None, + placed_kind: dict[str, str] | None, + placed_local: dict[str, str] | None, +) -> None: + """Note a held extra the pack carries in each record the caller asked for.""" + if placed is not None: + placed.add(full) + if placed_kind is not None: + placed_kind[full] = fe.get("category", "bios") + if placed_local is not None: + placed_local[full] = local + + def _extra_dest(u: dict) -> str: """Where a report entry lands before any platform prefix. @@ -504,6 +556,43 @@ def unplaceable_extras( return unplaced +@dataclass(frozen=True) +class _ScanContext: + """What every seed of one agnostic scan reads and fills.""" + + db: dict + files_db: dict + by_name: dict + seen_dests: set + extras_prefix: str + taken: dict[str, dict] | None + profiles: dict + + +@dataclass(frozen=True) +class _ResolvedDirs: + """Where a profile's own files resolve, the evidence a scan anchors on.""" + + dirs: dict[int, str] + named_only: set[int] + votes: dict[str, int] + + +@dataclass(frozen=True) +class _ScanSeed: + """One agnostic entry of one layout and the folder its scan walks.""" + + emu_name: str + layout: dict + entry: dict + name: str + companion_ext: str + image: dict | None + path_prefix: str + exact_dirs: frozenset[str] + folder: str + + def _agnostic_scan_extras( profiles: dict, relevant: set, @@ -513,7 +602,6 @@ def _agnostic_scan_extras( extras_prefix: str, standalone_set: set[str] | None = None, taken: dict[str, dict] | None = None, - all_profiles: dict | None = None, ) -> list[dict]: """Every interchangeable candidate a filename-agnostic core accepts. @@ -522,11 +610,11 @@ def _agnostic_scan_extras( The directory is the delicate part: anchoring it on a single filename walked into another emulator's tree, so the profile's own files have to agree before the scan follows an ambiguous name. - ``all_profiles`` are the profiles a placeholder's directory is proven - from, ``profiles`` when not given. """ extras: list[dict] = [] - files_db = db.get("files", {}) + context = _ScanContext( + db, db.get("files", {}), by_name, seen_dests, extras_prefix, taken, profiles, + ) # Third pass: the agnostic scan. For filename-agnostic cores, include all # DB files matching the system path prefix and size criteria. for emu_name, profile in sorted(profiles.items()): @@ -535,199 +623,253 @@ def _agnostic_scan_extras( if emu_name not in relevant: continue is_profile_agnostic = profile.get("bios_mode") == "agnostic" - if not is_profile_agnostic: - if not any(f.get("agnostic") for f in profile.get("files", [])): - continue - - # Where each of this profile's files resolves. A scan anchored on one - # filename walks into whatever tree that name happens to hit: rom1.bin - # is a PS2 ROM and a Roland SC-55 ROM, GameIndex.yaml belongs to four - # PS2 profiles and to an Android package. One match is not evidence - # of a directory, two files of the same profile agreeing is. - resolved_dirs: dict[int, str] = {} - named_only: set[int] = set() - agnostic_votes: dict[str, int] = {} - for candidate in profile.get("files", []): - if not isinstance(candidate, dict): - continue - local, status = resolve_local_file( - {**candidate, "source_profile": emu_name}, - db, - dest_hint=candidate.get("path", ""), - ) - if not local or "/" not in local: - continue - directory = local.rsplit("/", 1)[0] - if directory.endswith("/.variants"): - directory = directory[: -len("/.variants")] - resolved_dirs[id(candidate)] = directory + "/" - if not (resolution_is_hash_exact(status) or status == "path_exact"): - named_only.add(id(candidate)) - agnostic_votes[directory + "/"] = ( - agnostic_votes.get(directory + "/", 0) + 1 - ) - - own = profile - for profile, standalone in platform_layouts(emu_name, own, standalone_set or set()): + if not is_profile_agnostic and not any( + f.get("agnostic") for f in profile.get("files", []) + ): + continue + resolved = _resolved_dirs(emu_name, profile, db) + for layout, standalone in platform_layouts(emu_name, profile, standalone_set or set()): # A layout copies each entry in order (a launcher's folder, a # system directory): its files are looked up as the profile's # own, or a bound layout scans nothing (KNULLI's bios/ps2). origin = { id(view): entry - for view, entry in zip(profile.get("files", []), own.get("files", [])) + for view, entry in zip(layout.get("files", []), profile.get("files", [])) } - for f in profile.get("files", []): + for f in layout.get("files", []): if not is_profile_agnostic and not f.get("agnostic"): continue - fname = f.get("name", "") - if not fname: - continue - # A file of the build the platform does not run seeds nothing: - # armsx2's proverb.zip is fetched by the Android app into its own - # arcade/ folder, and the libretro packs carried it at system/. - mode = f.get("mode") - if (mode == "standalone" and not standalone) or ( - mode == "libretro" and standalone - ): - continue - # The scan walks the BIOS tree: a file read below the save - # directory is placed by the gap report's pass. - if (read_from_save_dir(f, standalone) or f.get("unplaced") - or f.get("root") in OUTER_ROOTS or f.get("unprefixed")): - continue - # An agnostic BIOS mode says the BIOS filename is free, not that - # every file the emulator loads is. A free filename still has a - # known shape, and that shape is what identifies a candidate: an - # entry declaring no size takes the whole directory, which is how - # the flag icons of an Android package walked into a PS2 pack. - if f.get("category", "bios") != "bios": - continue - if not declares_shape(f): - continue - # The free name is the BIOS directory's: a shader, a sound or a - # font the emulator reads from its resources tree has a fixed - # name and must not seed a scan (yaps2 flattened 189 of them to - # the root of the RetroArch pack). - bios_dir = profile.get("bios_directory") - fpath = f.get("path") or "" - if bios_dir and "/" in fpath and not fpath.startswith(bios_dir): - continue + seed = _scan_seed(context, emu_name, layout, f, standalone, origin, resolved) + if seed is not None: + extras.extend(_seed_extras(context, seed)) + return extras - # A companion is read under its image's name with the - # extension the code appends, in that case: only a file of - # that extension is one, spelled as the code spells it. Its - # size identifies nothing, PCSX2 reads min(size, filesize) - # (BiosTools.cpp:234), and a 4 MB bound swept every smaller - # file of the folder to the BIOS root. It sits beside its - # image, so the scan looks where the image resolves: the - # held ps2-0230a-20080220.rom1 is a DVD firmware dump filed - # under DVD Firmware/. - companion_ext, image = _companion_of(profile, fname) - path_prefix = ( - resolved_dirs.get(id(origin.get(id(image), image)), "") if image - else resolved_dirs.get(id(origin.get(id(f), f)), "") - ) - # A placeholder (.bin) resolves to nothing and anchors no - # directory. The folders where the system's other profiles - # resolve hashed files of that shape do, those folders only: - # LRPS2 and PCEE2 are made of placeholders, and on Batocera, - # where no other libretro PS2 core anchors pcsx2/bios/, the - # pack held nothing there. A companion (.nvm) looks - # where its image is proven: RetroBat's pcsx2/bios/ had - # scph39001.bin and its .nvm only at the BIOS root. - exact_dirs: frozenset[str] = frozenset() - anchor = image if image else f - if not path_prefix and "<" in str(anchor.get("name", "")): - exact_dirs = placeholder_directories( - anchor, emu_name, profile, db, all_profiles or profiles - ) - if not path_prefix and not exact_dirs: - continue - # A name matching one file identifies it. A name matching several - # identifies nothing on its own, so the profile's other files have - # to agree on the directory before the scan walks it. - ambiguous = ( - id(origin.get(id(f), f)) in named_only - and len(by_name.get(fname, [])) > 1 - ) - if ambiguous and agnostic_votes.get(path_prefix, 0) < 2: - continue +def _resolved_dirs(emu_name: str, profile: dict, db: dict) -> _ResolvedDirs: + """Where each of this profile's files resolves. - # Scan DB for all files under this prefix matching size - for sha1, entry in files_db.items(): - path = entry.get("path", "") - if exact_dirs: - if ("/" not in path or path.rsplit("/", 1)[0] not in exact_dirs - or "/.variants/" in path): - continue - elif not path.startswith(path_prefix): - continue - if not companion_ext and not shape_fits(f, path, entry.get("size", 0)): - continue - scan_name = entry.get("name", "") - if not scan_name: - continue - if companion_ext: - stem, _, ext = scan_name.rpartition(".") - if not stem or ext.lower() != companion_ext.lower(): - continue - # Read only beside the image the core selected, so a - # companion counts only next to an image the image's - # own test accepts: Namco System 246 .nvm and .mec - # dumps sit beside no PS2 BIOS. - if not _image_beside(path, stem, image, files_db): - continue - scan_name = f"{stem}.{companion_ext}" - # Into the directory the seed entry names, not the root: - # yaps2 reads its BIOS from pcsx2/bios/. - seed = f.get("path") or "" - if standalone: - seed = f.get("standalone_path") or seed - else: - seed = system_dir_path(f, seed or fname) - folder = seed.rsplit("/", 1)[0] if "/" in seed else "" - dest = f"{folder}/{scan_name}" if folder else scan_name - full_dest = prefixed_destination(dest, extras_prefix) - if full_dest in seen_dests and (taken is None or full_dest not in taken): - continue - extra = { - "name": scan_name, - "destination": dest, - # The scan already holds the entry it selected, so it - # names it by content. Emitting the filename alone - # sent the packing step back to a by-name lookup, and - # three PS2 resources came back from another - # emulator's copy of the same filename. - "sha1": sha1, - "required": False, - "hle_fallback": False, - "source_emulator": profile.get("emulator", emu_name), - "source_profile": emu_name, - "source_name": fname, - "source_path": f.get("path") or "", - "category": f.get("category", "bios"), - "source_system": f.get("system"), - "source_systems": entry_system_ids(f, profile), - "region": f.get("region"), - "selection_slot": f.get("selection_slot"), - "agnostic_scan": True, - } - extra["claim_order"] = (4, 0, emu_name) - if _take(extra, full_dest, seen_dests, taken): - extras.append(extra) - else: - # The image another core's scan placed is this core's - # too, and so are the companions it reads beside it: - # Recalbox's libretro PCSX2 opens .erom next to - # the images LRPS2 placed, which LRPS2 never reads. - held = taken.get(full_dest) if taken is not None else None - if held is None or held.get("sha1") != sha1: - continue - extras.extend(_companion_extras( - _companions(path, scan_name, _companion_extensions(profile, fname), files_db), - folder, extras_prefix, seen_dests, profile, emu_name, f, taken, - )) + A scan anchored on one filename walks into whatever tree that name + happens to hit: rom1.bin is a PS2 ROM and a Roland SC-55 ROM, + GameIndex.yaml belongs to four PS2 profiles and to an Android package. + One match is not evidence of a directory, two files of the same profile + agreeing is. + """ + resolved_dirs: dict[int, str] = {} + named_only: set[int] = set() + agnostic_votes: dict[str, int] = {} + for candidate in profile.get("files", []): + if not isinstance(candidate, dict): + continue + local, status = resolve_local_file( + {**candidate, "source_profile": emu_name}, + db, + dest_hint=candidate.get("path", ""), + ) + if not local or "/" not in local: + continue + directory = local.rsplit("/", 1)[0] + if directory.endswith("/.variants"): + directory = directory[: -len("/.variants")] + resolved_dirs[id(candidate)] = directory + "/" + if not (resolution_is_hash_exact(status) or status == "path_exact"): + named_only.add(id(candidate)) + agnostic_votes[directory + "/"] = ( + agnostic_votes.get(directory + "/", 0) + 1 + ) + return _ResolvedDirs(resolved_dirs, named_only, agnostic_votes) + + +def _seeds_a_scan(f: dict, layout: dict, standalone: bool) -> bool: + """Whether an agnostic entry may seed a scan of the BIOS directory.""" + # A file of the build the platform does not run seeds nothing: + # armsx2's proverb.zip is fetched by the Android app into its own + # arcade/ folder, and the libretro packs carried it at system/. + mode = f.get("mode") + if (mode == "standalone" and not standalone) or ( + mode == "libretro" and standalone + ): + return False + # The scan walks the BIOS tree: a file read below the save + # directory is placed by the gap report's pass. + if (read_from_save_dir(f, standalone) or f.get("unplaced") + or f.get("root") in OUTER_ROOTS or f.get("unprefixed")): + return False + # An agnostic BIOS mode says the BIOS filename is free, not that + # every file the emulator loads is. A free filename still has a + # known shape, and that shape is what identifies a candidate: an + # entry declaring no size takes the whole directory, which is how + # the flag icons of an Android package walked into a PS2 pack. + if f.get("category", "bios") != "bios": + return False + if not declares_shape(f): + return False + # The free name is the BIOS directory's: a shader, a sound or a + # font the emulator reads from its resources tree has a fixed + # name and must not seed a scan (yaps2 flattened 189 of them to + # the root of the RetroArch pack). + bios_dir = layout.get("bios_directory") + fpath = f.get("path") or "" + return not (bios_dir and "/" in fpath and not fpath.startswith(bios_dir)) + + +def _scan_seed( + context: _ScanContext, + emu_name: str, + layout: dict, + f: dict, + standalone: bool, + origin: dict[int, dict], + resolved: _ResolvedDirs, +) -> _ScanSeed | None: + """The scan an agnostic entry seeds, None when it seeds none.""" + fname = f.get("name", "") + if not fname or not _seeds_a_scan(f, layout, standalone): + return None + + # A companion is read under its image's name with the + # extension the code appends, in that case: only a file of + # that extension is one, spelled as the code spells it. Its + # size identifies nothing, PCSX2 reads min(size, filesize) + # (BiosTools.cpp:234), and a 4 MB bound swept every smaller + # file of the folder to the BIOS root. It sits beside its + # image, so the scan looks where the image resolves: the + # held ps2-0230a-20080220.rom1 is a DVD firmware dump filed + # under DVD Firmware/. + companion_ext, image = _companion_of(layout, fname) + + path_prefix = ( + resolved.dirs.get(id(origin.get(id(image), image)), "") if image + else resolved.dirs.get(id(origin.get(id(f), f)), "") + ) + # A placeholder (.bin) resolves to nothing and anchors no + # directory. The folders where the system's other profiles + # resolve hashed files of that shape do, those folders only: + # LRPS2 and PCEE2 are made of placeholders, and on Batocera, + # where no other libretro PS2 core anchors pcsx2/bios/, the + # pack held nothing there. A companion (.nvm) looks + # where its image is proven: RetroBat's pcsx2/bios/ had + # scph39001.bin and its .nvm only at the BIOS root. + exact_dirs: frozenset[str] = frozenset() + anchor = image if image else f + if not path_prefix and "<" in str(anchor.get("name", "")): + exact_dirs = placeholder_directories( + anchor, emu_name, layout, context.db, context.profiles + ) + if not path_prefix and not exact_dirs: + return None + # A name matching one file identifies it. A name matching several + # identifies nothing on its own, so the profile's other files have + # to agree on the directory before the scan walks it. + ambiguous = ( + id(origin.get(id(f), f)) in resolved.named_only + and len(context.by_name.get(fname, [])) > 1 + ) + if ambiguous and resolved.votes.get(path_prefix, 0) < 2: + return None + + # Into the directory the seed entry names, not the root: + # yaps2 reads its BIOS from pcsx2/bios/. + seed = f.get("path") or "" + if standalone: + seed = f.get("standalone_path") or seed + else: + seed = system_dir_path(f, seed or fname) + folder = seed.rsplit("/", 1)[0] if "/" in seed else "" + return _ScanSeed( + emu_name, layout, f, fname, companion_ext, image, path_prefix, exact_dirs, folder, + ) + + +def _scanned_name(context: _ScanContext, seed: _ScanSeed, entry: dict) -> tuple[str, str] | None: + """(path, name in the pack) of a database file the seed's scan accepts.""" + path = entry.get("path", "") + if seed.exact_dirs: + if ("/" not in path or path.rsplit("/", 1)[0] not in seed.exact_dirs + or "/.variants/" in path): + return None + elif not path.startswith(seed.path_prefix): + return None + if not seed.companion_ext and not shape_fits(seed.entry, path, entry.get("size", 0)): + return None + scan_name = entry.get("name", "") + if not scan_name: + return None + if seed.companion_ext: + stem, _, ext = scan_name.rpartition(".") + if not stem or ext.lower() != seed.companion_ext.lower(): + return None + # Read only beside the image the core selected, so a + # companion counts only next to an image the image's + # own test accepts: Namco System 246 .nvm and .mec + # dumps sit beside no PS2 BIOS. + if not _image_beside(path, stem, seed.image, context.files_db): + return None + scan_name = f"{stem}.{seed.companion_ext}" + return path, scan_name + + +def _scan_extra(seed: _ScanSeed, scan_name: str, dest: str, sha1: str) -> dict: + """The extra a scan emits for one database file.""" + f, layout, emu_name = seed.entry, seed.layout, seed.emu_name + extra = { + "name": scan_name, + "destination": dest, + # The scan already holds the entry it selected, so it + # names it by content. Emitting the filename alone + # sent the packing step back to a by-name lookup, and + # three PS2 resources came back from another + # emulator's copy of the same filename. + "sha1": sha1, + "required": False, + "hle_fallback": False, + "source_emulator": layout.get("emulator", emu_name), + "source_profile": emu_name, + "source_name": seed.name, + "source_path": f.get("path") or "", + "category": f.get("category", "bios"), + "source_system": f.get("system"), + "source_systems": entry_system_ids(f, layout), + "region": f.get("region"), + "selection_slot": f.get("selection_slot"), + "agnostic_scan": True, + } + extra["claim_order"] = (4, 0, emu_name) + return extra + + +def _seed_extras(context: _ScanContext, seed: _ScanSeed) -> list[dict]: + """The extras one seed's scan places, companions included.""" + extras: list[dict] = [] + taken = context.taken + # Scan DB for all files under this prefix matching size + for sha1, entry in context.files_db.items(): + scanned = _scanned_name(context, seed, entry) + if scanned is None: + continue + path, scan_name = scanned + dest = f"{seed.folder}/{scan_name}" if seed.folder else scan_name + full_dest = prefixed_destination(dest, context.extras_prefix) + if full_dest in context.seen_dests and (taken is None or full_dest not in taken): + continue + extra = _scan_extra(seed, scan_name, dest, sha1) + if _take(extra, full_dest, context.seen_dests, taken): + extras.append(extra) + else: + # The image another core's scan placed is this core's + # too, and so are the companions it reads beside it: + # Recalbox's libretro PCSX2 opens .erom next to + # the images LRPS2 placed, which LRPS2 never reads. + held = taken.get(full_dest) if taken is not None else None + if held is None or held.get("sha1") != sha1: + continue + extras.extend(_companion_extras( + _companions( + path, scan_name, _companion_extensions(seed.layout, seed.name), + context.files_db, + ), + seed.folder, context.extras_prefix, context.seen_dests, seed.layout, + seed.emu_name, seed.entry, taken, + )) return extras @@ -1220,7 +1362,7 @@ class ExtraPlacement: """The names a report entry's core goes by: its profile key and the upstream names its profile lists.""" key = entry.get("profile") or entry.get("emulator", "") - profile = self.profiles.get(key) or next( + profile: dict = self.profiles.get(key) or next( (pp for pp in self.profiles.values() if pp.get("emulator") == key), {} ) return {str(key), *(str(c) for c in profile.get("cores") or [])} - {""} @@ -1266,6 +1408,48 @@ def undeclared_report( ) +class CollectOptions(TypedDict, total=False): + """The records _collect_emulator_extras fills, and the report it reuses.""" + + claimants: dict[tuple[str, str, str], str] | None + unplaceable: list[dict] | None + baseline_claims: dict[str, dict] | None + absent: list[dict] | None + undeclared: list[dict] | None + + +@dataclass +class _FirstPass: + """The state the gap report's pass threads through its entries.""" + + config: dict + placement: ExtraPlacement + rank: _ClaimRanks + seen_dests: set[str] + # Destination -> the extra that took it, to record later claimants on. + taken: dict[str, dict] + claimants: dict[tuple[str, str, str], str] | None + unplaceable: list[dict] | None + absent: list[dict] | None + extras: list[dict] = field(default_factory=list) + + +@dataclass(frozen=True) +class _CopyScope: + """What the second pass reads to copy a covered file to another path.""" + + config_key: str + covered_names: set[str] + platform_names: set[str] + seen: set + seen_dests: set[str] + taken: dict[str, dict] + copied: dict[str, list[dict]] + by_name: dict + by_path_suffix: dict + extras_prefix: str + + def _collect_emulator_extras( config: dict, emulators_dir: str, @@ -1275,11 +1459,7 @@ def _collect_emulator_extras( emu_profiles: dict | None = None, target_cores: set[str] | None = None, include_all: bool = False, - claimants: dict[tuple[str, str, str], str] | None = None, - unplaceable: list[dict] | None = None, - baseline_claims: dict[str, dict] | None = None, - absent: list[dict] | None = None, - undeclared: list[dict] | None = None, + **options: Unpack[CollectOptions], ) -> list[dict]: """Collect core requirement files from emulator profiles not in the platform pack. @@ -1313,6 +1493,12 @@ def _collect_emulator_extras( Works for ANY platform (RetroArch, Batocera, Recalbox, etc.) """ + reject_unknown("_collect_emulator_extras", options, CollectOptions.__optional_keys__) + undeclared = options.get("undeclared") + claimants = options.get("claimants") + unplaceable = options.get("unplaceable") + absent = options.get("absent") + baseline_claims = options.get("baseline_claims") profiles = ( emu_profiles if emu_profiles is not None @@ -1322,7 +1508,6 @@ def _collect_emulator_extras( # Detect destination conventions for core extras placement = ExtraPlacement(config, base_dest, profiles) extras_prefix = placement.prefix - is_slug_based = placement.slug_based if undeclared is None: undeclared = undeclared_report( @@ -1339,86 +1524,15 @@ def _collect_emulator_extras( undeclared, placement, db, bool(config.get("case_insensitive_fs", False)) ) undeclared = rank.ordered(undeclared) - extras = [] - seen_dests: set[str] = set(seen) - # Destination -> the extra that took it, to record later claimants on. - taken: dict[str, dict] = {} + state = _FirstPass( + config, placement, rank, set(seen), {}, claimants, unplaceable, absent, + ) if baseline_claims is not None: for destination in seen: - taken[destination] = baseline_claims.setdefault(destination, {}) + state.taken[destination] = baseline_claims.setdefault(destination, {}) for u in undeclared: - if u.get("bundled"): - # Installed with the emulator for this build: already on the - # user's machine. The mode a file is bundled for was settled - # upstream, an entry scoped to another build never gets here. - continue - if u.get("unplaced"): - # Read in a folder outside the BIOS tree: named, held or not, - # never written where the emulator does not read it. - if unplaceable is not None: - unplaceable.append({**u, "unplaceable": u["unplaced"]}) - continue - if not u["in_repo"] and absent is None: - continue - # For archive entries, use the archive name for resolution - archive = u.get("archive") - name = archive if archive else u["name"] - dest = _extra_dest(u) - - # Slug-based platforms: prefix dest with system slug. A file read - # below a save directory kept per system goes below each system. - placements = placement.places(u, dest) - save_root = u.get("root") == "save" - if not u["in_repo"]: - # Each core reading it is named on the omission: a list built - # without the first reader still has to say the file is missing. - for placed in placements: - for claim in (u, *u.get("other_claims", [])): - missing = _report_extra(claim, name, placed[0]) - if save_root: - _save_fields(missing, placed[1], placement) - absent.append(missing) - continue - if not placements: - if unplaceable is not None: - reason = placement.unplaced_reason(u, dest) - unplaceable.append( - u if reason == "no_platform_slug" else {**u, "unplaceable": reason} - ) - continue - if claimants is not None: - # Keyed as extra_region_key keys the extra that wins the path: a - # losing claim kept the bare path, and on RetroDECK (declared - # root bios/) verify --region listed Rustation's scph1002b.bin - # as packed while the builder had withdrawn bios/scph1002b.bin. - claimants[(u.get("emulator", ""), u.get("name", ""), u.get("path") or "")] = ( - prefixed_destination( - sanitize_pack_path(placements[0][0]), declared_root(config) - ) - ) - for dest, full_dest in placements: - candidates = [ - _report_extra(c, name, dest) for c in (u, *u.get("other_claims", [])) - ] - if save_root: - for item in candidates: - _save_fields(item, full_dest, placement) - # Where the builder meets each claim: the strongest proof first, the - # report's profile order between equals, archives after loose files. - for item in candidates: - item["claim_order"] = ( - 1, 1 if u.get("archive") else 0, -rank.of_extra(item, full_dest), - item["source_profile"], - ) - candidates.sort(key=lambda item: item["claim_order"]) - extra, claims = candidates[0], candidates[1:] - if not _take(extra, full_dest, seen_dests, taken): - for claim in claims: - _another_claim(taken.get(full_dest), claim) - continue - for claim in claims: - _another_claim(extra, claim) - extras.append(extra) + _place_report_entry(state, u) + extras = state.extras # Second pass: find alternative destinations for files already in the pack. # A file declared by the platform or emitted above may also be needed at a @@ -1428,13 +1542,12 @@ def _collect_emulator_extras( # # Skip for slug-based platforms (RomM): alternative paths don't map to # the required {platform_slug}/{file} structure. - if is_slug_based: + if placement.slug_based: return extras relevant = resolve_platform_cores(config, profiles, target_cores=target_cores) standalone_set = platform_standalone_set(config) by_name = db.get("indexes", {}).get("by_name", {}) - by_path_suffix = db.get("indexes", {}).get("by_path_suffix", {}) # Build set of filenames already covered (platform baseline + first pass extras) # Enriched with canonical names from DB via MD5 (handles platform renaming) @@ -1444,7 +1557,11 @@ def _collect_emulator_extras( covered_names.add(e["name"]) # Destination -> the copies cores ask for there, in profile order. - copied: dict[str, list[dict]] = {} + scope = _CopyScope( + config.get("_key", ""), covered_names, platform_names, seen, state.seen_dests, + state.taken, {}, by_name, db.get("indexes", {}).get("by_path_suffix", {}), + extras_prefix, + ) for emu_name, profile in sorted(profiles.items()): if profile.get("type") in ("launcher", "alias"): continue @@ -1452,105 +1569,9 @@ def _collect_emulator_extras( continue for layout, is_standalone in platform_layouts(emu_name, profile, standalone_set): for f in layout.get("files", []): - fname = f.get("name", "") - if not fname: - continue - # Only duplicate files already covered at another destination. - # An unsourceable entry is a file no collection holds: the name - # covered elsewhere belongs to another program's file. - # Nor is one placed outside the BIOS tree by the layout. - if (fname not in covered_names or f.get("unsourceable") or f.get("unplaced") - or f.get("root") in OUTER_ROOTS): - continue - # Installed with the emulator or by the platform's package, - # as the first pass already decided for the same entry. - if not deliverable([f], is_standalone, config.get("_key", "")): - continue - # A member of an archive is read inside it, never as a loose - # file. The archive itself is placed by the passes above. - if f.get("archive"): - continue - # No path: the core reads it from nowhere a pack reaches - # (Dolphin's codehandler.bin ships in its Sys tree). - if "path" in f and f["path"] is None: - continue - file_mode = f.get("mode") - if file_mode == "standalone" and not is_standalone: - continue - if file_mode == "libretro" and is_standalone: - continue - if not read_from_system_dir(f, is_standalone): - continue - if is_standalone: - raw = f.get("standalone_path") or f.get("path") or fname - else: - raw = system_dir_path(f, f.get("path") or fname) - dest = f"{raw}{fname}" if raw.endswith("/") else raw - # A bare name is a destination too: the core reads it at the root - # of the directory core files go to, which the platform may not - # declare (Recalbox has its System Cards under pcenginecd/ only, - # geargrafx reads them from the root). - # A path of the install tree outside the BIOS folder keeps no - # prefix: EmuELEC's dolphin/dolphin-emu/GBA/gba_bios.bin. - full_dest = ( - sanitize_pack_path(dest) if f.get("unprefixed") - else prefixed_destination(dest, extras_prefix) - ) - if full_dest in seen_dests and full_dest not in taken: - continue - if (full_dest in taken or full_dest in copied) and full_dest not in seen \ - and fname not in platform_names: - # Another core's extra holds this path and only that - # extra covers the name: a pack without that core never - # reaches this copy, so this core claims nothing here. - # Batocera's launcher hands DuckStation one named BIOS; - # recorded on duckstation-legacy's scph100.bin, its claim - # kept the file for a target without legacy. - continue - # Only files the database indexes: members of a data - # directory are not in it and are not duplicated here. - if not ( - by_name.get(fname) - or by_name.get(dest.rsplit("/", 1)[-1]) - or by_path_suffix.get(dest) - ): - continue - extra = { - "name": fname, - "destination": dest, - "required": f.get("required", False), - "hle_fallback": f.get("hle_fallback", False), - "source_emulator": layout.get("emulator", emu_name), - "source_profile": emu_name, - # The key the gap report gives the same entry. - "source_name": fname, - "source_path": raw, - "category": f.get("category", "bios"), - "source_system": f.get("system"), - "source_systems": entry_system_ids(f, layout), - "region": f.get("region"), - "selection_slot": f.get("selection_slot"), - **set_fields(f, layout), - } - # The same identity the first pass carries: without it the copy - # resolves on its name alone and a same-named file of another - # game fills the slot the profile marks as not held. - for identity in PROFILE_IDENTITY_FIELDS: - if f.get(identity) not in (None, "", []): - extra[identity] = f[identity] - if f.get("contents"): - extra["contents"] = f["contents"] - if f.get("system_dir"): - extra["system_dir"] = f["system_dir"] - if f.get("unprefixed"): - extra["unprefixed"] = True - # A copy placed outside the BIOS tree resolves by its layout - # path alone, as the first pass does: EmuELEC's - # roms/mame/stvbios.zip took bios/Arcade/MAME's set by that tail. - if f.get("layout_path"): - extra["layout_path"] = f["layout_path"] - extra["claim_order"] = (2, 0, emu_name) - copied.setdefault(full_dest, []).append(extra) + copy = _second_pass_copy(scope, emu_name, layout, is_standalone, f) + if copy is not None: + scope.copied.setdefault(copy[0], []).append(copy[1]) # Several cores reading one archive in one roms folder: the copy of the # core the folder starts by default goes first, then the copy serving @@ -1558,31 +1579,236 @@ def _collect_emulator_extras( # neogeo.zip there, and the copy holding the 18 members its default # MAME 2003-Plus requires also holds the 3 MAME 2000 requires. defaults = _folder_defaults(config, profiles) - for full_dest, claims in copied.items(): + for full_dest, claims in scope.copied.items(): if len(claims) > 1: claims = sorted(claims, key=lambda c: ( not _starts_by_default(c, defaults), -_copy_serves(c, claims, db), )) - for extra in claims: - if _take(extra, full_dest, seen_dests, taken): - extras.append(extra) + extras.extend( + extra for extra in claims + if _take(extra, full_dest, state.seen_dests, state.taken) + ) extras.extend( _archive_prefix_extras( - profiles, relevant, covered_names, seen_dests, extras_prefix, - by_name, taken, standalone_set, + profiles, relevant, covered_names, state.seen_dests, extras_prefix, + by_name, state.taken, standalone_set, ) ) extras.extend( _agnostic_scan_extras( - profiles, relevant, db, by_name, seen_dests, extras_prefix, - standalone_set, taken, + profiles, relevant, db, by_name, state.seen_dests, extras_prefix, + standalone_set, state.taken, ) ) return extras + +def _place_report_entry(state: _FirstPass, u: dict) -> None: + """First pass: place one gap report entry, or record why it is not.""" + if u.get("bundled"): + # Installed with the emulator for this build: already on the + # user's machine. The mode a file is bundled for was settled + # upstream, an entry scoped to another build never gets here. + return + if u.get("unplaced"): + # Read in a folder outside the BIOS tree: named, held or not, + # never written where the emulator does not read it. + if state.unplaceable is not None: + state.unplaceable.append({**u, "unplaceable": u["unplaced"]}) + return + absent = state.absent + if not u["in_repo"] and absent is None: + return + # For archive entries, use the archive name for resolution + archive = u.get("archive") + name = archive if archive else u["name"] + dest = _extra_dest(u) + + # Slug-based platforms: prefix dest with system slug. A file read + # below a save directory kept per system goes below each system. + placement = state.placement + placements = placement.places(u, dest) + save_root = u.get("root") == "save" + if not u["in_repo"]: + # Each core reading it is named on the omission: a list built + # without the first reader still has to say the file is missing. + if absent is not None: + for placed in placements: + for claim in (u, *u.get("other_claims", [])): + missing = _report_extra(claim, name, placed[0]) + if save_root: + _save_fields(missing, placed[1], placement) + absent.append(missing) + return + if not placements: + if state.unplaceable is not None: + reason = placement.unplaced_reason(u, dest) + state.unplaceable.append( + u if reason == "no_platform_slug" else {**u, "unplaceable": reason} + ) + return + if state.claimants is not None: + # Keyed as extra_region_key keys the extra that wins the path: a + # losing claim kept the bare path, and on RetroDECK (declared + # root bios/) verify --region listed Rustation's scph1002b.bin + # as packed while the builder had withdrawn bios/scph1002b.bin. + state.claimants[(u.get("emulator", ""), u.get("name", ""), u.get("path") or "")] = ( + prefixed_destination( + sanitize_pack_path(placements[0][0]), declared_root(state.config) + ) + ) + for dest, full_dest in placements: + _claim_placement(state, u, name, dest, full_dest, save_root) + + +def _claim_placement( + state: _FirstPass, u: dict, name: str, dest: str, full_dest: str, save_root: bool, +) -> None: + """Race the entry's claims for one of its destinations.""" + candidates = [ + _report_extra(c, name, dest) for c in (u, *u.get("other_claims", [])) + ] + if save_root: + for item in candidates: + _save_fields(item, full_dest, state.placement) + # Where the builder meets each claim: the strongest proof first, the + # report's profile order between equals, archives after loose files. + for item in candidates: + item["claim_order"] = ( + 1, 1 if u.get("archive") else 0, -state.rank.of_extra(item, full_dest), + item["source_profile"], + ) + candidates.sort(key=lambda item: item["claim_order"]) + extra, claims = candidates[0], candidates[1:] + if not _take(extra, full_dest, state.seen_dests, state.taken): + for claim in claims: + _another_claim(state.taken.get(full_dest), claim) + return + for claim in claims: + _another_claim(extra, claim) + state.extras.append(extra) + + +def _copies_entry(f: dict, is_standalone: bool, scope: _CopyScope) -> bool: + """Whether the second pass may copy this layout entry to its own path.""" + fname = f.get("name", "") + if not fname: + return False + # Only duplicate files already covered at another destination. + # An unsourceable entry is a file no collection holds: the name + # covered elsewhere belongs to another program's file. + # Nor is one placed outside the BIOS tree by the layout. + if (fname not in scope.covered_names or f.get("unsourceable") or f.get("unplaced") + or f.get("root") in OUTER_ROOTS): + return False + # Installed with the emulator or by the platform's package, + # as the first pass already decided for the same entry. + if not deliverable([f], is_standalone, scope.config_key): + return False + # A member of an archive is read inside it, never as a loose + # file. The archive itself is placed by the passes above. + if f.get("archive"): + return False + # No path: the core reads it from nowhere a pack reaches + # (Dolphin's codehandler.bin ships in its Sys tree). + if "path" in f and f["path"] is None: + return False + file_mode = f.get("mode") + if file_mode == "standalone" and not is_standalone: + return False + if file_mode == "libretro" and is_standalone: + return False + return read_from_system_dir(f, is_standalone) + + +def _second_pass_copy( + scope: _CopyScope, emu_name: str, layout: dict, is_standalone: bool, f: dict, +) -> tuple[str, dict] | None: + """(full destination, extra) of a covered file this core reads elsewhere.""" + if not _copies_entry(f, is_standalone, scope): + return None + fname = f.get("name", "") + if is_standalone: + raw = f.get("standalone_path") or f.get("path") or fname + else: + raw = system_dir_path(f, f.get("path") or fname) + dest = f"{raw}{fname}" if raw.endswith("/") else raw + # A bare name is a destination too: the core reads it at the root + # of the directory core files go to, which the platform may not + # declare (Recalbox has its System Cards under pcenginecd/ only, + # geargrafx reads them from the root). + # A path of the install tree outside the BIOS folder keeps no + # prefix: EmuELEC's dolphin/dolphin-emu/GBA/gba_bios.bin. + full_dest = ( + sanitize_pack_path(dest) if f.get("unprefixed") + else prefixed_destination(dest, scope.extras_prefix) + ) + if full_dest in scope.seen_dests and full_dest not in scope.taken: + return None + if (full_dest in scope.taken or full_dest in scope.copied) and full_dest not in scope.seen \ + and fname not in scope.platform_names: + # Another core's extra holds this path and only that + # extra covers the name: a pack without that core never + # reaches this copy, so this core claims nothing here. + # Batocera's launcher hands DuckStation one named BIOS; + # recorded on duckstation-legacy's scph100.bin, its claim + # kept the file for a target without legacy. + return None + # Only files the database indexes: members of a data + # directory are not in it and are not duplicated here. + if not ( + scope.by_name.get(fname) + or scope.by_name.get(dest.rsplit("/", 1)[-1]) + or scope.by_path_suffix.get(dest) + ): + return None + return full_dest, _copy_extra(f, layout, emu_name, dest, raw) + + +def _copy_extra(f: dict, layout: dict, emu_name: str, dest: str, raw: str) -> dict: + """The extra the second pass emits for a copy.""" + fname = f.get("name", "") + extra = { + "name": fname, + "destination": dest, + "required": f.get("required", False), + "hle_fallback": f.get("hle_fallback", False), + "source_emulator": layout.get("emulator", emu_name), + "source_profile": emu_name, + # The key the gap report gives the same entry. + "source_name": fname, + "source_path": raw, + "category": f.get("category", "bios"), + "source_system": f.get("system"), + "source_systems": entry_system_ids(f, layout), + "region": f.get("region"), + "selection_slot": f.get("selection_slot"), + **set_fields(f, layout), + } + # The same identity the first pass carries: without it the copy + # resolves on its name alone and a same-named file of another + # game fills the slot the profile marks as not held. + for identity in PROFILE_IDENTITY_FIELDS: + if f.get(identity) not in (None, "", []): + extra[identity] = f[identity] + if f.get("contents"): + extra["contents"] = f["contents"] + if f.get("system_dir"): + extra["system_dir"] = f["system_dir"] + if f.get("unprefixed"): + extra["unprefixed"] = True + # A copy placed outside the BIOS tree resolves by its layout + # path alone, as the first pass does: EmuELEC's + # roms/mame/stvbios.zip took bios/Arcade/MAME's set by that tail. + if f.get("layout_path"): + extra["layout_path"] = f["layout_path"] + extra["claim_order"] = (2, 0, emu_name) + return extra + + class _ClaimRanks: """How strongly each claim on a contested destination proves its file. @@ -1908,6 +2134,19 @@ def _ships( )[1] is None +class RegionGroupOptions(TypedDict, total=False): + """What platform_region_groups reads besides the platform, and fills.""" + + include_all: bool + required_only: bool + extras_out: list[dict] | None + held_out: set[str] | None + undeclared: list[dict] | None + zip_contents: dict | None + data_registry: dict | None + slot_overrides: dict[str, str] | None + + def platform_region_groups( config: dict, systems: dict, @@ -1918,14 +2157,7 @@ def platform_region_groups( *, target_cores: set[str] | None = None, include_extras: bool = True, - include_all: bool = False, - required_only: bool = False, - extras_out: list[dict] | None = None, - held_out: set[str] | None = None, - undeclared: list[dict] | None = None, - zip_contents: dict | None = None, - data_registry: dict | None = None, - slot_overrides: dict[str, str] | None = None, + **options: Unpack[RegionGroupOptions], ) -> tuple[dict[str, list[tuple[str, str]]], dict[tuple[str, str, str], str]]: """Group a platform's pack candidates the way region filtering reads them. @@ -1944,6 +2176,15 @@ def platform_region_groups( and ``slot_overrides`` (keyed by full pack destination) are the ones the builder resolves with, so a held declaration is the one the pack ships. """ + reject_unknown("platform_region_groups", options, RegionGroupOptions.__optional_keys__) + include_all = options.get("include_all", False) + undeclared = options.get("undeclared") + required_only = options.get("required_only", False) + held_out = options.get("held_out") + extras_out = options.get("extras_out") + zip_contents = options.get("zip_contents") + data_registry = options.get("data_registry") + slot_overrides = options.get("slot_overrides") groups: dict[str, list[tuple[str, str]]] = {} for sys_id, system in systems.items(): members = groups.setdefault(sys_id, []) @@ -2096,7 +2337,7 @@ def emulator_region_drops( region_groups.setdefault(group_id, []).append( (dest, fe.get("name", "")) ) - if held is not None and _emulator_ships( + if held is not None and db is not None and _emulator_ships( fe, emu_name, profile, db, validation_index, zip_contents, data_registry, ): @@ -2104,6 +2345,10 @@ def emulator_region_drops( return region_mod.resolve_region_drops(region_groups, region_index, regions, held) +class SpareOptions(TypedDict, total=False): + data_registry: dict | None + + def emulator_required_spares( emu_name: str, profile: dict, @@ -2113,7 +2358,7 @@ def emulator_required_spares( db: dict, validation_index: dict, zip_contents: dict | None, - data_registry: dict | None, + **options: Unpack[SpareOptions], ) -> set[str]: """The destinations an emulator --required-only pack leaves out: of each required set, one held member stays, the first the code loads among @@ -2124,6 +2369,8 @@ def emulator_required_spares( from common import archive_entry from packpaths import _archive_destination, _resolve_destination + reject_unknown("emulator_required_spares", options, SpareOptions.__optional_keys__) + data_registry = options.get("data_registry") candidates: list[dict] = [] archives: list[str] = [] for fe in files: @@ -2136,9 +2383,10 @@ def emulator_required_spares( archive, profile.get("archive_prefix", ""), profile.get("pack_structure"), standalone, ), "entry": own}) - for fe in files: - if not fe.get("archive") and groups_of(fe): - candidates.append({"dest": _resolve_destination(fe, standalone), "entry": fe}) + candidates.extend( + {"dest": _resolve_destination(fe, standalone), "entry": fe} + for fe in files if not fe.get("archive") and groups_of(fe) + ) if not candidates: return set() spares = required_only_spares( From c13dfb5df50846a4e8e9691eb125f8a56deb05b6 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:15:44 +0100 Subject: [PATCH 9/9] refactor: quote the pack type aliases The aliases under TYPE_CHECKING hold no union a 3.9 parser would evaluate, and two docstrings drop their clause semicolons. --- scripts/generate_pack.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/generate_pack.py b/scripts/generate_pack.py index 9ba5304c7..ea76c0038 100644 --- a/scripts/generate_pack.py +++ b/scripts/generate_pack.py @@ -195,10 +195,10 @@ if TYPE_CHECKING: # How a manifest settles a platform declaration: the copy served, or None # and why not. - _Settle = Callable[[dict, str, str, str | None], tuple[str | None, str]] + _Settle = Callable[[dict, str, str, "str | None"], "tuple[str | None, str]"] # What a destination came to: its entries, whether it was filled, and the # report to make if no other member of its set fills it. - _Outcome = tuple[list[dict], bool, Callable[[], None] | None] + _Outcome = tuple[list[dict], bool, "Callable[[], None] | None"] yaml = require_yaml() @@ -3560,7 +3560,7 @@ def _run_verify_packs(args: argparse.Namespace) -> None: def _verification_summary(failing: list[str], verified: int, output_dir: str) -> bool: - """Print how --verify-packs ended; False when nothing was verified.""" + """Print how --verify-packs ended. False when nothing was verified.""" if failing: # Named at the end, where the log is read, and on stderr, as the # build path does: a failing pack sat among its own error lines. @@ -4143,7 +4143,7 @@ def _run_quick_mode( args: argparse.Namespace, parser: argparse.ArgumentParser, held: contextlib.ExitStack, ) -> bool: """Run a mode that builds nothing (a check of existing packs, the target - lists, a listing); True when one ran.""" + lists, a listing). True when one ran.""" # Quick-exit modes: --verify-packs alone = verify existing packs only # Combined with --all-variants, generation runs first then verify if args.verify_packs and not args.all_variants: