From 1c978f3083c81cfbd96d3a4c62bb3ede31d45bd3 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Fri, 20 Nov 2026 19:19:42 +0100 Subject: [PATCH] refactor: fold destinations with one rule comparable_destination moves to core.paths. Placed, placement_block, the shared group merge of model.platforms and LauncherPlace.fold read it in place of their own lower(). It also trims surrounding blanks and slashes and folds with casefold: no destination, collection path or launcher name differs under those rules today, so the output is the same. --- scripts/core/paths.py | 10 ++++++++++ scripts/model/platforms.py | 4 ++-- scripts/packs/archive.py | 14 ++++++++------ scripts/packs/paths.py | 4 ++-- scripts/packs/slots.py | 3 ++- scripts/resolve/collisions.py | 11 +---------- scripts/resolve/launchers.py | 4 ++-- 7 files changed, 27 insertions(+), 23 deletions(-) diff --git a/scripts/core/paths.py b/scripts/core/paths.py index 572e23e5c..67b7740f4 100644 --- a/scripts/core/paths.py +++ b/scripts/core/paths.py @@ -28,6 +28,16 @@ def sanitize_pack_path(raw: str) -> str: return "/".join(p for p in raw.split("/") if p and p not in ("..", ".")) +def comparable_destination(destination: str, fold: bool = True) -> str: + """Comparable form of a destination, since layers write it differently. + + Case is folded only where the platform's filesystem folds it: on Linux + RetroDECK ships bios/DISK.ROM and bios/disk.rom side by side. + """ + cleaned = destination.strip().strip("/").replace("\\", "/") + return cleaned.casefold() if fold else cleaned + + def declared_destination(entry: dict) -> str: """The destination a declaration names, its name when it names none, normalized as sanitize_pack_path does: the key the builder, the manifest diff --git a/scripts/model/platforms.py b/scripts/model/platforms.py index 4a5427402..9973d0878 100644 --- a/scripts/model/platforms.py +++ b/scripts/model/platforms.py @@ -9,7 +9,7 @@ try: except ImportError: yaml = None -from core.paths import PLATFORMS_DIR +from core.paths import PLATFORMS_DIR, comparable_destination from model.sources import source_yaml @@ -89,7 +89,7 @@ def _apply_shared_groups(config: dict, shared_groups: dict) -> None: # Lakka and RetroPie run on Linux, and np2kai opens 2608_bd.wav beside # System.dat's 2608_BD.WAV. def fold(destination: str) -> str: - return destination.lower() if config.get("case_insensitive_fs") else destination + return comparable_destination(destination, bool(config.get("case_insensitive_fs"))) for system in config.get("systems", {}).values(): for group_name in system.get("includes", []): diff --git a/scripts/packs/archive.py b/scripts/packs/archive.py index 9a484ffc0..11f140cea 100644 --- a/scripts/packs/archive.py +++ b/scripts/packs/archive.py @@ -14,7 +14,7 @@ from pathlib import Path from core.artifacts import set_plain_mode from core.deterministic_zip import FIXED_DATE_TIME from core.hashing import compute_hashes -from core.paths import sanitize_pack_path +from core.paths import comparable_destination, sanitize_pack_path from packs.paths import has_path_conflict, register_path, placement_block @@ -145,7 +145,7 @@ class Placed: tree walk all place files through this one record. ``content``, when given, records the sha1 of what each taken path holds, - keyed by its lowercased destination: a case twin carrying the same + keyed by its folded destination: a case twin carrying the same bytes serves the core on a filesystem that folds the names. """ @@ -159,16 +159,18 @@ class Placed: self.destinations.add(dest) register_path(dest, self.destinations, self.parents) if self.fold: - self.lower.add(dest.lower()) + self.lower.add(comparable_destination(dest)) if self.content is not None: - self.content[dest.lower()] = sha1 + self.content[comparable_destination(dest)] = sha1 def content_at(self, dest: str) -> str | None: """The sha1 recorded for the path a destination folds onto.""" - return (self.content or {}).get(dest.lower()) + return (self.content or {}).get(comparable_destination(dest)) def holds(self, dest: str) -> bool: - return dest in self.destinations or (self.fold and dest.lower() in self.lower) + return dest in self.destinations or ( + self.fold and comparable_destination(dest) in self.lower + ) def conflicts(self, dest: str) -> bool: return has_path_conflict(dest, self.destinations, self.parents) diff --git a/scripts/packs/paths.py b/scripts/packs/paths.py index 69a9be2cd..d8b3d43cf 100644 --- a/scripts/packs/paths.py +++ b/scripts/packs/paths.py @@ -9,7 +9,7 @@ from __future__ import annotations from collections.abc import Iterable from dataclasses import dataclass -from core.paths import sanitize_pack_path +from core.paths import comparable_destination, sanitize_pack_path from resolve.validation import read_from_save_dir @@ -177,7 +177,7 @@ def placement_block( """ if dest in seen_files: return "taken" - if case_insensitive and dest.lower() in seen_lower: + if case_insensitive and comparable_destination(dest) in seen_lower: return "case_twin" if has_path_conflict(dest, seen_files, seen_parents): return "path_conflict" diff --git a/scripts/packs/slots.py b/scripts/packs/slots.py index 75ba6bd4d..88e6b5834 100644 --- a/scripts/packs/slots.py +++ b/scripts/packs/slots.py @@ -10,7 +10,8 @@ from core.hashing import parse_md5_list from model.profiles import entry_system_ids, lists_files from resolve import nativemode from resolve.archives import archive_entry, holds_required_members -from resolve.collisions import accepted_by_all, content_sha1, lists_hashes, comparable_destination +from core.paths import comparable_destination +from resolve.collisions import accepted_by_all, content_sha1, lists_hashes from resolve.cores import resolve_platform_cores from resolve.layout import system_dir_path from resolve.localfile import resolution_is_hash_exact, resolve_local_file diff --git a/scripts/resolve/collisions.py b/scripts/resolve/collisions.py index c1b778759..885131ce1 100644 --- a/scripts/resolve/collisions.py +++ b/scripts/resolve/collisions.py @@ -7,21 +7,12 @@ import zlib from dataclasses import dataclass, field from core.hashing import compute_hashes, parse_hash_list, parse_md5_list +from core.paths import comparable_destination from core.ziptools import check_inside_zip from resolve.localfile import resolve_local_file, variant_base_name from resolve.packfiles import records_by_path, preferred_entries -def comparable_destination(destination: str, fold: bool = True) -> str: - """Comparable form of a destination, since layers write it differently. - - Case is folded only where the platform's filesystem folds it: on Linux - RetroDECK ships bios/DISK.ROM and bios/disk.rom side by side. - """ - cleaned = destination.strip().strip("/").replace("\\", "/") - return cleaned.casefold() if fold else cleaned - - @dataclass class Collision: """One destination a platform fills with two different files.""" diff --git a/scripts/resolve/launchers.py b/scripts/resolve/launchers.py index bd3111968..cc9533f53 100644 --- a/scripts/resolve/launchers.py +++ b/scripts/resolve/launchers.py @@ -25,7 +25,7 @@ from model.declarations import CONFIGS_ROOT, ROMS_ROOT, declared_root, outer_lay from model.profiles import lists_files from resolve.content import name_match_size_ok from resolve.localfile import resolve_local_file -from core.paths import sanitize_pack_path +from core.paths import comparable_destination, sanitize_pack_path from core.paths import REPO_ROOT # The kinds whose names say which file the launcher opens. A link of a named @@ -74,7 +74,7 @@ class LauncherPlace: passes_to: str = "" def fold(self, name: str) -> str: - return name.lower() if self.case_insensitive else name + return comparable_destination(name, self.case_insensitive) def basenames(self) -> set[str]: return {self.fold(p.rsplit("/", 1)[-1]) for p in self.paths}