File size: 5,055 Bytes
2f8cc61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Offline release integrity, provenance and schema checks (standard library)."""
from pathlib import Path
import hashlib
import importlib.util
import json
import math
import re
import shutil
import sys
import tempfile

REPO = Path(__file__).resolve().parents[1]
# Keep verification from adding bytecode files to the exact upload inventory.
sys.dont_write_bytecode = True


def sha256(path):
    return hashlib.sha256(path.read_bytes()).hexdigest()


def validate(repo=REPO):
    repo = Path(repo).resolve()
    manifest = json.loads((repo / "MANIFEST.json").read_text(encoding="utf-8"))
    assert manifest["project_id"] == "orielith9"
    entries = manifest["files"]
    expected_paths = {entry["path"] for entry in entries}
    assert len(expected_paths) == len(entries), "Duplicate manifest paths"
    actual_paths = {p.relative_to(repo).as_posix() for p in repo.rglob("*")
                    if p.is_file() and p.name != "MANIFEST.json" and "__pycache__" not in p.parts}
    assert actual_paths == expected_paths, "Repository inventory differs from manifest"
    for entry in entries:
        path = repo / entry["path"]
        assert not path.is_symlink() and repo in path.resolve().parents
        assert path.stat().st_size == entry["bytes"], f"Size mismatch: {entry['path']}"
        assert sha256(path) == entry["sha256"], f"Checksum mismatch: {entry['path']}"
    research = repo / "research"
    for line in (research / "SHA256SUMS.txt").read_text(encoding="utf-8").splitlines():
        digest, name = line.split("  ", 1)
        assert sha256(research / name) == digest, f"Source release checksum mismatch: {name}"

    schema = json.loads((repo / "dataset_schema.json").read_text(encoding="utf-8"))
    assert schema["experimental_records"] == 0
    card = (repo / "README.md").read_text(encoding="utf-8")
    assert card.startswith("---\n") and "\n---\n" in card
    metadata = card.split("\n---\n", 1)[0]
    names = re.findall(r"^  - config_name: ([a-z_]+)$", metadata, re.M)
    paths = re.findall(r"^        path: (data/[^\s]+)$", metadata, re.M)
    assert set(names) == set(schema["configs"]) and len(names) == len(schema["configs"])
    assert paths == [schema["configs"][name]["path"] for name in names]
    assert metadata.count("default: true") == 1
    ids = set()
    total = 0
    for name, config in schema["configs"].items():
        rows = [json.loads(line) for line in (repo / config["path"]).read_text(encoding="utf-8").splitlines()]
        assert len(rows) == config["rows"]
        total += len(rows)
        for row in rows:
            assert {key: type(value).__name__ for key, value in row.items()} == config["fields"], name
            assert row["id"] not in ids
            ids.add(row["id"])
            assert (repo / row["source_path"]).is_file()
            assert row["evidence_status"]
            assert all(math.isfinite(value) for value in row.values() if isinstance(value, float))
            if name == "design_parameters":
                json.loads(row["value_json"])
            if name == "reference_model":
                assert abs(row["true_tether_extension"] - row["reconstructed_tether_extension"]) < 1e-12
        if name == "layers":
            assert sum(row["thickness_um"] for row in rows) == 1600
    assert total == 297

    # Rebuild all transforms in a disposable directory, keeping original bytes intact.
    spec = importlib.util.spec_from_file_location("orielith_build", repo / "tools/build_dataset.py")
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    with tempfile.TemporaryDirectory(prefix="orielith-data-check-") as tmp:
        target = Path(tmp)
        shutil.copytree(research, target / "research")
        module.build_data(target)
        for rel in ["dataset_schema.json"] + [cfg["path"] for cfg in schema["configs"].values()]:
            assert (target / rel).read_bytes() == (repo / rel).read_bytes(), f"Nonreproducible transform: {rel}"

    for filename in ("README.md", "DATA_DICTIONARY.md", "RIGHTS.md"):
        text = (repo / filename).read_text(encoding="utf-8")
        for destination in re.findall(r"\]\(([^)]+)\)", text):
            if "://" in destination or destination.startswith("#"):
                continue
            assert (repo / destination.split("#")[0]).exists(), f"Broken link in {filename}: {destination}"
    return {"status": "passed", "project_id": manifest["project_id"],
            "repository_files": len(entries) + 1, "configurations": len(schema["configs"]),
            "records": total, "experimental_measurement_records": 0,
            "checks": ["repository inventory and SHA256", "original release SHA256",
                       "dataset metadata and primitive schemas", "provenance and deterministic IDs",
                       "finite model values and reconstruction residual", "nominal layer sum",
                       "byte-reproducible source transformations", "relative documentation links"]}


if __name__ == "__main__":
    print(json.dumps(validate(), indent=2))