Skip to content

Commit 7b8c475

Browse files
hjmjohnsonclaude
andcommitted
ENH: Preserve module pyproject.toml when rewriting ITK deps
Back up pyproject.toml to pyproject.toml.orig before rewriting ITK dependency pins, then restore the original after the wheel is built. The modified version is saved as pyproject.toml.whl for reference. Uses try/finally to guarantee restore even if the build fails. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 5fda1b7 commit 7b8c475

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

scripts/build_python_instance_base.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -772,12 +772,20 @@ def build_external_module_python_wheel(self):
772772
out_dir = self.module_source_dir / "dist"
773773
out_dir.mkdir(parents=True, exist_ok=True)
774774

775-
# Dynamically update ITK dependency pins to match the version being built
775+
# Dynamically update ITK dependency pins to match the version being built.
776+
# Back up the original pyproject.toml so the working tree is restored
777+
# after the wheel is produced.
776778
module_pyproject = self.module_source_dir / "pyproject.toml"
779+
pyproject_orig = module_pyproject.with_suffix(".toml.orig")
780+
pyproject_whl = module_pyproject.with_suffix(".toml.whl")
781+
deps_rewritten = False
777782
if module_pyproject.is_file():
778783
itk_ver = self.package_env_config.get("ITK_PACKAGE_VERSION", "")
779784
if itk_ver:
780-
self._update_module_itk_deps(module_pyproject, itk_ver)
785+
shutil.copy2(module_pyproject, pyproject_orig)
786+
deps_rewritten = self._update_module_itk_deps(
787+
module_pyproject, itk_ver
788+
)
781789

782790
# Ensure venv tools are first in PATH
783791
py_exe = str(self.package_env_config["PYTHON_EXECUTABLE"]) # Python3_EXECUTABLE
@@ -875,11 +883,21 @@ def build_external_module_python_wheel(self):
875883
# Module source directory to build
876884
cmd += [self.module_source_dir]
877885

878-
self.echo_check_call(cmd)
886+
try:
887+
self.echo_check_call(cmd)
879888

880-
# Post-process produced wheels (e.g., delocate on macOS x86_64)
881-
for wheel in out_dir.glob("*.whl"):
882-
self.fixup_wheel(str(wheel), remote_module_wheel=True)
889+
# Post-process produced wheels (e.g., delocate on macOS x86_64)
890+
for wheel in out_dir.glob("*.whl"):
891+
self.fixup_wheel(str(wheel), remote_module_wheel=True)
892+
finally:
893+
# Restore original pyproject.toml so the working tree stays clean
894+
if deps_rewritten and pyproject_orig.is_file():
895+
shutil.copy2(module_pyproject, pyproject_whl)
896+
shutil.move(str(pyproject_orig), str(module_pyproject))
897+
print(
898+
f"Restored {module_pyproject} "
899+
f"(modified version saved as {pyproject_whl.name})"
900+
)
883901

884902
def build_itk_python_wheels(self):
885903
"""Build all ITK Python wheels listed in ``WHEEL_NAMES.txt``."""

0 commit comments

Comments
 (0)