diff --git a/scripts/core/upstream.py b/scripts/core/upstream.py index cae488581..cb704fa92 100644 --- a/scripts/core/upstream.py +++ b/scripts/core/upstream.py @@ -419,6 +419,19 @@ def http_text(url: str) -> str | None: return None if body is None else body.decode("utf-8", errors="replace") +def remote_etag(url: str, timeout: float = 30) -> str | None: + """ETag, else Last-Modified, of a HEAD; "" when neither is sent, None + when the server does not answer.""" + req = urllib.request.Request( + url, method="HEAD", headers=forge_headers(url, user_agent="retrobios/1.0") + ) + try: + with urllib.request.urlopen(req, timeout=timeout) as resp: + return resp.headers.get("ETag") or resp.headers.get("Last-Modified") or "" + except (urllib.error.URLError, OSError): + return None + + def http_json(url: str) -> object | None: """Parsed JSON body of a GET, or None on 404. Replaced in tests.""" body = fetch_body(url, accept_json=True) diff --git a/scripts/packs/datadirs.py b/scripts/packs/datadirs.py index 45055f337..7bd55ae9f 100644 --- a/scripts/packs/datadirs.py +++ b/scripts/packs/datadirs.py @@ -17,6 +17,7 @@ from pathlib import Path from core.artifacts import file_lock as _file_lock, write_text_atomic from core.github import github_token +from core.upstream import remote_etag as _get_remote_etag from model.registry import VERSIONS_FILE, load_versions @@ -434,18 +435,6 @@ def _record_archive_size(key: str, entry: dict, versions_path: str) -> None: log.info("[%s] archive size recorded: %d bytes", key, size) -def _get_remote_etag(source_url: str) -> str | None: - """HEAD request to get ETag or Last-Modified for freshness check.""" - try: - req = urllib.request.Request( - source_url, method="HEAD", headers={"User-Agent": USER_AGENT} - ) - with urllib.request.urlopen(req, timeout=REQUEST_TIMEOUT) as resp: - return resp.headers.get("ETag") or resp.headers.get("Last-Modified") or "" - except (urllib.error.URLError, OSError): - return None - - def _short_tag(tag: str | None) -> str: """A commit sha or ETag cut for a log line, quotes dropped first: a quoted ETag cut at 12 characters kept its opening quote only.""" diff --git a/scripts/scraper/buildbot_system.py b/scripts/scraper/buildbot_system.py index d4f42bfe9..954fa3994 100644 --- a/scripts/scraper/buildbot_system.py +++ b/scripts/scraper/buildbot_system.py @@ -7,8 +7,8 @@ import subprocess import sys import urllib.error import urllib.parse -import urllib.request +from core.upstream import remote_etag as get_remote_etag from model.registry import data_dir_registry_at, load_versions from .base_scraper import fetch_text @@ -17,8 +17,6 @@ log = logging.getLogger(__name__) BUILDBOT_SYSTEM_URL = "https://buildbot.libretro.com/assets/system/" INDEX_URL = BUILDBOT_SYSTEM_URL + ".index" -USER_AGENT = "retrobios/1.0" -REQUEST_TIMEOUT = 15 DEFAULT_REGISTRY = "platforms/_data_dirs.yml" @@ -45,18 +43,6 @@ def load_tracked_entries( return entries -def get_remote_etag(url: str) -> str | None: - """HEAD request to get ETag.""" - try: - req = urllib.request.Request( - url, method="HEAD", headers={"User-Agent": USER_AGENT} - ) - with urllib.request.urlopen(req, timeout=REQUEST_TIMEOUT) as resp: - return resp.headers.get("ETag") or resp.headers.get("Last-Modified") or "" - except (urllib.error.URLError, OSError): - return None - - def check(registry_path: str = DEFAULT_REGISTRY) -> dict: """Run the buildbot check. Returns structured report.""" try: