| 1 |
"""Generate build-details.json (see PEP 739).""" |
| 2 |
|
| 3 |
# Script initially imported from: |
| 4 |
# https://github.com/FFY00/python-instrospection/blob/main/python_introspection/scripts/generate-build-details.py |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import argparse |
| 9 |
import collections |
| 10 |
import importlib.machinery |
| 11 |
import json |
| 12 |
import os |
| 13 |
import sys |
| 14 |
import sysconfig |
| 15 |
|
| 16 |
TYPE_CHECKING = False |
| 17 |
if TYPE_CHECKING: |
| 18 |
from typing import Any |
| 19 |
|
| 20 |
|
| 21 |
def version_info_to_dict(obj: sys._version_info) -> dict[str, Any]: |
| 22 |
field_names = ('major', 'minor', 'micro', 'releaselevel', 'serial') |
| 23 |
return {field: getattr(obj, field) for field in field_names} |
| 24 |
|
| 25 |
|
| 26 |
def get_dict_key(container: dict[str, Any], key: str) -> dict[str, Any]: |
| 27 |
for part in key.split('.'): |
| 28 |
container = container[part] |
| 29 |
return container |
| 30 |
|
| 31 |
|
| 32 |
def generate_data(schema_version: str) -> collections.defaultdict[str, Any]: |
| 33 |
"""Generate the build-details.json data (PEP 739). |
| 34 |
|
| 35 |
:param schema_version: The schema version of the data we want to generate. |
| 36 |
""" |
| 37 |
|
| 38 |
if schema_version != '1.0': |
| 39 |
raise ValueError(f'Unsupported schema_version: {schema_version}') |
| 40 |
|
| 41 |
data: collections.defaultdict[str, Any] = collections.defaultdict( |
| 42 |
lambda: collections.defaultdict(dict), |
| 43 |
) |
| 44 |
|
| 45 |
data['schema_version'] = schema_version |
| 46 |
|
| 47 |
data['base_prefix'] = sysconfig.get_config_var('installed_base') |
| 48 |
#data['base_interpreter'] = sys._base_executable |
| 49 |
data['base_interpreter'] = os.path.join( |
| 50 |
sysconfig.get_path('scripts'), |
| •51 |
"python" |
| 52 |
+ sysconfig.get_config_var('LDVERSION') |
| 53 |
+ sysconfig.get_config_var('EXE'), |
| 54 |
) |
| 55 |
data['platform'] = sysconfig.get_platform() |
| 56 |
|
| 57 |
data['language']['version'] = sysconfig.get_python_version() |
| 58 |
data['language']['version_info'] = version_info_to_dict(sys.version_info) |
| 59 |
|
| 60 |
data['implementation'] = vars(sys.implementation).copy() |
| 61 |
data['implementation']['version'] = version_info_to_dict(sys.implementation.version) |
| 62 |
# Fix cross-compilation |
| 63 |
if '_multiarch' in data['implementation']: |
| 64 |
data['implementation']['_multiarch'] = sysconfig.get_config_var('MULTIARCH') |
| 65 |
|
| 66 |
data['abi']['flags'] = list(sys.abiflags) |
| 67 |
|
| 68 |
data['suffixes']['source'] = importlib.machinery.SOURCE_SUFFIXES |
| 69 |
data['suffixes']['bytecode'] = importlib.machinery.BYTECODE_SUFFIXES |
| 70 |
#data['suffixes']['optimized_bytecode'] = importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES |
| 71 |
#data['suffixes']['debug_bytecode'] = importlib.machinery.DEBUG_BYTECODE_SUFFIXES |
| 72 |
data['suffixes']['extensions'] = importlib.machinery.EXTENSION_SUFFIXES |
| 73 |
|
| 74 |
LIBDIR = sysconfig.get_config_var('LIBDIR') |
| 75 |
LDLIBRARY = sysconfig.get_config_var('LDLIBRARY') |
| 76 |
LIBRARY = sysconfig.get_config_var('LIBRARY') |
| 77 |
PY3LIBRARY = sysconfig.get_config_var('PY3LIBRARY') |
| 78 |
LIBPYTHON = sysconfig.get_config_var('LIBPYTHON') |
| 79 |
LIBPC = sysconfig.get_config_var('LIBPC') |
| 80 |
INCLUDEPY = sysconfig.get_config_var('INCLUDEPY') |
| 81 |
|
| 82 |
if os.name == 'posix': |
| 83 |
# On POSIX, LIBRARY is always the static library, while LDLIBRARY is the |
| 84 |
# dynamic library if enabled, otherwise it's the static library. |
| 85 |
# If LIBRARY != LDLIBRARY, support for the dynamic library is enabled. |
| 86 |
has_dynamic_library = LDLIBRARY != LIBRARY |
| 87 |
has_static_library = sysconfig.get_config_var('STATIC_LIBPYTHON') |
| 88 |
elif os.name == 'nt': |
| 89 |
# Windows can only use a dynamic library or a static library. |
| 90 |
# If it's using a dynamic library, sys.dllhandle will be set. |
| 91 |
# Static builds on Windows are not really well supported, though. |
| 92 |
# More context: https://github.com/python/cpython/issues/110234 |
| 93 |
has_dynamic_library = hasattr(sys, 'dllhandle') |
| 94 |
has_static_library = not has_dynamic_library |
| 95 |
else: |
| 96 |
raise NotADirectoryError(f'Unknown platform: {os.name}') |
| 97 |
|
| 98 |
# On POSIX, EXT_SUFFIX is set regardless if extension modules are supported |
| 99 |
# or not, and on Windows older versions of CPython only set EXT_SUFFIX when |
| 100 |
# extension modules are supported, but newer versions of CPython set it |
| 101 |
# regardless. |
| 102 |
# |
| 103 |
# We only want to set abi.extension_suffix and stable_abi_suffix if |
| 104 |
# extension modules are supported. |
| 105 |
if has_dynamic_library: |
| 106 |
data['abi']['extension_suffix'] = sysconfig.get_config_var('EXT_SUFFIX') |
| 107 |
|
| 108 |
# EXTENSION_SUFFIXES has been constant for a long time, and currently we |
| 109 |
# don't have a better information source to find the stable ABI suffix. |
| 110 |
for suffix in importlib.machinery.EXTENSION_SUFFIXES: |
| 111 |
if suffix.startswith('.abi'): |
| 112 |
data['abi']['stable_abi_suffix'] = suffix |
| 113 |
break |
| 114 |
|
| 115 |
data['libpython']['dynamic'] = os.path.join(LIBDIR, LDLIBRARY) |
| 116 |
# FIXME: Not sure if windows has a different dll for the stable ABI, and |
| 117 |
# even if it does, currently we don't have a way to get its name. |
| 118 |
if PY3LIBRARY: |
| 119 |
data['libpython']['dynamic_stableabi'] = os.path.join(LIBDIR, PY3LIBRARY) |
| 120 |
|
| 121 |
# Os POSIX, this is defined by the LIBPYTHON Makefile variable not being |
| 122 |
# empty. On Windows, don't link extensions — LIBPYTHON won't be defined, |
| 123 |
data['libpython']['link_extensions'] = bool(LIBPYTHON) |
| 124 |
|
| 125 |
if has_static_library: |
| 126 |
data['libpython']['static'] = os.path.join(LIBDIR, LIBRARY) |
| 127 |
|
| 128 |
data['c_api']['headers'] = INCLUDEPY |
| 129 |
if LIBPC: |
| 130 |
data['c_api']['pkgconfig_path'] = LIBPC |
| 131 |
|
| 132 |
return data |
| 133 |
|
| 134 |
|
| 135 |
def make_paths_relative(data: dict[str, Any], config_path: str | None = None) -> None: |
| 136 |
# Make base_prefix relative to the config_path directory |
| 137 |
if config_path: |
| 138 |
data['base_prefix'] = relative_path(data['base_prefix'], |
| 139 |
os.path.dirname(config_path)) |
| 140 |
base_prefix = data['base_prefix'] |
| 141 |
|
| 142 |
# Update path values to make them relative to base_prefix |
| 143 |
PATH_KEYS = ( |
| 144 |
'base_interpreter', |
| 145 |
'libpython.dynamic', |
| 146 |
'libpython.dynamic_stableabi', |
| 147 |
'libpython.static', |
| 148 |
'c_api.headers', |
| 149 |
'c_api.pkgconfig_path', |
| 150 |
) |
| 151 |
for entry in PATH_KEYS: |
| 152 |
*parents, child = entry.split('.') |
| 153 |
# Get the key container object |
| 154 |
try: |
| 155 |
container = data |
| 156 |
for part in parents: |
| 157 |
container = container[part] |
| 158 |
if child not in container: |
| 159 |
raise KeyError(child) |
| 160 |
current_path = container[child] |
| 161 |
except KeyError: |
| 162 |
continue |
| 163 |
# Get the relative path |
| 164 |
new_path = relative_path(current_path, base_prefix) |
| 165 |
# Join '.' so that the path is formated as './path' instead of 'path' |
| 166 |
new_path = os.path.join('.', new_path) |
| 167 |
container[child] = new_path |
| 168 |
|
| 169 |
|
| 170 |
def relative_path(path: str, base: str) -> str: |
| 171 |
if os.name != 'nt': |
| 172 |
return os.path.relpath(path, base) |
| 173 |
|
| 174 |
# There are no relative paths between drives on Windows. |
| 175 |
path_drv, _ = os.path.splitdrive(path) |
| 176 |
base_drv, _ = os.path.splitdrive(base) |
| 177 |
if path_drv.lower() == base_drv.lower(): |
| 178 |
return os.path.relpath(path, base) |
| 179 |
|
| 180 |
return path |
| 181 |
|
| 182 |
|
| 183 |
def main() -> None: |
| 184 |
parser = argparse.ArgumentParser(exit_on_error=False) |
| 185 |
parser.add_argument('location') |
| 186 |
parser.add_argument( |
| 187 |
'--schema-version', |
| 188 |
default='1.0', |
| 189 |
help='Schema version of the build-details.json file to generate.', |
| 190 |
) |
| 191 |
parser.add_argument( |
| 192 |
'--relative-paths', |
| 193 |
action='store_true', |
| 194 |
help='Whether to specify paths as absolute, or as relative paths to ``base_prefix``.', |
| 195 |
) |
| 196 |
parser.add_argument( |
| 197 |
'--config-file-path', |
| 198 |
default=None, |
| 199 |
help='If specified, ``base_prefix`` will be set as a relative path to the given config file path.', |
| 200 |
) |
| 201 |
|
| 202 |
args = parser.parse_args() |
| 203 |
|
| 204 |
data = generate_data(args.schema_version) |
| 205 |
if args.relative_paths: |
| 206 |
make_paths_relative(data, args.config_file_path) |
| 207 |
|
| 208 |
json_output = json.dumps(data, indent=2) |
| 209 |
with open(args.location, 'w', encoding='utf-8') as f: |
| 210 |
f.write(json_output) |
| 211 |
f.write('\n') |
| 212 |
|
| 213 |
|
| 214 |
if __name__ == '__main__': |
| 215 |
main() |