File size: 2,452 Bytes
5cffa76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Validate AlphaGenome dataset layout, checksums, and minimal readability."""

from __future__ import annotations

import argparse
import gzip
import hashlib
from pathlib import Path
import sys

import yaml


def sha256_file(path: Path) -> str:
    digest = hashlib.sha256()
    with path.open("rb") as f:
        for chunk in iter(lambda: f.read(1024 * 1024), b""):
            digest.update(chunk)
    return digest.hexdigest()


def load_manifest(path: Path) -> dict:
    return yaml.safe_load(path.read_text(encoding="utf-8"))


def validate_entry(root: Path, entry: dict, check_sha256: bool) -> None:
    rel_path = entry["path"]
    path = root / rel_path
    if not path.is_file():
        raise FileNotFoundError(f"missing file: {rel_path}")
    size = path.stat().st_size
    if size != int(entry["size_bytes"]):
        raise ValueError(f"size mismatch for {rel_path}: {size} != {entry['size_bytes']}")
    expected = entry.get("sha256")
    if check_sha256 and expected:
        actual = sha256_file(path)
        if actual != expected:
            raise ValueError(f"sha256 mismatch for {rel_path}: {actual} != {expected}")
    if rel_path.endswith(".gz.tfrecord"):
        with gzip.open(path, "rb") as f:
            if not f.read(1):
                raise ValueError(f"gzip TFRecord is unreadable: {rel_path}")


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("--root", default=".", help="dataset package root")
    parser.add_argument(
        "--manifest",
        default="metadata/file_manifest.yaml",
        help="file manifest relative to --root",
    )
    parser.add_argument(
        "--check-sha256",
        action="store_true",
        help="verify full SHA256 for every listed file",
    )
    args = parser.parse_args()

    root = Path(args.root).resolve()
    manifest = load_manifest(root / args.manifest)
    entries = manifest["files"]
    for entry in entries:
        validate_entry(root, entry, args.check_sha256)

    print("dataset_validation_ok: true")
    print(f"checked_files: {len(entries)}")
    print(f"total_size_bytes: {sum(int(e['size_bytes']) for e in entries)}")
    print("full_sha256_checked:", bool(args.check_sha256))
    return 0


if __name__ == "__main__":
    try:
        raise SystemExit(main())
    except Exception as exc:
        print(f"dataset_validation_ok: false\nerror: {exc}", file=sys.stderr)
        raise SystemExit(1)