-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathvalidate_registry.py
More file actions
520 lines (415 loc) · 18.7 KB
/
validate_registry.py
File metadata and controls
520 lines (415 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/usr/bin/env python
"""Validate packages' meta.yaml and generate an output directory with json/images to be uploaded on Github Pages"""
from __future__ import annotations
import argparse
import asyncio
import json
import os
import re
import shutil
import sys
from dataclasses import KW_ONLY, dataclass, field
from importlib.resources import files
from pathlib import Path
from textwrap import dedent
from typing import TYPE_CHECKING, cast, override
import httpx
import jsonschema
import yaml
from httpx_limiter import ( # type: ignore[attr-defined]
AbstractRateLimiterRepository,
AsyncMultiRateLimitedTransport,
Rate,
)
from httpx_limiter.aiolimiter import AiolimiterAsyncLimiter # type: ignore[attr-defined]
from httpx_retries import Retry, RetryTransport
from PIL import Image
from ._logging import log, setup_logging
if TYPE_CHECKING:
from collections.abc import Awaitable, Generator, Iterable, Mapping, Sequence
from importlib.resources.abc import Traversable
from .schema import ScverseEcosystemPackages # pyright: ignore[reportMissingModuleSource]
# Constants
HERE = Path(__file__).parent
IMAGE_SIZE = 512
class ValidationError(Exception):
pass
RE_RTD = re.compile(
r"https?://(?P<domain>.*\.(?:readthedocs\.io|rtfd\.io|readthedocs-hosted\.com))/(?P<version>en/[^/]+)(?P<path>.*)"
)
@dataclass
class HTTPValidator[E = str]:
"""Validate HTTP URLs."""
client: httpx.AsyncClient
_: KW_ONLY
validated: set[E] = field(default_factory=set)
@dataclass
class LinkChecker(HTTPValidator):
"""Track known links and validate URLs."""
name: str
async def __call__(self, url: str, context: str) -> None:
"""Check if URL is duplicate, validate it exists, and register it.
Parameters
----------
url
The URL to check and register
context
Context information for error messages (e.g., file being validated)
"""
if m := re.fullmatch(RE_RTD, url):
new_url = f"https://{m['domain']}/" + (f"page{m['path']}" if m["path"].strip("/") else "")
msg = (
f"{self.name}:{context}: "
f"Please use the default version in ReadTheDocs URLs instead of {m['version']!r}:\n"
f"{url}\n->\n{new_url}"
)
raise ValidationError(msg)
if url in self.validated:
msg = f"{self.name}:{context}: Duplicate link: {url}"
raise ValidationError(msg)
try:
response = await self.client.head(url)
except Exception as e:
msg = f"{self.name}:{context}: URL {url} is not reachable: {e}"
raise ValidationError(msg) from e
if response.status_code != httpx.codes.OK:
msg = f"{self.name}:{context}: URL {url} is not reachable (error {response.status_code}). "
raise ValidationError(msg)
self.validated.add(url)
log.info(f"Validated {self.name} URL for {context}: {url!r}")
@dataclass
class GitHubUserValidator(HTTPValidator):
"""Validate GitHub usernames using the GitHub API."""
github_token: str | None = None
async def __call__(self, usernames: Sequence[str], context: str) -> None:
"""Validate that a GitHub username exists.
Parameters
----------
username
The GitHub username to validate
context
Context information for error messages (e.g., file being validated)
"""
if not (unvalidated := list(set(usernames) - self.validated)):
return
headers = {}
if self.github_token:
headers["Authorization"] = f"token {self.github_token}"
q = "\n".join(f"user{i}: user(login: {json.dumps(name)}) {{ login }}" for i, name in enumerate(unvalidated))
try:
response = await self.client.post(
"https://api.github.com/graphql", headers=headers, json={"query": f"query {{ {q} }}"}
)
except Exception as e:
msg = f"{context}: Failed to validate GitHub users {unvalidated!r}: {e}"
raise ValidationError(msg) from e
if response.status_code != httpx.codes.OK:
msg = f"{context}: Failed to validate GitHub users {unvalidated!r} (error {response.status_code})"
raise ValidationError(msg)
gql_response = response.json()
if errors := gql_response.get("errors"):
error_msgs = "\n".join(f"- {error['message']}" for error in errors)
msg = f"{context}: Failed to validate GitHub users {unvalidated!r}:\n{error_msgs}"
raise ValidationError(msg)
self.validated |= set(unvalidated)
log.info(f"Validated GitHub users for {context}: {unvalidated!r}")
@dataclass
class PyPIValidator(HTTPValidator):
"""Validate PyPI package names against the PyPI API."""
async def __call__(self, package_name: str, context: str) -> None:
"""Validate that a PyPI package exists.
Parameters
----------
package_name
The PyPI package name to validate
context
Context information for error messages (e.g., file being validated)
"""
if package_name in self.validated:
return
try:
response = await self.client.head(f"https://pypi.org/pypi/{package_name}/json")
except Exception as e:
msg = f"{context}: Failed to validate PyPI package {package_name!r}: {e}"
raise ValidationError(msg) from e
if response.status_code == httpx.codes.NOT_FOUND:
msg = f"{context}: PyPI package {package_name!r} does not exist"
raise ValidationError(msg)
if response.status_code != httpx.codes.OK:
msg = f"{context}: Failed to validate PyPI package {package_name!r} (error {response.status_code})"
raise ValidationError(msg)
self.validated.add(package_name)
log.info(f"Validated PyPI package for {context}: {package_name}")
@dataclass
class CondaValidator(HTTPValidator):
"""Validate Conda package identifiers using the Anaconda API."""
async def __call__(self, package_spec: str, context: str) -> None:
"""Validate that a Conda package exists.
Parameters
----------
package_spec
The Conda package specification (e.g., "conda-forge::scanpy")
context
Context information for error messages (e.g., file being validated)
"""
if package_spec in self.validated:
return
# Parse channel and package name
if "::" not in package_spec:
msg = f"{context}: Invalid Conda package spec {package_spec!r} (expected format: channel::package)"
raise ValidationError(msg)
channel, package_name = package_spec.split("::", 1)
# Check package exists on the channel
try:
response = await self.client.head(f"https://api.anaconda.org/package/{channel}/{package_name}")
except Exception as e:
msg = f"{context}: Failed to validate Conda package '{package_spec}': {e}"
raise ValidationError(msg) from e
if response.status_code == httpx.codes.NOT_FOUND:
msg = f"{context}: Conda package '{package_spec}' does not exist"
raise ValidationError(msg)
if response.status_code != httpx.codes.OK:
msg = f"{context}: Failed to validate Conda package '{package_spec}' (error {response.status_code})"
raise ValidationError(msg)
self.validated.add(package_spec)
log.info(f"Validated Conda package for {context}: {package_spec}")
@dataclass
class CRANValidator(HTTPValidator):
"""Validate CRAN package names using the CRAN API."""
async def __call__(self, package_name: str, context: str) -> None:
"""Validate that a CRAN package exists.
Parameters
----------
package_name
The CRAN package name to validate
context
Context information for error messages (e.g., file being validated)
"""
if package_name in self.validated:
return
# CRAN packages can be checked via the packages database
try:
response = await self.client.head(f"https://crandb.r-pkg.org/{package_name}")
except Exception as e:
msg = f"{context}: Failed to validate CRAN package '{package_name}': {e}"
raise ValidationError(msg) from e
if response.status_code == httpx.codes.NOT_FOUND:
msg = f"{context}: CRAN package '{package_name}' does not exist"
raise ValidationError(msg)
if response.status_code != httpx.codes.OK:
msg = f"{context}: Failed to validate CRAN package '{package_name}' (error {response.status_code})"
raise ValidationError(msg)
self.validated.add(package_name)
log.info(f"Validated CRAN package for {context}: {package_name}")
@dataclass
class BioconductorValidator(HTTPValidator):
"""Validate Bioconductor package names using the Bioconductor API."""
async def __call__(self, package_name: str, context: str) -> None:
"""Validate that a Bioconductor package exists.
Parameters
----------
package_name
The Bioconductor package name to validate
context
Context information for error messages (e.g., file being validated)
"""
if package_name in self.validated:
return
# Bioconductor packages can be checked via their web API
try:
response = await self.client.head(f"https://bioconductor.org/packages/{package_name}/")
except Exception as e:
msg = f"{context}: Failed to validate Bioconductor package '{package_name}': {e}"
raise ValidationError(msg) from e
if response.status_code == httpx.codes.NOT_FOUND:
msg = f"{context}: Bioconductor package '{package_name}' does not exist"
raise ValidationError(msg)
if response.status_code != httpx.codes.OK:
msg = f"{context}: Failed to validate Bioconductor package '{package_name}' (error {response.status_code})"
raise ValidationError(msg)
self.validated.add(package_name)
log.info(f"Validated Bioconductor package for {context}: {package_name}")
def check_image(img_path: Path) -> None:
"""Validates that the image exists and that it is either a SVG or fits into the 512x512 bounding box."""
if not img_path.exists():
msg = f"Image does not exist: {img_path}"
raise ValidationError(msg)
if img_path.suffix == ".svg":
return
with Image.open(img_path) as img:
width, height = img.size
if not ((width == IMAGE_SIZE and height <= IMAGE_SIZE) or (width <= IMAGE_SIZE and height == IMAGE_SIZE)):
msg = dedent(
f"""\
When validating {img_path}: Image must fit in a {IMAGE_SIZE}x{IMAGE_SIZE}px bounding box
and one dimension must be exactly {IMAGE_SIZE} px.
Actual dimensions (width, height): ({width}, ({height}))."
"""
)
raise ValidationError(msg)
class DomainBasedRateLimiterRepository(AbstractRateLimiterRepository):
"""Apply different rate limits based on the domain being requested."""
@override
def get_identifier(self, request: httpx.Request) -> str:
return request.url.host
@override
def create(self, request: httpx.Request) -> AiolimiterAsyncLimiter:
return AiolimiterAsyncLimiter.create(Rate.create(magnitude=25))
@dataclass
class Checker:
schema_file: Traversable
registry_dir: Path
_: KW_ONLY
github_token: str | None = None
def __post_init__(self) -> None:
self.schema = json.loads(self.schema_file.read_bytes())
# Create HTTP client with retry configuration using httpx_retries transport
transport: httpx.AsyncBaseTransport = AsyncMultiRateLimitedTransport.create(
repository=DomainBasedRateLimiterRepository()
)
transport = RetryTransport(transport, Retry(total=3, backoff_factor=2))
self.client = httpx.AsyncClient(follow_redirects=True, timeout=30.0, transport=transport)
# using different link checkers,
# because each of them may point to the same URL and this wouldn't qualify as duplicate
self.check_home = LinkChecker(self.client, name="home")
self.check_docs = LinkChecker(self.client, name="docs")
self.check_tutorial = LinkChecker(self.client, name="tutorial")
self.check_gh_users = GitHubUserValidator(self.client, self.github_token)
self.check_pypi = PyPIValidator(self.client)
self.check_conda = CondaValidator(self.client)
self.check_cran = CRANValidator(self.client)
self.check_bioc = BioconductorValidator(self.client)
async def validate_packages(self) -> tuple[Mapping[str, Sequence[Exception]], Sequence[ScverseEcosystemPackages]]:
"""Find all package `meta.yaml` files in the registry dir and yield package records."""
errors: dict[str, list[ValidationError]] = {}
package_metadata: list[ScverseEcosystemPackages] = []
async with self.client:
async for check in asyncio.as_completed(
self.check_package(meta_path)
for meta_path in sorted(self.registry_dir.rglob("meta.yaml"), key=lambda x: x.parent.name)
):
pkg_id, tmp_meta, pkg_errors = await check
errors[pkg_id] = pkg_errors
package_metadata.append(tmp_meta)
return errors, package_metadata
async def check_package(self, meta_file: Path) -> tuple[str, ScverseEcosystemPackages, list[ValidationError]]:
pkg_id = meta_file.parent.name
with meta_file.open() as f:
tmp_meta = cast("ScverseEcosystemPackages", yaml.load(f, yaml.SafeLoader))
pkg_errors: list[ValidationError] = []
try:
jsonschema.validate(tmp_meta, self.schema)
except jsonschema.ValidationError as e:
msg = f"{pkg_id}: Failed to validate meta.yaml: {e}"
log.error(msg)
pkg_errors.append(ValidationError(msg))
# Check logo (if available) and make path relative to root of registry
if "logo" in tmp_meta:
img_path = self.registry_dir / pkg_id / tmp_meta["logo"]
try:
check_image(img_path)
except ValidationError as e:
log.error(e)
pkg_errors.append(e)
tmp_meta["logo"] = str(img_path)
log.info(f"Validating {pkg_id}")
async for check in asyncio.as_completed(self.http_checks(pkg_id, tmp_meta)):
try:
await check
except ValidationError as e:
log.error(e)
pkg_errors.append(e)
return pkg_id, tmp_meta, pkg_errors
def http_checks(self, pkg_id: str, tmp_meta: ScverseEcosystemPackages) -> Generator[Awaitable[None]]:
# Check and register all links
yield self.check_home(tmp_meta["project_home"], pkg_id)
yield self.check_docs(tmp_meta["documentation_home"], pkg_id)
if url := tmp_meta.get("tutorials_home"):
yield self.check_tutorial(url, pkg_id)
# Validate GitHub usernames in contact field
if usernames := tmp_meta.get("contact"):
yield self.check_gh_users(usernames, pkg_id)
# Validate install packages
if install_info := tmp_meta.get("install"):
if pypi_name := install_info.get("pypi"):
yield self.check_pypi(pypi_name, pkg_id)
if conda_name := install_info.get("conda"):
yield self.check_conda(conda_name, pkg_id)
if cran_name := install_info.get("cran"):
yield self.check_cran(cran_name, pkg_id)
if bioconductor_name := install_info.get("bioconductor"):
yield self.check_bioc(bioconductor_name, pkg_id)
def make_output(
packages: Iterable[ScverseEcosystemPackages],
*,
outdir: Path | None = None,
) -> None:
"""Create the output directory.
If outdir is not set, don't copy anything, just print the JSON to stdout
Structure:
outdir
- ecosystem.json # contains package metadata
- packagexxx/icon.svg # original icon filenames under a folder for each package.
- packageyyy/icon.png
"""
packages_rel: list[ScverseEcosystemPackages] = []
for pkg in packages:
pkg_rel = pkg.copy()
if logo := pkg.get("logo"):
img_srcpath = Path(logo)
img_localpath = Path(img_srcpath.parent.name) / img_srcpath.name
pkg_rel["logo"] = str(img_localpath)
if outdir:
img_outpath = outdir / img_localpath
img_outpath.parent.mkdir()
shutil.copy(img_srcpath, img_outpath)
packages_rel.append(pkg_rel)
if outdir:
with (outdir / "packages.json").open("w") as f:
json.dump(packages_rel, f)
else:
json.dump(packages_rel, sys.stdout, indent=2)
class Args(argparse.Namespace):
registry_dir: Path
outdir: Path | None
def main(args: Sequence[str] | None = None) -> None:
"""Main entry point for the validate-registry command."""
setup_logging()
if args is None:
args = sys.argv[1:]
parser = argparse.ArgumentParser(
prog="validate-registry",
description=(
"Validate packages' meta.yaml and generate an output directory "
"with json/images to be uploaded on github pages."
),
)
parser.add_argument(
"--registry-dir",
type=Path,
# HERE is <root>/scripts/src/ecosystem_scripts/, so go up 3 levels
default=HERE.parent.parent.parent / "packages",
help="Path to the registry directory containing package meta.yaml files",
)
parser.add_argument("--outdir", type=Path, help="outdir that will contain the data to be uploaded on github pages")
parsed_args = parser.parse_args(args, Args())
if not parsed_args.registry_dir.is_dir():
msg = f"Invalid Registry directory: {parsed_args.registry_dir}"
raise ValueError(msg)
schema_file = files("ecosystem_scripts").joinpath("schema.json")
github_token = os.environ.get("GITHUB_TOKEN")
if parsed_args.outdir is not None:
parsed_args.outdir.mkdir(parents=True)
log.info("Starting validation")
checker = Checker(schema_file, parsed_args.registry_dir, github_token=github_token)
errors, packages = asyncio.run(checker.validate_packages())
if any(errors.values()):
log.error("Validation error occured in at least one package. Exiting.")
sys.exit(1)
else:
make_output(packages, outdir=parsed_args.outdir)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.exit(1)