|
| 1 | +"""Exact-release validation and selection for bundle catalog entries.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import re |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from packaging.version import InvalidVersion, Version |
| 9 | + |
| 10 | +from .._download_security import is_https_or_localhost_http |
| 11 | +from . import BundlerError |
| 12 | +from .catalogs import CatalogEntry |
| 13 | +from .versioning import is_semver |
| 14 | + |
| 15 | +_SHA256 = re.compile(r"^[0-9a-fA-F]{64}$") |
| 16 | +_CURRENT_ONLY = frozenset( |
| 17 | + { |
| 18 | + "version", |
| 19 | + "download_url", |
| 20 | + "sha256", |
| 21 | + "requires", |
| 22 | + "provides", |
| 23 | + "verified", |
| 24 | + "releases", |
| 25 | + } |
| 26 | +) |
| 27 | +_RESERVED = frozenset({"id", "version", "releases"}) |
| 28 | + |
| 29 | + |
| 30 | +def _raw(entry: CatalogEntry) -> dict[str, Any]: |
| 31 | + return entry.raw if entry.raw is not None else {} |
| 32 | + |
| 33 | + |
| 34 | +def _release_error(entry: CatalogEntry, version: str, error: BundlerError) -> BundlerError: |
| 35 | + return BundlerError(f"Bundle '{entry.id}' release '{version}' has {error}") |
| 36 | + |
| 37 | + |
| 38 | +def _validated_releases(entry: CatalogEntry) -> dict[str, dict[str, Any]]: |
| 39 | + """Return checked historical releases or reject an ambiguous entry.""" |
| 40 | + raw = _raw(entry) |
| 41 | + if "releases" not in raw: |
| 42 | + return {} |
| 43 | + releases = raw["releases"] |
| 44 | + if not isinstance(releases, dict): |
| 45 | + raise BundlerError(f"Bundle '{entry.id}' has an invalid releases mapping.") |
| 46 | + |
| 47 | + current = raw.get("version") |
| 48 | + if not isinstance(current, str) or not current.strip(): |
| 49 | + raise BundlerError(f"Bundle '{entry.id}' has releases but no current version.") |
| 50 | + if not is_semver(current): |
| 51 | + raise BundlerError( |
| 52 | + f"Bundle '{entry.id}' has an invalid current version '{current}'." |
| 53 | + ) |
| 54 | + try: |
| 55 | + seen = {Version(current)} |
| 56 | + except InvalidVersion: |
| 57 | + raise BundlerError( |
| 58 | + f"Bundle '{entry.id}' has an invalid current version '{current}'." |
| 59 | + ) from None |
| 60 | + |
| 61 | + for advertised, record in releases.items(): |
| 62 | + if not isinstance(advertised, str) or not advertised.strip(): |
| 63 | + raise BundlerError( |
| 64 | + f"Bundle '{entry.id}' has an invalid release version key." |
| 65 | + ) |
| 66 | + if not is_semver(advertised): |
| 67 | + raise BundlerError( |
| 68 | + f"Bundle '{entry.id}' has invalid release version '{advertised}'." |
| 69 | + ) |
| 70 | + try: |
| 71 | + normalized = Version(advertised) |
| 72 | + except InvalidVersion: |
| 73 | + raise BundlerError( |
| 74 | + f"Bundle '{entry.id}' has invalid release version '{advertised}'." |
| 75 | + ) from None |
| 76 | + if normalized in seen: |
| 77 | + raise BundlerError( |
| 78 | + f"Bundle '{entry.id}' repeats release version '{advertised}'." |
| 79 | + ) |
| 80 | + seen.add(normalized) |
| 81 | + if not isinstance(record, dict): |
| 82 | + raise BundlerError( |
| 83 | + f"Bundle '{entry.id}' release '{advertised}' must be an object." |
| 84 | + ) |
| 85 | + if _RESERVED.intersection(record): |
| 86 | + raise BundlerError( |
| 87 | + f"Bundle '{entry.id}' release '{advertised}' contains reserved fields." |
| 88 | + ) |
| 89 | + if ( |
| 90 | + not isinstance(record.get("download_url"), str) |
| 91 | + or not record["download_url"].strip() |
| 92 | + ): |
| 93 | + raise BundlerError( |
| 94 | + f"Bundle '{entry.id}' release '{advertised}' needs a download_url." |
| 95 | + ) |
| 96 | + if not is_https_or_localhost_http(record["download_url"]): |
| 97 | + raise BundlerError( |
| 98 | + f"Bundle '{entry.id}' release '{advertised}' has an invalid " |
| 99 | + "download_url." |
| 100 | + ) |
| 101 | + digest = record.get("sha256") |
| 102 | + if isinstance(digest, str): |
| 103 | + digest = digest.strip() |
| 104 | + if digest[:7].lower() == "sha256:": |
| 105 | + digest = digest[7:].strip() |
| 106 | + if not isinstance(digest, str) or not _SHA256.fullmatch(digest): |
| 107 | + raise BundlerError( |
| 108 | + f"Bundle '{entry.id}' release '{advertised}' needs a SHA-256 digest." |
| 109 | + ) |
| 110 | + for field in ("requires", "provides"): |
| 111 | + if field in record and not isinstance(record[field], dict): |
| 112 | + raise BundlerError( |
| 113 | + f"Bundle '{entry.id}' release '{advertised}' has invalid {field}." |
| 114 | + ) |
| 115 | + common = {key: value for key, value in raw.items() if key not in _CURRENT_ONLY} |
| 116 | + try: |
| 117 | + CatalogEntry.from_dict({**common, **record, "version": advertised}) |
| 118 | + except BundlerError as exc: |
| 119 | + raise _release_error(entry, advertised, exc) from exc |
| 120 | + return releases |
| 121 | + |
| 122 | + |
| 123 | +def select_release(entry: CatalogEntry, version: str | None) -> CatalogEntry | None: |
| 124 | + """Select an exact release from one winning catalog entry.""" |
| 125 | + releases = _validated_releases(entry) |
| 126 | + current = entry.version |
| 127 | + if version is None or version == current: |
| 128 | + return entry |
| 129 | + try: |
| 130 | + requested = Version(version) |
| 131 | + except InvalidVersion: |
| 132 | + return None |
| 133 | + try: |
| 134 | + if requested == Version(current): |
| 135 | + return entry |
| 136 | + except InvalidVersion: |
| 137 | + # Legacy entries without history may retain older non-PEP-440 spellings. |
| 138 | + pass |
| 139 | + raw = _raw(entry) |
| 140 | + for advertised, record in releases.items(): |
| 141 | + if requested == Version(advertised): |
| 142 | + common = { |
| 143 | + key: value for key, value in raw.items() if key not in _CURRENT_ONLY |
| 144 | + } |
| 145 | + return CatalogEntry.from_dict({**common, **record, "version": advertised}) |
| 146 | + return None |
| 147 | + |
| 148 | + |
| 149 | +def available_versions(entry: CatalogEntry) -> list[str]: |
| 150 | + """Return the current version, then historical releases newest first.""" |
| 151 | + releases = _validated_releases(entry) |
| 152 | + if not entry.version: |
| 153 | + return [] |
| 154 | + return [entry.version, *sorted(releases, key=Version, reverse=True)] |
0 commit comments