|
| 1 | +""" |
| 2 | +Helper script for installing `codeql_pack` targets. |
| 3 | +
|
| 4 | +This mainly wraps around a `pkg_install` script from `rules_pkg` adding: |
| 5 | +* resolving destination directory with respect to a provided `--build-file` |
| 6 | +* clean-up of target destination directory before a reinstall |
| 7 | +* installing imported zip files using a provided `--ripunzip` |
| 8 | +""" |
| 9 | + |
| 10 | +import argparse |
| 11 | +import pathlib |
| 12 | +import shutil |
| 13 | +import subprocess |
| 14 | +from python.runfiles import runfiles |
| 15 | + |
| 16 | +runfiles = runfiles.Create() |
| 17 | +assert runfiles, "Installer should be run with `bazel run`" |
| 18 | + |
| 19 | +parser = argparse.ArgumentParser(description=__doc__) |
| 20 | +parser.add_argument("--destdir", type=pathlib.Path, required=True, |
| 21 | + help="Desination directory, relative to `--build-file`") |
| 22 | +parser.add_argument("--pkg-install-script", required=True, |
| 23 | + help="The wrapped `pkg_install` installation script rlocation") |
| 24 | +parser.add_argument("--build-file", required=True, |
| 25 | + help="BUILD.bazel rlocation relative to which the installation should take place") |
| 26 | +parser.add_argument("--ripunzip", |
| 27 | + help="ripunzip executable rlocation. Must be provided if `--zip-manifest` is.") |
| 28 | +parser.add_argument("--zip-manifest", |
| 29 | + help="The rlocation of a file containing newline-separated `prefix:zip_file` entries") |
| 30 | +parser.add_argument("--cleanup", action=argparse.BooleanOptionalAction, default=True, |
| 31 | + help="Whether to wipe the destination directory before installing (true by default)") |
| 32 | +opts = parser.parse_args() |
| 33 | +if opts.zip_manifest and not opts.ripunzip: |
| 34 | + parser.error("Provide `--ripunzip` when specifying `--zip-manifest`") |
| 35 | + |
| 36 | +build_file = runfiles.Rlocation(opts.build_file) |
| 37 | +script = runfiles.Rlocation(opts.pkg_install_script) |
| 38 | +destdir = pathlib.Path(build_file).resolve().parent / opts.destdir |
| 39 | + |
| 40 | +if destdir.exists() and opts.cleanup: |
| 41 | + shutil.rmtree(destdir) |
| 42 | + |
| 43 | +destdir.mkdir(parents=True, exist_ok=True) |
| 44 | +subprocess.run([script, "--destdir", destdir], check=True) |
| 45 | + |
| 46 | +if opts.zip_manifest: |
| 47 | + ripunzip = runfiles.Rlocation(opts.ripunzip) |
| 48 | + zip_manifest = runfiles.Rlocation(opts.zip_manifest) |
| 49 | + with open(zip_manifest) as manifest: |
| 50 | + for line in manifest: |
| 51 | + prefix, _, zip = line.partition(":") |
| 52 | + assert zip, f"missing prefix for {prefix}, you should use prefix:zip format" |
| 53 | + dest = destdir / prefix |
| 54 | + dest.mkdir(parents=True, exist_ok=True) |
| 55 | + subprocess.run([ripunzip, "unzip-file", zip, "-d", dest], check=True) |
0 commit comments