|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import shutil |
| 6 | +import subprocess |
| 7 | +import tempfile |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +try: |
| 11 | + import tomllib |
| 12 | +except ModuleNotFoundError: # pragma: no cover |
| 13 | + import tomli as tomllib # type: ignore |
| 14 | + |
| 15 | + |
| 16 | +REPO_URL = "https://github.com/RobotControlStack/robot-control-stack.git" |
| 17 | + |
| 18 | + |
| 19 | +def load_version(project_root: Path) -> str: |
| 20 | + data = tomllib.loads((project_root / "pyproject.toml").read_text()) |
| 21 | + return data["project"]["version"] |
| 22 | + |
| 23 | + |
| 24 | +def find_shared_source(project_root: Path) -> Path: |
| 25 | + env_override = os.environ.get("RCS_FR3_SOURCE_DIR") |
| 26 | + if env_override: |
| 27 | + candidate = Path(env_override).resolve() |
| 28 | + if (candidate / "CMakeLists.txt").is_file(): |
| 29 | + return candidate |
| 30 | + msg = f"RCS_FR3_SOURCE_DIR does not point to a valid FR3 source tree: {candidate}" |
| 31 | + raise FileNotFoundError(msg) |
| 32 | + |
| 33 | + sibling = (project_root.parent / "rcs_fr3" / "src").resolve() |
| 34 | + if (sibling / "CMakeLists.txt").is_file(): |
| 35 | + return sibling |
| 36 | + |
| 37 | + version = load_version(project_root) |
| 38 | + tmpdir = Path(tempfile.mkdtemp(prefix="rcs_panda_fr3_")) |
| 39 | + try: |
| 40 | + subprocess.run( |
| 41 | + ["git", "clone", "--depth", "1", "--branch", f"v{version}", REPO_URL, str(tmpdir)], |
| 42 | + check=True, |
| 43 | + ) |
| 44 | + except subprocess.CalledProcessError: |
| 45 | + shutil.rmtree(tmpdir, ignore_errors=True) |
| 46 | + tmpdir = Path(tempfile.mkdtemp(prefix="rcs_panda_fr3_")) |
| 47 | + subprocess.run( |
| 48 | + ["git", "clone", "--depth", "1", REPO_URL, str(tmpdir)], |
| 49 | + check=True, |
| 50 | + ) |
| 51 | + cloned = tmpdir / "extensions" / "rcs_fr3" / "src" |
| 52 | + if not (cloned / "CMakeLists.txt").is_file(): |
| 53 | + msg = f"Could not locate FR3 shared sources in cloned repo at {cloned}" |
| 54 | + raise FileNotFoundError(msg) |
| 55 | + return cloned |
| 56 | + |
| 57 | + |
| 58 | +def copy_file(src: Path, dest: Path) -> None: |
| 59 | + dest.parent.mkdir(parents=True, exist_ok=True) |
| 60 | + shutil.copy2(src, dest) |
| 61 | + |
| 62 | + |
| 63 | +def materialize(project_root: Path, output_dir: Path) -> None: |
| 64 | + shared_src = find_shared_source(project_root) |
| 65 | + panda_src = project_root / "src_fr3" |
| 66 | + |
| 67 | + if output_dir.exists(): |
| 68 | + shutil.rmtree(output_dir) |
| 69 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 70 | + |
| 71 | + copy_file(shared_src / "CMakeLists.txt", output_dir / "CMakeLists.txt") |
| 72 | + shutil.copytree(shared_src / "hw", output_dir / "hw") |
| 73 | + copy_file(panda_src / "pybind" / "CMakeLists.txt", output_dir / "pybind" / "CMakeLists.txt") |
| 74 | + copy_file(shared_src / "pybind" / "rcs.cpp", output_dir / "pybind" / "rcs.cpp") |
| 75 | + |
| 76 | + |
| 77 | +def main() -> None: |
| 78 | + parser = argparse.ArgumentParser() |
| 79 | + parser.add_argument("--project-root", type=Path, required=True) |
| 80 | + parser.add_argument("--output-dir", type=Path, required=True) |
| 81 | + args = parser.parse_args() |
| 82 | + materialize(args.project_root.resolve(), args.output_dir.resolve()) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + main() |
0 commit comments