From 1b1b25b09871ca0a8804a2ed18b723bcd781c58f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20R=C3=B6hling?= Date: Mon, 27 Nov 2023 22:47:36 +0100 Subject: [PATCH] Improve Python install path detection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [ This is the companion PR to https://github.com/colcon/colcon-core/pull/601 ] This PR makes the customized Python path scheme logic in ament_cmake_python contingent on an install prefix other than `/usr`. This makes ament more distribution-friendly and prevents the accidental overwriting of system packages in case someone decides to install their Python packages to /usr. There is no other change in behavior. Signed-off-by: Timo Röhling --- ament_cmake_python/ament_cmake_python-extras.cmake | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ament_cmake_python/ament_cmake_python-extras.cmake b/ament_cmake_python/ament_cmake_python-extras.cmake index d8825818..63406d3a 100644 --- a/ament_cmake_python/ament_cmake_python-extras.cmake +++ b/ament_cmake_python/ament_cmake_python-extras.cmake @@ -47,13 +47,15 @@ macro(_ament_cmake_python_get_python_install_dir) set(_python_code "\ import os +import re import sysconfig -schemes = sysconfig.get_scheme_names() kwargs = {'vars': {'base': '${CMAKE_INSTALL_PREFIX}'}} -if 'deb_system' in schemes or 'osx_framework_library' in schemes: - kwargs['scheme'] = 'posix_prefix' -elif 'rpm_prefix' in schemes: - kwargs['scheme'] = 'rpm_prefix' +if not re.match(r'^/usr/?$', '${CMAKE_INSTALL_PREFIX}'): + schemes = sysconfig.get_scheme_names() + if 'deb_system' in schemes or 'osx_framework_library' in schemes: + kwargs['scheme'] = 'posix_prefix' + elif 'rpm_prefix' in schemes: + kwargs['scheme'] = 'rpm_prefix' print(os.path.relpath(sysconfig.get_path('purelib', **kwargs), start='${CMAKE_INSTALL_PREFIX}').replace(os.sep, '/'))" ) get_executable_path(_python_interpreter Python3::Interpreter CONFIGURE)