| 1 |
# fmt: off |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import os |
| 6 |
from contextlib import contextmanager |
| 7 |
from pathlib import Path |
| 8 |
from sys import version_info as _python_version_tuple |
| 9 |
from typing import Iterator, TypedDict |
| 10 |
|
| 11 |
from expandvars import expandvars |
| 12 |
|
| 13 |
from ._compat import load_toml_from_string |
| 14 |
from ._transformers import get_cli_kwargs_from_config, get_enabled_cli_flags_from_config |
| 15 |
|
| 16 |
|
| 17 |
class Config(TypedDict): |
| 18 |
env: dict[str, str] |
| 19 |
flags: dict[str, bool] |
| 20 |
kwargs: dict[str, str | dict[str, str]] |
| 21 |
src: list[str] |
| 22 |
|
| 23 |
|
| 24 |
def _configure_cython_line_tracing(config_kwargs: dict[str, str | dict[str, str]], cython_line_tracing_requested: bool) -> None: |
| 25 |
"""Configure Cython line tracing directives if requested.""" |
| 26 |
# If line tracing is requested, add it to the directives |
| 27 |
if cython_line_tracing_requested: |
| 28 |
directives = config_kwargs.setdefault('directive', {}) |
| 29 |
assert isinstance(directives, dict) # Type narrowing for mypy |
| 30 |
directives['linetrace'] = 'True' |
| 31 |
directives['profile'] = 'True' |
| 32 |
|
| 33 |
|
| 34 |
def get_local_cython_config() -> Config: |
| 35 |
"""Grab optional build dependencies from pyproject.toml config. |
| 36 |
|
| 37 |
:returns: config section from ``pyproject.toml`` |
| 38 |
:rtype: dict |
| 39 |
|
| 40 |
This basically reads entries from:: |
| 41 |
|
| 42 |
[tool.local.cythonize] |
| 43 |
# Env vars provisioned during cythonize call |
| 44 |
src = ["src/**/*.pyx"] |
| 45 |
|
| 46 |
[tool.local.cythonize.env] |
| 47 |
# Env vars provisioned during cythonize call |
| 48 |
LDFLAGS = "-lssh" |
| 49 |
|
| 50 |
[tool.local.cythonize.flags] |
| 51 |
# This section can contain the following booleans: |
| 52 |
# * annotate — generate annotated HTML page for source files |
| 53 |
# * build — build extension modules using distutils |
| 54 |
# * inplace — build extension modules in place using distutils (implies -b) |
| 55 |
# * force — force recompilation |
| 56 |
# * quiet — be less verbose during compilation |
| 57 |
# * lenient — increase Python compat by ignoring some compile time errors |
| 58 |
# * keep-going — compile as much as possible, ignore compilation failures |
| 59 |
annotate = false |
| 60 |
build = false |
| 61 |
inplace = true |
| 62 |
force = true |
| 63 |
quiet = false |
| 64 |
lenient = false |
| 65 |
keep-going = false |
| 66 |
|
| 67 |
[tool.local.cythonize.kwargs] |
| 68 |
# This section can contain args that have values: |
| 69 |
# * exclude=PATTERN exclude certain file patterns from the compilation |
| 70 |
# * parallel=N run builds in N parallel jobs (default: calculated per system) |
| 71 |
exclude = "**.py" |
| 72 |
parallel = 12 |
| 73 |
|
| 74 |
[tool.local.cythonize.kwargs.directives] |
| 75 |
# This section can contain compiler directives |
| 76 |
# NAME = "VALUE" |
| 77 |
|
| 78 |
[tool.local.cythonize.kwargs.compile-time-env] |
| 79 |
# This section can contain compile time env vars |
| 80 |
# NAME = "VALUE" |
| 81 |
|
| 82 |
[tool.local.cythonize.kwargs.options] |
| 83 |
# This section can contain cythonize options |
| 84 |
# NAME = "VALUE" |
| 85 |
""" |
| 86 |
config_toml_txt = (Path.cwd().resolve() / 'pyproject.toml').read_text() |
| 87 |
config_mapping = load_toml_from_string(config_toml_txt) |
| 88 |
return config_mapping['tool']['local']['cythonize'] # type: ignore[no-any-return] |
| 89 |
|
| 90 |
|
| 91 |
def make_cythonize_cli_args_from_config(config: Config, cython_line_tracing_requested: bool = False) -> list[str]: |
| 92 |
py_ver_arg = f'-{_python_version_tuple.major!s}' |
| 93 |
|
| 94 |
cli_flags = get_enabled_cli_flags_from_config(config['flags']) |
| 95 |
|
| 96 |
config_kwargs = config['kwargs'] |
| 97 |
_configure_cython_line_tracing(config_kwargs, cython_line_tracing_requested) |
| 98 |
|
| 99 |
cli_kwargs = get_cli_kwargs_from_config(config_kwargs) |
| 100 |
|
| 101 |
return cli_flags + [py_ver_arg] + cli_kwargs + ['--'] + config['src'] |
| 102 |
|
| 103 |
|
| 104 |
@contextmanager |
| 105 |
def patched_env(env: dict[str, str], cython_line_tracing_requested: bool) -> Iterator[None]: |
| 106 |
"""Temporary set given env vars. |
| 107 |
|
| 108 |
:param env: tmp env vars to set |
| 109 |
:type env: dict |
| 110 |
|
| 111 |
:yields: None |
| 112 |
""" |
| 113 |
orig_env = os.environ.copy() |
| 114 |
expanded_env = {name: expandvars(var_val) for name, var_val in env.items()} |
| 115 |
os.environ.update(expanded_env) |
| 116 |
|
| 117 |
if cython_line_tracing_requested: |
| 118 |
os.environ['CFLAGS'] = ' '.join(( |
| 119 |
os.getenv('CFLAGS', ''), |
| 120 |
'-DCYTHON_TRACE_NOGIL=1', # Implies CYTHON_TRACE=1 |
| 121 |
)).strip() |
| 122 |
try: |
| 123 |
yield |
| 124 |
finally: |
| 125 |
os.environ.clear() |
| 126 |
os.environ.update(orig_env) |