From f5bd4e692473d5ecd0142a11a8a69c315a713ee6 Mon Sep 17 00:00:00 2001 From: paugier Date: Sat, 3 Feb 2024 10:37:30 +0100 Subject: [PATCH] transonic-get-include --- data_tests/package_for_test_meson/meson.build | 33 ++--------------- pyproject.toml | 1 + src/transonic_cl/get_includes.py | 35 +++++++++++++++++++ tests/test_get_includes.py | 22 ++++++++++++ 4 files changed, 60 insertions(+), 31 deletions(-) create mode 100644 src/transonic_cl/get_includes.py create mode 100644 tests/test_get_includes.py diff --git a/data_tests/package_for_test_meson/meson.build b/data_tests/package_for_test_meson/meson.build index e4cfe48..97dc2d5 100644 --- a/data_tests/package_for_test_meson/meson.build +++ b/data_tests/package_for_test_meson/meson.build @@ -19,40 +19,11 @@ endif use_pythran = backend.contains('pythran') if use_pythran - incdir_numpy = run_command( - py, - [ - '-c', - '''import os -import numpy as np -try: - incdir = os.path.relpath(np.get_include()) -except Exception: - incdir = np.get_include() -print(incdir)''' - ], - check: true - ).stdout().strip() - + incdir_numpy = run_command('transonic-get-include', 'numpy', check: true).stdout().strip() inc_np = include_directories(incdir_numpy) np_dep = declare_dependency(include_directories: inc_np) - incdir_pythran = run_command( - py, - [ - '-c', - '''import os -import pythran; -incdir = os.path.dirname(pythran.__file__) -try: - incdir = os.path.relpath(incdir) -except Exception: - pass -print(incdir)''' - ], - check: true - ).stdout().strip() - + incdir_pythran = run_command('transonic-get-include', 'pythran', check: true).stdout().strip() pythran = find_program('pythran', native: true) cpp_args_pythran = [ diff --git a/pyproject.toml b/pyproject.toml index bfd2067..2a82fdb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,6 +33,7 @@ mpi = ["mpi4py"] [project.scripts] transonic = "transonic.run:run" +transonic-get-include = "transonic_cl.get_includes:main" [tool.pdm] package-dir = "src" diff --git a/src/transonic_cl/get_includes.py b/src/transonic_cl/get_includes.py new file mode 100644 index 0000000..a5fd7db --- /dev/null +++ b/src/transonic_cl/get_includes.py @@ -0,0 +1,35 @@ +import argparse +import sys +from pathlib import Path + +from importlib import import_module + + +def main(): + parser = argparse.ArgumentParser( + prog="transonic-get-include", + description="Get include directory for packages", + ) + + parser.add_argument("package", type=str, help="Package name") + + args = parser.parse_args() + + try: + mod = import_module(args.package) + except ImportError: + print("ImportError") + sys.exit(1) + + try: + path_include = Path(mod.get_include()) + except AttributeError: + print(f"No {args.package}.get_include") + sys.exit(1) + + try: + path_include = path_include.relative_to(Path.cwd()) + except ValueError: + pass + + print(path_include) diff --git a/tests/test_get_includes.py b/tests/test_get_includes.py new file mode 100644 index 0000000..79f5cf8 --- /dev/null +++ b/tests/test_get_includes.py @@ -0,0 +1,22 @@ +import pytest + +from transonic_cl.get_includes import main + + +class MyException(Exception): + pass + + +def test_get_include(monkeypatch): + monkeypatch.setattr("sys.argv", ["transonic-get-include", "numpy"]) + main() + + def _exit(exit_code): + if exit_code == 1: + raise MyException + + monkeypatch.setattr("sys.exit", _exit) + monkeypatch.setattr("sys.argv", ["transonic-get-include", "numpyyyy"]) + + with pytest.raises(MyException): + main()