File size: 1,833 Bytes
1c951bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Identifier integrity checker for UnsolvedEarth v1.0.0.
Verifies DOI syntax, UEARTH-xxxxxx problem IDs, SRC-EARTH-xxxxxx source IDs,
and REL-EARTH-xxxxxx relation IDs across all JSON and JSONL files.
"""

import json
import re
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
DOI_REGEX = re.compile(r"^10\.\d{4,9}/\S+$")


def main():
    problems = json.loads((ROOT / "problems.json").read_text(encoding="utf-8"))
    sources = json.loads((ROOT / "source_registry.json").read_text(encoding="utf-8"))

    errors = []
    doi_count = 0

    for s in sources:
        sid = s.get("source_id", "")
        if not re.match(r"^SRC-EARTH-\d{6}$", sid):
            errors.append(f"Invalid source_id: {sid}")
        ids = s.get("identifiers") or {}
        doi = ids.get("doi")
        if doi:
            if not DOI_REGEX.match(doi):
                errors.append(f"{sid}: Malformed DOI '{doi}'")
            else:
                doi_count += 1
        elif not (ids.get("isbn") or ids.get("report_number")):
            errors.append(f"{sid}: Missing DOI, ISBN, and report_number")

    for p in problems:
        pid = p.get("id", "")
        if not re.match(r"^UEARTH-\d{6}$", pid):
            errors.append(f"Invalid problem id: {pid}")
        sids = p.get("source_identifiers") or {}
        doi = sids.get("doi")
        if doi and not DOI_REGEX.match(doi):
            errors.append(f"{pid}: Malformed source DOI '{doi}'")

    print(f"Checked {len(problems)} problems and {len(sources)} sources ({doi_count} valid DOIs).")
    if errors:
        print(f"FAILED with {len(errors)} identifier errors:")
        for e in errors[:20]:
            print("  ", e)
        sys.exit(1)
    print("IDENTIFIER CHECK PASSED: 0 errors.")


if __name__ == "__main__":
    main()