feat: librarian helpers for superseded names, note patch, scoped RomM scan

This commit is contained in:
ginnoir committed 2026-08-14 14:57:17 -05:00
1 parent dd62312f87
commit 5445f82d91
2 files changed
+85

No files matched your search

+42
View File
@@ -0,0 +1,42 @@
from __future__ import annotations
import os, sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import enrich_vault as E
import enrich_web as W
ROMM_PLATFORM_IDS = {"GBA": 72, "PC": 200, "windows": 200}
def require_platform_id(platform: str) -> int:
if platform not in ROMM_PLATFORM_IDS:
raise KeyError(f"no RomM platform_id for {platform!r}; add it to ROMM_PLATFORM_IDS")
return ROMM_PLATFORM_IDS[platform]
def superseded_name(live_basename: str, old_version: str) -> str:
root, ext = os.path.splitext(live_basename)
return f"{root} ({old_version}){ext}"
def romm_scan_argv(platform_ids: list[int]) -> list[str]:
if not platform_ids:
raise ValueError("platform_ids is required; refusing a full-library walk")
ids = "[" + ", ".join(str(i) for i in platform_ids) + "]"
py = (
"import asyncio; from endpoints.sockets.scan import scan_platforms; "
"from handler.scan_handler import ScanType; "
f"asyncio.run(scan_platforms(platform_ids={ids}, metadata_sources=[], scan_type=ScanType.QUICK))"
)
return ["docker", "exec", "-w", "/backend", "romm", "python3", "-c", py]
def patch_note(path: str, version: str, release_date: str | None,
source_url: str | None, history_md: str | None, status: str | None) -> str:
fm_text, body, _ = E.read_note(path)
fm = E.parse_fm(fm_text)
fm["version"] = version
if release_date:
fm["release_date"] = release_date
if source_url:
fm["source"] = source_url
if status:
fm["status"] = status
if history_md:
body = W.set_section(body, "Version history", history_md.strip())
return f"---\n{E.emit_fm(fm)}\n---\n{body}"
+43
View File
@@ -0,0 +1,43 @@
import os, sys, tempfile, unittest
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
sys.path.insert(0, os.path.join(ROOT, "scripts"))
from update_swarm.librarian import patch_note, require_platform_id, romm_scan_argv, superseded_name
class TestLibrarian(unittest.TestCase):
def test_superseded_name(self):
self.assertEqual(
superseded_name("Pokemon - Z [Hack].zip", "v2.16 ES"),
"Pokemon - Z [Hack] (v2.16 ES).zip",
)
def test_romm_requires_platform_ids(self):
argv = romm_scan_argv([72, 200])
joined = " ".join(argv)
self.assertIn("platform_ids=[72,200]", joined.replace(" ", ""))
with self.assertRaises(ValueError):
romm_scan_argv([])
self.assertEqual(require_platform_id("GBA"), 72)
self.assertEqual(require_platform_id("PC"), 200)
with self.assertRaises(KeyError):
require_platform_id("GameCube")
def test_patch_note_version_and_history(self):
src = (
"---\n"
'title: "Pokémon Z"\n'
'version: "2.17"\n'
"status: Complete\n"
"---\n\n"
"## Summary\n\nKeep me.\n"
)
with tempfile.TemporaryDirectory() as td:
path = os.path.join(td, "Z.md")
open(path, "w", encoding="utf-8").write(src)
text = patch_note(path, "v2.13 (EN)", "2026-03-30", "https://example.com/z", "- EN 2.13\n", None)
self.assertIn('version: "v2.13 (EN)"', text)
self.assertIn("## Version history", text)
self.assertIn("## Summary\n\nKeep me.", text)
self.assertNotIn("**bold**", text.split("## Summary")[1].split("##")[0])
if __name__ == "__main__":
unittest.main()