| 1 |
""" |
| 2 |
Locate `libspatialindex` shared library and header files. |
| 3 |
""" |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import ctypes |
| 8 |
import importlib.metadata |
| 9 |
import os |
| 10 |
import platform |
| 11 |
import sys |
| 12 |
from ctypes.util import find_library |
| 13 |
from pathlib import Path |
| 14 |
|
| 15 |
_cwd = Path(__file__).parent |
| 16 |
_sys_prefix = Path(sys.prefix) |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
_candidates = [] |
| 21 |
if "SPATIALINDEX_C_LIBRARY" in os.environ: |
| 22 |
_candidates.append(Path(os.environ["SPATIALINDEX_C_LIBRARY"])) |
| 23 |
_candidates += [_cwd / "lib", _cwd, Path("")] |
| 24 |
|
| 25 |
|
| 26 |
def load() -> ctypes.CDLL: |
| 27 |
"""Load the `libspatialindex` shared library. |
| 28 |
|
| 29 |
:returns: Loaded shared library |
| 30 |
""" |
| 31 |
if os.name == "nt": |
| 32 |
|
| 33 |
if "64" in platform.architecture()[0]: |
| 34 |
arch = "64" |
| 35 |
else: |
| 36 |
arch = "32" |
| 37 |
lib_name = f"spatialindex_c-{arch}.dll" |
| 38 |
|
| 39 |
|
| 40 |
if (_sys_prefix / "conda-meta").exists() or "conda" in sys.version: |
| 41 |
_candidates.append(_sys_prefix / "Library" / "bin") |
| 42 |
|
| 43 |
|
| 44 |
oldenv = os.environ.get("PATH", "").strip().rstrip(";") |
| 45 |
|
| 46 |
for path in _candidates: |
| 47 |
if not path.exists(): |
| 48 |
continue |
| 49 |
|
| 50 |
|
| 51 |
os.environ["PATH"] = ";".join([str(path), oldenv]) |
| 52 |
try: |
| 53 |
rt = ctypes.cdll.LoadLibrary(str(path / lib_name)) |
| 54 |
if rt is not None: |
| 55 |
return rt |
| 56 |
except OSError: |
| 57 |
pass |
| 58 |
except BaseException as err: |
| 59 |
print(f"rtree.finder unexpected error: {err!s}", file=sys.stderr) |
| 60 |
finally: |
| 61 |
os.environ["PATH"] = oldenv |
| 62 |
raise OSError(f"could not find or load {lib_name}") |
| 63 |
|
| 64 |
elif os.name == "posix": |
| 65 |
|
| 66 |
|
| 67 |
if platform.system() == "Darwin": |
| 68 |
|
| 69 |
lib_name = "libspatialindex_c.dylib" |
| 70 |
else: |
| 71 |
|
| 72 |
lib_name = "libspatialindex_c.so" |
| 73 |
|
| 74 |
|
| 75 |
try: |
| 76 |
pkg_files = importlib.metadata.files("rtree") |
| 77 |
if pkg_files is not None: |
| 78 |
for file in pkg_files: |
| 79 |
if ( |
| 80 |
file.parent.name == "rtree.libs" |
| 81 |
and file.stem.startswith("libspatialindex") |
| 82 |
and ".so" in file.suffixes |
| 83 |
): |
| 84 |
_candidates.insert(1, Path(file.locate())) |
| 85 |
break |
| 86 |
except importlib.metadata.PackageNotFoundError: |
| 87 |
pass |
| 88 |
|
| 89 |
for cand in _candidates: |
| 90 |
if cand.is_dir(): |
| 91 |
|
| 92 |
path = cand |
| 93 |
target = cand / lib_name |
| 94 |
elif cand.is_file(): |
| 95 |
|
| 96 |
path = cand.parent |
| 97 |
target = cand |
| 98 |
else: |
| 99 |
continue |
| 100 |
|
| 101 |
if not target.exists(): |
| 102 |
continue |
| 103 |
|
| 104 |
try: |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
rt = ctypes.cdll.LoadLibrary(str(target)) |
| 109 |
if rt is not None: |
| 110 |
return rt |
| 111 |
except BaseException as err: |
| 112 |
print( |
| 113 |
f"rtree.finder ({target}) unexpected error: {err!s}", |
| 114 |
file=sys.stderr, |
| 115 |
) |
| 116 |
|
| 117 |
try: |
| 118 |
|
| 119 |
pth = find_library("spatialindex_c") |
| 120 |
if pth is not None: |
| 121 |
return ctypes.cdll.LoadLibrary(pth) |
| 122 |
|
| 123 |
except BaseException: |
| 124 |
pass |
| 125 |
|
| 126 |
raise OSError("Could not load libspatialindex_c library") |
| 127 |
|
| 128 |
|
| 129 |
def get_include() -> str: |
| 130 |
"""Return the directory that contains the spatialindex \\*.h files. |
| 131 |
|
| 132 |
:returns: Path to include directory or "" if not found. |
| 133 |
""" |
| 134 |
|
| 135 |
try: |
| 136 |
pkg_files = importlib.metadata.files("rtree") |
| 137 |
if pkg_files is not None: |
| 138 |
for path in pkg_files: |
| 139 |
if path.name == "SpatialIndex.h": |
| 140 |
return str(Path(path.locate()).parent.parent) |
| 141 |
except importlib.metadata.PackageNotFoundError: |
| 142 |
pass |
| 143 |
|
| 144 |
|
| 145 |
path_to_spatialindex_h = Path("include/spatialindex/SpatialIndex.h") |
| 146 |
|
| 147 |
|
| 148 |
if os.name == "nt": |
| 149 |
file = _sys_prefix / "Library" / path_to_spatialindex_h |
| 150 |
else: |
| 151 |
file = _sys_prefix / path_to_spatialindex_h |
| 152 |
if file.is_file(): |
| 153 |
return str(file.parent.parent) |
| 154 |
|
| 155 |
|
| 156 |
libdir = Path(load()._name).parent |
| 157 |
file = libdir.parent / path_to_spatialindex_h |
| 158 |
if file.is_file(): |
| 159 |
return str(file.parent.parent) |
| 160 |
|
| 161 |
|
| 162 |
file = Path("/usr") / path_to_spatialindex_h |
| 163 |
if file.is_file(): |
| 164 |
return str(file.parent.parent) |
| 165 |
|
| 166 |
|
| 167 |
return "" |