-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathvalidate_catalog.py
More file actions
67 lines (54 loc) · 1.99 KB
/
Copy pathvalidate_catalog.py
File metadata and controls
67 lines (54 loc) · 1.99 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
#!/usr/bin/env python3
"""Validate the canonical FlyPython resource catalog."""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
try:
from tools.catalog import CatalogLoadError, load_catalog, validate_catalog
except ModuleNotFoundError: # Direct ``python tools/validate_catalog.py`` execution.
from catalog import CatalogLoadError, load_catalog, validate_catalog
ROOT_DIR = Path(__file__).resolve().parent.parent
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--catalog", type=Path, default=ROOT_DIR / "catalog"
)
parser.add_argument("--output", type=Path, help="optional JSON report path")
parser.add_argument("--max-review-age-days", type=int, default=366)
return parser
def run(argv: list[str] | None = None) -> int:
args = build_parser().parse_args(argv)
if args.max_review_age_days < 1:
print("--max-review-age-days must be positive", file=sys.stderr)
return 2
try:
data = load_catalog(args.catalog)
except CatalogLoadError as exc:
print(str(exc), file=sys.stderr)
return 2
issues = validate_catalog(data, max_review_age_days=args.max_review_age_days)
report = {
"catalog": str(args.catalog),
"valid": not issues,
"issue_count": len(issues),
"issues": [issue.as_dict() for issue in issues],
}
if args.output:
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(
json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8"
)
if issues:
for issue in issues:
print(
f"{issue.location}: {issue.code}: {issue.message}", file=sys.stderr
)
return 1
print(f"catalog valid: {len(data.get('resources', []))} resources")
return 0
def main() -> None:
raise SystemExit(run())
if __name__ == "__main__":
main()