-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap_isaac_sources.py
More file actions
262 lines (227 loc) · 7.79 KB
/
bootstrap_isaac_sources.py
File metadata and controls
262 lines (227 loc) · 7.79 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md).
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
"""Bootstrap Isaac source repositories for MLX/macOS porting work."""
from __future__ import annotations
import argparse
import json
import os
import re
import subprocess
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Sequence
@dataclass(frozen=True)
class RepoSpec:
"""Repository metadata for a source checkout."""
name: str
url: str
default_ref: str
use_lfs: bool = False
@dataclass(frozen=True)
class CloneResult:
"""Manifest entry describing a cloned source repository."""
name: str
path: str
remote: str
requested_ref: str
resolved_sha: str
lfs_enabled: bool
DEFAULT_REPOS = {
"isaaclab": RepoSpec(
name="IsaacLab",
url="https://github.com/isaac-sim/IsaacLab.git",
default_ref="main",
),
"isaacsim": RepoSpec(
name="IsaacSim",
url="https://github.com/isaac-sim/IsaacSim.git",
default_ref="main",
use_lfs=True,
),
"isaac_ros": RepoSpec(
name="isaac_ros_common",
url="https://github.com/NVIDIA-ISAAC-ROS/isaac_ros_common.git",
default_ref="main",
),
}
SHA_PATTERN = re.compile(r"^[0-9a-fA-F]{7,40}$")
def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace:
"""Parse CLI arguments for the bootstrap flow."""
parser = argparse.ArgumentParser(description="Clone Isaac source repositories for MLX/macOS port work.")
parser.add_argument(
"--dest",
type=Path,
default=Path.cwd(),
help="Workspace directory where the repositories should be cloned.",
)
parser.add_argument(
"--ref",
type=str,
default=None,
help="Branch, tag, or commit SHA to checkout in each cloned repository. Defaults to each repo's default ref.",
)
parser.add_argument(
"--depth",
type=int,
default=None,
help="Optional shallow clone depth. Ignored for commit-SHA checkouts unless a matching fetch is required.",
)
parser.add_argument(
"--with-isaacsim",
action="store_true",
help="Also clone IsaacSim into the destination workspace.",
)
parser.add_argument(
"--with-isaac-ros",
action="store_true",
help="Also clone isaac_ros_common into the destination workspace.",
)
parser.add_argument(
"--lfs",
action="store_true",
help="Run `git lfs install` and `git lfs pull` for repositories that require LFS, currently IsaacSim.",
)
parser.add_argument(
"--manifest",
type=Path,
default=None,
help="Optional path for the emitted JSON manifest. Defaults to <dest>/isaac_sources_manifest.json.",
)
parser.add_argument(
"--isaaclab-url",
type=str,
default=DEFAULT_REPOS["isaaclab"].url,
help=argparse.SUPPRESS,
)
parser.add_argument(
"--isaacsim-url",
type=str,
default=DEFAULT_REPOS["isaacsim"].url,
help=argparse.SUPPRESS,
)
parser.add_argument(
"--isaac-ros-url",
type=str,
default=DEFAULT_REPOS["isaac_ros"].url,
help=argparse.SUPPRESS,
)
return parser.parse_args(argv)
def run_command(command: Sequence[str], *, cwd: Path | None = None) -> subprocess.CompletedProcess[str]:
"""Run a command and raise with useful stderr on failure."""
result = subprocess.run(
list(command),
cwd=str(cwd) if cwd is not None else None,
check=False,
capture_output=True,
text=True,
)
if result.returncode != 0:
raise subprocess.CalledProcessError(
result.returncode,
list(command),
output=result.stdout,
stderr=result.stderr,
)
return result
def is_commit_sha(ref: str | None) -> bool:
"""Return True when the requested ref looks like a commit SHA."""
return bool(ref and SHA_PATTERN.fullmatch(ref))
def clone_repository(
repo: RepoSpec,
*,
dest: Path,
requested_ref: str | None = None,
depth: int | None = None,
enable_lfs: bool = False,
) -> CloneResult:
"""Clone a repository and resolve its checked-out commit SHA."""
repo_path = dest / repo.name
if repo_path.exists():
raise FileExistsError(f"Destination already exists: {repo_path}")
requested_ref = requested_ref or repo.default_ref
clone_command = ["git", "clone"]
if depth is not None and not is_commit_sha(requested_ref):
clone_command.extend(["--depth", str(depth)])
if requested_ref and not is_commit_sha(requested_ref):
clone_command.extend(["--branch", requested_ref])
clone_command.extend([repo.url, str(repo_path)])
run_command(clone_command)
if requested_ref and is_commit_sha(requested_ref):
fetch_command = ["git", "-C", str(repo_path), "fetch", "origin", requested_ref]
if depth is not None:
fetch_command.extend(["--depth", str(depth)])
run_command(fetch_command)
run_command(["git", "-C", str(repo_path), "checkout", requested_ref])
if enable_lfs and repo.use_lfs:
run_command(["git", "lfs", "install"], cwd=repo_path)
run_command(["git", "lfs", "pull"], cwd=repo_path)
resolved_sha = run_command(["git", "-C", str(repo_path), "rev-parse", "HEAD"]).stdout.strip()
return CloneResult(
name=repo.name,
path=str(repo_path),
remote=repo.url,
requested_ref=requested_ref,
resolved_sha=resolved_sha,
lfs_enabled=enable_lfs and repo.use_lfs,
)
def write_manifest(results: list[CloneResult], manifest_path: Path) -> Path:
"""Write clone results to a JSON manifest file."""
manifest_path.parent.mkdir(parents=True, exist_ok=True)
payload = {
"repositories": [asdict(result) for result in results],
}
manifest_path.write_text(json.dumps(payload, indent=2) + "\n")
return manifest_path
def build_repo_specs(args: argparse.Namespace) -> list[RepoSpec]:
"""Build the clone set from CLI arguments."""
repos = [
RepoSpec(
name=DEFAULT_REPOS["isaaclab"].name,
url=args.isaaclab_url,
default_ref=DEFAULT_REPOS["isaaclab"].default_ref,
use_lfs=DEFAULT_REPOS["isaaclab"].use_lfs,
)
]
if args.with_isaacsim:
repos.append(
RepoSpec(
name=DEFAULT_REPOS["isaacsim"].name,
url=args.isaacsim_url,
default_ref=DEFAULT_REPOS["isaacsim"].default_ref,
use_lfs=DEFAULT_REPOS["isaacsim"].use_lfs,
)
)
if args.with_isaac_ros:
repos.append(
RepoSpec(
name=DEFAULT_REPOS["isaac_ros"].name,
url=args.isaac_ros_url,
default_ref=DEFAULT_REPOS["isaac_ros"].default_ref,
use_lfs=DEFAULT_REPOS["isaac_ros"].use_lfs,
)
)
return repos
def main(argv: Sequence[str] | None = None) -> int:
"""Run the bootstrap flow."""
args = parse_args(argv)
args.dest.mkdir(parents=True, exist_ok=True)
results = [
clone_repository(
repo,
dest=args.dest,
requested_ref=args.ref,
depth=args.depth,
enable_lfs=args.lfs,
)
for repo in build_repo_specs(args)
]
manifest_path = args.manifest or args.dest / "isaac_sources_manifest.json"
write_manifest(results, manifest_path)
for result in results:
print(f"[bootstrap] cloned {result.name} @ {result.resolved_sha} -> {result.path}")
print(f"[bootstrap] manifest -> {manifest_path}")
return 0
if __name__ == "__main__":
raise SystemExit(main())