Datasets:
File size: 1,927 Bytes
15b65c6 | 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 | # SPDX-License-Identifier: Apache-2.0
# /// script
# requires-python = ">=3.11"
# dependencies = ["jsonschema"]
# ///
"""Check every row of metadata.jsonl, filling in each file's size and SHA-256.
uv run tools/validate.py
Run from the dataset root before an upload. A row whose folder is here is checked
against its files; one whose folder was not downloaded, against the schema only.
Exits non-zero, naming the rows that fail.
"""
import hashlib
import json
import sys
from pathlib import Path
from jsonschema import Draft202012Validator
META = Path("metadata.jsonl")
schema = Draft202012Validator(json.loads(Path("schema.json").read_text()))
rows = [json.loads(line) for line in META.read_text().splitlines() if line.strip()]
errors = []
local = 0
for row in rows:
asset = row.get("id", "?")
model, preview = Path(row.get("file", "")), Path(row.get("file_name", ""))
if model.parts[:1] != (asset,) or preview.parts[:1] != (asset,):
errors.append(f"{asset}: file and file_name must sit in the folder {asset}/")
if not Path(asset).is_dir():
errors += [f"{asset}: {e.message}" for e in schema.iter_errors(row)]
continue
local += 1
if model.is_file():
with model.open("rb") as f:
row["sha256"] = hashlib.file_digest(f, "sha256").hexdigest()
row["size_bytes"] = model.stat().st_size
else:
errors.append(f"{asset}: no file {model}")
if not preview.is_file():
errors.append(f"{asset}: no preview {preview}; run tools/preview.py {asset}")
errors += [f"{asset}: {e.message}" for e in schema.iter_errors(row)]
ids = [row.get("id") for row in rows]
errors += [f"{i}: id used twice" for i in sorted({i for i in ids if ids.count(i) > 1})]
META.write_text("".join(json.dumps(row) + "\n" for row in rows))
print(
"\n".join(errors) or f"{len(rows)} rows valid, {local} checked against their files"
)
sys.exit(bool(errors))
|