From f48b66b540fcfb0a2f99c5b9ee4895c4ef614cd2 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Wed, 18 Nov 2026 12:51:34 +0100 Subject: [PATCH] refactor: keep split packs beside platformpack --- scripts/generate_pack.py | 2 +- scripts/packs/bysystem.py | 189 ---------------------------------- scripts/packs/platformpack.py | 174 ++++++++++++++++++++++++++++++- scripts/pipeline.py | 4 +- tests/test_no_game_data.py | 3 +- tests/test_split_extras.py | 2 +- tests/test_split_names.py | 2 +- tests/test_target_verify.py | 8 +- 8 files changed, 182 insertions(+), 202 deletions(-) delete mode 100644 scripts/packs/bysystem.py diff --git a/scripts/generate_pack.py b/scripts/generate_pack.py index b22bd9198..c28c2acdd 100644 --- a/scripts/generate_pack.py +++ b/scripts/generate_pack.py @@ -97,7 +97,7 @@ from resolve.packfiles import ( if TYPE_CHECKING: pass from packs.archive import DamagedArchiveError -from packs.bysystem import generate_split_packs +from packs.platformpack import generate_split_packs from packs.datatrees import _MISSING_DATA_DIRS from packs.emulatorpack import generate_emulator_pack, generate_system_pack from packs.finalize import _baseline_shortfall, verify_and_finalize_packs diff --git a/scripts/packs/bysystem.py b/scripts/packs/bysystem.py deleted file mode 100644 index 653bffb66..000000000 --- a/scripts/packs/bysystem.py +++ /dev/null @@ -1,189 +0,0 @@ -"""A platform pack cut into one pack per system or per manufacturer.""" - -from __future__ import annotations - -import contextlib -import io -import os -from typing import TYPE_CHECKING - -from model.platforms import load_platform_config -from model.profiles import load_emulator_profiles -from packs.archive import PackRecord, _reject_unknown -from packs.emulatorpack import _system_display_name -from packs.extras import _collect_emulator_extras, extras_for_systems as _extras_for_systems -from packs.naming import _name_part, _narrowings, _platform_pack_stem -from packs.platformpack import PackNarrowing, _PartScope, _report_left_out, generate_pack -from resolve.cores import platform_scope_systems -from resolve.gamedata import without_game_data - -if TYPE_CHECKING: - from typing_extensions import Unpack - - -def _group_systems_by_manufacturer( - systems: dict[str, dict], - db: dict, - bios_dir: str, -) -> dict[str, list[str]]: - """Group system IDs by manufacturer for --split --group-by manufacturer.""" - from model.systems import derive_manufacturer - - groups: dict[str, list[str]] = {} - for sid, sys_data in systems.items(): - mfr = derive_manufacturer(sid, sys_data) - groups.setdefault(mfr, []).append(sid) - 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, - db: dict, - bios_dir: str, - output_dir: str, - group_by: str = "system", - **options: Unpack[SplitOptions], -) -> list[str]: - """Generate split packs (one ZIP per system or manufacturer). - - The parts add up to the full pack: each is built against the full pack's - 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( - 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 - # one directory under one SHA256SUMS.txt. - group_tag = "" if group_by == "system" else f"_By{group_by.title()}" - # The parts carry the version, so the directory does too: a rescrape that - # moves the version would otherwise leave the old parts beside the new - # under one SHA256SUMS.txt. - # Named by the stem the full pack reads: assembled here by hand, a '/' in - # a version nested the directory where the check never looked for it. - part_prefix = ( - f"{_platform_pack_stem([platform_name], platform_name, platforms_dir)}{split_tags}" - ) - split_dir = os.path.join(output_dir, f"{part_prefix}{group_tag}_Split") - 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(narrowing["emulators_dir"]) - narrowing["emu_profiles"] = emu_profiles - full = PackRecord() - with contextlib.redirect_stdout(io.StringIO()): - generate_pack( - platform_name, platforms_dir, db, bios_dir, split_dir, - record=full, **narrowing, - ) - - 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 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, 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} - - 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}, - ) - if zip_path: - results.append(zip_path) - - # Extras whose system the platform does not declare, or that name none, - # belong to no group: they go in a part of their own, or the parts would - # not add up to the full pack (4507 of RetroArch's 5430 were left out). - distributed = { - id(fe) - for fe in _extras_for_systems( - all_extras, [s for ids in groups.values() for s in ids] - ) - } - undistributed = [fe for fe in all_extras if id(fe) not in distributed] - if undistributed: - zip_path = part( - f"{part_prefix}_Other_Cores_BIOS_Pack.zip", - {"precomputed_extras": undistributed, "extras_only": True}, - ) - if zip_path: - results.append(zip_path) - - return results diff --git a/scripts/packs/platformpack.py b/scripts/packs/platformpack.py index b9f4fa492..efeea9e35 100644 --- a/scripts/packs/platformpack.py +++ b/scripts/packs/platformpack.py @@ -1,8 +1,9 @@ -"""Building a platform pack: plan, variant choice, slot settlement, the archive.""" +"""Building a platform pack, whole or cut per system: plan, variant choice, slot settlement, the archive.""" from __future__ import annotations import contextlib +import io import os import tempfile import zipfile @@ -32,6 +33,7 @@ from packs.archive import ( _writing_pack, ) from packs.datatrees import _data_directory_members, core_data_trees, pack_markers +from packs.emulatorpack import _system_display_name from packs.extras import ( ExtraResolver, _collect_emulator_extras, @@ -42,7 +44,7 @@ from packs.extras import ( platform_region_groups, undeclared_report, ) -from packs.naming import _narrowings, _platform_pack_name +from packs.naming import _name_part, _narrowings, _platform_pack_name, _platform_pack_stem from packs.paths import _flat, save_layout from packs.readme import _build_agnostic_rename_readme, _build_readme, save_place from resolve import packfiles as packresolve, regions as resolve_regions, slot as slot_mod @@ -1140,3 +1142,171 @@ def generate_pack( build.run() build.report() return plan.zip_path + + +def _group_systems_by_manufacturer( + systems: dict[str, dict], + db: dict, + bios_dir: str, +) -> dict[str, list[str]]: + """Group system IDs by manufacturer for --split --group-by manufacturer.""" + from model.systems import derive_manufacturer + + groups: dict[str, list[str]] = {} + for sid, sys_data in systems.items(): + mfr = derive_manufacturer(sid, sys_data) + groups.setdefault(mfr, []).append(sid) + 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, + db: dict, + bios_dir: str, + output_dir: str, + group_by: str = "system", + **options: Unpack[SplitOptions], +) -> list[str]: + """Generate split packs (one ZIP per system or manufacturer). + + The parts add up to the full pack: each is built against the full pack's + 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( + 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 + # one directory under one SHA256SUMS.txt. + group_tag = "" if group_by == "system" else f"_By{group_by.title()}" + # The parts carry the version, so the directory does too: a rescrape that + # moves the version would otherwise leave the old parts beside the new + # under one SHA256SUMS.txt. + # Named by the stem the full pack reads: assembled here by hand, a '/' in + # a version nested the directory where the check never looked for it. + part_prefix = ( + f"{_platform_pack_stem([platform_name], platform_name, platforms_dir)}{split_tags}" + ) + split_dir = os.path.join(output_dir, f"{part_prefix}{group_tag}_Split") + 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(narrowing["emulators_dir"]) + narrowing["emu_profiles"] = emu_profiles + full = PackRecord() + with contextlib.redirect_stdout(io.StringIO()): + generate_pack( + platform_name, platforms_dir, db, bios_dir, split_dir, + record=full, **narrowing, + ) + + 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 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, 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} + + 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}, + ) + if zip_path: + results.append(zip_path) + + # Extras whose system the platform does not declare, or that name none, + # belong to no group: they go in a part of their own, or the parts would + # not add up to the full pack (4507 of RetroArch's 5430 were left out). + distributed = { + id(fe) + for fe in _extras_for_systems( + all_extras, [s for ids in groups.values() for s in ids] + ) + } + undistributed = [fe for fe in all_extras if id(fe) not in distributed] + if undistributed: + zip_path = part( + f"{part_prefix}_Other_Cores_BIOS_Pack.zip", + {"precomputed_extras": undistributed, "extras_only": True}, + ) + if zip_path: + results.append(zip_path) + + return results diff --git a/scripts/pipeline.py b/scripts/pipeline.py index 1275f7d1e..899160999 100644 --- a/scripts/pipeline.py +++ b/scripts/pipeline.py @@ -863,7 +863,7 @@ def check_split_packs( comparison of this step. """ from packs import archive - from packs import bysystem + from packs import platformpack as packs_platformpack from packs import platformpack from resolve import packfiles as packresolve from core.ziptools import build_zip_contents_index @@ -897,7 +897,7 @@ def check_split_packs( platformpack.generate_pack( platform, platforms_dir, db, "bios", "", record=full, **args, ) - bysystem.generate_split_packs( + packs_platformpack.generate_split_packs( platform, platforms_dir, db, "bios", "", records=parts, **args, ) lines = split_differences(full, parts, archive._same_bytes) diff --git a/tests/test_no_game_data.py b/tests/test_no_game_data.py index cb9d13de3..fb1e67335 100644 --- a/tests/test_no_game_data.py +++ b/tests/test_no_game_data.py @@ -32,7 +32,6 @@ from model.platforms import load_platform_config, _platform_config_cache from model.profiles import _emulator_profiles_cache import generate_db from packs import archive as packs_archive -from packs import bysystem from packs import manifest as packs_manifest from packs import platformpack import install @@ -214,7 +213,7 @@ class PackNarrowing(_Fixture): def test_split_parts_add_up_to_the_narrowed_pack(self): printed = io.StringIO() with contextlib.redirect_stdout(printed): - parts = bysystem.generate_split_packs( + parts = platformpack.generate_split_packs( "handheld", str(self.platforms), self.db, str(self.bios), str(self.out), emulators_dir=str(self.emulators), emu_profiles=PROFILES, offline=True, no_game_data=True, diff --git a/tests/test_split_extras.py b/tests/test_split_extras.py index 78794a98a..0ea27e6f7 100644 --- a/tests/test_split_extras.py +++ b/tests/test_split_extras.py @@ -22,7 +22,7 @@ from core.hashing import compute_hashes from model.profiles import load_emulator_profiles from packs.archive import PackRecord from packs.platformpack import generate_pack -from packs.bysystem import generate_split_packs +from packs.platformpack import generate_split_packs REPO_ROOT = Path(__file__).resolve().parent.parent diff --git a/tests/test_split_names.py b/tests/test_split_names.py index e9a5da330..47bf45df3 100644 --- a/tests/test_split_names.py +++ b/tests/test_split_names.py @@ -22,7 +22,7 @@ import yaml from model.platforms import _platform_config_cache from core.ziptools import build_zip_contents_index from core.hashing import compute_hashes -from packs.bysystem import generate_split_packs +from packs.platformpack import generate_split_packs class SplitPartNames(unittest.TestCase): diff --git a/tests/test_target_verify.py b/tests/test_target_verify.py index b8cc0e74d..7eb03acfd 100644 --- a/tests/test_target_verify.py +++ b/tests/test_target_verify.py @@ -137,7 +137,7 @@ class ASplitBuildsOnlyTheTargetsSystems(unittest.TestCase): from unittest import mock import generate_pack - from packs import bysystem + from packs import platformpack as packs_platformpack from packs import platformpack from model.profiles import load_emulator_profiles @@ -154,7 +154,7 @@ class ASplitBuildsOnlyTheTargetsSystems(unittest.TestCase): mock.patch.object(platformpack, "generate_pack", record), \ mock.patch.object(generate_pack, "_collect_emulator_extras", return_value=[]): - bysystem.generate_split_packs( + packs_platformpack.generate_split_packs( "retroarch", platforms, {"files": {}}, "bios", out, emu_profiles=profiles, target_cores=_target_cores_for("retroarch", "switch", platforms), @@ -208,7 +208,7 @@ class TheSplitDirectoryReadsThePackStem(unittest.TestCase): from unittest import mock import generate_pack - from packs import bysystem + from packs import platformpack as packs_platformpack from packs import naming config = {"platform": "Foo: Bar", "version": "release/1.22", "systems": {}} @@ -217,7 +217,7 @@ class TheSplitDirectoryReadsThePackStem(unittest.TestCase): return_value=config), \ mock.patch.object(generate_pack, "_collect_emulator_extras", return_value=[]): - bysystem.generate_split_packs( + packs_platformpack.generate_split_packs( "foo", "platforms", {"files": {}}, "bios", out, emu_profiles={}, ) stem = naming._platform_pack_stem(["foo"], "foo", "platforms")