diff --git a/scripts/update_swarm/census.py b/scripts/update_swarm/census.py new file mode 100644 index 0000000..aaea6f4 --- /dev/null +++ b/scripts/update_swarm/census.py @@ -0,0 +1,98 @@ +from __future__ import annotations +import hashlib, os, re, zipfile + +CHANGELOG_NAMES = {"changelog.txt", "changelog.md", "changes.txt", "readme.txt", "readme.md"} +CURRENT_RE = re.compile(r"(?im)^\s*(?:current\s+version|version)\s*[:\-]\s*(.+?)\s*$") +INI_VER = re.compile(r"(?im)^\s*Version\s*=\s*(.+?)\s*$") +FILE_VER = re.compile(r"\[(?:v)?(\d+(?:\.\d+)*)\]|\((?:v)?(\d+(?:\.\d+)*)\)|v(\d+(?:\.\d+)*)", re.I) +ROM_VER = re.compile(rb"Version\s+[vV]?(\d+(?:\.\d+)*)") + +def _sha_size(path: str) -> tuple[str, int]: + h = hashlib.sha256() + with open(path, "rb") as f: + for chunk in iter(lambda: f.read(1024 * 1024), b""): + h.update(chunk) + return h.hexdigest(), os.path.getsize(path) + +def _from_text(text: str) -> str | None: + m = CURRENT_RE.search(text or "") + return m.group(1).strip() if m else None + +def _sidecar_version(file_path: str) -> str | None: + parent = os.path.dirname(file_path) + for name in os.listdir(parent) if os.path.isdir(parent) else []: + low = name.lower() + if low in CHANGELOG_NAMES or low == "game.ini": + with open(os.path.join(parent, name), encoding="utf-8", errors="ignore") as fh: + text = fh.read() + if low == "game.ini": + m = INI_VER.search(text) + if m: + return m.group(1).strip() + else: + found = _from_text(text) + if found: + return found + return None + +def _zip_version(file_path: str) -> str | None: + try: + with zipfile.ZipFile(file_path) as z: + names = z.namelist() + for n in names: + base = os.path.basename(n).lower() + if base in CHANGELOG_NAMES: + found = _from_text(z.read(n).decode("utf-8", "ignore")) + if found: + return found + for n in names: + if os.path.basename(n).lower() == "game.ini": + m = INI_VER.search(z.read(n).decode("utf-8", "ignore")) + if m: + return m.group(1).strip() + except zipfile.BadZipFile: + return None + return None + +def _filename_version(file_path: str) -> str | None: + m = FILE_VER.search(os.path.basename(file_path)) + if not m: + return None + return next(g for g in m.groups() if g) + +def _rom_version(file_path: str) -> str | None: + with open(file_path, "rb") as f: + blob = f.read(512 * 1024) + m = ROM_VER.search(blob) + return m.group(1).decode() if m else None + +def census_job(job: dict, file_path: str | None) -> dict: + rec = { + "stem": job["stem"], + "installed_version": None, + "source": "unknown", + "evidence": "", + "sha256": None, + "size": None, + "language": None, + } + if file_path and os.path.isfile(file_path): + rec["sha256"], rec["size"] = _sha_size(file_path) + for found, evidence in ( + (_sidecar_version(file_path), "sidecar"), + (_zip_version(file_path), "zip"), + (_filename_version(file_path), "filename"), + (_rom_version(file_path), "rom-bytes"), + ): + if found: + rec["installed_version"] = found + rec["source"] = "file" + rec["evidence"] = evidence + return rec + if job.get("note_version"): + rec["installed_version"] = job["note_version"] + rec["source"] = "note" + rec["evidence"] = "vault" + return rec + rec["evidence"] = "none" + return rec diff --git a/tests/update_swarm/fixtures/library/changelog-game/changelog.txt b/tests/update_swarm/fixtures/library/changelog-game/changelog.txt new file mode 100644 index 0000000..7cdf4d0 --- /dev/null +++ b/tests/update_swarm/fixtures/library/changelog-game/changelog.txt @@ -0,0 +1 @@ +Current Version: 3.3.1 diff --git a/tests/update_swarm/fixtures/library/changelog-game/dummy.bin b/tests/update_swarm/fixtures/library/changelog-game/dummy.bin new file mode 100644 index 0000000..e69de29 diff --git a/tests/update_swarm/fixtures/library/ini-game/Game.ini b/tests/update_swarm/fixtures/library/ini-game/Game.ini new file mode 100644 index 0000000..86b154d --- /dev/null +++ b/tests/update_swarm/fixtures/library/ini-game/Game.ini @@ -0,0 +1,3 @@ +[Game] +Title=Ini Game +Version=1.74.1 diff --git a/tests/update_swarm/fixtures/library/ini-game/dummy.bin b/tests/update_swarm/fixtures/library/ini-game/dummy.bin new file mode 100644 index 0000000..e69de29 diff --git a/tests/update_swarm/fixtures/library/named/Pokemon - Named [Hack][3.3.1].zip b/tests/update_swarm/fixtures/library/named/Pokemon - Named [Hack][3.3.1].zip new file mode 100644 index 0000000..15cb0ec Binary files /dev/null and b/tests/update_swarm/fixtures/library/named/Pokemon - Named [Hack][3.3.1].zip differ diff --git a/tests/update_swarm/test_census.py b/tests/update_swarm/test_census.py new file mode 100644 index 0000000..e87b8b8 --- /dev/null +++ b/tests/update_swarm/test_census.py @@ -0,0 +1,43 @@ +import hashlib, os, sys, tempfile, unittest, zipfile +ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) +sys.path.insert(0, os.path.join(ROOT, "scripts")) +from update_swarm.census import census_job + +FIX = os.path.join(os.path.dirname(__file__), "fixtures", "library") + +def job(stem, note_version=None): + return {"stem": stem, "library_path": None, "note_version": note_version} + +class TestCensus(unittest.TestCase): + def test_changelog_current_version_line(self): + rec = census_job(job("C"), os.path.join(FIX, "changelog-game", "dummy.bin")) + self.assertEqual(rec["installed_version"], "3.3.1") + self.assertEqual(rec["source"], "file") + + def test_game_ini_version(self): + rec = census_job(job("I"), os.path.join(FIX, "ini-game", "dummy.bin")) + self.assertEqual(rec["installed_version"], "1.74.1") + self.assertEqual(rec["source"], "file") + + def test_filename_token(self): + rec = census_job(job("N"), os.path.join(FIX, "named", "Pokemon - Named [Hack][3.3.1].zip")) + self.assertEqual(rec["installed_version"], "3.3.1") + + def test_zip_inner_changelog(self): + with tempfile.TemporaryDirectory() as td: + zpath = os.path.join(td, "game.zip") + with zipfile.ZipFile(zpath, "w") as z: + z.writestr("README.txt", "Current Version: 2.13\n") + rec = census_job(job("Z"), zpath) + self.assertEqual(rec["installed_version"], "2.13") + + def test_note_fallback_then_unknown(self): + rec = census_job(job("X", note_version="9.9"), None) + self.assertEqual(rec["source"], "note") + self.assertEqual(rec["installed_version"], "9.9") + rec2 = census_job(job("Y"), None) + self.assertEqual(rec2["source"], "unknown") + self.assertIsNone(rec2["installed_version"]) + +if __name__ == "__main__": + unittest.main()