Skip to content

Commit 0749552

Browse files
committed
Make swpython usable from the source build tree
Add a build-tree swpython wrapper at build/bin/ pointing at the bundled Python in the build dir, with PYTHONPATH set so shapeworks_py.so is importable. Extend bundled_pip_install to also `pip install -e` the in-source Python packages (shapeworks, DeepSSMUtils, etc.), so devs can run `swpython RunUseCase.py` against their build without `ninja install` or packaging. Exclude editable-install marker files (__editable__*, *.egg-link, easy-install.pth) from the install copy so the deployed bundle is clean — the install rules continue to copy package source dirs in directly.
1 parent 8256ab3 commit 0749552

2 files changed

Lines changed: 67 additions & 12 deletions

File tree

cmake/BundledPython.cmake

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,27 +160,74 @@ endif()
160160
add_compile_definitions(SHAPEWORKS_BUNDLED_PYTHON_SOURCE_DIR="${CMAKE_SOURCE_DIR}/Python")
161161
add_compile_definitions(SHAPEWORKS_BUNDLED_PYTHON_CONDA_PREFIX="${_conda_prefix}")
162162

163+
# ---------------------------------------------------------------------------
164+
# In-source ShapeWorks Python packages — editable-installed into the bundled
165+
# Python so 'swpython RunUseCase.py' against a source build picks up edits to
166+
# Python/shapeworks etc. without rebuilding. The install rules copy these
167+
# directories in separately for the deployed bundle.
168+
# ---------------------------------------------------------------------------
169+
set(_sw_editable_packages
170+
"${CMAKE_SOURCE_DIR}/Python/shapeworks"
171+
"${CMAKE_SOURCE_DIR}/Python/DataAugmentationUtilsPackage"
172+
"${CMAKE_SOURCE_DIR}/Python/DatasetUtilsPackage"
173+
"${CMAKE_SOURCE_DIR}/Python/DeepSSMUtilsPackage"
174+
"${CMAKE_SOURCE_DIR}/Python/DocumentationUtilsPackage"
175+
"${CMAKE_SOURCE_DIR}/Python/ShapeCohortGenPackage"
176+
)
177+
163178
# ---------------------------------------------------------------------------
164179
# Custom target: install pip packages into the bundled Python
165180
# ---------------------------------------------------------------------------
166181
if(WIN32)
167182
# On Windows GHA runners, pip's platformdirs crashes trying to read
168183
# CSIDL_COMMON_APPDATA from the registry. The --isolated flag skips
169184
# all config file and environment variable processing.
170-
add_custom_target(bundled_pip_install
171-
COMMAND "${BUNDLED_PYTHON_EXECUTABLE}" -m pip install --isolated --upgrade pip
172-
COMMAND "${BUNDLED_PYTHON_EXECUTABLE}" -m pip install --isolated -r "${CMAKE_SOURCE_DIR}/python_requirements_bundled.txt"
173-
COMMENT "Installing runtime pip packages into bundled Python"
174-
VERBATIM
175-
)
185+
set(_pip_flags --isolated)
176186
else()
177-
add_custom_target(bundled_pip_install
178-
COMMAND "${BUNDLED_PYTHON_EXECUTABLE}" -m pip install --upgrade pip
179-
COMMAND "${BUNDLED_PYTHON_EXECUTABLE}" -m pip install -r "${CMAKE_SOURCE_DIR}/python_requirements_bundled.txt"
180-
COMMENT "Installing runtime pip packages into bundled Python"
181-
VERBATIM
182-
)
187+
set(_pip_flags "")
188+
endif()
189+
190+
add_custom_target(bundled_pip_install
191+
COMMAND "${BUNDLED_PYTHON_EXECUTABLE}" -m pip install ${_pip_flags} --upgrade pip
192+
COMMAND "${BUNDLED_PYTHON_EXECUTABLE}" -m pip install ${_pip_flags} -r "${CMAKE_SOURCE_DIR}/python_requirements_bundled.txt"
193+
COMMENT "Installing runtime pip packages into bundled Python"
194+
VERBATIM
195+
)
196+
197+
foreach(_pkg ${_sw_editable_packages})
198+
if(EXISTS "${_pkg}/setup.py")
199+
add_custom_command(TARGET bundled_pip_install POST_BUILD
200+
COMMAND "${BUNDLED_PYTHON_EXECUTABLE}" -m pip install ${_pip_flags} --no-deps -e "${_pkg}"
201+
COMMENT "Editable-installing ${_pkg} into bundled Python"
202+
VERBATIM
203+
)
204+
endif()
205+
endforeach()
206+
207+
# ---------------------------------------------------------------------------
208+
# Build-tree swpython wrapper — points at the bundled python in the build dir
209+
# and puts CMAKE_RUNTIME_OUTPUT_DIRECTORY on PYTHONPATH so shapeworks_py.so is
210+
# importable. The install-tree variant is generated in InstallBundledPython.cmake.
211+
# ---------------------------------------------------------------------------
212+
if(WIN32)
213+
set(_dev_wrapper "${CMAKE_BINARY_DIR}/bin/swpython.bat")
214+
file(WRITE "${_dev_wrapper}"
215+
"@echo off
216+
set \"PYTHONHOME=${BUNDLED_PYTHON_ROOT}\"
217+
set \"PYTHONPATH=${CMAKE_BINARY_DIR}/bin\"
218+
\"${BUNDLED_PYTHON_EXECUTABLE}\" %*
219+
")
220+
else()
221+
set(_dev_wrapper "${CMAKE_BINARY_DIR}/bin/swpython")
222+
file(WRITE "${_dev_wrapper}"
223+
"#!/bin/bash
224+
export PYTHONHOME='${BUNDLED_PYTHON_ROOT}'
225+
export PYTHONPATH='${CMAKE_BINARY_DIR}/bin'
226+
exec '${BUNDLED_PYTHON_EXECUTABLE}' \"\$@\"
227+
")
228+
execute_process(COMMAND chmod 755 "${_dev_wrapper}")
183229
endif()
230+
message(STATUS "Build-tree swpython wrapper: ${_dev_wrapper}")
184231

185232
message(STATUS "Bundled Python root: ${BUNDLED_PYTHON_ROOT}")
186233
message(STATUS "Bundled Python executable: ${BUNDLED_PYTHON_EXECUTABLE}")

cmake/InstallBundledPython.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ install(DIRECTORY "${BUNDLED_PYTHON_ROOT}/Lib/"
5454
PATTERN "turtledemo" EXCLUDE
5555
PATTERN "turtle.py" EXCLUDE
5656
PATTERN "lib2to3" EXCLUDE
57+
PATTERN "*.egg-link" EXCLUDE
58+
PATTERN "__editable__*" EXCLUDE
59+
PATTERN "easy-install.pth" EXCLUDE
5760
)
5861

5962
# ---------------------------------------------------------------------------
@@ -198,6 +201,11 @@ install(DIRECTORY "${BUNDLED_PYTHON_ROOT}/lib/python3.12/"
198201
PATTERN "turtledemo" EXCLUDE
199202
PATTERN "turtle.py" EXCLUDE
200203
PATTERN "lib2to3" EXCLUDE
204+
# Editable-install markers left by `pip install -e` during bundled_pip_install
205+
# — pollute the deployed bundle and only point at the build machine's source tree.
206+
PATTERN "*.egg-link" EXCLUDE
207+
PATTERN "__editable__*" EXCLUDE
208+
PATTERN "easy-install.pth" EXCLUDE
201209
)
202210

203211
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)