File size: 3,398 Bytes
ca40870
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f3a1d4
 
ca40870
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
#!/usr/bin/env python3
"""Validate and publish a completed Bactrainus dataset checkout."""

from __future__ import annotations

import argparse
import os
import subprocess
import sys
from collections.abc import Sequence
from pathlib import Path

DEFAULT_REPO_ID = "bactrianus/bactrainus-hotpotqa"
UPLOAD_PATTERNS = (
    "README.md",
    "LICENSE",
    "ATTRIBUTION.md",
    "DATA_PROVENANCE.md",
    "SOURCE_MANIFEST.json",
    "SOURCE_PATCHES.json",
    "architecture.svg",
    "CHECKSUMS.sha256",
    "data/**/*.parquet",
    "scripts/*.py",
)


def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace:
    package_root = Path(__file__).resolve().parents[1]
    parser = argparse.ArgumentParser(
        description=(
            "Validate a completed Bactrainus dataset checkout and optionally "
            "publish it to Hugging Face."
        )
    )
    parser.add_argument("--root", type=Path, default=package_root)
    parser.add_argument("--repo-id", default=DEFAULT_REPO_ID)
    parser.add_argument(
        "--token-env",
        default="HF_TOKEN",
        help="environment variable holding a write token (default: HF_TOKEN)",
    )
    parser.add_argument(
        "--execute",
        action="store_true",
        help="perform the upload after validation; omitted means validation only",
    )
    return parser.parse_args(argv)


def validate_checkout(root: Path) -> None:
    validator = Path(__file__).with_name("validate_release.py").resolve()
    subprocess.run(
        [sys.executable, str(validator), "--root", str(root)],
        check=True,
    )
    required = (root / "data", root / "CHECKSUMS.sha256", root / "README.md")
    missing = [str(path) for path in required if not path.exists()]
    if missing:
        raise FileNotFoundError(f"validated checkout is missing: {missing}")


def publish(root: Path, repo_id: str, token_env: str) -> str:
    token = os.environ.get(token_env)
    if not token:
        raise RuntimeError(f"set {token_env} before publishing")
    try:
        from huggingface_hub import HfApi
    except ModuleNotFoundError as error:
        raise RuntimeError("install huggingface_hub before publishing") from error

    api = HfApi(token=token)
    api.create_repo(repo_id=repo_id, repo_type="dataset", private=False, exist_ok=True)
    result = api.upload_folder(
        folder_path=root,
        repo_id=repo_id,
        repo_type="dataset",
        allow_patterns=list(UPLOAD_PATTERNS),
        ignore_patterns=["README_PENDING.md", "**/__pycache__/**", "*.pyc"],
        commit_message="Publish complete validated Bactrainus HotpotQA training data",
    )
    return str(result)


def main(argv: Sequence[str] | None = None) -> int:
    args = parse_args(argv)
    root = args.root.resolve()
    if not root.is_dir():
        print(f"error: dataset root does not exist: {root}", file=sys.stderr)
        return 2
    try:
        validate_checkout(root)
        if not args.execute:
            print("Validation passed. Re-run with --execute to publish.")
            return 0
        url = publish(root, args.repo_id, args.token_env)
    except (FileNotFoundError, RuntimeError, subprocess.CalledProcessError) as error:
        print(f"error: {error}", file=sys.stderr)
        return 1
    print(f"Published validated dataset revision: {url}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())