File size: 3,530 Bytes
ee63202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Validate the structure and checksums of the raw dataset release."""

from __future__ import annotations

import csv
import hashlib
import sys
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]


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


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


def main() -> int:
    failures = []
    manifest_path = ROOT / "metadata" / "manuscripts.csv"
    with manifest_path.open(encoding="utf-8", newline="") as handle:
        records = list(csv.DictReader(handle))

    if len(records) != 27:
        failures.append(f"Expected 27 manifest rows, found {len(records)}")
    if len({row["archive_identifier"] for row in records}) != 26:
        failures.append("Expected 26 unique Archive.org identifiers")
    if sum(row["source_script"] == "Sharada" for row in records) != 2:
        failures.append("Expected 2 Sharada records")

    expected_checksum_paths = set()
    for row in records:
        for field in (
            "transcription_iast_path",
            "transcription_devanagari_path",
            "archive_pdf_path",
        ):
            expected_checksum_paths.add(row[field])
            path = ROOT / row[field]
            if not path.is_file():
                failures.append(f"Missing {field}: {path.relative_to(ROOT)}")
        expected_checksum_paths.add(
            f"data/raw/archive_metadata/{row['archive_identifier']}.json"
        )
        scan_path = ROOT / row["archive_pdf_path"]
        if scan_path.is_file() and scan_path.stat().st_size != int(row["archive_pdf_size"]):
            failures.append(f"Size mismatch: {scan_path.relative_to(ROOT)}")
        if scan_path.is_file() and sha1(scan_path) != row["archive_pdf_sha1"]:
            failures.append(f"Archive.org SHA-1 mismatch: {scan_path.relative_to(ROOT)}")

    checksum_path = ROOT / "metadata" / "file_checksums.sha256"
    checksum_entries = set()
    with checksum_path.open(encoding="utf-8") as handle:
        for line_number, line in enumerate(handle, 1):
            expected, relative = line.rstrip("\n").split("  ", 1)
            if relative in checksum_entries:
                failures.append(f"Duplicate checksum target on line {line_number}: {relative}")
            checksum_entries.add(relative)
            path = ROOT / relative
            if not path.is_file():
                failures.append(f"Checksum target missing on line {line_number}: {relative}")
            elif sha256(path) != expected:
                failures.append(f"Checksum mismatch: {relative}")

    for relative in sorted(expected_checksum_paths - checksum_entries):
        failures.append(f"Required checksum entry missing: {relative}")
    for relative in sorted(checksum_entries - expected_checksum_paths):
        failures.append(f"Unexpected checksum entry: {relative}")

    if failures:
        print("Raw release validation failed:", file=sys.stderr)
        for failure in failures:
            print(f"- {failure}", file=sys.stderr)
        return 1

    print("Raw release valid: 27 transcription records, 26 scans, 2 scripts")
    return 0


if __name__ == "__main__":
    sys.exit(main())