""" "Import/Convert" an **existing local LeRobot dataset directory** to LeRobot's standard cache location `$HF_LEROBOT_HOME/` (OpenPi/LeRobot loads datasets from here by repo_id by default). The requirement is to import: `/workspace/ghsun/libero_plus_lerobot_ori_10pct` into `$HF_LEROBOT_HOME` (or a specified output directory), so it can be loaded directly by LeRobot/OpenPi via `repo_id`. Usage example: ```bash python openpi/convert_libero_plus.py \ --src_dir /workspace/ghsun/libero_plus_lerobot_ori_10pct \ --repo_id local/libero_plus_lerobot_ori_10pct ``` Notes: - If `--hardlink=True` and source/destination are on the same filesystem, hard links will be preferred for speed and to save disk space; otherwise, it automatically falls back to standard copy. - This script **does not modify data content**, it only handles the "placement into standard location" conversion/transfer. """ from __future__ import annotations import argparse import os import shutil from pathlib import Path def _default_hf_lerobot_home() -> Path: """ Try to align with LeRobot's default behavior: - Prioritize environment variable HF_LEROBOT_HOME - Otherwise use HF_HOME/lerobot (if huggingface_hub is available) - Otherwise fallback to ~/.cache/huggingface/lerobot """ env = os.getenv("HF_LEROBOT_HOME") if env: return Path(env).expanduser() try: from huggingface_hub.constants import HF_HOME # type: ignore return (Path(HF_HOME) / "lerobot").expanduser() except Exception: # noqa: BLE001 return Path.home() / ".cache" / "huggingface" / "lerobot" HF_LEROBOT_HOME = _default_hf_lerobot_home() def _copytree(src: Path, dst: Path, *, hardlink: bool) -> None: def _copy_or_link(s: str, d: str) -> str: if hardlink: try: os.link(s, d) return d except OSError: # May fail across filesystems, fallback to copy pass shutil.copy2(s, d) return d shutil.copytree(src, dst, copy_function=_copy_or_link, dirs_exist_ok=False) def main() -> None: parser = argparse.ArgumentParser(description="Import local LeRobot dataset to $HF_LEROBOT_HOME/") parser.add_argument( "--src_dir", type=str, default="/workspace/ghsun/libero_plus_lerobot_ori_10pct", help="Root directory of the dataset to import (should contain meta/info.json)", ) parser.add_argument( "--repo_id", type=str, default="local/libero_plus_lerobot_ori_10pct", help="Target repo_id under the output root directory after import (will become a directory)", ) parser.add_argument( "--output_root", type=str, default=None, help="Output root directory; if not provided, uses $HF_LEROBOT_HOME (default: ~/.cache/huggingface/lerobot)", ) parser.add_argument("--overwrite", action="store_true", help="Delete and overwrite if target already exists") parser.add_argument("--no-overwrite", dest="overwrite", action="store_false", help="Raise error if target already exists") parser.set_defaults(overwrite=True) parser.add_argument("--hardlink", action="store_true", help="Prefer hard links (fallback to copy on failure)") parser.add_argument("--no-hardlink", dest="hardlink", action="store_false", help="Disable hard links, copy only") parser.set_defaults(hardlink=True) args = parser.parse_args() src = Path(args.src_dir).expanduser().resolve() if not src.exists(): raise FileNotFoundError(f"src_dir does not exist: {src}") if not (src / "meta" / "info.json").exists(): raise FileNotFoundError(f"LeRobot meta info file not found: {src / 'meta' / 'info.json'}") output_root = Path(args.output_root).expanduser().resolve() if args.output_root else HF_LEROBOT_HOME dst = (output_root / args.repo_id).resolve() if dst.exists(): if not args.overwrite: raise FileExistsError(f"Target already exists and overwrite=False: {dst}") shutil.rmtree(dst) dst.parent.mkdir(parents=True, exist_ok=True) _copytree(src, dst, hardlink=args.hardlink) print(f"Done: Imported to {dst}") print(f"You can now load it in OpenPi using repo_id='{args.repo_id}' (default root={output_root})") if __name__ == "__main__": main()