diff --git a/scripts/download.py b/scripts/download.py index 91ceeebca..5e6271c3a 100644 --- a/scripts/download.py +++ b/scripts/download.py @@ -32,11 +32,12 @@ from pathlib import Path sys.path.insert(0, os.path.dirname(__file__)) from core.hashing import compute_hashes, parse_sha256sums +from core.paths import REPO_ROOT from core.ziptools import safe_extract_zip +from model.releaserecord import PACK_SUFFIX, RECORD, load_record, match_key, pack_label, recorded_pack DEFAULT_API = "https://api.github.com" REPO = "Abdess/retrobios" -PACK_SUFFIX = "_BIOS_Pack.zip" CHECKSUMS_ASSET = "SHA256SUMS.txt" STAGING_PREFIX = ".retrobios-download-" MAX_CHECKSUMS_BYTES = 1 << 20 @@ -143,7 +144,7 @@ def group_packs(release: dict) -> list[Pack]: packs.append( Pack( name=base, - platform=base[: -len(PACK_SUFFIX)].replace("_", " "), + platform=pack_label(base), parts=parts, size=sum(part.get("size", 0) for part in parts), joined=base in joined, @@ -158,20 +159,24 @@ def list_platforms(release: dict) -> list[str]: return sorted(pack.platform for pack in group_packs(release)) -def _match_key(value: str) -> str: - """Letters and digits only: the platform is `misterfpga`, the asset MiSTer_FPGA.""" - return re.sub(r"[^a-z0-9]", "", value.lower()) - - def find_pack(release: dict, platform: str) -> Pack | None: - """Find the pack for a platform name.""" - needle = _match_key(platform) + """Find the pack for a platform name. + + release.json names the platforms each pack serves, and is read when it + records this release. Otherwise the platform is matched against the pack + names, the shortest first: RetroArch_v1.22.2 is the full pack, and + RetroArch_v1.22.2_AndroidArm64V8A one target's. + """ + packs = group_packs(release) + record = load_record(REPO_ROOT / RECORD) + if record.get("tag") and record.get("tag") == release.get("tag_name"): + name = recorded_pack(platform, record) + return next((pack for pack in packs if pack.name == name), None) + needle = match_key(platform) if not needle: return None - for pack in group_packs(release): - if needle in _match_key(pack.name): - return pack - return None + matches = [pack for pack in packs if needle in match_key(pack.name)] + return min(matches, key=lambda pack: (len(match_key(pack.name)), pack.name), default=None) def fetch_checksums(release: dict) -> dict[str, str]: diff --git a/scripts/model/releaserecord.py b/scripts/model/releaserecord.py index 04e71e4db..4d5bc3199 100644 --- a/scripts/model/releaserecord.py +++ b/scripts/model/releaserecord.py @@ -3,6 +3,7 @@ from __future__ import annotations import json +import re from pathlib import Path from core.paths import PLATFORMS_DIR @@ -17,6 +18,7 @@ SCHEMA_VERSION = 1 # current pack's files, or the command when no release publishes one. The # address outlives every release, so a link written elsewhere keeps working. GET_DIR = "get" +PACK_SUFFIX = "_BIOS_Pack.zip" def load_record(path: str | Path = RECORD) -> dict: @@ -28,6 +30,33 @@ def load_record(path: str | Path = RECORD) -> dict: return {} +def match_key(value: str) -> str: + """Letters and digits only: the platform is `misterfpga`, the pack MiSTer_FPGA.""" + return re.sub(r"[^a-z0-9]", "", value.lower()) + + +def pack_label(name: str) -> str: + """RetroArch_Lakka_v1.22.2_BIOS_Pack.zip reads as RetroArch Lakka v1.22.2.""" + return name.split(PACK_SUFFIX.removesuffix(".zip"), 1)[0].replace("_", " ") + + +def pack_owner(name: str, pack: dict) -> str: + """The platform a pack is named after: RetroArch_Lakka is RetroArch's.""" + key = match_key(name) + return sorted( + pack.get("platforms", ()), key=lambda p: key.find(match_key(p)) % (len(key) + 1) + )[0] + + +def recorded_pack(platform: str, record: dict) -> str | None: + """The pack the record says serves a platform, its id spelled any way.""" + needle = match_key(platform) + for name, pack in sorted(record.get("packs", {}).items()): + if needle in {match_key(p) for p in pack.get("platforms", ())}: + return name + return None + + def get_page(platform: str, site: str = "") -> str: """The permanent download address of a platform, under *site*.""" return f"{site}{GET_DIR}/{platform}/" diff --git a/scripts/release/notes.py b/scripts/release/notes.py index dd7c8750b..7a9120da7 100644 --- a/scripts/release/notes.py +++ b/scripts/release/notes.py @@ -3,13 +3,13 @@ from __future__ import annotations import json -import re import subprocess from collections.abc import Callable from core.sizes import size_text from model.guides import install_command from model.platforms import load_platform_config +from model.releaserecord import pack_label, pack_owner from model.registry import ( list_registered_platforms, list_release_pack_platforms, @@ -26,25 +26,12 @@ SITE = "https://abdess.github.io/retrobios/" HANDHELD_PAGE = "wiki/installer/#on-a-handheld-the-retrobios-port" -def _display(pack: str) -> str: - """RetroArch_Lakka_v1.22.2_BIOS_Pack.zip reads as RetroArch Lakka v1.22.2.""" - return pack.split("_BIOS_Pack", 1)[0].replace("_", " ") - - -def _owner(name: str, pack: dict) -> str: - """The platform a pack is named after: RetroArch_Lakka is RetroArch's.""" - key = _match_key(name) - return sorted( - pack.get("platforms", ()), key=lambda p: key.find(_match_key(p)) % (len(key) + 1) - )[0] - - def _extract_folders(record: dict, platforms_dir: str) -> dict[str, str]: """Pack name to the folder the registry says it extracts into.""" registry = load_platform_registry(platforms_dir) folders: dict[str, str] = {} for name, pack in sorted(record.get("packs", {}).items()): - folder = registry.get(_owner(name, pack), {}).get("extract_to") + folder = registry.get(pack_owner(name, pack), {}).get("extract_to") if folder: folders[name] = folder return folders @@ -76,11 +63,6 @@ def no_pack_paragraph(platforms_dir: str) -> list[str]: ] -def _match_key(value: str) -> str: - """Letters and digits only: the platform is `misterfpga`, the pack MiSTer_FPGA.""" - return re.sub(r"[^a-z0-9]", "", value.lower()) - - def _link(pack: dict, tag: str) -> str: """The pack's files on this release, one link per part.""" base = f"https://github.com/{REPO}/releases/download/{tag}" @@ -239,7 +221,7 @@ def render(record: dict, platforms_dir: str, whats_new: str, for name, pack in sorted(packs.items(), key=lambda item: -item[1]["extracted_size"]): folder = folders.get(name) lines.append( - f"| {_display(name)} | {_link(pack, tag)} |" + f"| {pack_label(name)} | {_link(pack, tag)} |" f" {size_text(pack['extracted_size'])} | {pack['files']:,} |" f" {f'`{folder}`' if folder else '-'} |" ) diff --git a/tests/test_download.py b/tests/test_download.py index a830664d6..7db14c3b9 100644 --- a/tests/test_download.py +++ b/tests/test_download.py @@ -29,6 +29,7 @@ import time import unittest import zipfile from pathlib import Path +from unittest import mock from packs import parts as packs_parts @@ -216,6 +217,26 @@ class TestPackGrouping(unittest.TestCase): pack = download.find_pack(_release(RELEASE_ASSETS), "retroarch") self.assertEqual(pack.name, "RetroArch_Lakka_v1.22.2_BIOS_Pack.zip") + def test_find_pack_prefers_the_full_pack_to_a_target_pack(self): + names = ["RetroArch_v1.22.2_AndroidArm64V8A_BIOS_Pack.zip", "RetroArch_v1.22.2_BIOS_Pack.zip"] + pack = download.find_pack(_release(names), "retroarch") + self.assertEqual(pack.name, "RetroArch_v1.22.2_BIOS_Pack.zip") + + def test_find_pack_reads_the_record_of_this_release(self): + record = {"tag": "v1", "packs": { + "RetroArch_Lakka_v1.22.2_BIOS_Pack.zip": {"platforms": ["lakka"]}, + "RetroArch_v1.22.2_BIOS_Pack.zip": {"platforms": ["retroarch"]}, + }} + release = {**_release(["RetroArch_Lakka_v1.22.2_BIOS_Pack.zip", + "RetroArch_v1.22.2_BIOS_Pack.zip"]), "tag_name": "v1"} + with mock.patch.object(download, "load_record", return_value=record): + self.assertEqual(download.find_pack(release, "lakka").name, + "RetroArch_Lakka_v1.22.2_BIOS_Pack.zip") + self.assertIsNone(download.find_pack(release, "batocera")) + with mock.patch.object(download, "load_record", return_value={**record, "tag": "v0"}): + self.assertEqual(download.find_pack(release, "lakka").name, + "RetroArch_Lakka_v1.22.2_BIOS_Pack.zip") + def test_find_pack_returns_none_for_an_unknown_platform(self): self.assertIsNone(download.find_pack(_release(RELEASE_ASSETS), "nintendo"))