Debian Codegraph @

mesonbuild/scripts/python_info.py

raw
1 #!/usr/bin/env python
2
3 # ignore all lints for this file, since it is run by python2 as well
4
5 # type: ignore
6 # pylint: disable=deprecated-module
7
8 import sys
9
10 # do not inject mesonbuild.scripts
11 # python -P would work too, but is exclusive to >=3.11
12 if sys.path[0].endswith('scripts'):
13 del sys.path[0]
14
15 import json, os, sysconfig
16
17 def get_distutils_paths(scheme=None, prefix=None):
18 import distutils.dist
19 distribution = distutils.dist.Distribution()
20 install_cmd = distribution.get_command_obj('install')
21 if prefix is not None:
22 install_cmd.prefix = prefix
23 if scheme:
24 install_cmd.select_scheme(scheme)
25 install_cmd.finalize_options()
26 return {
27 'data': install_cmd.install_data,
28 'include': os.path.dirname(install_cmd.install_headers),
29 'platlib': install_cmd.install_platlib,
30 'purelib': install_cmd.install_purelib,
31 'scripts': install_cmd.install_scripts,
32 }
33
34 # On Debian derivatives, the Python interpreter shipped by the distribution uses
35 # a custom install scheme, deb_system, for the system install, and changes the
36 # default scheme to a custom one pointing to /usr/local and replacing
37 # site-packages with dist-packages.
38 # See https://github.com/mesonbuild/meson/issues/8739.
39 #
40 # We should be using sysconfig, but before 3.10.3, Debian only patches distutils.
41 # So we may end up falling back.
42
43 def get_install_paths():
44 if sys.version_info >= (3, 10):
45 scheme = sysconfig.get_default_scheme()
46 else:
47 scheme = sysconfig._get_default_scheme()
48
49 if sys.version_info >= (3, 10, 3):
50 if 'deb_system' in sysconfig.get_scheme_names():
51 scheme = 'deb_system'
52 else:
53 import distutils.command.install
54 if 'deb_system' in distutils.command.install.INSTALL_SCHEMES:
55 paths = get_distutils_paths(scheme='deb_system')
56 install_paths = get_distutils_paths(scheme='deb_system', prefix='')
57 return paths, install_paths
58
59 paths = sysconfig.get_paths(scheme=scheme)
60 empty_vars = {'base': '', 'platbase': '', 'installed_base': ''}
61 install_paths = sysconfig.get_paths(scheme=scheme, vars=empty_vars)
62 return paths, install_paths
63
64 paths, install_paths = get_install_paths()
65
66 def links_against_libpython():
67 # on versions supporting python-embed.pc, this is the non-embed lib
68 #
69 # PyPy is not yet up to 3.12 and work is still pending to export the
70 # relevant information (it doesn't automatically provide arbitrary
71 # Makefile vars)
72 if sys.version_info >= (3, 8) and not is_pypy:
73 variables = sysconfig.get_config_vars()
74 return bool(variables.get('LIBPYTHON', 'yes'))
75 else:
76 from distutils.core import Distribution, Extension
77 cmd = Distribution().get_command_obj('build_ext')
78 cmd.ensure_finalized()
79 return bool(cmd.get_libraries(Extension('dummy', [])))
80
81 variables = sysconfig.get_config_vars()
82 variables.update({'base_prefix': getattr(sys, 'base_prefix', sys.prefix)})
83
84 is_pypy = '__pypy__' in sys.builtin_module_names
85
86 if sys.version_info < (3, 0):
87 suffix = variables.get('SO')
88 elif sys.version_info < (3, 8, 7):
89 # https://bugs.python.org/issue?@action=redirect&bpo=39825
90 from distutils.sysconfig import get_config_var
91 suffix = get_config_var('EXT_SUFFIX')
92 else:
93 suffix = variables.get('EXT_SUFFIX')
94
95 limited_api_suffix = None
96 if sys.version_info >= (3, 2):
97 try:
98 from importlib.machinery import EXTENSION_SUFFIXES
99 limited_api_suffix = EXTENSION_SUFFIXES[1]
100 except Exception:
101 pass
102
103 # pypy supports modules targeting the limited api but
104 # does not use a special suffix to distinguish them:
105 # https://doc.pypy.org/en/latest/cpython_differences.html#permitted-abi-tags-in-extensions
106 if is_pypy:
107 limited_api_suffix = suffix
108
109 # Whether we're targeting a free-threaded CPython interpreter
110 is_freethreaded = bool(variables.get('Py_GIL_DISABLED', False))
111
112 print(json.dumps({
113 'variables': variables,
114 'paths': paths,
115 'sysconfig_paths': sysconfig.get_paths(),
116 'install_paths': install_paths,
117 'version': sysconfig.get_python_version(),
118 'platform': sysconfig.get_platform(),
119 'is_pypy': is_pypy,
120 'is_venv': sys.prefix != variables['base_prefix'],
121 'link_libpython': links_against_libpython(),
122 'suffix': suffix,
123 'limited_api_suffix': limited_api_suffix,
124 'is_freethreaded': is_freethreaded,
125 }))