Skip to content

Commit

Permalink
Merge pull request #44 from asmodehn/pip_system
Browse files Browse the repository at this point in the history
Now checking for pip --system option before using.
  • Loading branch information
asmodehn authored Aug 30, 2016
2 parents 2ae292b + 5d6b014 commit 9ef026f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 22 deletions.
86 changes: 66 additions & 20 deletions cmake/catkin-pip-prefix.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function(find_catkin_system_pip)
set(PIP_VERSION_COMMAND ${CATKIN_SYS_PIP} --version)

string(REPLACE ";" " " PIP_VERSION_CMDSTR "${PIP_VERSION_COMMAND}")
message(STATUS " ... Running ${PIP_VERSION_CMDSTR} ...")
#message(STATUS " ... Running ${PIP_VERSION_CMDSTR} ...")

execute_process(
COMMAND ${PIP_VERSION_COMMAND}
Expand All @@ -51,33 +51,69 @@ function(find_catkin_system_pip)
ERROR_VARIABLE PIP_VERSION_ERROR
)

message(STATUS " ... Done ... [${PIP_VERSION_RESULT}]: ${PIP_VERSION_VARIABLE}")
#message(STATUS " ... Done ... [${PIP_VERSION_RESULT}]: ${PIP_VERSION_VARIABLE}")
if (PIP_VERSION_RESULT)
message(STATUS "Command ${PIP_VERSION_CMDSTR} FAILED !")
message(STATUS "System Introspection Command ${PIP_VERSION_CMDSTR} FAILED !")
message(FATAL_ERROR "${PIP_VERSION_ERROR}")
else()
# we need to parse the result to extract the version
string(REPLACE " " ";" PIP_VERSION_VARIABLE_LIST ${PIP_VERSION_VARIABLE})
list(GET PIP_VERSION_VARIABLE_LIST 1 CATKIN_SYS_PIP_VERSION)
set (CATKIN_SYS_PIP_VERSION ${CATKIN_SYS_PIP_VERSION} CACHE STRING "Major version of pip detected on the system")
set (CATKIN_SYS_PIP_VERSION ${CATKIN_SYS_PIP_VERSION} CACHE INTERNAL "Major version of pip detected on the system")

message(STATUS " System pip version detected : ${CATKIN_SYS_PIP_VERSION}")

string(REPLACE "." ";" VERSION_LIST ${CATKIN_SYS_PIP_VERSION})
list(GET VERSION_LIST 0 CATKIN_SYS_PIP_VERSION_MAJOR)
set(CATKIN_SYS_PIP_VERSION_MAJOR ${CATKIN_SYS_PIP_VERSION_MAJOR} CACHE STRING "Major version of pip detected on the system" )
list(GET VERSION_LIST 1 CATKIN_SYS_PIP_VERSION_MINOR)
set(CATKIN_SYS_PIP_VERSION_MINOR ${CATKIN_SYS_PIP_VERSION_MINOR} CACHE STRING "Minor version of pip detected on the system" )
list(GET VERSION_LIST 2 CATKIN_SYS_PIP_VERSION_PATCH)
set(CATKIN_SYS_PIP_VERSION_PATCH ${CATKIN_SYS_PIP_VERSION_PATCH} CACHE STRING "Patch version of pip detected on the system" )
endif()

# Detecting debian pip "--system" extension
# ros@ros-Kinetic-VM:~/Project$ pip install --help | grep -e "--system"
# --system Install using the system scheme (overrides --user on Debian systems)
# ros@ros-Kinetic-VM:~/Project$ pip install --help | grep -e "--system" | wc -l
# 1

set(PIP_INSTALL_HELP_COMMAND ${CATKIN_SYS_PIP} install --help)

string(REPLACE ";" " " PIP_INSTALL_HELP_CMDSTR "${PIP_VERSION_COMMAND}")
#message(STATUS " ... Running ${PIP_INSTALL_HELP_CMDSTR} ...")

execute_process(
COMMAND ${PIP_INSTALL_HELP_COMMAND}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE PIP_INSTALL_HELP_RESULT
OUTPUT_VARIABLE PIP_INSTALL_HELP_VARIABLE
ERROR_VARIABLE PIP_INSTALL_HELP_ERROR
)

# TODO Detect debian "--system"
# ros@ros-Kinetic-VM:~/Project$ pip install --help | grep -e "--system"
# --system Install using the system scheme (overrides --user on Debian systems)
# ros@ros-Kinetic-VM:~/Project$ pip install --help | grep -e "--system" | wc -l
# 1
#message(STATUS " ... Done ... [${PIP_INSTALL_HELP_RESULT}]: ${PIP_INSTALL_HELP_VARIABLE}")
if (PIP_INSTALL_HELP_RESULT)
message(STATUS "System Introspection Command ${PIP_INSTALL_HELP_CMDSTR} FAILED !")
message(FATAL_ERROR "${PIP_INSTALL_HELP_ERROR}")
else()
message(STATUS " System pip install detected options : ")
string(REPLACE "\n" ";" PIP_INSTALL_HELP_VARIABLE_LIST ${PIP_INSTALL_HELP_VARIABLE})

# CMake list search
foreach(line IN LISTS PIP_INSTALL_HELP_VARIABLE_LIST)
# message("LINE : ${line}")
# parsing line by line to extract available options
string(REPLACE " " ";" line_list ";${line};")

list (FIND line_list "--user" _index)
if (${_index} GREATER -1)
message(STATUS " --user")
set(CATKIN_SYS_PIP_HAS_USER TRUE CACHE INTERNAL "Whether detected system pip can do 'pip install --user'" )
endif()

list (FIND line_list "--system" _index)
if (${_index} GREATER -1)
message(STATUS " --system")
set(CATKIN_SYS_PIP_HAS_SYSTEM TRUE CACHE INTERNAL "Whether detected system pip can do 'pip install --system'" )
endif()

endforeach()

endif()

else(CATKIN_SYS_PIP)
message( FATAL_ERROR "pip system command not found. Make sure you have installed the python-pip package on your system.")
endif()
Expand Down Expand Up @@ -109,42 +145,52 @@ function(catkin_pip_setup_prefix ws_prefix)
message(STATUS " ... Retrieving catkin_pip requirements using system pip ...")

if (${CATKIN_SYS_PIP_VERSION} VERSION_LESS 6.0.0)
if(${CATKIN_SYS_PIP_HAS_SYSTEM})
# if --system is present, it means the default behavior has been changed to --user (by debian/ubuntu)
# and we have to specify --system in order to use --target
set(OPT_SYSTEM "--system")
else(${CATKIN_SYS_PIP_HAS_SYSTEM})
set(OPT_SYSTEM "")
endif(${CATKIN_SYS_PIP_HAS_SYSTEM})

# We need to find a pip command that works for old pip versions (indigo supports trusty which is pip v1.5.4)
# Note --target here means we cannot check if a package is already installed or not before installing, using old pip.
# which means we have to reinstall dependencies everytime and specify --exists-action w to avoid "already exists" errors
# Avoid --install-option since the setuptools version found will be different the first time and the following times
execute_process(
COMMAND ${CATKIN_SYS_PIP} -q install -r "${CATKIN_PIP_REQUIREMENTS_PATH}/catkin-pip-base.req" --download-cache "${CMAKE_BINARY_DIR}/pip-cache" --target "${ws_prefix}/@CATKIN_PIP_GLOBAL_PYTHON_DESTINATION@" --exists-action w
COMMAND ${CATKIN_SYS_PIP} -q install ${OPT_SYSTEM} -r "${CATKIN_PIP_REQUIREMENTS_PATH}/catkin-pip-base.req" --download-cache "${CMAKE_BINARY_DIR}/pip-cache" --target "${ws_prefix}/@CATKIN_PIP_GLOBAL_PYTHON_DESTINATION@" --exists-action w
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE PIP_RESULT
OUTPUT_VARIABLE PIP_VARIABLE
ERROR_VARIABLE PIP_ERROR
)
message(STATUS " ... Done ... [${PIP_RESULT}]: ${PIP_VARIABLE}")
if (PIP_RESULT)
message(STATUS "Command ${CATKIN_SYS_PIP} install -r \"${CATKIN_PIP_REQUIREMENTS_PATH}/catkin-pip-base.req\" --download-cache \"${CMAKE_BINARY_DIR}/pip-cache\" --target \"${ws_prefix}/@CATKIN_PIP_GLOBAL_PYTHON_DESTINATION@\" --exists-action w FAILED")
message(STATUS "Command ${CATKIN_SYS_PIP} install ${OPT_SYSTEM} -r \"${CATKIN_PIP_REQUIREMENTS_PATH}/catkin-pip-base.req\" --download-cache \"${CMAKE_BINARY_DIR}/pip-cache\" --target \"${ws_prefix}/@CATKIN_PIP_GLOBAL_PYTHON_DESTINATION@\" --exists-action w FAILED")
message(FATAL_ERROR "${PIP_ERROR}")
endif()
else () # assuming latest pip
# We need to find a pip command that works for pip versions (kinetic supports xenial which is pip v8.1.1)
# Careful --system is a debian porting extension. This will not work if the system pip command loads the pip package of pip itself, after the environment has been setup...
execute_process(
COMMAND ${CATKIN_SYS_PIP} -q install --system -r "${CATKIN_PIP_REQUIREMENTS_PATH}/catkin-pip-base.req" --cache-dir "${CMAKE_BINARY_DIR}/pip-cache" --target "${ws_prefix}/@CATKIN_PIP_GLOBAL_PYTHON_DESTINATION@" --exists-action w
COMMAND ${CATKIN_SYS_PIP} -q install ${OPT_SYSTEM} -r "${CATKIN_PIP_REQUIREMENTS_PATH}/catkin-pip-base.req" --cache-dir "${CMAKE_BINARY_DIR}/pip-cache" --target "${ws_prefix}/@CATKIN_PIP_GLOBAL_PYTHON_DESTINATION@" --exists-action w
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE PIP_RESULT
OUTPUT_VARIABLE PIP_VARIABLE
ERROR_VARIABLE PIP_ERROR
)
message(STATUS " ... Done ... [${PIP_RESULT}]: ${PIP_VARIABLE}")
if (PIP_RESULT)
message(STATUS "Command ${CATKIN_SYS_PIP} install --system -r \"${CATKIN_PIP_REQUIREMENTS_PATH}/catkin-pip-base.req\" --cache-dir \"${CMAKE_BINARY_DIR}/pip-cache\" --target \"${ws_prefix}/@CATKIN_PIP_GLOBAL_PYTHON_DESTINATION@\" --exists-action w FAILED")
message(STATUS "Command ${CATKIN_SYS_PIP} install ${OPT_SYSTEM} -r \"${CATKIN_PIP_REQUIREMENTS_PATH}/catkin-pip-base.req\" --cache-dir \"${CMAKE_BINARY_DIR}/pip-cache\" --target \"${ws_prefix}/@CATKIN_PIP_GLOBAL_PYTHON_DESTINATION@\" --exists-action w FAILED")
message(FATAL_ERROR "${PIP_ERROR}")
endif()
endif()


set(CATKIN_PIP python -m pip) # to make sure we use our recently downloaded pip version (its entrypoints were not installed by old pip/setuptools)

unset(CATKIN_SYS_PIP CACHE) # we dont need this any longer
# We are now using the latest pip from pip package, and not the system one.

# Fixing security since python 2.7.6 on trusty is broken : https://stackoverflow.com/questions/29099404/ssl-insecureplatform-error-when-using-requests-package
# Also reinstalling pip to finally get it in bin/
Expand Down
2 changes: 0 additions & 2 deletions cmake/catkin-pip.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ catkin_add_env_hooks(42.site_packages SHELLS bash DIRECTORY ${CATKIN_PIP_ENV_HOO
catkin_pip_setup_prefix(${CATKIN_PIP_ENV})

# Setting up python site-packages directory in devel workspace

message(STATUS " ... Creating pip packages prefix ...")
# If needed we create the directory (to avoid later errors)
# It is required on isolated builds
file(MAKE_DIRECTORY ${CATKIN_DEVEL_PREFIX}/@CATKIN_PIP_GLOBAL_PYTHON_DESTINATION@)
Expand Down

0 comments on commit 9ef026f

Please sign in to comment.