-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'gravelspoon' script that can be used in a meson.build file to ob…
…tain the NumPy include files.
- Loading branch information
1 parent
1fe3e2e
commit cb145aa
Showing
4 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Command line scripts. | ||
# | ||
# Author: Malte J. Ziebarth ([email protected]) | ||
# | ||
# Copyright 2023, Malte J. Ziebarth | ||
# SPDX-License-Identifier: MIT | ||
|
||
from .gravelspoon import numpy_get_include |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Breaking out of the setuptools build isolation to use the system's | ||
# NumPy includes. | ||
# | ||
# Author: Malte J. Ziebarth ([email protected]) | ||
# | ||
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ requires = ["setuptools>=61"] | |
|
||
[project] | ||
name = "mebuex" | ||
version = "1.1.7" | ||
version = "1.2.0" | ||
authors = [ | ||
{name = "Malte J. Ziebarth", email = "[email protected]"}, | ||
] | ||
|
@@ -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"] | ||
packages = ["mebuex","mebuex.scripts"] |