|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +DOCKER_UV_BINARY_CACHE_CONTEXT_DIR = "loopx_uv_cache" |
| 10 | +DOCKER_UV_BINARY_CACHE_BEGIN = "# BEGIN LOOPX_SKILLSBENCH_UV_BINARY_CACHE" |
| 11 | +DOCKER_UV_BINARY_CACHE_END = "# END LOOPX_SKILLSBENCH_UV_BINARY_CACHE" |
| 12 | +UV_BINARY_CACHE_KEYS = ( |
| 13 | + "dockerfile_uv_binary_cache_context_created", |
| 14 | + "dockerfile_uv_binary_cache_available", |
| 15 | + "dockerfile_uv_binary_cache_binary_count", |
| 16 | + "dockerfile_uv_binary_cache_has_uv", |
| 17 | + "dockerfile_uv_binary_cache_has_uvx", |
| 18 | + "dockerfile_uv_binary_cache_dockerfile_patch_applied", |
| 19 | + "dockerfile_uv_binary_cache_raw_path_recorded", |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +def empty_uv_binary_cache_metadata() -> dict[str, Any]: |
| 24 | + return { |
| 25 | + "dockerfile_uv_binary_cache_context_created": False, |
| 26 | + "dockerfile_uv_binary_cache_available": False, |
| 27 | + "dockerfile_uv_binary_cache_binary_count": 0, |
| 28 | + "dockerfile_uv_binary_cache_has_uv": False, |
| 29 | + "dockerfile_uv_binary_cache_has_uvx": False, |
| 30 | + "dockerfile_uv_binary_cache_dockerfile_patch_applied": False, |
| 31 | + "dockerfile_uv_binary_cache_raw_path_recorded": False, |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | +def dockerfile_uv_binary_cache_prelude() -> str: |
| 36 | + return ( |
| 37 | + f"COPY {DOCKER_UV_BINARY_CACHE_CONTEXT_DIR}/ /opt/loopx_uv_cache/\n" |
| 38 | + "RUN set -eux; \\\n" |
| 39 | + f" : \"{DOCKER_UV_BINARY_CACHE_BEGIN}\"; \\\n" |
| 40 | + " if [ -x /opt/loopx_uv_cache/uv ]; then install -m 0755 /opt/loopx_uv_cache/uv /usr/local/bin/uv; fi; \\\n" |
| 41 | + " if [ -x /opt/loopx_uv_cache/uvx ]; then install -m 0755 /opt/loopx_uv_cache/uvx /usr/local/bin/uvx; fi; \\\n" |
| 42 | + " if command -v uv >/dev/null 2>&1 && command -v uvx >/dev/null 2>&1 && uv --version >/dev/null 2>&1 && uvx --version >/dev/null 2>&1; then exit 0; fi; \\\n" |
| 43 | + " rm -f /usr/local/bin/uv /usr/local/bin/uvx; \\\n" |
| 44 | + f" : \"{DOCKER_UV_BINARY_CACHE_END}\"; \\\n" |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +def host_uv_binary_cache_candidates() -> dict[str, Path]: |
| 49 | + """Return host uv binaries that are safe to copy into Linux Docker images.""" |
| 50 | + |
| 51 | + if not sys.platform.startswith("linux"): |
| 52 | + return {} |
| 53 | + machine = os.uname().machine.lower() |
| 54 | + if machine not in {"x86_64", "amd64"}: |
| 55 | + return {} |
| 56 | + candidates: dict[str, Path] = {} |
| 57 | + for name in ("uv", "uvx"): |
| 58 | + raw = shutil.which(name) |
| 59 | + if not raw: |
| 60 | + continue |
| 61 | + path = Path(raw) |
| 62 | + if path.is_file() and os.access(path, os.X_OK): |
| 63 | + candidates[name] = path |
| 64 | + return candidates |
| 65 | + |
| 66 | + |
| 67 | +def stage_uv_binary_cache_context(environment_dir: Path) -> dict[str, Any]: |
| 68 | + """Stage optional host uv/uvx binaries into the Docker build context.""" |
| 69 | + |
| 70 | + cache_dir = environment_dir / DOCKER_UV_BINARY_CACHE_CONTEXT_DIR |
| 71 | + if cache_dir.exists(): |
| 72 | + shutil.rmtree(cache_dir) |
| 73 | + cache_dir.mkdir(parents=True, exist_ok=True) |
| 74 | + (cache_dir / ".loopx_keep").write_text( |
| 75 | + "Optional LoopX uv binary cache for SkillsBench Docker bootstrap.\n", |
| 76 | + encoding="utf-8", |
| 77 | + ) |
| 78 | + copied: list[str] = [] |
| 79 | + for name, source in host_uv_binary_cache_candidates().items(): |
| 80 | + target = cache_dir / name |
| 81 | + shutil.copy2(source, target) |
| 82 | + target.chmod(0o755) |
| 83 | + copied.append(name) |
| 84 | + return { |
| 85 | + "dockerfile_uv_binary_cache_context_created": True, |
| 86 | + "dockerfile_uv_binary_cache_available": bool(copied), |
| 87 | + "dockerfile_uv_binary_cache_binary_count": len(copied), |
| 88 | + "dockerfile_uv_binary_cache_has_uv": "uv" in copied, |
| 89 | + "dockerfile_uv_binary_cache_has_uvx": "uvx" in copied, |
| 90 | + "dockerfile_uv_binary_cache_raw_path_recorded": False, |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | +def discover_uv_binary_cache_metadata( |
| 95 | + prepared_task: Path, |
| 96 | + dockerfile_text: str, |
| 97 | +) -> dict[str, Any]: |
| 98 | + cache_dir = prepared_task / "environment" / DOCKER_UV_BINARY_CACHE_CONTEXT_DIR |
| 99 | + has_uv = (cache_dir / "uv").exists() |
| 100 | + has_uvx = (cache_dir / "uvx").exists() |
| 101 | + return { |
| 102 | + "dockerfile_uv_binary_cache_context_created": cache_dir.exists(), |
| 103 | + "dockerfile_uv_binary_cache_available": has_uv or has_uvx, |
| 104 | + "dockerfile_uv_binary_cache_binary_count": int(has_uv) + int(has_uvx), |
| 105 | + "dockerfile_uv_binary_cache_has_uv": has_uv, |
| 106 | + "dockerfile_uv_binary_cache_has_uvx": has_uvx, |
| 107 | + "dockerfile_uv_binary_cache_dockerfile_patch_applied": ( |
| 108 | + DOCKER_UV_BINARY_CACHE_BEGIN in dockerfile_text |
| 109 | + ), |
| 110 | + "dockerfile_uv_binary_cache_raw_path_recorded": False, |
| 111 | + } |
0 commit comments