File size: 3,242 Bytes
e37c13d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Publish the release — and the code that produced it — to the Hub.

The dataset ships both halves of its own tooling so a download is
self-contained and reproducible:

    toolbox/      react_toolbox     — consume the data
    preprocess/   react_preprocess  — rebuild it from source recordings

Git stays the source of truth for both; this module mirrors them onto the
dataset repo (and into the local staging tree, so the code sits next to the
data on disk too).
"""
from __future__ import annotations

import shutil
from pathlib import Path

from .config import HF_REPO, STAGE_ROOT

PACKAGE_ROOT = Path(__file__).resolve().parent
SOURCE_ROOT = PACKAGE_ROOT.parent                    # twm/

CODE_TREES = {
    "preprocess": SOURCE_ROOT / "react_preprocess",
    "toolbox": SOURCE_ROOT / "react_toolbox",
}

SKIP = {"__pycache__", ".pytest_cache", ".DS_Store"}


def _files(tree: Path) -> list[Path]:
    return sorted(p for p in tree.rglob("*")
                  if p.is_file() and not any(s in p.parts for s in SKIP)
                  and p.suffix in {".py", ".md", ".txt", ".json", ".toml"})


def stage_code(stage_root: Path = STAGE_ROOT) -> dict[str, int]:
    """Copy the code trees next to the data in the local staging area."""
    counts = {}
    for name, tree in CODE_TREES.items():
        if not tree.exists():
            continue
        dest = Path(stage_root) / name
        if dest.exists():
            shutil.rmtree(dest)
        n = 0
        for src in _files(tree):
            out = dest / src.relative_to(tree)
            out.parent.mkdir(parents=True, exist_ok=True)
            shutil.copy2(src, out)
            n += 1
        counts[name] = n
    return counts


def code_operations(trees: dict[str, Path] | None = None) -> list:
    """CommitOperationAdd entries mirroring the code trees onto the repo."""
    from huggingface_hub import CommitOperationAdd

    ops = []
    for name, tree in (trees or CODE_TREES).items():
        if not tree.exists():
            continue
        for src in _files(tree):
            ops.append(CommitOperationAdd(
                path_in_repo=f"{name}/{src.relative_to(tree).as_posix()}",
                path_or_fileobj=str(src)))
    return ops


def parquet_operations(stage_root: Path, tasks=("motherboard", "pushT")) -> list:
    """CommitOperationAdd entries for every per-episode parquet."""
    from huggingface_hub import CommitOperationAdd

    ops = []
    for task in tasks:
        meta = Path(stage_root) / task / "meta"
        for src in sorted(meta.rglob("episode_*.parquet")):
            ops.append(CommitOperationAdd(
                path_in_repo=f"data/{task}/meta/{src.parent.name}/{src.name}",
                path_or_fileobj=str(src)))
    return ops


def publish(ops: list, message: str, repo: str = HF_REPO, dry_run: bool = False):
    """Push one commit to the dataset repo."""
    if dry_run or not ops:
        return {"committed": 0, "dry_run": True,
                "paths": [o.path_in_repo for o in ops][:20]}
    from huggingface_hub import HfApi

    HfApi().create_commit(repo_id=repo, repo_type="dataset",
                          operations=ops, commit_message=message)
    return {"committed": len(ops), "dry_run": False}