Repository navigation
Expand file tree
/
Copy pathdownload_cli.py
More file actions
executable file
·367 lines (289 loc) · 12.4 KB
/
Copy pathdownload_cli.py
File metadata and controls
executable file
·367 lines (289 loc) · 12.4 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python3
"""Download Claude Code CLI binary for bundling in wheel.
This script is run during the wheel build process to fetch the Claude Code CLI
binary using the official install script and place it in the package directory.
"""
import os
import platform
import random
import shutil
import subprocess
import sys
import tempfile
import time
from collections.abc import Callable
from pathlib import Path
from typing import NamedTuple, NoReturn
# scripts/ is not a package. Running this file directly puts scripts/ on
# sys.path, but loading it by path (as the tests do) does not. Appended, not
# prepended: the tests' entry outlives the import, and a prepended scripts/
# could shadow a stdlib module for the whole pytest process.
_SCRIPTS_DIR = str(Path(__file__).parent)
if _SCRIPTS_DIR not in sys.path:
sys.path.append(_SCRIPTS_DIR)
import _cli_version_validation as version_validation # noqa: E402
# Re-exported: this module's callers and tests refer to download_cli.VERSION_PATTERN.
VERSION_PATTERN = version_validation.VERSION_PATTERN
# The Windows installer reads the version and the downloaded script's path out
# of the environment under these names, so neither is part of the command text
# PowerShell parses. `$env:NAME` in argument position expands to exactly one
# argument -- the same argv separation the Unix path gets from
# `["bash", script, version]`.
INSTALL_VERSION_ENV_VAR = "CLAUDE_CLI_INSTALL_VERSION"
INSTALL_SCRIPT_ENV_VAR = "CLAUDE_CLI_INSTALL_SCRIPT"
# How many times retry_install() runs an attempt before giving up.
MAX_INSTALL_ATTEMPTS = 3
# What an unset CLAUDE_CLI_VERSION means, and -- being the installer's own
# default -- the one value both install paths express by passing no argument.
DEFAULT_VERSION = "latest"
def get_cli_version() -> str:
"""Get the CLI version to download from environment or default.
Returns the stripped value -- use it, rather than the raw environment
string, for everything downstream.
Raises:
ValueError: If CLAUDE_CLI_VERSION is set to something other than a
dist-tag ("latest", "stable") or a value matching VERSION_PATTERN.
"""
version = os.environ.get("CLAUDE_CLI_VERSION", DEFAULT_VERSION)
return version_validation.validate_version(
version, source="CLAUDE_CLI_VERSION", allow_dist_tag=True
)
def find_installed_cli() -> Path | None:
"""Find the installed Claude CLI binary."""
system = platform.system()
if system == "Windows":
# Windows installation locations (matches test.yml: $USERPROFILE\.local\bin)
locations = [
Path.home() / ".local" / "bin" / "claude.exe",
Path(os.environ.get("LOCALAPPDATA", "")) / "Claude" / "claude.exe",
]
else:
# Unix installation locations
locations = [
Path.home() / ".local" / "bin" / "claude",
Path("/usr/local/bin/claude"),
Path.home() / "node_modules" / ".bin" / "claude",
]
# Also check PATH
cli_path = shutil.which("claude")
if cli_path:
return Path(cli_path)
for path in locations:
if path.exists() and path.is_file():
return path
return None
def run_command(command: list[str], env: dict[str, str] | None = None) -> None:
"""Run one install command with no shell and no inherited stdin.
install.sh runs `claude install`, which branches on `[ -t 0 ]`. Under the
old `curl | bash` its stdin was the pipe, so it never saw a TTY; keep it
that way rather than handing it the caller's terminal.
``env`` replaces the child's environment when given; None inherits ours.
"""
subprocess.run(
command,
check=True,
capture_output=True,
stdin=subprocess.DEVNULL,
env=env,
)
def _reject_install_script(script_path: str, reason: str) -> NoReturn:
"""Report a downloaded installer we refuse to execute, and exit."""
head = Path(script_path).read_bytes()[:64]
print(
f"Error: downloaded install script {reason} (first bytes: {head!r}). "
f"Refusing to execute it.",
file=sys.stderr,
)
sys.exit(1)
def check_install_script(script_path: str) -> None:
"""Reject a downloaded install.sh that is not a shell script.
claude.ai answers unknown paths with HTTP 200 and an HTML body, which
`curl -f` cannot detect, so check the shebang before executing the file.
A wrong body is deterministic, so this fails fast instead of retrying.
"""
with Path(script_path).open("rb") as f:
magic = f.read(2)
if magic != b"#!":
_reject_install_script(script_path, "does not start with a shebang")
def check_powershell_install_script(script_path: str) -> None:
"""Reject a downloaded install.ps1 that is not a PowerShell script.
The same HTTP 200 + HTML body is invisible to Invoke-RestMethod. A .ps1 has
no shebang to check, so reject what the error page actually is -- an
XML/HTML document, whose first non-blank character is '<' -- and an empty
body. A wrong body is deterministic, so this fails fast instead of retrying.
'<' alone would be too blunt: a .ps1 may legitimately open with '<#', the
comment-based-help block real installers start with. No HTML or XML document
begins with that, so exempt it.
"""
body = Path(script_path).read_bytes()
# A UTF-8 BOM is legal in a .ps1 and common in PowerShell-authored files.
stripped = body.removeprefix(b"\xef\xbb\xbf").lstrip()
if not stripped:
_reject_install_script(script_path, "is empty")
if stripped.startswith(b"<") and not stripped.startswith(b"<#"):
_reject_install_script(script_path, "looks like an HTML or XML document")
def _decode(stream: bytes | str | None) -> str:
"""One captured stream as text. run_command() captures bytes; be tolerant."""
if stream is None:
return ""
if isinstance(stream, bytes):
return stream.decode(errors="replace")
return stream
def retry_install(attempt: Callable[[], None]) -> None:
"""Run an install attempt, retrying the whole attempt on command failure."""
# Small jitter to stagger parallel matrix builds hitting the same endpoint
time.sleep(random.uniform(0, 5))
for attempt_num in range(1, MAX_INSTALL_ATTEMPTS + 1):
try:
attempt()
return
except OSError as e:
# The command could not be started at all: no `curl`, no `bash`, no
# `powershell` on PATH. Deterministic -- a second attempt cannot
# make the binary appear -- so fail immediately rather than sleeping
# through three of them and then blaming the download.
print(f"Error: could not run the install command: {e}", file=sys.stderr)
sys.exit(1)
except subprocess.CalledProcessError as e:
if attempt_num == MAX_INSTALL_ATTEMPTS:
print(
f"Error downloading CLI after {MAX_INSTALL_ATTEMPTS} attempts: {e}",
file=sys.stderr,
)
print(f"stdout: {_decode(e.stdout)}", file=sys.stderr)
print(f"stderr: {_decode(e.stderr)}", file=sys.stderr)
sys.exit(1)
delay = 2**attempt_num
print(
f"Install attempt {attempt_num} failed (exit {e.returncode}), "
f"retrying in {delay}s...",
file=sys.stderr,
)
time.sleep(delay)
# One command to run: its argv, and the environment to run it with (None
# inherits ours).
_Command = tuple[list[str], dict[str, str] | None]
class _InstallPlan(NamedTuple):
"""How one platform downloads the installer, checks its body, and runs it."""
script_path: str
download: _Command
check: Callable[[str], None]
install: _Command
def _powershell(command: str) -> list[str]:
"""A PowerShell invocation of ``command``."""
return ["powershell", "-ExecutionPolicy", "Bypass", "-Command", command]
def _windows_plan(tmpdir: str, version: str) -> _InstallPlan:
script_path = str(Path(tmpdir) / "install.ps1")
download_env = {**os.environ, INSTALL_SCRIPT_ENV_VAR: script_path}
download_cmd = _powershell(
"$ProgressPreference = 'SilentlyContinue'; "
"Invoke-RestMethod -Uri https://claude.ai/install.ps1 "
f"-OutFile $env:{INSTALL_SCRIPT_ENV_VAR}"
)
install_env = dict(download_env)
install_command = f"& $env:{INSTALL_SCRIPT_ENV_VAR}"
if version != DEFAULT_VERSION:
install_command += f" $env:{INSTALL_VERSION_ENV_VAR}"
install_env[INSTALL_VERSION_ENV_VAR] = version
return _InstallPlan(
script_path=script_path,
download=(download_cmd, download_env),
check=check_powershell_install_script,
install=(_powershell(install_command), install_env),
)
def _unix_plan(tmpdir: str, version: str) -> _InstallPlan:
script_path = str(Path(tmpdir) / "install.sh")
# -L follows the cross-host redirect to the bootstrap script.
# --retry-all-errors covers 429 from claude.ai when multiple matrix jobs
# fetch install.sh simultaneously.
curl_cmd = [
"curl",
"-fsSL",
"--retry",
"5",
"--retry-delay",
"2",
"--retry-all-errors",
"-o",
script_path,
"https://claude.ai/install.sh",
]
bash_cmd = ["bash", script_path]
if version != DEFAULT_VERSION:
bash_cmd.append(version)
return _InstallPlan(
script_path=script_path,
download=(curl_cmd, None),
check=check_install_script,
install=(bash_cmd, None),
)
def download_cli() -> None:
"""Download Claude Code CLI using the official install script.
Both platforms download the installer to a temp file, check its body, and
only then execute it -- rather than piping the response straight into a
shell or into `iex`. The whole sequence is retried, so a truncated body is
never reused across attempts.
Both pass "latest" by passing no argument at all: it is the installer's own
default. Every other accepted value -- a concrete version, or "stable" --
goes through the argument path.
"""
version = get_cli_version()
print(f"Downloading Claude Code CLI version: {version}")
build_plan = _windows_plan if platform.system() == "Windows" else _unix_plan
with tempfile.TemporaryDirectory() as tmpdir:
plan = build_plan(tmpdir, version)
def attempt() -> None:
run_command(*plan.download)
plan.check(plan.script_path)
run_command(*plan.install)
retry_install(attempt)
def copy_cli_to_bundle() -> None:
"""Copy the installed CLI to the package _bundled directory."""
# Find project root (parent of scripts directory)
script_dir = Path(__file__).parent
project_root = script_dir.parent
bundle_dir = project_root / "src" / "claude_agent_sdk" / "_bundled"
# Ensure bundle directory exists
bundle_dir.mkdir(parents=True, exist_ok=True)
# Find installed CLI
cli_path = find_installed_cli()
if not cli_path:
print("Error: Could not find installed Claude CLI binary", file=sys.stderr)
sys.exit(1)
print(f"Found CLI at: {cli_path}")
# Determine target filename based on platform
system = platform.system()
target_name = "claude.exe" if system == "Windows" else "claude"
target_path = bundle_dir / target_name
# Copy the binary
print(f"Copying CLI to: {target_path}")
shutil.copy2(cli_path, target_path)
# Make it executable (Unix-like systems)
if system != "Windows":
target_path.chmod(0o755)
print(f"Successfully bundled CLI binary: {target_path}")
# Print size info
size_mb = target_path.stat().st_size / (1024 * 1024)
print(f"Binary size: {size_mb:.2f} MB")
def main() -> None:
"""Main entry point."""
print("=" * 60)
print("Claude Code CLI Download Script")
print("=" * 60)
# Download CLI
download_cli()
# Copy to bundle directory
copy_cli_to_bundle()
print("=" * 60)
print("CLI download and bundling complete!")
print("=" * 60)
if __name__ == "__main__":
# This script runs as a build step, so report a bad CLAUDE_CLI_VERSION the
# way every other failure here is reported -- one line on stderr, exit 1 --
# instead of letting a traceback out. The shared validator keeps raising:
# only the entry point turns it into an exit status.
try:
main()
except ValueError as exc:
print(f"Error: {exc}", file=sys.stderr)
sys.exit(1)