|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import pathlib |
| 6 | +import re |
6 | 7 | import typing |
7 | 8 | from collections.abc import Mapping |
| 9 | +from urllib.parse import urlparse |
8 | 10 |
|
9 | 11 | import pydantic |
| 12 | +from packageurl import PackageURL |
10 | 13 | from packaging.utils import NormalizedName, canonicalize_name |
11 | 14 | from packaging.version import Version |
12 | 15 | from pydantic_core import CoreSchema, core_schema |
@@ -98,6 +101,99 @@ def _validate_envkey(v: typing.Any) -> str: |
98 | 101 | GlobalChangelog = Mapping[Variant, list[str]] |
99 | 102 | VariantChangelog = Mapping[PackageVersion, list[str]] |
100 | 103 |
|
| 104 | +_SPDX_ACTOR_RE = re.compile(r"^(Organization|Person|Tool):\s+\S.*$", flags=re.DOTALL) |
| 105 | + |
| 106 | + |
| 107 | +def _validate_url(v: str) -> str: |
| 108 | + """Validate that *v* has an ``http`` or ``https`` scheme and a host.""" |
| 109 | + if not isinstance(v, str): |
| 110 | + raise TypeError(f"expected str, got {type(v)}: {v!r}") |
| 111 | + v = v.strip() |
| 112 | + parsed = urlparse(v) |
| 113 | + if parsed.scheme not in ("http", "https"): |
| 114 | + raise ValueError(f"URL must use http or https scheme, got {v!r}") |
| 115 | + if not parsed.netloc: |
| 116 | + raise ValueError(f"URL is missing a host/netloc component: {v!r}") |
| 117 | + return v |
| 118 | + |
| 119 | + |
| 120 | +# purl type (e.g. "pypi", "generic", "github") |
| 121 | +def _validate_purl_type(v: str) -> str: |
| 122 | + """Strip, lowercase, and reject empty purl type strings.""" |
| 123 | + if not isinstance(v, str): |
| 124 | + raise TypeError(f"expected str, got {type(v)}: {v!r}") |
| 125 | + v = v.strip().lower() |
| 126 | + if not v: |
| 127 | + raise ValueError("purl type must not be empty") |
| 128 | + return v |
| 129 | + |
| 130 | + |
| 131 | +PurlType = typing.Annotated[ |
| 132 | + str, |
| 133 | + pydantic.BeforeValidator(_validate_purl_type), |
| 134 | +] |
| 135 | + |
| 136 | + |
| 137 | +# repository URL used as a purl qualifier |
| 138 | +RepositoryUrl = typing.Annotated[ |
| 139 | + str, |
| 140 | + pydantic.BeforeValidator(_validate_url), |
| 141 | +] |
| 142 | + |
| 143 | + |
| 144 | +# full purl string identifying an upstream source package |
| 145 | +def _validate_upstream_purl(v: str) -> str: |
| 146 | + """Validate that *v* is a well-formed purl string.""" |
| 147 | + if not isinstance(v, str): |
| 148 | + raise TypeError(f"expected str, got {type(v)}: {v!r}") |
| 149 | + v = v.strip() |
| 150 | + try: |
| 151 | + PackageURL.from_string(v) |
| 152 | + except ValueError as err: |
| 153 | + raise ValueError(f"invalid upstream purl {v!r}: {err}") from err |
| 154 | + return v |
| 155 | + |
| 156 | + |
| 157 | +UpstreamPurl = typing.Annotated[ |
| 158 | + str, |
| 159 | + pydantic.BeforeValidator(_validate_upstream_purl), |
| 160 | +] |
| 161 | + |
| 162 | + |
| 163 | +# SPDX documentNamespace base URL |
| 164 | +SpdxNamespace = typing.Annotated[ |
| 165 | + str, |
| 166 | + pydantic.BeforeValidator(_validate_url), |
| 167 | +] |
| 168 | + |
| 169 | + |
| 170 | +# SPDX actor value for supplier / creator fields |
| 171 | +def _validate_spdx_actor(v: str) -> str: |
| 172 | + """Validate SPDX 2.3 actor format. |
| 173 | +
|
| 174 | + Must be ``NOASSERTION`` or ``<Category>: <name>`` where |
| 175 | + category is ``Organization``, ``Person``, or ``Tool``. |
| 176 | + """ |
| 177 | + if not isinstance(v, str): |
| 178 | + raise TypeError(f"expected str, got {type(v)}: {v!r}") |
| 179 | + v = v.strip() |
| 180 | + if v == "NOASSERTION": |
| 181 | + return v |
| 182 | + if not _SPDX_ACTOR_RE.match(v): |
| 183 | + raise ValueError( |
| 184 | + f"SPDX actor must be 'NOASSERTION' or " |
| 185 | + f"'Organization: <name>' / 'Person: <name>' / 'Tool: <name>', " |
| 186 | + f"got {v!r}" |
| 187 | + ) |
| 188 | + return v |
| 189 | + |
| 190 | + |
| 191 | +SpdxActor = typing.Annotated[ |
| 192 | + str, |
| 193 | + pydantic.BeforeValidator(_validate_spdx_actor), |
| 194 | +] |
| 195 | + |
| 196 | + |
101 | 197 | # Annotations |
102 | 198 | RawAnnotations = Mapping[str, str] |
103 | 199 |
|
|
0 commit comments