From cb145aa3808be810563ba27e4ec0c8f796c634cf Mon Sep 17 00:00:00 2001 From: "Malte J. Ziebarth" Date: Fri, 29 Dec 2023 21:37:31 +0100 Subject: [PATCH] Add 'gravelspoon' script that can be used in a meson.build file to obtain the NumPy include files. --- README.md | 19 +++++++++++ mebuex/scripts/__init__.py | 8 +++++ mebuex/scripts/gravelspoon.py | 64 +++++++++++++++++++++++++++++++++++ pyproject.toml | 7 ++-- 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 mebuex/scripts/__init__.py create mode 100644 mebuex/scripts/gravelspoon.py diff --git a/README.md b/README.md index 899dce1..44f47dc 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,20 @@ setup(name='mypackage', ) ``` +### Finding the NumPy include directory +Mebuex ships with a Python script to discover the system NumPy includes when +building in the isolated build environment. The `gravelspoon` command that +helps break out of the build prison can be used in a `meson.build` file as +follows: +```python +incpath_np = run_command( + gravelspoon +) + +incdir_np = include_directories([incpath_np]) +``` + + ## Install Mebuex can be installed with Pip: ```bash @@ -57,6 +71,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +### [1.2.0] - 2023-12-29 +#### Added +- Add the `gravelspoon` command to break out of the setuptools build isolation + and find the system NumPy includes. + ### [1.1.7] - 2023-08-20 #### Added - Print error message stderr when re-raising assumed compilation error. diff --git a/mebuex/scripts/__init__.py b/mebuex/scripts/__init__.py new file mode 100644 index 0000000..b2ac4a8 --- /dev/null +++ b/mebuex/scripts/__init__.py @@ -0,0 +1,8 @@ +# Command line scripts. +# +# Author: Malte J. Ziebarth (mjz.science@fmvkb.de) +# +# Copyright 2023, Malte J. Ziebarth +# SPDX-License-Identifier: MIT + +from .gravelspoon import numpy_get_include diff --git a/mebuex/scripts/gravelspoon.py b/mebuex/scripts/gravelspoon.py new file mode 100644 index 0000000..b3829b3 --- /dev/null +++ b/mebuex/scripts/gravelspoon.py @@ -0,0 +1,64 @@ +# Breaking out of the setuptools build isolation to use the system's +# NumPy includes. +# +# Author: Malte J. Ziebarth (mjz.science@fmvkb.de) +# +# Copyright 2023, Malte J. Ziebarth +# SPDX-License-Identifier: MIT + + +def numpy_get_include(): + """ + This function tries to find the NumPy include directory, + potentially within a build isolation, and prints the include + path to standard out. + """ + try: + # In case things work out of the box: + import numpy + print(numpy.get_include()) + + except ImportError: + import subprocess + import sys + from pathlib import Path + from os import rename, symlink + np_incl_byte = subprocess.check_output( + [sys.executable,'-c','import os;os.chdir("..");import numpy;' + 'print(numpy.get_include())'], + env={} + ) + + np_include = np_incl_byte.decode().strip() + + # Make numpy available in the isolated environment this code is running + # in. Do this by symlinking the previously discovered system NumPy + # package into a site-package directory found in the isolated + # environment Python path. + found_site_packages = False + success = False + for path in sys.path[::-1]: + p = Path(path) + if 'site-packages' in path: + found_site_packages = True + p = Path(path) + if not p.exists(): + continue + is_dir = (p / "numpy").is_dir() + if is_dir: + rename((p / "numpy").resolve(), (p / "numpyold").resolve()) + symlink(Path(np_include).parent.parent.resolve(), + (p / "numpy").resolve()) + success = True + break + + if not success: + msg = "Could not link the NumPy package to the isolated " \ + "site-packages." + if found_site_packages: + msg += " Found site-packages but did not exist." + else: + msg += " Found no site-pacakges." + raise RuntimeError(msg) + + print(np_include) diff --git a/pyproject.toml b/pyproject.toml index 7a29fd6..723ee9b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ requires = ["setuptools>=61"] [project] name = "mebuex" -version = "1.1.7" +version = "1.2.0" authors = [ {name = "Malte J. Ziebarth", email = "mjz.science@fmvkb.de"}, ] @@ -28,5 +28,8 @@ license = {text="MIT"} "Homepage" = "https://github.com/mjziebarth/Mebuex" "Bug Tracker" = "https://github.com/mjziebarth/Mebuex/issues" +[project.scripts] +gravelspoon = "mebuex.scripts:numpy_get_include" + [tool.setuptools] -packages = ["mebuex"] \ No newline at end of file +packages = ["mebuex","mebuex.scripts"] \ No newline at end of file