Files
pokemon/scripts/update_swarm/census.py
T
ginnoir cb294f72a0 fix: address whole-branch review findings for update-swarm
- versions: require entire normalized string to be numeric so dates
  and hotfix suffixes compare as incomparable instead of eq/gt
- hosts: exact host or dot-suffix match rejects lookalikes like
  notmediafire.com and evilmega.nz; GitHub stays exact-set
- census: lazy extractor loop so a later OSError no longer discards an
  earlier sidecar changelog hit
- jobs: watchlist parses only table rows whose vault-note cell is a
  bare wikilink (skips parenthetical related/complete notes); wanted
  also matches bold names in Wanted.md tables with Pokemon prefix
  stripped and casefolded
- cli: validate-scouts and judge raise SystemExit when jobs.json has
  jobs but scouts/ has zero valid files
- fetch: download forwards timeout=60 to urlopen and writes to a
  .part file then os.replace, removing the partial on any failure
2026-08-14 15:11:16 -05:00

106 lines
3.7 KiB
Python

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)
if not os.path.isdir(parent):
return None
names = os.listdir(parent)
for name in names:
if name.lower() in CHANGELOG_NAMES:
with open(os.path.join(parent, name), encoding="utf-8", errors="ignore") as fh:
found = _from_text(fh.read())
if found:
return found
for name in names:
if name.lower() == "game.ini":
with open(os.path.join(parent, name), encoding="utf-8", errors="ignore") as fh:
m = INI_VER.search(fh.read())
if m:
return m.group(1).strip()
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)
extractors = (
(_sidecar_version, "sidecar"),
(_zip_version, "zip"),
(_filename_version, "filename"),
(_rom_version, "rom-bytes"),
)
for fn, evidence in extractors:
try:
found = fn(file_path)
except OSError:
found = None
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