refactor: split the platform pack plan and zip

This commit is contained in:
Abdessamad Derraz committed 2026-11-24 06:20:08 +01:00
1 parent c8258522a4
commit 83055b87ea
15 files changed
+1033 -991

No files matched your search

+2 -2
View File
@@ -7,9 +7,9 @@ from packs.supplemental import SupplementalIndex, data_hit, declared_content
from resolve.content import content_fits, name_match_size_ok
from resolve.directories import directory_members
from resolve.layout import system_dir_path
from resolve.localfile import held_record_for
from resolve.lookup import held_record_for
from resolve.ownership import foreign_tree, owner_tokens
from resolve.validation import read_from_save_dir
from resolve.entryscope import read_from_save_dir
def _resolve_source(
+1 -1
View File
@@ -32,7 +32,7 @@ from packs.manifestentry import (
)
from packs.manifestselect import SelectionIndex
from packs.archive import Placed
from packs.platformpack import one_per_slot_drops
from packs.packplan import one_per_slot_drops
from resolve import regions as resolve_regions, slot as slot_mod
from resolve.contentlayout import TARGET_KEY, content_folder_boards
from resolve.cores import (
+317
View File
@@ -0,0 +1,317 @@
"""What a platform pack holds before it is written: settings, variants and slot choices."""
from __future__ import annotations
import os
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, TypedDict
from core.options import reject_unknown
from core.paths import EMULATORS_DIR
from model.platforms import load_platform_config
from model.profiles import load_emulator_profiles
from packs import slots
from packs.archive import PackRecord
from packs.extras import platform_region_groups, undeclared_report
from packs.naming import pack_narrowings, platform_pack_name
from packs.packcounts import PackCounts
from resolve import regions as resolve_regions, slot as slot_mod
from resolve.contentlayout import TARGET_KEY
from resolve.cores import platform_scope_systems, system_scope_error
from resolve.gamedata import GameDataCut, without_game_data
from resolve.nativemode import platform_mode
from resolve.packfiles import preferred_entries as _preferred_entries
from resolve.validation import build_validation_index
from resolve.acceptance import destination_owners
if TYPE_CHECKING:
from typing_extensions import Unpack
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,
emulators_dir: str,
db: dict,
base_dest: str,
emu_profiles: dict | None,
target_cores: set[str] | None,
source: str,
**options: Unpack[_VariantOptions],
) -> tuple[set, list, list]:
"""Which regional and slot alternatives this pack leaves out.
Both reductions are decided once, over the platform baseline and the
core extras together: deciding them separately is how the report and
the pack came to withdraw different files for the same request.
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
# pass must not land in every system of its profile for the other.
region_groups: dict[str, list[tuple[str, str]]] = {}
held: set[str] = set()
if regions or one_per_slot:
region_groups, _extra_dests = platform_region_groups(
config,
pack_systems,
emulators_dir,
db,
base_dest,
emu_profiles,
target_cores=target_cores,
include_extras=(source != "platform"),
include_all=(source == "truth"),
required_only=options.get("required_only", False),
held_out=held,
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 = resolve_regions.platform_region_index(
config, emu_profiles or {}, target_cores
)
region_drops = resolve_regions.resolve_region_drops(
region_groups, region_index, regions, held
)
region_fallbacks = resolve_regions.fallback_groups(
region_groups, region_index, regions
)
slot_undecidable: list[str] = []
if one_per_slot:
slot_drops, slot_undecidable = one_per_slot_drops(
config, emu_profiles, target_cores, region_groups, region_drops, held
)
region_drops = region_drops | slot_drops
return region_drops, region_fallbacks, slot_undecidable
def one_per_slot_drops(
config: dict,
emu_profiles: dict | None,
target_cores: set[str] | None,
region_groups: dict[str, list[tuple[str, str]]],
region_drops: set[str],
held: set[str],
) -> tuple[set[str], list[str]]:
"""What --one-per-slot withdraws once the regions have, and the slots no
declared order decides. The pack and the manifest both read it."""
slot_groups = {
sys_id: [(d, n) for d, n in members if d not in region_drops]
for sys_id, members in region_groups.items()
}
return slot_mod.resolve_slot_drops(
slot_groups,
slot_mod.platform_slot_index(config, emu_profiles or {}, target_cores),
held,
)
@dataclass(frozen=True)
class PackSettings:
"""PackOptions with their defaults."""
emulators_dir: str = EMULATORS_DIR
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
uncached_out: set[str] | None = None
counts: list[PackCounts] | None = None
@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 platform_mode(self.config)
@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 | str:
"""The configuration, scope and checks a platform pack reads, or the
reason 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 = pack_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 resolve.cores 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:
return system_scope_error(config, platform_name, opts.system_filter, opts.target_name)
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,
)
File diff suppressed because it is too large. Load diff
+2 -1
View File
@@ -7,7 +7,8 @@ from typing import TYPE_CHECKING
from packs.datatrees import uncached_warning
if TYPE_CHECKING:
from packs.platformpack import PlatformPackOutcome, SplitOutcome
from packs.platformzip import PlatformPackOutcome
from packs.platformpack import SplitOutcome
_STATUS_LABELS = {"untested": "UNTESTED", "excluded": "EXCLUDED", "missing": "MISSING"}
+676
View File
@@ -0,0 +1,676 @@
"""One platform pack's ZIP written from its plan."""
from __future__ import annotations
import os
import tempfile
import zipfile
from collections.abc import Callable, Iterable
from dataclasses import dataclass, field
from core.hashing import compute_hashes
from core.paths import declared_destination
from core.ziptools import is_archive_path
from model.alternatives import core_file_spares
from model.declarations import OUTER_ROOTS, declared_root
from model.profiles import installed_data_refs_in
from model.registry import load_platform_registry
from packs.archive import (
CASE_FOLDING_OS,
PackRecord,
Placed,
add_pack_member,
add_zip_to_pack,
extract_zip_to_archive,
instructions_note,
same_bytes,
write_generated_member,
)
from packs.datatrees import core_data_trees, data_directory_members, pack_markers
from packs.declsettle import DeclarationJudge, Settlement
from packs.extras import (
collect_emulator_extras,
detect_extras_prefix,
extra_full_destination,
extra_region_key,
extras_for_systems as _extras_for_systems,
)
from packs.packplan import PlatformPlan
from packs.paths import flat_arcname, platform_destination, save_layout
from packs.readme import build_agnostic_rename_readme, build_readme, save_place
from resolve import packfiles as packresolve
from resolve.cores import platform_scope_systems
from resolve.gamedata import GameDataCut
from resolve.nativemode import hash_mismatch_excludes_file, reads_file_contents
from resolve.packfiles import download_external, resolve_file
from resolve.validation import parse_validation
from resolve.acceptance import existence_discrepancy, extra_alternatives
def _case_twin_pairs(
twins: list[str], seen_destinations: set[str], base_dest: str, flatten: bool,
) -> list[tuple[str, str]]:
"""Each spelling left out, with the spelling the pack holds."""
held = {
flat_arcname(dest, base_dest, flatten).lower(): flat_arcname(dest, base_dest, flatten)
for dest in seen_destinations
}
return sorted({(twin, held[twin.lower()]) for twin in twins if twin.lower() in held})
def _matches_an_alternative(fe: dict, local_path: str, profiles: dict) -> bool:
"""Whether the copy is another version the same profile accepts there.
A profile lists every version its code takes under one name (each GRP
of Raze's table, eduke32's demo and retail NWINTER.GRP). The extra
carries the first, and the held copy matching a sibling is no mismatch.
"""
siblings = extra_alternatives(fe, profiles)
if len(siblings) < 2:
return False
# A sibling the code takes without a check (Raze's "BLOOD: Unknown
# Version") accepts whatever copy fills the slot.
if any(not parse_validation(entry.get("validation")) for entry in siblings):
return True
actual = compute_hashes(local_path, frozenset({"sha1", "md5", "crc32"}))
for entry in siblings:
for algorithm in ("sha1", "md5", "crc32"):
declared = entry.get(algorithm)
values = declared if isinstance(declared, list) else [declared]
if any(v and str(v).lower() == actual.get(algorithm, "") for v in values):
return True
return False
def left_out_lines(cut: GameDataCut, systems: set[str], cores: bool) -> list[str]:
"""The game data a --no-game-data build left out, and why, one line each.
``systems`` limits the platform layer to the systems this pack covers,
and ``cores`` says whether this pack collected the core files itself:
a split part does not, its run names them once.
"""
kept = [
item for item in cut.left_out
if (item.layer == "platform" and item.system in systems)
or (item.layer == "core" and cores)
]
return GameDataCut(cut.config, cut.profiles, kept).report_lines()
@dataclass
class PlatformPackOutcome:
"""What one platform pack build came to, as print_platform_pack reports it.
``refusal`` is set, and ``zip_path`` None, when the systems asked for
leave nothing to build.
"""
zip_path: str | None
refusal: str = ""
source: str = "full"
verification_mode: str = ""
total_files: int = 0
core_count: int = 0
tree_count: int = 0
file_status: dict[str, str] = field(default_factory=dict)
file_reasons: dict[str, str] = field(default_factory=dict)
core_discrepancies: list[str] = field(default_factory=list)
slot_undecidable: list[str] = field(default_factory=list)
uncached: list[tuple[str, str]] = field(default_factory=list)
left_out: list[str] = field(default_factory=list)
@dataclass
class PlatformPackBuild:
"""One platform pack being written: its members and what each slot came to.
A platform file is settled by the DeclarationJudge the install manifest
reads too, and a core extra by the lookup build_platform_pack binds.
"""
plan: PlatformPlan
zf: zipfile.ZipFile | PackRecord
declarations: DeclarationJudge
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)
# (registry key, cache path) of each data directory the cache lacks.
uncached: list[tuple[str, str]] = field(default_factory=list)
# The archive path of each member taken from a data directory.
trees: 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_arcname(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 _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.declarations.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 _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, full_dest = platform_destination(file_entry, plan.base_dest)
if plan.region_drops and dest in plan.region_drops:
return
if not dest:
self._judge_unnamed(sys_id, file_entry)
return
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
storage = file_entry.get("storage", "embedded")
if storage == "user_provided":
if not already_packed:
self._write_instructions(file_entry, full_dest)
return
settled = self.declarations.settle(
file_entry, sys_id, dest, plan.slot_overrides.get(full_dest),
)
if settled.status == "external":
self._pack_external(file_entry, full_dest)
return
if settled.status == "not_found":
if not already_packed:
self.file_status[full_dest] = "missing"
self.file_reasons[full_dest] = "not found"
return
if settled.agnostic_dir and settled.resolved:
self._note_rename(file_entry, settled.resolved, settled.agnostic_dir)
if self._settle_declared(file_entry, full_dest, settled, already_packed) and settled.path:
self._ship_declared(file_entry, full_dest, settled.path, 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.declarations.verdict(file_entry, held, held_status)[1]:
self.file_status[fkey] = "excluded"
else:
self.file_status.setdefault(fkey, "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")
instr_name, text = instructions_note(file_entry)
instr_path = (
f"{base_dest}/{instr_name}" if base_dest else instr_name
)
write_generated_member(self.zf, self._flat(instr_path), text)
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:
# Built in memory, nothing is fetched: verify cannot fetch either.
fetch = (
download_external(file_entry, tmp_path)
if self.plan.opts.record is None else None
)
if fetch is not None and fetch.fetched:
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] = (
fetch.reason if fetch is not None
else "external file not fetched for an in-memory build"
)
finally:
if os.path.exists(tmp_path):
os.unlink(tmp_path)
def _note_rename(self, file_entry: dict, local_path: str, agnostic_path: str) -> None:
# A filename-agnostic core's substitute ships under the declared
# name: a note says which image it is.
plan = self.plan
original_name = os.path.basename(local_path)
dest_name = file_entry.get("name", "")
if original_name == dest_name:
return
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)
def _settle_declared(
self, file_entry: dict, full_dest: str, settled: Settlement, already_packed: bool,
) -> bool:
"""Record what a declaration came to, and whether its copy ships."""
plan = self.plan
if settled.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] = settled.refusal
return False
if not hash_mismatch_excludes_file(plan.verification_mode) and (
settled.status == "hash_mismatch" and settled.resolved
):
# 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, settled.resolved, 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. The copy the cores chose ships whatever an earlier
# declaration of this path came to, as the installer places it.
if settled.disagreement and self.file_status.get(full_dest) == "ok":
self.file_reasons.setdefault(
full_dest, f"{plan.platform_display} says OK but {settled.disagreement}"
)
return True
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 is_archive_path(local_path):
extract_zip_to_archive(local_path, flat_dest, self.zf)
elif is_archive_path(local_path):
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 = declared_destination(fe)
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 is_archive_path(local_path):
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
required_only = plan.opts.required_only
added = 0
uncached: list[tuple[str, str]] = []
for src, full in data_directory_members(
pack_systems, plan.opts.data_registry, plan.platform_name, plan.base_dest,
self.placed, required_only=required_only,
core_trees=core_trees, uncached_out=uncached,
installed=installed_data_refs_in(plan.platform_name, plan.emu_profiles or {}),
):
arcname = self._flat(full)
# The installer writes a data tree member without the bit.
add_pack_member(self.zf, src, arcname, executable=False)
self.trees.append(arcname)
if isinstance(self.zf, PackRecord):
self.zf.trees.append(arcname)
added += 1
self.uncached.extend(uncached)
if plan.opts.uncached_out is not None:
plan.opts.uncached_out.update(ref_key for ref_key, _path in uncached)
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 self.placed.block(full) == "case_twin"
and self.resolve_extra(fe, declared_destination(fe))[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),
platforms_dir=plan.platforms_dir,
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 outcome(self) -> PlatformPackOutcome:
plan, opts = self.plan, self.plan.opts
return PlatformPackOutcome(
zip_path=plan.zip_path,
source=opts.source,
verification_mode=plan.verification_mode,
total_files=self.total_files,
core_count=self.core_count,
tree_count=self.tree_count,
file_status=self.file_status,
file_reasons=self.file_reasons,
core_discrepancies=self.core_discrepancies,
slot_undecidable=plan.slot_undecidable,
uncached=self.uncached,
left_out=(
left_out_lines(
plan.cut,
systems=set(plan.pack_systems) if opts.source != "truth" else set(),
cores=opts.source != "platform" and opts.precomputed_extras is None,
)
if plan.cut is not None else []
),
)
def _platform_registry(platforms_dir: str) -> dict:
"""The platforms of _registry.yml, or nothing when the file is absent."""
return load_platform_registry(platforms_dir)
+2 -1
View File
@@ -17,7 +17,8 @@ SCRIPTS = Path(__file__).resolve().parents[1] / "scripts"
COMMANDS = {
"generate_pack.py": (
"packs/archive.py", "packs/naming.py", "packs/scope.py", "packs/datatrees.py",
"packs/platformpack.py", "packs/platformreport.py", "packs/emulatorpack.py",
"packs/platformpack.py", "packs/packplan.py", "packs/platformzip.py", "packs/platformreport.py",
"packs/emulatorpack.py",
"packs/md5pack.py", "packs/targets.py",
"packs/manifestentry.py", "packs/manifestcores.py", "packs/standalone.py",
"packs/manifest.py", "packs/manifestbuild.py", "packs/finalize.py", "packs/outputdir.py", "packs/variants.py",
+2 -2
View File
@@ -4321,7 +4321,7 @@ class AnAcceptedVersionIsNoCoreDiscrepancy(unittest.TestCase):
"source_path": "NWINTER.GRP", "source_profile": "eduke32"}
def test_a_sibling_version_matches(self):
from scripts.packs.platformpack import _matches_an_alternative
from scripts.packs.platformzip import _matches_an_alternative
with tempfile.TemporaryDirectory() as tmp:
held = Path(tmp) / "NWINTER.GRP"
@@ -4332,7 +4332,7 @@ class AnAcceptedVersionIsNoCoreDiscrepancy(unittest.TestCase):
self._extra(), str(held), {"eduke32": profile}))
def test_a_copy_no_entry_declares_still_disagrees(self):
from scripts.packs.platformpack import _matches_an_alternative
from scripts.packs.platformzip import _matches_an_alternative
with tempfile.TemporaryDirectory() as tmp:
held = Path(tmp) / "NWINTER.GRP"
+4 -3
View File
@@ -18,6 +18,7 @@ from pathlib import Path
import yaml
from packs import archive
from packs import platformpack
from packs import platformzip
from model.platforms import load_platform_config
@@ -85,14 +86,14 @@ class ThePackReadmeNamesTheSpellingsItLeavesOut(unittest.TestCase):
}}}))
_platform_config_cache.clear()
texts: dict[str, str] = {}
real = platformpack.write_generated_member
real = platformzip.write_generated_member
def capture(zf, arcname, text):
texts[arcname] = text
return real(zf, arcname, text)
platformpack.write_generated_member = capture
self.addCleanup(setattr, platformpack, "write_generated_member", real)
platformzip.write_generated_member = capture
self.addCleanup(setattr, platformzip, "write_generated_member", real)
platformpack.build_platform_pack(
"p", "platforms", self.db, "bios", "", emulators_dir="emulators",
offline=True, record=archive.PackRecord(),
+2 -2
View File
@@ -34,7 +34,7 @@ from model.registry import list_registered_platforms, load_data_dir_registry
from resolve.localfile import resolve_local_file
from resolve.ownership import BUCKET_FOLDERS, names_manufacturer
from collection.indexes import shadowed_alias_index
from packs import manifest as packs_manifest, md5pack, naming as packs_naming, platformpack
from packs import manifest as packs_manifest, md5pack, naming as packs_naming, packplan, platformpack
from resolve import ownership
from packs.coverage import verify_platform
@@ -197,7 +197,7 @@ class DiskRomGoesToThePc88File(_Collection):
manifest = self._manifest()
original = (platforms.load_platform_config, localfile.system_makers)
# Every module that reads the loader by its own name sees the stub.
readers = (packs_manifest, md5pack, packs_naming, platformpack, platforms, cliargs, cores, grouping)
readers = (packs_manifest, md5pack, packs_naming, packplan, platformpack, platforms, cliargs, cores, grouping)
def untagged(*args, **kwargs):
config = original[0](*args, **kwargs)
+2 -1
View File
@@ -266,8 +266,9 @@ class EveryNarrowingIsCompared(unittest.TestCase):
from orchestrate import narrowed
from packs import platformpack
from packs import platformzip
refused = platformpack.PlatformPackOutcome(zip_path=None, refusal="no core runs x")
refused = platformzip.PlatformPackOutcome(zip_path=None, refusal="no core runs x")
inputs = ({}, {}, {}, {}, [(["p"], "p")])
printed = io.StringIO()
with mock.patch.object(narrowed, "comparison_inputs", return_value=inputs), \
+4 -4
View File
@@ -22,7 +22,7 @@ class SlotPassReadsTheRegionGrouping(unittest.TestCase):
def test_ranked_mega_cd_alternatives_are_decided(self):
if not (REPO_ROOT / "database.json").is_file():
self.skipTest("database.json not built")
from packs import platformpack
from packs import packplan
from model.database import load_database
from model.profiles import load_emulator_profiles
from model.platforms import load_platform_config
@@ -33,7 +33,7 @@ class SlotPassReadsTheRegionGrouping(unittest.TestCase):
db = load_database("database.json")
profiles = load_emulator_profiles("emulators")
config = load_platform_config("retroarch", "platforms")
drops, _fallbacks, _undecided = platformpack._select_variants(
drops, _fallbacks, _undecided = packplan._select_variants(
config, config["systems"], "emulators", db,
config.get("base_destination", ""), profiles, None, "full",
regions=None, one_per_slot=True,
@@ -57,7 +57,7 @@ class VariantGroupSpansSystems(unittest.TestCase):
"""
if not (REPO_ROOT / "database.json").is_file():
self.skipTest("database.json not built")
from packs import platformpack
from packs import packplan
from model import region
from model.database import load_database
from model.profiles import load_emulator_profiles
@@ -69,7 +69,7 @@ class VariantGroupSpansSystems(unittest.TestCase):
db = load_database("database.json")
profiles = load_emulator_profiles("emulators")
config = load_platform_config("retroarch", "platforms")
drops, _fallbacks, _undecided = platformpack._select_variants(
drops, _fallbacks, _undecided = packplan._select_variants(
config, config["systems"], "emulators", db,
config.get("base_destination", ""), profiles, None, "full",
regions=region.parse_requested("us"), one_per_slot=False,
+4 -4
View File
@@ -271,7 +271,7 @@ class TestConflicts(unittest.TestCase):
"""
import inspect
from packs import coverage, manifest, platformpack
from packs import coverage, manifest, packplan
for function in (
packs_slots.platform_claims,
@@ -292,7 +292,7 @@ class TestConflicts(unittest.TestCase):
# the ZIP builder, the install manifest and the report. The manifest
# deciding nothing gave the installer other bytes than the ZIP.
for tool, modules, calls in (
("generate_pack", (platformpack, manifest), 2), ("verify", (coverage,), 1),
("generate_pack", (packplan, manifest), 2), ("verify", (coverage,), 1),
):
source = "".join(inspect.getsource(module) for module in modules)
with self.subTest(tool=tool):
@@ -528,7 +528,7 @@ class TestBuilderAndVerifierAgree(unittest.TestCase):
# The rule lives in one place. A second copy would let the pack and the
# report describe different files, which the project forbids outright.
scripts = Path(__file__).resolve().parents[1] / "scripts"
builder = (scripts / "packs" / "platformpack.py", scripts / "packs" / "manifest.py")
builder = (scripts / "packs" / "packplan.py", scripts / "packs" / "manifest.py")
verifier = (scripts / "packs" / "coverage.py",)
for source in (*builder, *verifier):
text = source.read_text(encoding="utf-8")
@@ -545,7 +545,7 @@ class TestBuilderAndVerifierAgree(unittest.TestCase):
# override lookup both sides must perform. The old anchor was a string
# the test above forbids, so the window was the whole file.
mode_test = re.compile(r"""[=!]=\s*["'](?:md5|sha1|existence)["']""")
for name in ("packs/platformpack.py", "packs/manifestbuild.py", "packs/platformtally.py"):
for name in ("packs/platformzip.py", "packs/manifestbuild.py", "packs/platformtally.py"):
source = Path(__file__).resolve().parents[1] / "scripts" / name
body = source.read_text(encoding="utf-8")
anchors = [m.start() for m in re.finditer(r"slot_overrides\.get\(", body)]
+6 -3
View File
@@ -150,7 +150,7 @@ class ASplitBuildsOnlyTheTargetsSystems(unittest.TestCase):
def test_no_part_is_asked_for_a_dropped_system(self):
from unittest import mock
from packs import manifestbuild, platformpack
from packs import manifestbuild, platformpack, platformzip
from packs import platformpack as packs_platformpack
from model.profiles import load_emulator_profiles
@@ -161,12 +161,13 @@ class ASplitBuildsOnlyTheTargetsSystems(unittest.TestCase):
def record(*_a, **kw):
if kw.get("system_filter"):
asked.append(kw["system_filter"])
return platformpack.PlatformPackOutcome(zip_path=None)
return platformzip.PlatformPackOutcome(zip_path=None)
with tempfile.TemporaryDirectory() as out, \
mock.patch.object(platformpack, "build_platform_pack", record), \
mock.patch.object(platformpack, "print_split_packs"), \
mock.patch.object(platformpack, "collect_emulator_extras", return_value=[]), \
mock.patch.object(platformzip, "collect_emulator_extras", return_value=[]), \
mock.patch.object(manifestbuild, "collect_emulator_extras", return_value=[]):
packs_platformpack.generate_split_packs(
"retroarch", platforms, {"files": {}}, "bios", out,
@@ -316,16 +317,18 @@ class TheSplitDirectoryReadsThePackStem(unittest.TestCase):
def test_a_reserved_character_is_made_safe_as_in_the_full_pack(self):
from unittest import mock
from packs import manifest, manifestbuild, md5pack, naming
from packs import manifest, manifestbuild, md5pack, naming, packplan, platformzip
from packs import platformpack as packs_platformpack
config = {"platform": "Foo: Bar", "version": "release/1.22", "systems": {}}
with tempfile.TemporaryDirectory() as out, \
mock.patch.object(packs_platformpack, "load_platform_config", return_value=config), \
mock.patch.object(packplan, "load_platform_config", return_value=config), \
mock.patch.object(naming, "load_platform_config", return_value=config), \
mock.patch.object(manifest, "load_platform_config", return_value=config), \
mock.patch.object(md5pack, "load_platform_config", return_value=config), \
mock.patch.object(packs_platformpack, "collect_emulator_extras", return_value=[]), \
mock.patch.object(platformzip, "collect_emulator_extras", return_value=[]), \
mock.patch.object(manifestbuild, "collect_emulator_extras", return_value=[]):
packs_platformpack.generate_split_packs(
"foo", "platforms", {"files": {}}, "bios", out, emu_profiles={},
+2 -2
View File
@@ -360,11 +360,11 @@ class AVersionTakenWithoutACheck(unittest.TestCase):
))
def test_the_pack_reads_it_as_an_alternative(self):
from packs import platformpack
from packs import platformzip
fe = {"name": "BLOOD.RFF", "source_profile": "raze", "source_path": "BLOOD.RFF"}
with tempfile.TemporaryDirectory() as tmp:
self.assertTrue(platformpack._matches_an_alternative(
self.assertTrue(platformzip._matches_an_alternative(
fe, self._file(tmp), {"raze": self.RAZE}
))