|
| 1 | +#!/usr/bin/env spack-python |
| 2 | +# |
| 3 | +# Run this script in an active, concretized Spack environment to fetch Go |
| 4 | +# dependencies and store them in $GOMODCACHE. You must either run it with |
| 5 | +# 'spack-python' or have 'spack-python' in your $PATH. Ensure $GOMODCACHE has |
| 6 | +# the same value when 'spack install' is run. |
| 7 | +# |
| 8 | +# For each spec that is a GoPackage, it will attempt to use that spec's 'go' |
| 9 | +# dependency to execute '', but will fall back to searching $PATH if that |
| 10 | +# dependency has not already been installed. |
| 11 | +# |
| 12 | +# Alex Richert, Apr 2025 |
| 13 | +# |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +from spack.environment import active_environment |
| 18 | +from spack.build_systems.go import GoPackage |
| 19 | +from spack.package_base import PackageBase |
| 20 | +from spack.util.executable import Executable, which |
| 21 | +from llnl.util.filesystem import working_dir |
| 22 | +from spack.store import find |
| 23 | +from spack.error import SpackError |
| 24 | + |
| 25 | +# Load the current environment |
| 26 | +env = active_environment() |
| 27 | +if not env: |
| 28 | + raise SpackError("No active Spack environment") |
| 29 | + |
| 30 | +gomodcache = os.getenv("GOMODCACHE") |
| 31 | +if not gomodcache: |
| 32 | + raise SpackError("GOMODCACHE must be set") |
| 33 | + |
| 34 | +# Find each spec that is a GoPackage |
| 35 | +# and fetch its dependencies to $GOMODCACHE |
| 36 | +for spec in env.all_specs(): |
| 37 | + if not spec.concrete: |
| 38 | + continue |
| 39 | + pkg_cls = spec.package.__class__ |
| 40 | + if issubclass(pkg_cls, GoPackage): |
| 41 | + print(f"Found spec with GoPackage: {spec.name}@{spec.version}/{spec.dag_hash()}") |
| 42 | + else: |
| 43 | + continue |
| 44 | + pkg = spec.package |
| 45 | + pkg.do_stage() |
| 46 | + |
| 47 | + dep_go_path = os.path.join(spec["go"].prefix.bin, "go") |
| 48 | + if which(dep_go_path): |
| 49 | + go_exe = Executable(dep_go_path) |
| 50 | + elif which("go"): |
| 51 | + go_exe = Executable("go") |
| 52 | + else: |
| 53 | + raise SpackError("Could not find 'go' executable") |
| 54 | + |
| 55 | + with working_dir(pkg.stage.source_path): |
| 56 | + go_exe("mod", "download") |
0 commit comments