Datasets:
Download scripts/validate_raw_release.py from nkeetha/paninian-sanskrit: direct link, hf CLI and curl.
- Browser
- Download file 3.53 kB
-
https://huggingface.co/datasets/nkeetha/paninian-sanskrit/resolve/main/scripts/validate_raw_release.py
- Command line
-
hf download hf://datasets/nkeetha/paninian-sanskrit/scripts/validate_raw_release.py
-
curl -L -o validate_raw_release.py https://huggingface.co/datasets/nkeetha/paninian-sanskrit/resolve/main/scripts/validate_raw_release.py
3.53 kB
| #!/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()) | |