|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Download the stagehand-server binary for local development. |
| 4 | +
|
| 5 | +This script downloads the appropriate binary for your platform from GitHub releases |
| 6 | +and places it in bin/sea/ for use during development and testing. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python scripts/download-binary.py [--version VERSION] |
| 10 | +
|
| 11 | +Examples: |
| 12 | + python scripts/download-binary.py |
| 13 | + python scripts/download-binary.py --version v3.2.0 |
| 14 | +""" |
| 15 | + |
| 16 | +import sys |
| 17 | +import platform |
| 18 | +import argparse |
| 19 | +import urllib.request |
| 20 | +from pathlib import Path |
| 21 | + |
| 22 | + |
| 23 | +def get_platform_info() -> tuple[str, str]: |
| 24 | + """Determine platform and architecture.""" |
| 25 | + system = platform.system().lower() |
| 26 | + machine = platform.machine().lower() |
| 27 | + |
| 28 | + if system == "darwin": |
| 29 | + plat = "darwin" |
| 30 | + elif system == "windows": |
| 31 | + plat = "win32" |
| 32 | + else: |
| 33 | + plat = "linux" |
| 34 | + |
| 35 | + arch = "arm64" if machine in ("arm64", "aarch64") else "x64" |
| 36 | + return plat, arch |
| 37 | + |
| 38 | + |
| 39 | +def get_binary_filename(plat: str, arch: str) -> str: |
| 40 | + """Get the expected binary filename for this platform.""" |
| 41 | + name = f"stagehand-server-{plat}-{arch}" |
| 42 | + return name + (".exe" if plat == "win32" else "") |
| 43 | + |
| 44 | + |
| 45 | +def get_local_filename(plat: str, arch: str) -> str: |
| 46 | + """Get the local filename (what the code expects to find).""" |
| 47 | + name = f"stagehand-{plat}-{arch}" |
| 48 | + return name + (".exe" if plat == "win32" else "") |
| 49 | + |
| 50 | + |
| 51 | +def download_binary(version: str) -> None: |
| 52 | + """Download the binary for the current platform.""" |
| 53 | + plat, arch = get_platform_info() |
| 54 | + binary_filename = get_binary_filename(plat, arch) |
| 55 | + local_filename = get_local_filename(plat, arch) |
| 56 | + |
| 57 | + # GitHub release URL |
| 58 | + repo = "browserbase/stagehand" |
| 59 | + tag = version if version.startswith("stagehand-server/v") else f"stagehand-server/{version}" |
| 60 | + url = f"https://github.com/{repo}/releases/download/{tag}/{binary_filename}" |
| 61 | + |
| 62 | + # Destination path |
| 63 | + repo_root = Path(__file__).parent.parent |
| 64 | + dest_dir = repo_root / "bin" / "sea" |
| 65 | + dest_dir.mkdir(parents=True, exist_ok=True) |
| 66 | + dest_path = dest_dir / local_filename |
| 67 | + |
| 68 | + if dest_path.exists(): |
| 69 | + print(f"✓ Binary already exists: {dest_path}") |
| 70 | + response = input(" Overwrite? [y/N]: ").strip().lower() |
| 71 | + if response != "y": |
| 72 | + print(" Skipping download.") |
| 73 | + return |
| 74 | + |
| 75 | + print(f"📦 Downloading binary for {plat}-{arch}...") |
| 76 | + print(f" From: {url}") |
| 77 | + print(f" To: {dest_path}") |
| 78 | + |
| 79 | + try: |
| 80 | + # Download with progress |
| 81 | + def reporthook(block_num, block_size, total_size): |
| 82 | + downloaded = block_num * block_size |
| 83 | + if total_size > 0: |
| 84 | + percent = min(downloaded * 100 / total_size, 100) |
| 85 | + mb_downloaded = downloaded / (1024 * 1024) |
| 86 | + mb_total = total_size / (1024 * 1024) |
| 87 | + print(f"\r Progress: {percent:.1f}% ({mb_downloaded:.1f}/{mb_total:.1f} MB)", end="") |
| 88 | + |
| 89 | + urllib.request.urlretrieve(url, dest_path, reporthook) |
| 90 | + print() # New line after progress |
| 91 | + |
| 92 | + # Make executable on Unix |
| 93 | + if plat != "win32": |
| 94 | + import os |
| 95 | + os.chmod(dest_path, 0o755) |
| 96 | + |
| 97 | + size_mb = dest_path.stat().st_size / (1024 * 1024) |
| 98 | + print(f"✅ Downloaded successfully: {dest_path} ({size_mb:.1f} MB)") |
| 99 | + print(f"\n💡 You can now run: uv run python test_local_mode.py") |
| 100 | + |
| 101 | + except urllib.error.HTTPError as e: |
| 102 | + print(f"\n❌ Error: Failed to download binary (HTTP {e.code})") |
| 103 | + print(f" URL: {url}") |
| 104 | + print(f"\n Available releases at: https://github.com/{repo}/releases") |
| 105 | + sys.exit(1) |
| 106 | + except Exception as e: |
| 107 | + print(f"\n❌ Error: {e}") |
| 108 | + sys.exit(1) |
| 109 | + |
| 110 | + |
| 111 | +def main() -> None: |
| 112 | + parser = argparse.ArgumentParser( |
| 113 | + description="Download stagehand-server binary for local development", |
| 114 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 115 | + epilog=""" |
| 116 | +Examples: |
| 117 | + python scripts/download-binary.py |
| 118 | + python scripts/download-binary.py --version v3.2.0 |
| 119 | + python scripts/download-binary.py --version stagehand-server/v3.2.0 |
| 120 | + """, |
| 121 | + ) |
| 122 | + parser.add_argument( |
| 123 | + "--version", |
| 124 | + default="v3.2.0", |
| 125 | + help="Version to download (default: v3.2.0)", |
| 126 | + ) |
| 127 | + |
| 128 | + args = parser.parse_args() |
| 129 | + download_binary(args.version) |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + main() |
0 commit comments