From 78945fb9832e989453fbabc471d45bb71805eca8 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Fri, 5 Nov 2021 01:03:50 -0400 Subject: python module: add option to specify a python environment to install to The default behavior of installing relative to prefix may be unexpected, and is definitely wrong in many cases. Give users control in order to specify that yes, they actually want to install to a venv. This is particularly useful for projects that use meson as a build system for a python module, where *all* files shall be installed into the python site-packages. --- mesonbuild/modules/python.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'mesonbuild/modules') diff --git a/mesonbuild/modules/python.py b/mesonbuild/modules/python.py index 2b62ea3..a91ec58 100644 --- a/mesonbuild/modules/python.py +++ b/mesonbuild/modules/python.py @@ -338,11 +338,13 @@ variables.update({'base_prefix': getattr(sys, 'base_prefix', sys.prefix)}) print(json.dumps({ 'variables': variables, 'paths': paths, + 'sysconfig_paths': sysconfig.get_paths(), 'install_paths': install_paths, 'sys_paths': sys.path, 'version': sysconfig.get_python_version(), 'platform': sysconfig.get_platform(), 'is_pypy': '__pypy__' in sys.builtin_module_names, + 'is_venv': sys.prefix != variables['base_prefix'], 'link_libpython': links_against_libpython(), })) ''' @@ -352,7 +354,9 @@ if T.TYPE_CHECKING: install_paths: T.Dict[str, str] is_pypy: bool + is_venv: bool link_libpython: bool + sysconfig_paths: T.Dict[str, str] paths: T.Dict[str, str] platform: str suffix: str @@ -377,7 +381,9 @@ class PythonExternalProgram(ExternalProgram): self.info: 'PythonIntrospectionDict' = { 'install_paths': {}, 'is_pypy': False, + 'is_venv': False, 'link_libpython': False, + 'sysconfig_paths': {}, 'paths': {}, 'platform': 'sentinal', 'variables': {}, @@ -422,7 +428,22 @@ class PythonExternalProgram(ExternalProgram): return rel_path value = state.get_option(f'{key}dir', module='python') if value: + if state.is_user_defined_option('install_env', module='python'): + raise mesonlib.MesonException(f'python.{key}dir and python.install_env are mutually exclusive') return value + + install_env = state.get_option('install_env', module='python') + if install_env == 'auto': + install_env = 'venv' if self.info['is_venv'] else 'system' + + if install_env == 'system': + rel_path = os.path.join(self.info['variables']['prefix'], rel_path) + elif install_env == 'venv': + if not self.info['is_venv']: + raise mesonlib.MesonException('python.install_env cannot be set to "venv" unless you are in a venv!') + # inside a venv, deb_system is *never* active hence info['paths'] may be wrong + rel_path = self.info['sysconfig_paths'][key] + # Use python's path relative to prefix, and warn if that's not a location # python will lookup for modules. abs_path = Path(state.get_option('prefix'), rel_path) -- cgit v1.1