|
| 1 | +#!/usr/bin/env spack-python |
| 2 | +"""Produce a bootstrap json file for a selected package""" |
| 3 | + |
| 4 | +import glob |
| 5 | +import hashlib |
| 6 | +import json |
| 7 | +import os |
| 8 | +import sys |
| 9 | + |
| 10 | +import spack.deptypes as dt |
| 11 | +import spack.spec |
| 12 | +import spack.traverse |
| 13 | + |
| 14 | +# Each entry in clingo.json has the following keys: |
| 15 | +# |
| 16 | +# "spec": root spec to be matched |
| 17 | +# "binaries": list of tuples (pkg name, dag hash, sha256 sum) |
| 18 | + |
| 19 | + |
| 20 | +def sha256(path): |
| 21 | + fn = hashlib.sha256() |
| 22 | + with open(path, "rb") as f: |
| 23 | + fn.update(f.read()) |
| 24 | + return fn.hexdigest() |
| 25 | + |
| 26 | + |
| 27 | +def tarball_hash(path: str): |
| 28 | + # extract hash from /path/to/<...>-<hash>.spack |
| 29 | + name, _ = os.path.splitext(os.path.basename(path)) |
| 30 | + return name.split("-")[-1] |
| 31 | + |
| 32 | + |
| 33 | +def run(pkg: str, deps=dt.NONE, python: bool = False): |
| 34 | + name = "clingo-bootstrap" if pkg == "clingo" else pkg |
| 35 | + shas = { |
| 36 | + tarball_hash(tarball): sha256(tarball) |
| 37 | + for tarball in glob.glob("./build_cache/**/*.spack", recursive=True) |
| 38 | + } |
| 39 | + |
| 40 | + specs = [ |
| 41 | + spack.spec.Spec.from_specfile(f) for f in glob.glob("./build_cache/*.json") if name in f |
| 42 | + ] |
| 43 | + |
| 44 | + assert len(specs) > 0, f"No specs found for {name}" |
| 45 | + |
| 46 | + fmt = "{name}{@version}{%compiler.name} platform={platform} target={target}" |
| 47 | + |
| 48 | + if python: |
| 49 | + fmt_spec = lambda s: f"{s.format(fmt)} ^python@{s.dependencies('python')[0].version}" |
| 50 | + else: |
| 51 | + fmt_spec = lambda s: s.format(fmt) |
| 52 | + |
| 53 | + mirror_info = [ |
| 54 | + { |
| 55 | + "spec": fmt_spec(s), |
| 56 | + "binaries": [ |
| 57 | + (s.name, s.dag_hash(), shas[s.dag_hash()]) |
| 58 | + for s in reversed(list(s.traverse(order="topo", deptype=deps))) |
| 59 | + if not s.external |
| 60 | + ], |
| 61 | + } |
| 62 | + for s in specs |
| 63 | + ] |
| 64 | + |
| 65 | + # sort as strings, cause Spec instances with deps don't sort properly |
| 66 | + mirror_info.sort(key=lambda x: x["spec"]) |
| 67 | + |
| 68 | + with open(f"./{pkg}.json", "w") as f: |
| 69 | + json.dump({"verified": mirror_info}, f, sort_keys=True, indent=2) |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + assert len(sys.argv) == 2, f"Usage: {sys.argv[0]} <clingo|gnupg|patchelf>" |
| 74 | + pkg = sys.argv[1] |
| 75 | + # unfortunately we refer to clingo-bootstrap by alias "clingo" |
| 76 | + assert pkg in ("clingo", "gnupg", "patchelf") |
| 77 | + run( |
| 78 | + pkg, |
| 79 | + # clingo is special: statically links libstdc++ and other deps are loaded by interpreter |
| 80 | + deps=dt.NONE if pkg == "clingo" else dt.LINK | dt.RUN, |
| 81 | + python=pkg == "clingo", |
| 82 | + ) |
0 commit comments