|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +For now we'll run most with shell=True so that we can move away from this quickly if necessary |
| 4 | +""" |
| 5 | + |
| 6 | +import argparse |
| 7 | +import os |
| 8 | +import platform |
| 9 | +from typing import List |
| 10 | +import sys |
| 11 | +import logging |
| 12 | +import subprocess |
| 13 | + |
| 14 | +LOGLEVEL = os.environ.get("LOGLEVEL", "INFO").upper() |
| 15 | +logging.basicConfig(level=LOGLEVEL) |
| 16 | +log = logging.getLogger(__name__) |
| 17 | + |
| 18 | +CIBUILDWHEEL_VERSION = "2.16.2" |
| 19 | +CIBUILDWHEEL_PLATFORM = "auto" |
| 20 | + |
| 21 | + |
| 22 | +def parseargs(args: list[str]) -> argparse.Namespace: |
| 23 | + parser = argparse.ArgumentParser() |
| 24 | + parser.add_argument( |
| 25 | + "--mode", |
| 26 | + choices=["install_ci_deps", "cicd_dpf_ssw_aligner_rs", "cicd_dpf_mrcfile"], |
| 27 | + help="mode to run", |
| 28 | + required=True, |
| 29 | + ) |
| 30 | + parser.add_argument( |
| 31 | + "--cibw_platform", |
| 32 | + choices=["auto", "linux", "macos", "windows"], |
| 33 | + help="cibuildwheel platform to build", |
| 34 | + default=CIBUILDWHEEL_PLATFORM, |
| 35 | + ) |
| 36 | + return parser.parse_args(args) |
| 37 | + |
| 38 | + |
| 39 | +def run_l(cmd: list[str]) -> None: |
| 40 | + str_cmd = " ".join(cmd) |
| 41 | + log.debug(f"Running: {str_cmd}") |
| 42 | + ret = subprocess.run(cmd) |
| 43 | + if ret.returncode: |
| 44 | + raise RuntimeError(f"Failure with command: {str_cmd}") |
| 45 | + |
| 46 | + |
| 47 | +def run_s(cmd: str, env_add: dict[str, str]) -> None: |
| 48 | + log.debug(f"Running: {cmd}") |
| 49 | + env = dict(os.environ) |
| 50 | + env.update(env_add) |
| 51 | + ret = subprocess.run(cmd, env=env, shell=True) |
| 52 | + if ret.returncode: |
| 53 | + raise RuntimeError(f"Failure with command: {cmd}") |
| 54 | + |
| 55 | + |
| 56 | +def install_rust_cibw_command(): |
| 57 | + return ( |
| 58 | + "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs" |
| 59 | + " | sh -s -- --default-toolchain none -y" |
| 60 | + " && rustup toolchain install nightly --allow-downgrade --profile minimal --component clippy" |
| 61 | + " && rustup show" |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +def install_ci_deps(): |
| 66 | + cmds = f""" |
| 67 | +python -m pip install cibuildwheel=={CIBUILDWHEEL_VERSION} |
| 68 | +curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain none -y |
| 69 | +rustup toolchain install nightly --allow-downgrade --profile minimal --component clippy |
| 70 | +rustup show |
| 71 | +pip install pipx |
| 72 | +""" |
| 73 | + match platform.system(): |
| 74 | + case "Darwin": |
| 75 | + cmds += "rustup target add aarch64-apple-darwin x86_64-apple-darwin" |
| 76 | + case "Windows": |
| 77 | + cmds += "rustup target add x86_64-pc-windows-msvc i686-pc-windows-msvc" |
| 78 | + for cmd in cmds.split("\n"): |
| 79 | + if cmd: |
| 80 | + run_s(cmd, {}) |
| 81 | + |
| 82 | + |
| 83 | +def cicd_dpf_ssw_aligner_rs(): |
| 84 | + env = { |
| 85 | + "CIBW_PLATFORM": CIBUILDWHEEL_PLATFORM, |
| 86 | + "CIBW_ARCHS": "auto", |
| 87 | + "CIBW_ENVIRONMENT": 'PATH="$HOME/.cargo/bin:$PATH" CARGO_TERM_COLOR="always"', |
| 88 | + "CIBW_ENVIRONMENT_WINDOWS": 'PATH="$UserProfile\\.cargo\\bin;$PATH"', |
| 89 | + "CIBW_BEFORE_BUILD": install_rust_cibw_command(), |
| 90 | + "CIBW_BUILD_VERBOSITY": "1", |
| 91 | + } |
| 92 | + |
| 93 | + cmds = """ |
| 94 | +python -m cibuildwheel ./src/python/dpf-ssw-aligner-rs --output-dir wheelhouse |
| 95 | +ls wheelhouse |
| 96 | +pipx run build --sdist --outdir wheelhouse ./src/python/dpf-ssw-aligner-rs |
| 97 | +ls wheelhouse |
| 98 | + """ |
| 99 | + for cmd in cmds.split("\n"): |
| 100 | + if cmd: |
| 101 | + run_s(cmd, env) |
| 102 | + |
| 103 | + |
| 104 | +def cicd_dpf_mrcfile(): |
| 105 | + env = { |
| 106 | + "CIBW_PLATFORM": CIBUILDWHEEL_PLATFORM, |
| 107 | + "CIBW_ARCHS": "auto", |
| 108 | + "CIBW_ENVIRONMENT": 'PATH="$HOME/.cargo/bin:$PATH" CARGO_TERM_COLOR="always"', |
| 109 | + "CIBW_ENVIRONMENT_WINDOWS": 'PATH="$UserProfile\\.cargo\\bin;$PATH"', |
| 110 | + "CIBW_BEFORE_BUILD": install_rust_cibw_command(), |
| 111 | + "CIBW_BUILD_VERBOSITY": "1", |
| 112 | + } |
| 113 | + |
| 114 | + cmds = """ |
| 115 | +python -m cibuildwheel ./src/python/dpf-mrcfile --output-dir wheelhouse |
| 116 | +ls wheelhouse |
| 117 | +pipx run build --sdist --outdir wheelhouse ./src/python/dpf-mrcfile |
| 118 | +ls wheelhouse |
| 119 | + """ |
| 120 | + for cmd in cmds.split("\n"): |
| 121 | + if cmd: |
| 122 | + run_s(cmd, env) |
| 123 | + |
| 124 | + |
| 125 | +def main(_args: List[str]) -> None: |
| 126 | + args = parseargs(_args) |
| 127 | + global CIBUILDWHEEL_PLATFORM |
| 128 | + CIBUILDWHEEL_PLATFORM = args.cibw_platform |
| 129 | + match args.mode: |
| 130 | + case "install_ci_deps": |
| 131 | + install_ci_deps() |
| 132 | + case "cicd_dpf_ssw_aligner_rs": |
| 133 | + install_ci_deps() |
| 134 | + cicd_dpf_ssw_aligner_rs() |
| 135 | + case "cicd_dpf_mrcfile": |
| 136 | + install_ci_deps() |
| 137 | + cicd_dpf_mrcfile() |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + main(sys.argv[1:]) |
0 commit comments