Skip to content

Commit

Permalink
transonic-get-include
Browse files Browse the repository at this point in the history
  • Loading branch information
paugier committed Feb 3, 2024
1 parent 46de4b1 commit f5bd4e6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 31 deletions.
33 changes: 2 additions & 31 deletions data_tests/package_for_test_meson/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
35 changes: 35 additions & 0 deletions src/transonic_cl/get_includes.py
Original file line number Diff line number Diff line change
@@ -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)
22 changes: 22 additions & 0 deletions tests/test_get_includes.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit f5bd4e6

Please sign in to comment.