From fb17242a78d63706de6dda51668a26bcceeb02ba Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 04:34:20 +0100 Subject: [PATCH] refactor: one bounded join for pack file names bounded_pack_name repeated bounded_join with another joiner and budget, and md5pack hashed its selection inline. bounded_join takes joiner, budget and suffix, selection_digest serves all three; emulator packs keep the '_' joiner, so no name changes. --- scripts/packs/emulatorpack.py | 10 +++++--- scripts/packs/md5pack.py | 7 ++---- scripts/packs/naming.py | 41 +++++++++++++++------------------ tests/test_audit_regressions.py | 5 +++- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/scripts/packs/emulatorpack.py b/scripts/packs/emulatorpack.py index 94f3b7261..1a9f130e4 100644 --- a/scripts/packs/emulatorpack.py +++ b/scripts/packs/emulatorpack.py @@ -39,7 +39,7 @@ from packs.archive import ( from packs.datatrees import MISSING_DATA_DIRS, pack_data_tree from packs.extras import emulator_region_drops, emulator_required_spares, expand_agnostic_entries from packs.naming import ( - bounded_join, bounded_pack_name, file_name_part, pack_narrowings, system_display_name, + 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 @@ -492,8 +492,12 @@ def generate_emulator_pack( "full", regions, None, False, required_only, standalone ) ) - zip_name = options.get("pack_name") or bounded_pack_name( - display_names, [n for n, _p in selected], f"{narrow_tags}{PACK_SUFFIX}" + zip_name = options.get("pack_name") or bounded_join( + display_names, + [n for n, _p in selected], + joiner="_", + budget=MAX_PACK_NAME_BYTES, + suffix=f"{narrow_tags}{PACK_SUFFIX}", ) zip_path = os.path.join(output_dir, zip_name) if record is None: diff --git a/scripts/packs/md5pack.py b/scripts/packs/md5pack.py index cf159093e..560735adc 100644 --- a/scripts/packs/md5pack.py +++ b/scripts/packs/md5pack.py @@ -2,7 +2,6 @@ from __future__ import annotations -import hashlib import os import sys from dataclasses import dataclass @@ -14,7 +13,7 @@ from model.platforms import load_platform_config from model.profiles import ProfileSelectionError, 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 +from packs.naming import file_name_part, pack_narrowings, selection_digest from packs.paths import resolve_destination from resolve.packfiles import resolve_file, sha1_for_hash @@ -140,9 +139,7 @@ def _md5_pack_name( ) # The hash list is what chooses the content: two lists under one name # overwrote each other in the same output directory. - selection = hashlib.sha1( - "\n".join(sorted(f"{kind}:{value.lower()}" for kind, value in hashes)).encode() - ).hexdigest()[:8] + selection = selection_digest([f"{kind}:{value.lower()}" for kind, value in hashes]) return f"{file_name_part(context_name, '_')}{mode_part}_Custom_{selection}{custom_tags}{PACK_SUFFIX}" diff --git a/scripts/packs/naming.py b/scripts/packs/naming.py index bc4633c0f..a897be272 100644 --- a/scripts/packs/naming.py +++ b/scripts/packs/naming.py @@ -109,20 +109,31 @@ def system_tag(system_filter: list[str] | None) -> str: MAX_SYSTEM_TAG_BYTES = 120 -def bounded_join(parts: list[str], keys: list[str]) -> str: +def selection_digest(keys: list[str]) -> str: + """Eight hex digits that tell two selections apart in a file name.""" + return hashlib.sha1("\n".join(sorted(keys)).encode("utf-8")).hexdigest()[:8] + + +def bounded_join( + parts: list[str], + keys: list[str], + *, + joiner: str = "+", + budget: int = MAX_SYSTEM_TAG_BYTES, + suffix: str = "", +) -> str: """Parts joined while they fit, else the first, a count and a digest. Thirty systems joined made a 261-byte file name and the ZIP could not - be opened. The digest keeps two different selections apart. The joiner - is one no part holds: with "_", sega-dreamcast + arcade and + be opened. The digest keeps two different selections apart. The default + joiner is one no system name holds: with "_", sega-dreamcast + arcade and sega-dreamcast-arcade both named Dreamcast_Arcade, and the second build - replaced the first. + replaced the first. The suffix counts in the budget. """ - joined = "+".join(parts) - if len(joined.encode("utf-8")) <= MAX_SYSTEM_TAG_BYTES: + joined = joiner.join(parts) + suffix + if len(joined.encode("utf-8")) <= budget: return joined - digest = hashlib.sha1("\n".join(sorted(keys)).encode("utf-8")).hexdigest()[:8] - return f"{parts[0]}_and_{len(parts) - 1}_more_{digest}" + return f"{parts[0]}_and_{len(parts) - 1}_more_{selection_digest(keys)}{suffix}" _UNSAFE_IN_NAME = re.compile(r'[/\\:*?"<>|]') @@ -195,20 +206,6 @@ def platform_pack_name( MAX_PACK_NAME_BYTES = 200 -def bounded_pack_name(display_names: list[str], keys: list[str], suffix: str) -> str: - """The emulator pack's file name, its emulators joined while they fit. - - Past the limit the name keeps the first emulator, says how many more the - pack holds, and carries a digest of the selection so two different - selections never share a file. - """ - joined = "_".join(display_names) + suffix - if len(joined.encode("utf-8")) <= MAX_PACK_NAME_BYTES: - return joined - digest = hashlib.sha1("\n".join(sorted(keys)).encode("utf-8")).hexdigest()[:8] - return f"{display_names[0]}_and_{len(display_names) - 1}_more_{digest}{suffix}" - - def expected_pack_names( platforms: list[str], platforms_dir: str, diff --git a/tests/test_audit_regressions.py b/tests/test_audit_regressions.py index cc7a6015d..d69d5fd61 100644 --- a/tests/test_audit_regressions.py +++ b/tests/test_audit_regressions.py @@ -3406,7 +3406,10 @@ class APackNameFitsAFileName(unittest.TestCase): before writing anything, while verify --system snes reported.""" def test_a_long_selection_is_bounded_and_told_apart(self): - from scripts.packs.naming import MAX_PACK_NAME_BYTES, bounded_pack_name + from scripts.packs.naming import MAX_PACK_NAME_BYTES, bounded_join + + def bounded_pack_name(display: list[str], keys: list[str], suffix: str) -> str: + return bounded_join(display, keys, joiner="_", budget=MAX_PACK_NAME_BYTES, suffix=suffix) names = [f"Emulator_Number_{i}" for i in range(23)] long_name = bounded_pack_name(names, names, "_BIOS_Pack.zip")