utils/dep_freshness/registries/http.py
Krzysztof kuhy Rudnicki a4ea305164 dep_freshness: Gradle ecosystem -- version catalogs, wrapper, Maven registries
The gate had no way to read a Gradle build, which left the two Kotlin
forks (tachiyomisy, syncyomi-src) outside the fleet-wide rule.

- parsers/gradle.py reads gradle/libs.versions.toml ([libraries] and
  [plugins] in every shape: inline string, module + version.ref,
  group/name/version, rich {strictly|require|prefer} tables) into
  `maven:<group>:<artifact>` deps, pointing the fix at the [versions]
  line when a ref is used; BOM-managed entries without a version are
  skipped. gradle-wrapper.properties becomes `toolchain:gradle`.
- registries/maven.py asks Google Maven, Central and the plugin portal
  in that order, reading the full <versions> list -- AndroidX writes
  alphas into <latest>. Plugins resolve through their marker artifact.
- registries/toolchain.gradle_latest reads services.gradle.org.
- http.get_text shares the transport with get_json (XML bodies).
- Templates: the pre-commit `files:` and the CI cache key know the
  catalog. 100% branch on every new file; fixtures only.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Yc4TiPjhbQLfH35QBPfrbP
2026-09-12 19:10:42 +02:00

94 lines
3.0 KiB
Python

"""The only module that touches the network.
Two things matter here. First, a single 2s per-host reachability probe
short-circuits every later request, so an offline run fails in seconds instead
of timing out once per package across ~900 lookups. Second, failures are
returned, never raised: the gate must degrade to a cached answer rather than
crash a pre-commit hook the user is forbidden from bypassing.
"""
from __future__ import annotations
import json
import socket
from typing import Any
from urllib.error import HTTPError, URLError
from urllib.parse import urlsplit
from urllib.request import Request, urlopen
from dep_freshness._tables import HTTP_TIMEOUT, PROBE_TIMEOUT, USER_AGENT
class Offline(Exception):
"""The host is unreachable; callers should fall back to the cache."""
_reachable: dict[str, bool] = {}
_forced_offline = False
def force_offline(value: bool = True) -> None:
"""Make every request raise `Offline` without touching the network."""
global _forced_offline
_forced_offline = value
def reset_probes() -> None:
"""Forget cached reachability verdicts (tests, and `--refresh`)."""
_reachable.clear()
def host_reachable(url: str) -> bool:
"""One cheap TCP probe per host, memoised for the run."""
if _forced_offline:
return False
parts = urlsplit(url)
host = parts.hostname or ""
if host in _reachable:
return _reachable[host]
port = parts.port or (443 if parts.scheme == "https" else 80)
try:
with socket.create_connection((host, port), timeout=PROBE_TIMEOUT):
_reachable[host] = True
except OSError:
_reachable[host] = False
return _reachable[host]
def get_text(url: str, accept: str | None = None) -> str | None:
"""Fetch a body as text, or raise `Offline` / return None on 404.
Same contract as `get_json`; Maven metadata is XML, not JSON.
"""
if not host_reachable(url):
raise Offline(url)
headers = {"User-Agent": USER_AGENT}
if accept:
headers["Accept"] = accept
request = Request(url, headers=headers)
try:
with urlopen(request, timeout=HTTP_TIMEOUT) as response:
return response.read().decode("utf-8")
except HTTPError as exc:
if exc.code == 404:
return None
raise Offline(f"{url}: HTTP {exc.code}") from exc
except (URLError, TimeoutError, OSError, ValueError) as exc:
raise Offline(f"{url}: {exc}") from exc
def get_json(url: str, accept: str | None = None) -> Any:
"""Fetch and decode JSON, or raise `Offline` / return None on 404.
`Offline` means "ask the cache"; None means "the registry answered and
this package genuinely is not there", which is a real finding rather than
a degraded run and must not be papered over with a stale cache entry.
"""
body = get_text(url, accept)
if body is None:
return None
try:
return json.loads(body)
except ValueError as exc:
raise Offline(f"{url}: {exc}") from exc