refactor: one selection for emulator pack entries

emulatorcheck and emulatorpack each expanded a profile, filtered it by
system, by --required-only and by region, and chose its archive units
and loose slots. emulator_pack_entries now does it once and both read
it. The --required-only spares are taken among the entries the pack
still carries, as their docstring says: the builder passed the
unnarrowed list, so an optional member of a required set could be kept
over a required one the pack ships. No profile has such a member today.
This commit is contained in:
Abdessamad Derraz committed 2026-11-21 21:42:19 +01:00
1 parent 0617598e21
commit e0cbf35adc
5 files changed
+195 -191

No files matched your search

+40 -131
View File
@@ -14,7 +14,6 @@ from model.alternatives import set_requirements, settle_sets, slot_set_keys
from model.database import sha1_at
from model.profiles import (
ProfileSelectionError,
entry_serves_systems,
load_emulator_profiles,
no_profile_for_systems,
profiles_for_systems,
@@ -22,6 +21,7 @@ from model.profiles import (
)
from model.registry import load_data_dir_registry
from packs.archive import Placed
from packs.emulatorentries import EmulatorPackEntries, EmulatorSelection, emulator_pack_entries
from packs.verdicts import (
RELEASE_ASSET_PENDING,
Severity,
@@ -31,8 +31,6 @@ from packs.verdicts import (
emulator_severity,
storage_verdict,
)
from resolve.archives import archive_entry, archive_is_optional
from resolve.directories import expand_directory_entries
from resolve.validation import (
build_validation_index,
build_ground_truth,
@@ -99,54 +97,6 @@ _SLOT_PREFERENCE: tuple[Callable[[tuple], bool], ...] = (
)
def _emulator_files(
emu_name: str,
profile: dict,
db: dict,
standalone: bool,
all_profiles: dict,
system_ids: list[str] | None,
required_only: bool,
region_drops: set[str],
) -> list[dict]:
"""The entries of a profile the emulator pack carries."""
from packs.extras import expand_agnostic_entries
from packs.paths import resolve_destination
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 required_only:
optional_archives = {
fe["archive"] for fe in files
if fe.get("archive")
and archive_is_optional(archive_entry(fe["archive"], files), fe["archive"], files)
}
files = [
fe for fe in files
if fe.get("archive") not in optional_archives
and (fe.get("archive") or fe.get("required") is not False)
]
if region_drops:
# Loose files only, as the emulator pack: an archive ships whole,
# and dropping kronos' regional stvbios.zip members rebuilt the
# archive here from 4 of its 14 members and changed its required.
files = [
fe
for fe in files
if fe.get("archive")
or resolve_destination(fe, standalone) not in region_drops
]
return files
def _archive_result(
archive: str, required: bool, local_path: str | None, resolve_status: str,
) -> dict:
@@ -287,12 +237,12 @@ class _EmulatorPack:
self.data_dir_notices.append(ref)
def verify_profile(
self, emu_name: str, profile: dict, files: list[dict], spares: set[str],
self, emu_name: str, profile: dict, carried: EmulatorPackEntries,
) -> None:
"""Verify one profile's entries and add them to the report."""
self.note_data_dirs(profile)
if not files:
if not carried.carries_entries:
self.details.append(
{
"name": f"({emu_name})",
@@ -309,34 +259,17 @@ class _EmulatorPack:
# Each destination's outcome, settled by set once the profile is
# read: an archive unit is a member like a loose file (Kronos reads
# kronos/stvbios.zip, else stvbios.zip).
placed_results = self._archive_results(emu_name, profile, files, spares)
self._place_loose(emu_name, self._loose_results(emu_name, files), placed_results)
placed_results = self._archive_results(emu_name, carried)
self._place_loose(emu_name, self._loose_results(emu_name, carried), placed_results)
self._settle(emu_name, profile, placed_results)
self.occupy_trees(emu_name, profile)
def _archive_results(
self, emu_name: str, profile: dict, files: list[dict], spares: set[str],
) -> list[PlacedResult]:
def _archive_results(self, emu_name: str, carried: EmulatorPackEntries) -> list[PlacedResult]:
# Verify archives as units (e.g., neogeo.zip, aes.zip)
from packs.paths import archive_destination
from resolve.packfiles import resolve_file
placed_results: list[PlacedResult] = []
seen_archives: set[str] = set()
for file_entry in files:
archive = file_entry.get("archive")
if not archive or archive in seen_archives:
continue
seen_archives.add(archive)
own_entry = archive_entry(archive, files)
# Where the pack puts it: an archive_prefix or pack_structure
# moves it, and a loose same-named entry is another file.
dest = archive_destination(
archive, profile.get("archive_prefix", ""),
profile.get("pack_structure"), self.standalone,
)
if dest in spares:
continue
for archive, own_entry, dest in carried.archives:
local_path, resolve_status = self.pending(own_entry, *resolve_file(
own_entry,
self.db,
@@ -345,12 +278,10 @@ class _EmulatorPack:
data_dir_registry=self.data_registry,
offline=True,
))
required = any(
f.get("archive") == archive and f.get("required", True)
for f in files
)
members = [f for f in carried.entries if f.get("archive") == archive]
required = any(f.get("required", True) for f in members)
result = _archive_result(archive, required, local_path, resolve_status)
result["system"] = file_entry.get("system", "")
result["system"] = members[0].get("system", "")
result["hle_fallback"] = False
result["ground_truth"] = build_ground_truth(archive, self.validation_index)
result["destination"] = dest
@@ -372,9 +303,8 @@ class _EmulatorPack:
return placed_results
def _loose_results(
self, emu_name: str, files: list[dict],
self, emu_name: str, carried: EmulatorPackEntries,
) -> dict[str, list[tuple[dict, bool, bool, dict]]]:
from packs.paths import resolve_destination
from resolve.packfiles import resolve_file
# Results of this emulator, by destination. Several entries of one
@@ -383,38 +313,31 @@ class _EmulatorPack:
# holding one of them fills the slot, and the others are not gaps.
emu_results: dict[str, list[tuple[dict, bool, bool, dict]]] = {}
for file_entry in files:
# Skip archive members (verified as archive units above)
if file_entry.get("archive"):
continue
for dest, alternatives in carried.slots.items():
for file_entry in alternatives:
# The emulator pack's resolution, release assets included.
local_path, resolve_status = self.pending(file_entry, *resolve_file(
{**with_archive_members(file_entry, carried.entries), "source_profile": emu_name},
self.db,
BIOS_DIR,
self.zip_contents,
dest_hint=file_entry.get("path", ""),
data_dir_registry=self.data_registry,
offline=True,
))
if resolve_status in ("not_found", "user_provided", "external"):
local_path = None
name = file_entry.get("name", "")
required = file_entry.get("required", True)
hle = file_entry.get("hle_fallback", False)
dest_hint = file_entry.get("path", "")
# The emulator pack's resolution, release assets included.
local_path, resolve_status = self.pending(file_entry, *resolve_file(
{**with_archive_members(file_entry, files), "source_profile": emu_name},
self.db,
BIOS_DIR,
self.zip_contents,
dest_hint=dest_hint,
data_dir_registry=self.data_registry,
offline=True,
))
if resolve_status in ("not_found", "user_provided", "external"):
local_path = None
name = file_entry.get("name", "")
required = file_entry.get("required", True)
hle = file_entry.get("hle_fallback", False)
result = self._loose_result(
file_entry, name, required, local_path, resolve_status, emu_name,
)
result["system"] = file_entry.get("system", "")
result["hle_fallback"] = hle
result["ground_truth"] = build_ground_truth(name, self.validation_index)
# The slot the emulator pack places this entry in: standalone_path
# under --standalone, the sanitised path.
dest = resolve_destination(file_entry, self.standalone) or name
emu_results.setdefault(dest, []).append((result, required, hle, file_entry))
result = self._loose_result(
file_entry, name, required, local_path, resolve_status, emu_name,
)
result["system"] = file_entry.get("system", "")
result["hle_fallback"] = hle
result["ground_truth"] = build_ground_truth(name, self.validation_index)
emu_results.setdefault(dest, []).append((result, required, hle, file_entry))
return emu_results
def _loose_result(
@@ -619,8 +542,6 @@ def verify_emulator(
file or tree marked ``required: false`` is left out, and an archive when
it and every member it carries are optional, as the builder leaves them.
"""
from packs.paths import resolve_destination
# Also load aliases for redirect messages
all_profiles = load_emulator_profiles(emulators_dir, skip_aliases=False)
@@ -653,24 +574,12 @@ def verify_emulator(
zip_contents=zip_contents, data_registry=data_registry,
)
selection = EmulatorSelection(
db, standalone, all_profiles, validation_index, zip_contents, data_registry,
system_ids=system_ids, required_only=required_only, region_drops=region_drops,
)
for emu_name, profile in sorted(selected):
files = _emulator_files(
emu_name, profile, db, standalone, all_profiles, system_ids,
required_only, region_drops,
)
spares: set[str] = set()
if required_only:
from packs.extras import emulator_required_spares
spares = emulator_required_spares(
emu_name, profile, files, standalone, region_drops, db,
validation_index, zip_contents, data_registry=data_registry,
)
files = [
fe for fe in files
if fe.get("archive") or resolve_destination(fe, standalone) not in spares
]
pack.verify_profile(emu_name, profile, files, spares)
pack.verify_profile(emu_name, profile, emulator_pack_entries(emu_name, profile, selection))
counts = {
Severity.OK: 0,
+133
View File
@@ -0,0 +1,133 @@
"""The entries of a profile an emulator pack carries."""
from __future__ import annotations
from dataclasses import dataclass, field
from model.profiles import entry_serves_systems
from packs.extras import emulator_required_spares, expand_agnostic_entries
from packs.paths import archive_destination, resolve_destination
from resolve.archives import archive_entry, archive_is_optional
from resolve.directories import expand_directory_entries
from resolve.validation import deliverable
@dataclass(frozen=True)
class EmulatorSelection:
"""What narrows every profile of one emulator or system pack."""
db: dict
standalone: bool
all_profiles: dict
validation_index: dict
zip_contents: dict | None
data_registry: dict | None
system_ids: list[str] | None = None
required_only: bool = False
region_drops: set[str] = field(default_factory=set)
@dataclass(frozen=True)
class EmulatorPackEntries:
"""One profile's share of the pack: its archive units and loose slots."""
# Every entry serving the pack's systems, before narrowing: an archive's
# members and a loose file resolved as an archive are read from these.
entries: list[dict]
# Archive units the pack carries, in declaration order:
# (archive name, its own entry, destination).
archives: list[tuple[str, dict, str]]
# Loose destinations the pack carries, each with the entries the code
# accepts there, in declaration order.
slots: dict[str, list[dict]]
# Whether anything is left once narrowed, archive members included.
carries_entries: bool
def _profile_entries(emu_name: str, profile: dict, selection: EmulatorSelection) -> list[dict]:
"""The deliverable entries of a profile, expanded, for the pack's systems."""
files = expand_agnostic_entries(
expand_directory_entries(
deliverable(profile.get("files", []), selection.standalone),
selection.db,
selection.standalone,
),
emu_name, profile, selection.db, selection.standalone, selection.all_profiles,
)
if selection.system_ids:
system_ids = selection.system_ids
files = [fe for fe in files if entry_serves_systems(fe, profile, system_ids)]
return files
def _narrowed(files: list[dict], selection: EmulatorSelection) -> list[dict]:
"""The entries --required-only and the region filter leave."""
if selection.required_only:
optional_archives = {
fe["archive"] for fe in files
if fe.get("archive")
and archive_is_optional(archive_entry(fe["archive"], files), fe["archive"], files)
}
files = [
fe for fe in files
if fe.get("archive") not in optional_archives
and (fe.get("archive") or fe.get("required") is not False)
]
if selection.region_drops:
# Loose files only: an archive ships whole, and dropping kronos'
# regional stvbios.zip members rebuilt the archive from 4 of its 14
# members and changed its required.
files = [
fe
for fe in files
if fe.get("archive")
or resolve_destination(fe, selection.standalone) not in selection.region_drops
]
return files
def emulator_pack_entries(
emu_name: str, profile: dict, selection: EmulatorSelection,
) -> EmulatorPackEntries:
"""What the emulator pack carries of one profile. The builder and
verify --emulator both read this."""
standalone = selection.standalone
entries = _profile_entries(emu_name, profile, selection)
files = _narrowed(entries, selection)
spares = emulator_required_spares(
emu_name, profile, files, standalone, selection.region_drops, selection.db,
selection.validation_index, selection.zip_contents,
data_registry=selection.data_registry,
) if selection.required_only else set()
archives: list[tuple[str, dict, str]] = []
seen: set[str] = set()
for fe in files:
name = fe.get("archive")
if not name or name in seen:
continue
seen.add(name)
# Where the pack puts it: an archive_prefix or pack_structure moves
# it, and a loose same-named entry is another file.
dest = archive_destination(
name, profile.get("archive_prefix", ""), profile.get("pack_structure"), standalone,
)
if dest not in spares:
archives.append((name, archive_entry(name, entries), dest))
# The entries of one profile on one destination are the releases the
# code accepts there (raze's seven SW.GRP): the slot has one outcome
# however many alternatives it lists.
slots: dict[str, list[dict]] = {}
carries_entries = False
for fe in files:
if fe.get("archive"):
carries_entries = True
continue
dest = resolve_destination(fe, standalone)
if dest in spares:
continue
carries_entries = True
if dest:
slots.setdefault(dest, []).append(fe)
return EmulatorPackEntries(entries, archives, slots, carries_entries)
+14 -56
View File
@@ -19,7 +19,6 @@ from core.ziptools import build_zip_contents_index, is_archive_path
from model.alternatives import settle_sets, slot_set_keys
from model.profiles import (
ProfileSelectionError,
entry_serves_systems,
load_emulator_profiles,
no_profile_for_systems,
profiles_for_systems,
@@ -37,14 +36,12 @@ from packs.archive import (
writing_pack,
)
from packs.datatrees import pack_data_tree
from packs.extras import emulator_region_drops, emulator_required_spares, expand_agnostic_entries
from packs.emulatorentries import EmulatorSelection, emulator_pack_entries
from packs.extras import emulator_region_drops
from packs.naming import (
MAX_PACK_NAME_BYTES, bounded_join, file_name_part, pack_narrowings, system_display_name,
)
from packs.paths import archive_destination, resolve_destination
from resolve import packfiles as packresolve
from resolve.archives import archive_entry, archive_is_optional
from resolve.directories import expand_directory_entries
from resolve.packfiles import download_external, resolve_file
from resolve.validation import (
build_validation_index,
@@ -132,18 +129,12 @@ class _EmulatorPackBuild:
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)
]
carried = emulator_pack_entries(emu_name, profile, EmulatorSelection(
self.db, self.standalone, self.all_profiles, self.validation_index,
self.zip_contents, self.data_registry, system_ids=self.system_ids,
required_only=self.required_only, region_drops=self.region_drops,
))
files = carried.entries
if not files and not profile.get("data_directories"):
self.progress(f" No files needed for {profile.get('emulator', emu_name)}")
@@ -151,26 +142,13 @@ class _EmulatorPackBuild:
# 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
outcomes: list[_Outcome] = [
self._pack_archive(own_entry, archive_name, archive_dest)
for archive_name, own_entry, archive_dest in sorted(
carried.archives, key=lambda unit: unit[0]
)
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():
]
for dest, alternatives in carried.slots.items():
outcome = self._pack_slot(emu_name, files, dest, alternatives)
if outcome is not None:
outcomes.append(outcome)
@@ -215,26 +193,6 @@ class _EmulatorPackBuild:
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.
+2
View File
@@ -22,12 +22,14 @@ COMMANDS = {
"packs/manifest.py", "packs/finalize.py", "packs/outputdir.py", "packs/variants.py",
"packs/manifestrun.py", "packs/packcheck.py", "packs/runscope.py", "packs/platformrun.py",
"packs/packoptions.py", "packs/packmodes.py", "packs/packcounts.py",
"packs/emulatorentries.py", "packs/integrity.py",
),
"verify.py": (
"packs/verdicts.py", "packs/gaps.py", "packs/coverage.py", "packs/emulatorcheck.py",
"packs/report.py", "packs/shares.py", "packs/verifyplatforms.py", "packs/verifysummary.py",
"packs/verifyoutput.py", "packs/strictgate.py", "packs/verifyoptions.py",
"packs/verifyprint.py", "packs/runscope.py", "packs/verifyrun.py",
"packs/emulatorentries.py",
),
"cross_reference.py": (
"packs/crossref.py", "packs/crossrefscope.py", "packs/crossrefrun.py",
+6 -4
View File
@@ -3850,11 +3850,13 @@ class AnArchiveShipsWholeUnderARegion(unittest.TestCase):
required changed, while the emulator pack ships the archive whole."""
def test_verify_filters_loose_files_only(self):
source = script_source("verify.py")
self.assertIn(
'if fe.get("archive")\n or resolve_destination(fe, standalone) not in region_drops',
source,
loose_only = (
'if fe.get("archive")\n or resolve_destination(fe, selection.standalone)'
' not in selection.region_drops'
)
# The pack and the report read the same selection.
self.assertIn(loose_only, script_source("verify.py"))
self.assertIn(loose_only, script_source("generate_pack.py"))
class APathConflictKeepsTheMainVerdict(unittest.TestCase):