diff --git a/scripts/packs/manifest.py b/scripts/packs/manifest.py index 23e778d37..d8c90ad7d 100644 --- a/scripts/packs/manifest.py +++ b/scripts/packs/manifest.py @@ -50,7 +50,7 @@ from packs.manifestentry import ( record_unplaceable, ) from packs.manifestselect import SelectionIndex -from packs.paths import save_layout +from packs.paths import platform_destination, save_layout from packs.scope import pack_sections, platform_list_version, system_titles from packs.standalone import standalone_copies, standalone_layouts from resolve.contentlayout import TARGET_KEY @@ -255,12 +255,11 @@ class _ManifestBuild: for file_entry in system.get("files", []): if opts.required_only and file_entry.get("required") is False: continue - dest = sanitize_pack_path(file_entry.get("destination", file_entry["name"])) + dest, full_dest = platform_destination(file_entry, self.base_dest) if not dest: continue if self.region_drops and dest in self.region_drops: continue - full_dest = f"{self.base_dest}/{dest}" if self.base_dest else dest yield sys_id, file_entry, dest, full_dest def declaration_order(self, full_dest: str) -> list[tuple[list[str], dict, str]]: diff --git a/scripts/packs/md5pack.py b/scripts/packs/md5pack.py index 9421ed598..8d463ca77 100644 --- a/scripts/packs/md5pack.py +++ b/scripts/packs/md5pack.py @@ -13,7 +13,7 @@ from model.profiles import load_emulator_profiles, select_emulator_profiles from model.releaserecord import PACK_SUFFIX from packs.archive import add_pack_member, writing_pack from packs.naming import file_name_part, pack_narrowings, selection_digest -from packs.paths import resolve_destination +from packs.paths import platform_destination, resolve_destination, under_base from resolve.packfiles import resolve_file, sha1_for_hash if TYPE_CHECKING: @@ -168,10 +168,14 @@ def _md5_destination(entry: dict, layout: _Md5Layout, opts: _Md5Settings) -> str # its standalone_path, which --standalone has to reach. dest = resolve_destination(matched_fe, opts.standalone) else: - dest = matched_fe.get("destination", matched_fe.get("name", name)) + # Where the platform pack and the installer put the declaration. + _dest, full_dest = platform_destination(matched_fe, layout.base_dest) + if full_dest: + return full_dest - if layout.base_dest and not dest.startswith(layout.base_dest): - return f"{layout.base_dest}/{dest}" + base = layout.base_dest + if base and dest != base and not dest.startswith(f"{base}/"): + return under_base(dest, base) return dest diff --git a/scripts/packs/paths.py b/scripts/packs/paths.py index d8b3d43cf..fc5f98fdf 100644 --- a/scripts/packs/paths.py +++ b/scripts/packs/paths.py @@ -9,10 +9,29 @@ from __future__ import annotations from collections.abc import Iterable from dataclasses import dataclass -from core.paths import comparable_destination, sanitize_pack_path +from core.paths import comparable_destination, declared_destination, sanitize_pack_path from resolve.validation import read_from_save_dir +def under_base(path: str, base_dest: str) -> str: + """A pack path below the platform's base destination, joined as a path.""" + return f"{base_dest}/{path}" if base_dest else path + + +def platform_destination(entry: dict, base_dest: str) -> tuple[str, str]: + """(destination, pack path) of a platform declaration. + + The destination is the declared one, else the name, made safe. The pack + path puts it below the base destination. Both are empty for a + declaration that names no file. The platform pack, the install manifest + and the hash-selected pack all place a declaration here. + """ + dest = declared_destination(entry) + if not dest: + return "", "" + return dest, under_base(dest, base_dest) + + @dataclass(frozen=True) class SaveLayout: """Where a platform's frontend keeps the save directory it hands a core. diff --git a/scripts/packs/platformpack.py b/scripts/packs/platformpack.py index d290f6962..a3acd2832 100644 --- a/scripts/packs/platformpack.py +++ b/scripts/packs/platformpack.py @@ -13,7 +13,7 @@ from typing import TYPE_CHECKING, TypedDict from model.registry import load_platform_registry from core.hashing import compute_hashes from core.options import reject_unknown -from core.paths import EMULATORS_DIR, declared_destination, sanitize_pack_path +from core.paths import EMULATORS_DIR, declared_destination from core.ziptools import is_archive_path from model.alternatives import core_file_spares from model.declarations import OUTER_ROOTS, declared_root @@ -50,7 +50,7 @@ from packs.extras import ( ) from packs.naming import file_name_part, pack_narrowings, platform_pack_name, platform_pack_stem from packs.packcounts import PackCounts, pack_counts -from packs.paths import flat_arcname, save_layout +from packs.paths import flat_arcname, platform_destination, save_layout from packs.platformreport import print_platform_pack, print_split_packs 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 @@ -559,13 +559,12 @@ class _PlatformPackBuild: plan, opts = self.plan, self.plan.opts if opts.required_only and file_entry.get("required") is False: return - dest = sanitize_pack_path(file_entry.get("destination", file_entry["name"])) + 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 - full_dest = f"{plan.base_dest}/{dest}" if plan.base_dest else dest already_packed = self.placed.holds(full_dest) preferred = plan.preferred_entries.get(full_dest) diff --git a/tests/test_pack_destinations.py b/tests/test_pack_destinations.py index f3d7dcc1b..61a9dfe83 100644 --- a/tests/test_pack_destinations.py +++ b/tests/test_pack_destinations.py @@ -109,6 +109,29 @@ class AUserProvidedSlot(_Fixture): self.assertEqual(report["pack_destinations"], []) +class AHashSelectedPack(_Fixture): + """--from-md5 placed a declaration under the base destination only when + its path did not start with the same letters: Batocera's bios_CD_U.bin + went to the root of the pack, scph5501.bin to bios/.""" + + def test_every_declaration_lands_under_the_base_destination(self): + from packs.md5pack import generate_md5_pack + + self._platform([ + {"name": "sgb.sfc", "destination": "system_sgb.sfc"}, + {"name": "program.rom", "destination": "program.rom"}, + ]) + hashes = [("md5", entry["md5"]) for entry in self.db["files"].values() + if entry["name"] != "core.bin"] + outcome = generate_md5_pack( + hashes, self.db, str(self.bios), str(self.root / "out"), + platform_name="paths", platforms_dir=str(self.platforms), + ) + with zipfile.ZipFile(outcome.zip_path) as zf: + members = set(zf.namelist()) + self.assertEqual(members, {"system/system_sgb.sfc", "system/program.rom"}) + + class AnEntryWithoutAFileTakesNoPath(unittest.TestCase): """EmuDeck's system:md5 entries name no file. verify credited the bare base destination as a pack path, and the comparison failed on ''."""