From 5359d6f8da2332686a80efe1e3843dc9fee6c846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20R=C3=B6hling?= Date: Wed, 21 Feb 2024 21:03:32 +0100 Subject: [PATCH] Improve Python install path detection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR makes the customized Python path scheme logic in colcon contingent on an install prefix other than `/usr`. This makes colcon more distribution-friendly and prevents the accidental overwriting of system packages in case someone decides to install their packages to `/usr`. There is no other change in behavior. Signed-off-by: Timo Röhling --- colcon_core/python_install_path.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/colcon_core/python_install_path.py b/colcon_core/python_install_path.py index 122615d8..ca439a9c 100644 --- a/colcon_core/python_install_path.py +++ b/colcon_core/python_install_path.py @@ -18,16 +18,21 @@ def get_python_install_path(name, vars_=()): """ kwargs = {} kwargs['vars'] = dict(vars_) - # Avoid deb_system because it means using --install-layout deb - # which ignores --prefix and hardcodes it to /usr - if 'deb_system' in sysconfig.get_scheme_names() or \ - 'osx_framework_library' in sysconfig.get_scheme_names(): - kwargs['scheme'] = 'posix_prefix' - # The presence of the rpm_prefix scheme indicates that posix_prefix - # has been patched to inject `local` into the installation locations. - # The rpm_prefix scheme is a backup of what posix_prefix was before it was - # patched. - elif 'rpm_prefix' in sysconfig.get_scheme_names(): - kwargs['scheme'] = 'rpm_prefix' + + install_base = kwargs['vars'].get('base', sysconfig.get_config_var('base')) + if install_base != sysconfig.get_config_var('base'): + # If we are not actually installing to the default location, the default + # path scheme may be inadequate. Check if we need to override. + schemes = sysconfig.get_scheme_names() + if 'deb_system' in schemes or 'osx_framework_library' in schemes: + # Avoid deb_system because it requires using --install-layout deb + # which ignores --prefix and hardcodes it to /usr + kwargs['scheme'] = 'posix_prefix' + elif 'rpm_prefix' in schemes: + # The presence of the rpm_prefix scheme indicates that posix_prefix + # has been patched to inject `local` into the installation + # locations. The rpm_prefix scheme is a backup of what posix_prefix + # was before it was patched. + kwargs['scheme'] = 'rpm_prefix' return Path(sysconfig.get_path(name, **kwargs))