chopratejas--headroom
0ef5fcb1c5
Security / Dependency audit (pip-audit) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Security / Secret scan (gitleaks) (push) Has been cancelled
rust / test (ubuntu) (push) Has been cancelled
rust / simulator e2e (macos-latest) (push) Has been cancelled
rust / simulator e2e (ubuntu-latest) (push) Has been cancelled
rust / simulator e2e (windows-latest) (push) Has been cancelled
rust / wheels (aarch64-apple-darwin) (push) Has been cancelled
rust / wheels (x86_64-unknown-linux-gnu) (push) Has been cancelled
rust / wheels (x86_64-apple-darwin) (push) Has been cancelled
rust / audit (push) Has been cancelled
rust / parity (nightly, allowed to fail during Phase 0) (push) Has been cancelled
CI / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 0s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 0s
Dev Containers / validate-worktree (push) Failing after 0s
CI / changes (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Deploy Documentation / deploy (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, claude) (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, codex) (push) Failing after 1s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
OpenCode Plugin / typecheck + build + test (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 1s
Release Please / release-please (push) Failing after 1s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 1s
Init E2E / docker-init-e2e (push) Failing after 4s
Merge Conflicts / merge-conflicts (push) Failing after 4s
CI / lint (push) Has been cancelled
CI / build-wheel (push) Has been cancelled
CI / build-wheel-windows (push) Has been cancelled
CI / prefetch-model (push) Has been cancelled
CI / test-dashboard-ui (push) Has been cancelled
CI / test (1) (push) Has been cancelled
CI / test (2) (push) Has been cancelled
CI / test (3) (push) Has been cancelled
CI / test (4) (push) Has been cancelled
CI / test-extras (push) Has been cancelled
CI / test-agno (push) Has been cancelled
CI / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / promote-latest (push) Has been cancelled
Init Native E2E / init-native (macos-latest, claude) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, codex) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, copilot) (push) Has been cancelled
Install Native E2E / install-native (macos-latest) (push) Has been cancelled
Wrap Native E2E / wrap-native (macos-latest) (push) Has been cancelled
180 行
6.1 KiB
Python
180 行
6.1 KiB
Python
"""Download and install lean-ctx binary from GitHub releases."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import logging
|
|
import os
|
|
import platform
|
|
import stat
|
|
import subprocess
|
|
import tarfile
|
|
import zipfile
|
|
from pathlib import Path
|
|
from urllib.request import urlopen
|
|
|
|
from headroom._subprocess import run
|
|
|
|
from . import LEAN_CTX_BIN_DIR, LEAN_CTX_VERSION
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
GITHUB_RELEASE_URL = "https://github.com/yvgude/lean-ctx/releases/download"
|
|
|
|
|
|
def _detect_runtime_target_triple() -> str:
|
|
"""Detect platform and return the lean-ctx release target triple."""
|
|
system = platform.system()
|
|
machine = platform.machine()
|
|
|
|
if system == "Darwin":
|
|
arch = "aarch64" if machine == "arm64" else "x86_64"
|
|
return f"{arch}-apple-darwin"
|
|
if system == "Linux":
|
|
arch = "aarch64" if machine == "aarch64" else "x86_64"
|
|
suffix = "unknown-linux-musl" if _is_musl() else "unknown-linux-gnu"
|
|
return f"{arch}-{suffix}"
|
|
if system == "Windows":
|
|
return "x86_64-pc-windows-msvc"
|
|
|
|
raise RuntimeError(f"Unsupported platform: {system} {machine}")
|
|
|
|
|
|
def _is_musl() -> bool:
|
|
try:
|
|
result = run(
|
|
["ldd", "--version"],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=2,
|
|
check=False,
|
|
)
|
|
return "musl" in (result.stdout + result.stderr).lower()
|
|
except Exception:
|
|
return False
|
|
|
|
|
|
def _get_target_triple() -> str:
|
|
"""Return the requested lean-ctx target triple, honoring explicit overrides."""
|
|
return _get_explicit_target_triple() or _detect_runtime_target_triple()
|
|
|
|
|
|
def _get_explicit_target_triple() -> str:
|
|
"""Return the explicitly requested lean-ctx target triple, if any."""
|
|
return (
|
|
os.environ.get("HEADROOM_LEAN_CTX_TARGET", "").strip()
|
|
or os.environ.get("LEAN_CTX_TARGET", "").strip()
|
|
)
|
|
|
|
|
|
def _binary_name_for_target(target: str) -> str:
|
|
"""Return the expected binary name for a target triple."""
|
|
return "lean-ctx.exe" if "windows" in target else "lean-ctx"
|
|
|
|
|
|
def _should_verify_target(target: str) -> bool:
|
|
"""Verify runtime-detected targets; explicit overrides may be cross-target."""
|
|
if _get_explicit_target_triple():
|
|
return False
|
|
return target == _detect_runtime_target_triple()
|
|
|
|
|
|
def _get_download_url(version: str) -> tuple[str, str]:
|
|
"""Get download URL and extension for this platform."""
|
|
target = _get_target_triple()
|
|
ext = "zip" if "windows" in target else "tar.gz"
|
|
url = f"{GITHUB_RELEASE_URL}/{version}/lean-ctx-{target}.{ext}"
|
|
return url, ext
|
|
|
|
|
|
def download_lean_ctx(version: str | None = None) -> Path:
|
|
"""Download lean-ctx binary from GitHub releases."""
|
|
version = version or LEAN_CTX_VERSION
|
|
target = _get_target_triple()
|
|
url, ext = _get_download_url(version)
|
|
target_path = LEAN_CTX_BIN_DIR / _binary_name_for_target(target)
|
|
|
|
LEAN_CTX_BIN_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
logger.info("Downloading lean-ctx %s from %s ...", version, url)
|
|
|
|
try:
|
|
if not url.startswith(("http://", "https://")):
|
|
raise ValueError(f"Invalid URL scheme in {url}")
|
|
try:
|
|
with urlopen(url, timeout=30) as response:
|
|
data = response.read()
|
|
except Exception as download_err:
|
|
if "CERTIFICATE_VERIFY_FAILED" in str(download_err):
|
|
raise RuntimeError(
|
|
"TLS verification failed downloading lean-ctx; "
|
|
"fix the local trust store and retry."
|
|
) from download_err
|
|
raise
|
|
except Exception as e:
|
|
raise RuntimeError(f"Failed to download lean-ctx from {url}: {e}") from e
|
|
|
|
try:
|
|
if ext == "tar.gz":
|
|
with tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") as tar:
|
|
for member in tar.getmembers():
|
|
if member.name.endswith("/lean-ctx") or member.name == "lean-ctx":
|
|
member.name = target_path.name
|
|
tar.extract(member, LEAN_CTX_BIN_DIR)
|
|
break
|
|
else:
|
|
raise RuntimeError("lean-ctx binary not found in archive")
|
|
elif ext == "zip":
|
|
with zipfile.ZipFile(io.BytesIO(data)) as zf:
|
|
for name in zf.namelist():
|
|
if name.endswith("lean-ctx.exe") or name.endswith("/lean-ctx"):
|
|
with zf.open(name) as src, open(target_path, "wb") as dst:
|
|
dst.write(src.read())
|
|
break
|
|
else:
|
|
raise RuntimeError("lean-ctx binary not found in archive")
|
|
except (tarfile.TarError, zipfile.BadZipFile) as e:
|
|
raise RuntimeError(f"Failed to extract lean-ctx archive: {e}") from e
|
|
|
|
if "windows" not in target:
|
|
target_path.chmod(target_path.stat().st_mode | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH)
|
|
|
|
if _should_verify_target(target):
|
|
try:
|
|
result = run(
|
|
[str(target_path), "--version"],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=5,
|
|
)
|
|
if result.returncode != 0:
|
|
raise RuntimeError(f"lean-ctx verification failed: {result.stderr}")
|
|
logger.info("lean-ctx installed: %s", result.stdout.strip())
|
|
except FileNotFoundError as e:
|
|
raise RuntimeError("lean-ctx binary not found after extraction") from e
|
|
except subprocess.TimeoutExpired as e:
|
|
raise RuntimeError("lean-ctx verification timed out") from e
|
|
else:
|
|
logger.info(
|
|
"lean-ctx installed for target %s at %s (verification skipped)",
|
|
target,
|
|
target_path,
|
|
)
|
|
|
|
return target_path
|
|
|
|
|
|
def ensure_lean_ctx(version: str | None = None) -> Path | None:
|
|
"""Ensure lean-ctx is installed — download if needed."""
|
|
from . import get_lean_ctx_path
|
|
|
|
existing = get_lean_ctx_path()
|
|
if existing:
|
|
return existing
|
|
|
|
try:
|
|
return download_lean_ctx(version)
|
|
except RuntimeError as e:
|
|
logger.warning("Could not install lean-ctx: %s", e)
|
|
return None
|