File size: 3,064 Bytes
a863f79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import hashlib
import json
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
REL = ROOT / "release" / "data_v1"
DOC = ROOT / "release" / "croissant.json"

REQUIRED_TOP = ("@context", "@type", "conformsTo", "name", "description", "license", "distribution", "recordSet")
RAI_KEYS = (
    "rai:dataCollection",
    "rai:dataUseCases",
    "rai:dataLimitations",
    "rai:dataBiases",
    "rai:personalSensitiveInformation",
    "rai:dataSocialImpact",
    "rai:hasDataUsePermission",
)


def sha256(path):
    h = hashlib.sha256()
    with open(path, "rb") as fh:
        for chunk in iter(lambda: fh.read(1 << 20), b""):
            h.update(chunk)
    return h.hexdigest()


def main():
    doc = json.loads(DOC.read_text(encoding="utf-8"))
    problems = []

    for key in REQUIRED_TOP:
        if key not in doc:
            problems.append(f"缺少顶层字段 {key}")
    if doc.get("@type") != "sc:Dataset":
        problems.append("@type 必须是 sc:Dataset")
    if doc.get("conformsTo") != "http://mlcommons.org/croissant/1.0":
        problems.append("conformsTo 必须指向 Croissant 1.0")
    ctx = doc.get("@context") or {}
    if not isinstance(ctx, dict) or "cr" not in ctx:
        problems.append("@context 缺少 cr 命名空间")
    for key in RAI_KEYS:
        if not doc.get(key):
            problems.append(f"缺少 RAI 字段 {key}")

    checked_files = 0
    checked_hashes = 0
    for obj in doc.get("distribution", []):
        if obj.get("@type") != "cr:FileObject":
            problems.append(f"distribution 条目类型错误:{obj.get('name')}")
        url = obj.get("contentUrl")
        if not url:
            problems.append(f"FileObject 缺少 contentUrl:{obj.get('name')}")
            continue
        path = REL / url
        if not path.exists():
            problems.append(f"contentUrl 指向不存在的路径:{url}")
            continue
        checked_files += 1
        if "sha256" in obj:
            if path.is_file():
                if sha256(path) != obj["sha256"]:
                    problems.append(f"sha256 不匹配:{url}")
                else:
                    checked_hashes += 1
            else:
                problems.append(f"目录不应带 sha256:{url}")

    for rs in doc.get("recordSet", []):
        if rs.get("@type") != "cr:RecordSet":
            problems.append(f"recordSet 类型错误:{rs.get('name')}")
        if not rs.get("field"):
            problems.append(f"recordSet 没有 field:{rs.get('name')}")
        for fld in rs.get("field", []):
            if fld.get("@type") != "cr:Field" or not fld.get("dataType"):
                problems.append(f"字段定义不完整:{fld.get('name')}")

    print(json.dumps({
        "ok": not problems,
        "file_objects": len(doc.get("distribution", [])),
        "checked_paths": checked_files,
        "verified_hashes": checked_hashes,
        "problems": problems,
    }, ensure_ascii=False, indent=2))
    if problems:
        raise SystemExit(1)


if __name__ == "__main__":
    main()