From 4185ad79cad4737d178b5a7e494cf9ab2155d287 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Mon, 23 Nov 2026 23:44:46 +0100 Subject: [PATCH] fix: download.sh picks a pack like download.py --- scripts/download.sh | 82 ++++++++++++++++++++++++++++++++------ tests/test_download.py | 89 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+), 13 deletions(-) diff --git a/scripts/download.sh b/scripts/download.sh index bc2c79afd..2a77b7165 100755 --- a/scripts/download.sh +++ b/scripts/download.sh @@ -75,6 +75,73 @@ normalize() { printf '%s' "$1" | tr '[:upper:]' '[:lower:]' | tr -cd 'a-z0-9' } +# release.json beside a checkout, the file download.py reads. A script piped +# to bash has no file of its own, so no record. +record_file() { + local self="${BASH_SOURCE[0]:-}" + [ -f "$self" ] || return 0 + local record + record="$(cd "$(dirname "$self")/.." && pwd)/release.json" + if [ -f "$record" ]; then + printf '%s\n' "$record" + fi +} + +release_tag() { + printf '%s' "$1" | tr ',' '\n' | + sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | sed -n 1p +} + +record_tag() { + sed -n 's/^ "tag": "\([^"]*\)",\{0,1\}$/\1/p' "$1" | sed -n 1p +} + +# The pack the record says serves a platform, first by name. Read from the +# layout release_record.py writes: two-space indent, one string per line. +recorded_pack() { + awk -v needle="$2" ' + function key(s) { s = tolower(s); gsub(/[^a-z0-9]/, "", s); return s } + /^ "packs": \{/ { packs = 1; next } + packs && /^ }/ { packs = 0; next } + packs && /^ "[^"]*": \{/ { name = $0; sub(/^ "/, "", name); sub(/".*/, "", name); next } + packs && /^ "platforms": \[$/ { listed = 1; next } + listed && /^ \]/ { listed = 0; next } + listed { + value = $0 + gsub(/^[[:space:]]*"|",?[[:space:]]*$/, "", value) + if (key(value) == needle) print name + } + ' "$1" | LC_ALL=C sort | sed -n 1p +} + +# The pack that serves a platform, by the rule of release/packgroups.py. The +# record names it when it records this release. Otherwise the shortest pack +# name that spells the platform wins, ties broken by name: RetroArch_v1.22.2 +# is the full pack, RetroArch_v1.22.2_AndroidArm64V8A one target's. +choose_pack() { + local release_json="$1" normalized="$2" + local record tag + record=$(record_file) + tag=$(release_tag "$release_json") + if [ -n "$record" ] && [ -n "$tag" ] && [ "$(record_tag "$record")" = "$tag" ]; then + local name + name=$(recorded_pack "$record" "$normalized") + if [ -n "$name" ]; then + pack_names "$release_json" | grep -Fx -- "$name" || true + fi + return 0 + fi + [ -n "$normalized" ] || return 0 + local candidate key + pack_names "$release_json" | while read -r candidate; do + [ -n "$candidate" ] || continue + key=$(normalize "$candidate") + case "$key" in + *"$normalized"*) printf '%s %s\n' "${#key}" "$candidate" ;; + esac + done | LC_ALL=C sort -k1,1n -k2,2 | sed -n 1p | cut -d' ' -f2 +} + # The anonymous quota is 60 calls an hour per address. The token goes to # GitHub's API only: RETROBIOS_API can name any host. The header is read # from stdin so the token stays out of the process list. @@ -106,19 +173,8 @@ download_pack() { local release_json release_json=$(fetch_release) - local archive="" - local candidate - while read -r candidate; do - [ -n "$candidate" ] || continue - case "$(normalize "$candidate")" in - *"$normalized"*) - archive="$candidate" - break - ;; - esac - done < None: + """A second whole pack, listed and summed like the first.""" + archive = self.root / "assets" / pack_name + with zipfile.ZipFile(archive, "w", zipfile.ZIP_DEFLATED) as zf: + for name, data in payload.items(): + zf.writestr(name, data) + sums = self.root / "assets" / "SHA256SUMS.txt" + digest = hashlib.sha256(archive.read_bytes()).hexdigest() + sums.write_text(sums.read_text() + f"{digest} {pack_name}\n") + release = json.loads(self.index.read_text()) + release["assets"].insert(0, { + "name": pack_name, + "size": archive.stat().st_size, + "browser_download_url": f"{self.base}/assets/{pack_name}", + }) + self.index.write_text(json.dumps(release)) + + def tag(self, tag_name: str) -> None: + release = json.loads(self.index.read_text()) + release["tag_name"] = tag_name + self.index.write_text(json.dumps(release)) + def drop_last_asset(self) -> None: """A part the release lists but no longer serves: its URL gives 404.""" (self.root / "assets" / self.names[-1]).unlink() @@ -711,6 +733,73 @@ class TestWholePackShell(WholePackCase, TestDownloadShell): pass +@unittest.skipUnless( + shutil.which("curl") and shutil.which("unzip"), "curl and unzip required" +) +class TestPackChoice(DownloaderCase): + """Both downloaders pick a platform's pack by one rule: download.sh took + the first name in sort order that spelled the platform, the Android + target pack ahead of the full one.""" + + volumes = 1 + pack_name = "RetroArch_v1.22.2_BIOS_Pack.zip" + platform = "retroarch" + target_pack = "RetroArch_v1.22.2_AndroidArm64V8A_BIOS_Pack.zip" + target_payload = {"bios/android.bin": b"\x50" * 4096} + + def setUp(self): + super().setUp() + self.server.add_pack(self.target_pack, self.target_payload) + + def run_command(self, command: list[str]) -> subprocess.CompletedProcess: + return subprocess.run( + [*command, self.platform, str(self.dest)], env=self.env(), + capture_output=True, text=True, timeout=120, check=False, + ) + + def assert_full_pack(self, proc: subprocess.CompletedProcess) -> None: + self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr) + self.assert_extracted() + self.assertFalse((self.dest / "bios/android.bin").exists()) + + def test_the_shell_takes_the_full_pack(self): + self.assert_full_pack(self.run_command(["bash", str(SHELL)])) + + def test_python_takes_the_full_pack(self): + self.assert_full_pack( + self.run_command(["python3", str(REPO_ROOT / "scripts" / "download.py")]) + ) + + def test_the_shell_reads_the_record_of_this_release(self): + """The record names a pack whose name does not spell the platform.""" + checkout = scratch_dir(self) + (checkout / "scripts").mkdir() + shutil.copy(SHELL, checkout / "scripts" / "download.sh") + record = {"schema_version": 1, "tag": "v1", "packs": { + self.pack_name: {"files": 2, "assets": [self.pack_name], "platforms": ["lakka"]}, + self.target_pack: {"files": 1, "assets": [self.target_pack], "platforms": []}, + }} + (checkout / "release.json").write_text(json.dumps(record, indent=2) + "\n") + self.server.tag("v1") + shell = ["bash", str(checkout / "scripts" / "download.sh")] + release = latest.get_latest_release(self.server.base) + + self.platform = "lakka" + self.assertEqual(packgroups.find_pack(release, "lakka", record).name, self.pack_name) + self.assert_full_pack(self.run_command(shell)) + + self.platform = "retroarch" + self.assertIsNone(packgroups.find_pack(release, "retroarch", record)) + proc = self.run_command(shell) + self.assertEqual(proc.returncode, 1, proc.stdout + proc.stderr) + self.assertIn("Platform 'retroarch' not found", proc.stdout) + + self.server.tag("v0") + self.platform = "lakka" + self.assertIsNone(packgroups.find_pack(release | {"tag_name": "v0"}, "lakka", record)) + self.assertEqual(self.run_command(shell).returncode, 1) + + class ZipPartsCase(DownloaderCase): """A pack published as parts that are each an archive."""