Skip to content

Commit

Permalink
[Python] Install Python bindings with pip if available
Browse files Browse the repository at this point in the history
As directly invoking `python setup.py` is considered deprecated behavior
by the PyPA, use `python -m pip install` to install the Python bindings
if the Python interpreter has a valid version of pip associated with it.

If there is a valid pip, build and install the Python bindings with
`python -m pip install`.

If there is no valid pip, warn the user and give them instructions on how to
get pip on their machine. Attempt to still install the Python bindings by falling
back on the deprecated `python setup.py build` and `python setup.py install` which
will also invoke Easy Install if setuptools v0.60.0+ is used.
  • Loading branch information
matthewfeickert committed Feb 4, 2022
1 parent bf64501 commit 31e3d71
Showing 1 changed file with 54 additions and 17 deletions.
71 changes: 54 additions & 17 deletions bindings/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,62 @@ endif()

configure_file(${SETUP_PY_IN} ${SETUP_PY})

add_custom_command(OUTPUT ${OUTPUT}
COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} build
DEPENDS ${DEPS})
# Check it the Python interpreter has a valid version of pip
execute_process(
COMMAND ${PYTHON_EXECUTABLE} -m pip --version
RESULT_VARIABLE VALID_PIP_EXIT_CODE
OUTPUT_QUIET
)

add_custom_target(python_target ALL DEPENDS ${OUTPUT} XrdCl)
if ( NOT ${VALID_PIP_EXIT_CODE} EQUAL 0 )
# Attempt to still install with deprecated invocation of setup.py
message(WARNING
" ${PYTHON_EXECUTABLE} does not have a valid pip associated with it."
" It is recommended that you install a version of pip to install Python"
" packages and bindings. If you are unable to install a version of pip"
" through a package manager or with your Python build try using the PyPA's"
" get-pip.py bootstrapping script ( https://github.com/pypa/get-pip ).\n"
" The installation of the Python bindings will attempt to continue using"
" the deprecated method of `${PYTHON_EXECUTABLE} setup.py install`."
)

# Get the distribution name on Debian families
execute_process( COMMAND grep -i ^NAME=\" /etc/os-release
OUTPUT_VARIABLE DEB_DISTRO )
STRING(REGEX REPLACE "^NAME=\"" "" DEB_DISTRO "${DEB_DISTRO}")
STRING(REGEX REPLACE "\".*" "" DEB_DISTRO "${DEB_DISTRO}")
# https://docs.python.org/3/install/#splitting-the-job-up
add_custom_command(OUTPUT ${OUTPUT}
COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} build
DEPENDS ${DEPS})

if( DEB_DISTRO STREQUAL "Debian" OR DEB_DISTRO STREQUAL "Ubuntu" )
set(PYTHON_LAYOUT "unix" CACHE STRING "Python installation layout (deb or unix)")
set(DEB_INSTALL_ARGS "--install-layout ${PYTHON_LAYOUT}")
endif()
add_custom_target(python_target ALL DEPENDS ${OUTPUT} XrdCl)

# Get the distribution name on Debian families
execute_process( COMMAND grep -i ^NAME= /etc/os-release
OUTPUT_VARIABLE DEB_DISTRO )
STRING(REGEX REPLACE "^NAME=\"" "" DEB_DISTRO "${DEB_DISTRO}")
STRING(REGEX REPLACE "\".*" "" DEB_DISTRO "${DEB_DISTRO}")

if( DEB_DISTRO STREQUAL "Debian" OR DEB_DISTRO STREQUAL "Ubuntu" )
set(PYTHON_LAYOUT "unix" CACHE STRING "Python installation layout (deb or unix)")
set(DEB_INSTALL_ARGS "--install-layout ${PYTHON_LAYOUT}")
endif()

install(
CODE
"EXECUTE_PROCESS(
COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} install --prefix \$ENV{DESTDIR}/${CMAKE_INSTALL_PREFIX} ${DEB_INSTALL_ARGS} --record PYTHON_INSTALLED)" )
install(
CODE
"EXECUTE_PROCESS(
COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} install \
--prefix \$ENV{DESTDIR}/${CMAKE_INSTALL_PREFIX} \
${DEB_INSTALL_ARGS} \
--record PYTHON_INSTALLED
)"
)

else()
# Use --force-reinstall to ensure a clean install if a rebuild needs to happen
install(
CODE
"EXECUTE_PROCESS(
COMMAND ${PYTHON_EXECUTABLE} -m pip install \
--force-reinstall \
--prefix \$ENV{DESTDIR}/${CMAKE_INSTALL_PREFIX} \
${CMAKE_CURRENT_BINARY_DIR}
)"
)
endif()

0 comments on commit 31e3d71

Please sign in to comment.