refactor: keep split packs beside platformpack

This commit is contained in:
Abdessamad Derraz committed 2026-11-18 13:47:31 +01:00
1 parent 0b532366d0
commit f48b66b540
8 files changed
+182 -202

No files matched your search

+1 -1
View File
@@ -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
-189
View File
@@ -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
+172 -2
View File
@@ -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
+2 -2
View File
@@ -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)