|
| 1 | +"""Utility for testing presence and usability of .pxd files in the installation |
| 2 | +
|
| 3 | +Usage: |
| 4 | +------ |
| 5 | +python check_pxd_in_installation.py path/to/install_dir/of/scikit-learn |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +import sys |
| 10 | +import pathlib |
| 11 | +import tempfile |
| 12 | +import textwrap |
| 13 | +import subprocess |
| 14 | + |
| 15 | + |
| 16 | +sklearn_dir = pathlib.Path(sys.argv[1]) |
| 17 | +pxd_files = list(sklearn_dir.glob("**/*.pxd")) |
| 18 | + |
| 19 | +print("> Found pxd files:") |
| 20 | +for pxd_file in pxd_files: |
| 21 | + print(' -', pxd_file) |
| 22 | + |
| 23 | +print("\n> Trying to compile a cython extension cimporting all corresponding " |
| 24 | + "modules\n") |
| 25 | +with tempfile.TemporaryDirectory() as tmpdir: |
| 26 | + tmpdir = pathlib.Path(tmpdir) |
| 27 | + # A cython test file which cimports all modules corresponding to found |
| 28 | + # pxd files. |
| 29 | + # e.g. sklearn/tree/_utils.pxd becomes `cimport sklearn.tree._utils` |
| 30 | + with open(tmpdir / 'tst.pyx', 'w') as f: |
| 31 | + for pxd_file in pxd_files: |
| 32 | + to_import = str(pxd_file.relative_to(sklearn_dir)) |
| 33 | + to_import = to_import.replace(os.path.sep, '.') |
| 34 | + to_import = to_import.replace('.pxd', '') |
| 35 | + f.write('cimport sklearn.' + to_import + '\n') |
| 36 | + |
| 37 | + # A basic setup file to build the test file. |
| 38 | + # We set the language to c++ and we use numpy.get_include() because |
| 39 | + # some modules require it. |
| 40 | + with open(tmpdir / 'setup_tst.py', 'w') as f: |
| 41 | + f.write(textwrap.dedent( |
| 42 | + """ |
| 43 | + from distutils.core import setup |
| 44 | + from distutils.extension import Extension |
| 45 | + from Cython.Build import cythonize |
| 46 | + import numpy |
| 47 | +
|
| 48 | + extensions = [Extension("tst", |
| 49 | + sources=["tst.pyx"], |
| 50 | + language="c++", |
| 51 | + include_dirs=[numpy.get_include()])] |
| 52 | +
|
| 53 | + setup(ext_modules=cythonize(extensions)) |
| 54 | + """)) |
| 55 | + |
| 56 | + subprocess.run(["python", "setup_tst.py", "build_ext", "-i"], |
| 57 | + check=True, cwd=tmpdir) |
| 58 | + |
| 59 | + print("\n> Compilation succeeded !") |
0 commit comments