Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modules: python: better handling of the Python paths for Debian #9288

Merged
merged 2 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions mesonbuild/modules/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,43 @@ def set_env(name, value):


INTROSPECT_COMMAND = '''\
import os.path
import sysconfig
import json
import sys

install_paths = sysconfig.get_paths(vars={'base': '', 'platbase': '', 'installed_base': ''})
import distutils.command.install

def get_distutils_paths(scheme=None, prefix=None):
import distutils.dist
distribution = distutils.dist.Distribution()
install_cmd = distribution.get_command_obj('install')
if prefix is not None:
install_cmd.prefix = prefix
if scheme:
install_cmd.select_scheme(scheme)
install_cmd.finalize_options()
return {
'data': install_cmd.install_data,
'include': os.path.dirname(install_cmd.install_headers),
'platlib': install_cmd.install_platlib,
'purelib': install_cmd.install_purelib,
'scripts': install_cmd.install_scripts,
}

# On Debian derivatives, the Python interpreter shipped by the distribution uses
# a custom install scheme, deb_system, for the system install, and changes the
# default scheme to a custom one pointing to /usr/local and replacing
# site-packages with dist-packages.
# See https://github.com/mesonbuild/meson/issues/8739.
# XXX: We should be using sysconfig, but Debian only patches distutils.

if 'deb_system' in distutils.command.install.INSTALL_SCHEMES:
paths = get_distutils_paths(scheme='deb_system')
install_paths = get_distutils_paths(scheme='deb_system', prefix='')
else:
paths = sysconfig.get_paths()
empty_vars = {'base': '', 'platbase': '', 'installed_base': ''}
install_paths = sysconfig.get_paths(vars=empty_vars)

def links_against_libpython():
from distutils.core import Distribution, Extension
Expand All @@ -290,7 +322,7 @@ def links_against_libpython():

print(json.dumps({
'variables': sysconfig.get_config_vars(),
'paths': sysconfig.get_paths(),
'paths': paths,
'install_paths': install_paths,
xclaesse marked this conversation as resolved.
Show resolved Hide resolved
'sys_paths': sys.path,
'version': sysconfig.get_python_version(),
Expand Down Expand Up @@ -373,14 +405,9 @@ def _get_path(self, key: str) -> None:
sys_paths = self.info['sys_paths']
rel_path = self.info['install_paths'][key][1:]
if not any(p.endswith(rel_path) for p in sys_paths if not p.startswith(user_dir)):
# On Debian derivatives sysconfig install path is broken and is not
# included in the locations python actually lookup.
# See https://github.com/mesonbuild/meson/issues/8739.
mlog.warning('Broken python installation detected. Python files',
'installed by Meson might not be found by python interpreter.',
once=True)
if mesonlib.is_debianlike():
rel_path = 'lib/python3/dist-packages'
return rel_path


Expand Down
4 changes: 2 additions & 2 deletions test cases/python/1 basic/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ if py_version.version_compare('< 3.2')
endif

py_purelib = py.get_path('purelib')
if not py_purelib.endswith('site-packages')
if not (py_purelib.endswith('site-packages') or py_purelib.endswith('dist-packages'))
error('Python3 purelib path seems invalid? ' + py_purelib)
endif
message('Python purelib path:', py_purelib)

# could be 'lib64' or 'Lib' on some systems
py_platlib = py.get_path('platlib')
if not py_platlib.endswith('site-packages')
if not (py_platlib.endswith('site-packages') or py_platlib.endswith('dist-packages'))
error('Python3 platlib path seems invalid? ' + py_platlib)
endif

Expand Down
4 changes: 2 additions & 2 deletions test cases/python3/1 basic/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ if py3_version.version_compare('< 3.2')
endif

py3_purelib = py3_mod.sysconfig_path('purelib')
if not py3_purelib.to_lower().startswith('lib') or not py3_purelib.endswith('site-packages')
if not py3_purelib.to_lower().startswith('lib') or not (py3_purelib.endswith('site-packages') or py3_purelib.endswith('dist-packages'))
error('Python3 purelib path seems invalid?')
endif

# could be 'lib64' or 'Lib' on some systems
py3_platlib = py3_mod.sysconfig_path('platlib')
if not py3_platlib.to_lower().startswith('lib') or not py3_platlib.endswith('site-packages')
if not py3_platlib.to_lower().startswith('lib') or not (py3_platlib.endswith('site-packages') or py3_platlib.endswith('dist-packages'))
error('Python3 platlib path seems invalid?')
endif

Expand Down