|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import pathlib |
| 3 | +from typing import List |
| 4 | + |
| 5 | +import yaml |
| 6 | + |
| 7 | + |
| 8 | +def read_env_file( |
| 9 | + env_file: pathlib.Path, |
| 10 | + fallback_name: str, |
| 11 | + fallback_version: str, |
| 12 | + fallback_platform: str, |
| 13 | + fallback_channels: List[str], |
| 14 | +) -> dict: |
| 15 | + with env_file.open("r") as f: |
| 16 | + env_dict = yaml.safe_load(f) |
| 17 | + |
| 18 | + env_dict.setdefault("name", fallback_name) |
| 19 | + env_dict.setdefault("version", fallback_version) |
| 20 | + env_dict.setdefault("platform", fallback_platform) |
| 21 | + env_dict.setdefault("channels", fallback_channels) |
| 22 | + |
| 23 | + return env_dict |
| 24 | + |
| 25 | + |
| 26 | +def get_conda_metapackage_cmdline( |
| 27 | + env_dict: dict, home: str, license_id: str, summary: str |
| 28 | +): |
| 29 | + cmdline = [ |
| 30 | + "conda", |
| 31 | + "metapackage", |
| 32 | + env_dict["name"], |
| 33 | + env_dict["version"], |
| 34 | + "--no-anaconda-upload", |
| 35 | + "--home", |
| 36 | + home, |
| 37 | + "--license", |
| 38 | + license_id, |
| 39 | + "--summary", |
| 40 | + summary, |
| 41 | + ] |
| 42 | + for channel in env_dict["channels"]: |
| 43 | + cmdline.extend(["--channel", channel]) |
| 44 | + cmdline.extend(["--dependencies"] + env_dict["dependencies"]) |
| 45 | + |
| 46 | + return cmdline |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + import argparse |
| 51 | + import os |
| 52 | + import subprocess |
| 53 | + import shutil |
| 54 | + import sys |
| 55 | + |
| 56 | + import conda_build.config |
| 57 | + |
| 58 | + conda_build_config = conda_build.config.Config() |
| 59 | + |
| 60 | + cwd = pathlib.Path(".").absolute() |
| 61 | + here = pathlib.Path(__file__).parent.absolute().relative_to(cwd) |
| 62 | + distname = os.getenv("DISTNAME", "radioconda") |
| 63 | + platform = os.getenv("PLATFORM", conda_build_config.subdir) |
| 64 | + source = "/".join( |
| 65 | + ( |
| 66 | + os.getenv("GITHUB_SERVER_URL", "https://github.com"), |
| 67 | + os.getenv("GITHUB_REPOSITORY", "ryanvolz/radioconda"), |
| 68 | + ) |
| 69 | + ) |
| 70 | + license_id = os.getenv("LICENSE_ID", "BSD-3-Clause") |
| 71 | + summary = os.getenv("METAPACKAGE_SUMMARY", f"Metapackage for {distname}.") |
| 72 | + |
| 73 | + parser = argparse.ArgumentParser( |
| 74 | + description=( |
| 75 | + "Build environment metapackage using conda-build." |
| 76 | + " Additional command-line options will be passed to conda metapackage." |
| 77 | + ) |
| 78 | + ) |
| 79 | + parser.add_argument( |
| 80 | + "env_file", |
| 81 | + type=pathlib.Path, |
| 82 | + nargs="?", |
| 83 | + default=here / "installer_specs" / f"{distname}-{platform}.yml", |
| 84 | + help=( |
| 85 | + "Environment yaml file for a particular platform" |
| 86 | + " (name ends in the platform identifier)." |
| 87 | + " (default: %(default)s)" |
| 88 | + ), |
| 89 | + ) |
| 90 | + parser.add_argument( |
| 91 | + "-o", |
| 92 | + "--output_dir", |
| 93 | + type=pathlib.Path, |
| 94 | + default=here / "dist" / "conda-bld", |
| 95 | + help=( |
| 96 | + "Output directory in which the metapackage will be placed." |
| 97 | + " (default: %(default)s)" |
| 98 | + ), |
| 99 | + ) |
| 100 | + parser.add_argument( |
| 101 | + "--home", |
| 102 | + default=source, |
| 103 | + help="The homepage for the metapackage. (default: %(default)s)", |
| 104 | + ) |
| 105 | + parser.add_argument( |
| 106 | + "--license", |
| 107 | + default=license_id, |
| 108 | + help="The SPDX license identifier for the metapackage. (default: %(default)s)", |
| 109 | + ) |
| 110 | + parser.add_argument( |
| 111 | + "--summary", |
| 112 | + default=summary, |
| 113 | + help="Summary of the package. (default: %(default)s)", |
| 114 | + ) |
| 115 | + |
| 116 | + args, metapackage_args = parser.parse_known_args() |
| 117 | + |
| 118 | + env_dict = read_env_file( |
| 119 | + args.env_file, |
| 120 | + fallback_name=distname, |
| 121 | + fallback_version="0", |
| 122 | + fallback_platform=platform, |
| 123 | + fallback_channels=["conda-forge"], |
| 124 | + ) |
| 125 | + |
| 126 | + cmdline = get_conda_metapackage_cmdline( |
| 127 | + env_dict=env_dict, home=args.home, license_id=args.license, summary=args.summary |
| 128 | + ) |
| 129 | + cmdline.extend(metapackage_args) |
| 130 | + |
| 131 | + env = os.environ.copy() |
| 132 | + env["CONDA_SUBDIR"] = env_dict["platform"] |
| 133 | + |
| 134 | + proc = subprocess.run(cmdline, env=env) |
| 135 | + |
| 136 | + try: |
| 137 | + proc.check_returncode() |
| 138 | + except subprocess.CalledProcessError: |
| 139 | + sys.exit(1) |
| 140 | + |
| 141 | + bldpkgs_dir = pathlib.Path(conda_build_config.croot) / env_dict["platform"] |
| 142 | + pkg_paths = list(bldpkgs_dir.glob(f"{env_dict['name']}-{env_dict['version']}*.bz2")) |
| 143 | + pkg_out_dir = args.output_dir / env_dict["platform"] |
| 144 | + pkg_out_dir.mkdir(parents=True, exist_ok=True) |
| 145 | + |
| 146 | + for pkg in pkg_paths: |
| 147 | + shutil.copy(pkg, pkg_out_dir) |
0 commit comments