Skip to content

Commit 1f32f3d

Browse files
committed
REF: Add build_tarball method for Linux and macOS ITK Python builds
- Implemented `build_tarball` in `LinuxBuildPythonInstance` and `MacOSBuildPythonInstance` for tarball creation and compression. - Updated `build_python_instance_base.py` to include `build_tarball` as an abstract method. - Enhanced build scripts with functionality to mirror shell script tarball behaviors for respective platforms.
1 parent 2b5280b commit 1f32f3d

3 files changed

Lines changed: 143 additions & 4 deletions

File tree

‎scripts/build_python_instance_base.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,10 @@ def venv_paths(self):
424424
def fixup_wheel(self, filepath, lib_paths: str = ""): # pragma: no cover - abstract
425425
pass
426426

427+
@abstractmethod
428+
def build_tarball(self):
429+
pass
430+
427431
@abstractmethod
428432
def prepare_build_env(self) -> None: # pragma: no cover - abstract
429433
pass

‎scripts/linux_build_python_instance.py‎

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import shutil
1111

1212
from linux_venv_utils import create_linux_venvs
13-
from wheel_builder_utils import echo_check_call, _remove_tree
13+
from wheel_builder_utils import echo_check_call, _remove_tree, push_dir
1414

1515

1616
class LinuxBuildPythonInstance(BuildPythonInstanceBase):
@@ -168,6 +168,68 @@ def fixup_wheel(self, filepath, lib_paths: str = "") -> None:
168168
)
169169
return
170170

171+
def build_tarball(self):
172+
"""Create a compressed tarball of the ITK Python build tree (Linux).
173+
174+
Mirrors scripts/dockcross-manylinux-build-tarball.sh behavior:
175+
- tar selected content from ITKPythonPackage
176+
* ITKPythonPackage/ITK-*
177+
* ITKPythonPackage/oneTBB*
178+
* ITKPythonPackage/requirements-dev.txt
179+
* ITKPythonPackage/scripts
180+
- zstd compress with options (-10 -T6 --long=31)
181+
"""
182+
base_dir = Path(self.IPP_SOURCE_DIR)
183+
tar_name = "ITKPythonBuilds-linux.tar"
184+
tar_path = base_dir / tar_name
185+
zst_path = base_dir / f"{tar_name}.zst"
186+
187+
itk_pkg = base_dir / "ITKPythonPackage"
188+
189+
with push_dir(base_dir):
190+
# Build list of paths to include (expand existing patterns)
191+
include_paths: list[str] = []
192+
193+
# ITK builds and oneTBB
194+
for p in sorted(itk_pkg.glob("ITK-*")):
195+
include_paths.append(str(p))
196+
for p in sorted(itk_pkg.glob("oneTBB*")):
197+
include_paths.append(str(p))
198+
199+
# requirements-dev.txt
200+
req = itk_pkg / "requirements-dev.txt"
201+
if req.exists():
202+
include_paths.append(str(req))
203+
204+
# scripts directory
205+
if (itk_pkg / "scripts").exists():
206+
include_paths.append(str(itk_pkg / "scripts"))
207+
208+
if not include_paths:
209+
include_paths = [
210+
str(itk_pkg / "ITK-*"),
211+
str(itk_pkg / "oneTBB*"),
212+
str(itk_pkg / "requirements-dev.txt"),
213+
str(itk_pkg / "scripts"),
214+
]
215+
216+
# Create tar
217+
echo_check_call(["tar", "-cf", str(tar_path), *include_paths])
218+
219+
# Compress with zstd
220+
echo_check_call(
221+
[
222+
"zstd",
223+
"-f",
224+
"-10",
225+
"-T6",
226+
"--long=31",
227+
str(tar_path),
228+
"-o",
229+
str(zst_path),
230+
]
231+
)
232+
171233
def venv_paths(self) -> None:
172234
"""Resolve virtualenv tool paths.
173235
py_env may be a name under IPP_SOURCE_DIR/venvs or an absolute/relative path to a venv.
@@ -255,10 +317,10 @@ def _discover_local_pythons() -> list[str]:
255317
base = Path("/opt/python")
256318
if not base.exists():
257319
return []
258-
names.extend([p.name for p in venvs_dir.iterdir() if p.is_dir()])
320+
names.extend([p.name for p in base.iterdir() if p.is_dir()])
259321
return sorted(names)
260322

261-
default_py_envs = _discover_linux_pythons() + _discover_ipp_venvs()
323+
default_py_envs = _discover_local_pythons() + _discover_ipp_venvs()
262324

263325
return default_py_envs
264326

‎scripts/macos_build_python_instance.py‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from build_python_instance_base import BuildPythonInstanceBase
77
from macos_venv_utils import create_macos_venvs
88

9-
from wheel_builder_utils import echo_check_call
9+
from wheel_builder_utils import echo_check_call, push_dir
1010

1111

1212
class MacOSBuildPythonInstance(BuildPythonInstanceBase):
@@ -66,6 +66,79 @@ def fixup_wheel(self, filepath, lib_paths: str = "") -> None:
6666
echo_check_call([str(delocate_listdeps), str(filepath)])
6767
echo_check_call([str(delocate_wheel), str(filepath)])
6868

69+
def build_tarball(self):
70+
"""Create a compressed tarball of the ITK Python build tree (macOS).
71+
72+
Mirrors scripts/macpython-build-tarball.sh behavior:
73+
- dot_clean ITKPythonPackage
74+
- tar selected content
75+
- zstd compress with options (-10 -T6 --long=31)
76+
- add "-arm64" suffix for arm64 architectures
77+
"""
78+
base_dir = Path(self.IPP_SOURCE_DIR)
79+
arch_postfix = "-arm64" if self.platform_architechture == "arm64" else ""
80+
tar_name = f"ITKPythonBuilds-macosx{arch_postfix}.tar"
81+
tar_path = base_dir / tar_name
82+
zst_path = base_dir / f"{tar_name}.zst"
83+
84+
itk_pkg = base_dir / "ITKPythonPackage"
85+
86+
# Optional: clean AppleDouble files if tool is available
87+
with push_dir(base_dir):
88+
try:
89+
echo_check_call(["dot_clean", str(itk_pkg.name)])
90+
except Exception:
91+
# dot_clean may not be available; continue without it
92+
pass
93+
94+
# Build list of paths to include (expand existing patterns)
95+
include_paths: list[str] = []
96+
97+
# ITK builds
98+
for p in sorted((itk_pkg.glob("ITK-*"))):
99+
include_paths.append(str(p))
100+
101+
# On macOS, TBB contents are not included (kept empty in bash)
102+
103+
# venvs directory
104+
if (itk_pkg / "venvs").exists():
105+
include_paths.append(str(itk_pkg / "venvs"))
106+
107+
# Required extraction marker
108+
req_file = base_dir / "ITKPythonPackageRequiredExtractionDir.txt"
109+
if req_file.exists():
110+
include_paths.append(str(req_file))
111+
112+
# scripts directory
113+
if (itk_pkg / "scripts").exists():
114+
include_paths.append(str(itk_pkg / "scripts"))
115+
116+
# Fall back to original paths if expansions found nothing
117+
if not include_paths:
118+
include_paths = [
119+
str(itk_pkg / "ITK-*"),
120+
str(itk_pkg / "venvs"),
121+
str(base_dir / "ITKPythonPackageRequiredExtractionDir.txt"),
122+
str(itk_pkg / "scripts"),
123+
]
124+
125+
# Create tar
126+
echo_check_call(["tar", "-cf", str(tar_path), *include_paths])
127+
128+
# Compress with zstd
129+
echo_check_call(
130+
[
131+
"zstd",
132+
"-f",
133+
"-10",
134+
"-T6",
135+
"--long=31",
136+
str(tar_path),
137+
"-o",
138+
str(zst_path),
139+
]
140+
)
141+
69142
def venv_paths(self) -> None:
70143
"""Resolve virtualenv tool paths.
71144
py_env may be a name under IPP_SOURCE_DIR/venvs or an absolute/relative path to a venv.

0 commit comments

Comments
 (0)