| 1 |
# fmt: off |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import sys |
| 6 |
from itertools import chain |
| 7 |
from pathlib import Path |
| 8 |
from typing import Sequence |
| 9 |
|
| 10 |
from Cython.Compiler.Main import compile as _translate_cython_cli_cmd |
| 11 |
from Cython.Compiler.Main import parse_command_line as _split_cython_cli_args |
| 12 |
|
| 13 |
from ._cython_configuration import get_local_cython_config as _get_local_cython_config |
| 14 |
from ._cython_configuration import ( |
| 15 |
make_cythonize_cli_args_from_config as _make_cythonize_cli_args_from_config, |
| 16 |
) |
| 17 |
from ._cython_configuration import patched_env as _patched_cython_env |
| 18 |
|
| 19 |
_PROJECT_PATH = Path(__file__).parents[2] |
| 20 |
|
| 21 |
|
| 22 |
def run_main_program(argv: Sequence[str]) -> int | str: |
| 23 |
"""Invoke ``translate-cython`` or fail.""" |
| 24 |
if len(argv) != 2: |
| 25 |
return 'This program only accepts one argument -- "translate-cython"' |
| 26 |
|
| 27 |
if argv[1] != 'translate-cython': |
| 28 |
return 'This program only implements the "translate-cython" subcommand' |
| 29 |
|
| 30 |
config = _get_local_cython_config() |
| 31 |
config['flags'] = {'keep-going': config['flags']['keep-going']} |
| 32 |
config['src'] = list( |
| 33 |
map( |
| 34 |
str, |
| 35 |
chain.from_iterable( |
| 36 |
map(_PROJECT_PATH.glob, config['src']), |
| 37 |
), |
| 38 |
), |
| 39 |
) |
| 40 |
translate_cython_cli_args = _make_cythonize_cli_args_from_config(config) |
| 41 |
|
| 42 |
cython_options, cython_sources = _split_cython_cli_args( |
| 43 |
translate_cython_cli_args, |
| 44 |
) |
| 45 |
|
| 46 |
with _patched_cython_env(config['env'], cython_line_tracing_requested=True): |
| 47 |
return _translate_cython_cli_cmd( # type: ignore[no-any-return] |
| 48 |
cython_sources, |
| 49 |
cython_options, |
| 50 |
).num_errors |
| 51 |
|
| 52 |
|
| 53 |
if __name__ == '__main__': |
| 54 |
sys.exit(run_main_program(argv=sys.argv)) |