|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Build ITK + all remote module Python wheels from latest main branches. |
| 3 | +
|
| 4 | +Usage:: |
| 5 | +
|
| 6 | + python scripts/build_all_latest_wheels.py [--platform-env linux-py311] |
| 7 | + python scripts/build_all_latest_wheels.py --help |
| 8 | +""" |
| 9 | + |
| 10 | +import argparse |
| 11 | +import re |
| 12 | +import shutil |
| 13 | +import subprocess |
| 14 | +import sys |
| 15 | +from datetime import datetime |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | + |
| 19 | +def run(cmd: list[str], **kwargs) -> subprocess.CompletedProcess: |
| 20 | + print(f" $ {' '.join(str(c) for c in cmd)}") |
| 21 | + return subprocess.run(cmd, check=True, **kwargs) |
| 22 | + |
| 23 | + |
| 24 | +def clone(repo: str, dest: Path, branch: str | None = None) -> bool: |
| 25 | + cmd = ["git", "clone", "--depth", "1"] |
| 26 | + if branch: |
| 27 | + cmd += ["--branch", branch] |
| 28 | + cmd += [repo, str(dest)] |
| 29 | + try: |
| 30 | + run(cmd, capture_output=True) |
| 31 | + return True |
| 32 | + except subprocess.CalledProcessError: |
| 33 | + return False |
| 34 | + |
| 35 | + |
| 36 | +def parse_remote_modules(itk_dir: Path) -> list[tuple[str, str]]: |
| 37 | + """Parse ITK remote .cmake files, return [(name, git_url), ...].""" |
| 38 | + modules = [] |
| 39 | + for rc in sorted((itk_dir / "Modules" / "Remote").glob("*.remote.cmake")): |
| 40 | + name = rc.stem.replace(".remote", "") |
| 41 | + text = rc.read_text() |
| 42 | + m = re.search(r"GIT_REPOSITORY\s+(\S+)", text) |
| 43 | + if m: |
| 44 | + modules.append((name, m.group(1))) |
| 45 | + return modules |
| 46 | + |
| 47 | + |
| 48 | +def build_wheels( |
| 49 | + ipp_dir: Path, |
| 50 | + platform_env: str, |
| 51 | + build_dir: Path, |
| 52 | + itk_source: Path, |
| 53 | + module_source: Path | None = None, |
| 54 | + skip_itk: bool = False, |
| 55 | +) -> bool: |
| 56 | + cmd = [ |
| 57 | + "pixi", |
| 58 | + "run", |
| 59 | + "-e", |
| 60 | + platform_env, |
| 61 | + "--", |
| 62 | + "python", |
| 63 | + "scripts/build_wheels.py", |
| 64 | + "--platform-env", |
| 65 | + platform_env, |
| 66 | + "--itk-git-tag", |
| 67 | + "main", |
| 68 | + "--itk-source-dir", |
| 69 | + str(itk_source), |
| 70 | + "--no-build-itk-tarball-cache", |
| 71 | + "--no-use-sudo", |
| 72 | + "--build-dir-root", |
| 73 | + str(build_dir), |
| 74 | + ] |
| 75 | + if module_source: |
| 76 | + cmd += ["--module-source-dir", str(module_source)] |
| 77 | + if skip_itk: |
| 78 | + cmd += ["--skip-itk-build", "--skip-itk-wheel-build"] |
| 79 | + |
| 80 | + try: |
| 81 | + run(cmd, cwd=ipp_dir) |
| 82 | + return True |
| 83 | + except subprocess.CalledProcessError: |
| 84 | + return False |
| 85 | + |
| 86 | + |
| 87 | +def main(): |
| 88 | + parser = argparse.ArgumentParser(description=__doc__) |
| 89 | + parser.add_argument( |
| 90 | + "--platform-env", default="linux-py311", help="Pixi environment name" |
| 91 | + ) |
| 92 | + parser.add_argument( |
| 93 | + "--ipp-branch", |
| 94 | + default="python-build-system", |
| 95 | + help="ITKPythonPackage branch to use", |
| 96 | + ) |
| 97 | + parser.add_argument( |
| 98 | + "--ipp-repo", |
| 99 | + default="https://github.com/BRAINSia/ITKPythonPackage.git", |
| 100 | + help="ITKPythonPackage git URL", |
| 101 | + ) |
| 102 | + parser.add_argument( |
| 103 | + "--itk-repo", |
| 104 | + default="https://github.com/InsightSoftwareConsortium/ITK.git", |
| 105 | + help="ITK git URL", |
| 106 | + ) |
| 107 | + args = parser.parse_args() |
| 108 | + |
| 109 | + timestamp = datetime.now().strftime("%Y%m%d%H%M%S") |
| 110 | + workdir = Path(f"/tmp/{timestamp}_LatestITKPython") |
| 111 | + dist_dir = workdir / "dist" |
| 112 | + dist_dir.mkdir(parents=True) |
| 113 | + build_dir = workdir / "build" |
| 114 | + |
| 115 | + print(f"=== Build directory: {workdir}") |
| 116 | + print(f"=== Platform: {args.platform_env}") |
| 117 | + |
| 118 | + # 1) Clone ITK |
| 119 | + print("=== Cloning ITK (main)...") |
| 120 | + itk_dir = workdir / "ITK" |
| 121 | + clone(args.itk_repo, itk_dir, branch="main") |
| 122 | + |
| 123 | + # 2) Clone ITKPythonPackage |
| 124 | + print(f"=== Cloning ITKPythonPackage ({args.ipp_branch})...") |
| 125 | + ipp_dir = workdir / "ITKPythonPackage" |
| 126 | + clone(args.ipp_repo, ipp_dir, branch=args.ipp_branch) |
| 127 | + |
| 128 | + # 3) Clone remote modules |
| 129 | + print("=== Cloning remote modules...") |
| 130 | + modules_dir = workdir / "modules" |
| 131 | + modules_dir.mkdir() |
| 132 | + remote_modules = parse_remote_modules(itk_dir) |
| 133 | + |
| 134 | + module_list: list[str] = [] |
| 135 | + for name, repo in remote_modules: |
| 136 | + mod_dir = modules_dir / name |
| 137 | + if not clone(repo, mod_dir): |
| 138 | + print(f" WARNING: Failed to clone {name}, skipping") |
| 139 | + continue |
| 140 | + # Keep only modules with Python wrapping |
| 141 | + if (mod_dir / "wrapping").is_dir() and (mod_dir / "pyproject.toml").is_file(): |
| 142 | + module_list.append(name) |
| 143 | + else: |
| 144 | + shutil.rmtree(mod_dir) |
| 145 | + |
| 146 | + print(f"=== {len(module_list)} modules with Python wrapping") |
| 147 | + |
| 148 | + # 4) Build ITK wheels |
| 149 | + print("=== Building ITK Python wheels...") |
| 150 | + if not build_wheels(ipp_dir, args.platform_env, build_dir, itk_dir): |
| 151 | + print("FATAL: ITK wheel build failed") |
| 152 | + sys.exit(1) |
| 153 | + |
| 154 | + # Copy ITK wheels to dist |
| 155 | + for whl in (build_dir / "dist").glob("*.whl"): |
| 156 | + shutil.copy2(whl, dist_dir) |
| 157 | + |
| 158 | + # 5) Build each remote module wheel |
| 159 | + failed: list[str] = [] |
| 160 | + for name in module_list: |
| 161 | + print(f"=== Building {name}...") |
| 162 | + mod_dir = modules_dir / name |
| 163 | + if build_wheels( |
| 164 | + ipp_dir, |
| 165 | + args.platform_env, |
| 166 | + build_dir, |
| 167 | + itk_dir, |
| 168 | + module_source=mod_dir, |
| 169 | + skip_itk=True, |
| 170 | + ): |
| 171 | + for whl in (mod_dir / "dist").glob("*.whl"): |
| 172 | + shutil.copy2(whl, dist_dir) |
| 173 | + else: |
| 174 | + print(f" FAILED: {name}") |
| 175 | + failed.append(name) |
| 176 | + |
| 177 | + # 6) Summary |
| 178 | + wheels = list(dist_dir.glob("*.whl")) |
| 179 | + print() |
| 180 | + print("=== Build complete ===") |
| 181 | + print(f"Wheels: {dist_dir}") |
| 182 | + print(f"{len(wheels)} total wheels produced") |
| 183 | + |
| 184 | + if failed: |
| 185 | + print(f"\nFailed modules ({len(failed)}):") |
| 186 | + for name in failed: |
| 187 | + print(f" {name}") |
| 188 | + sys.exit(1) |
| 189 | + |
| 190 | + |
| 191 | +if __name__ == "__main__": |
| 192 | + main() |
0 commit comments