Skip to content

Commit 4121440

Browse files
committed
Reimplement how GEOS is found in setup file
1 parent 32cb65b commit 4121440

File tree

1 file changed

+39
-50
lines changed

1 file changed

+39
-50
lines changed

packages/basemap/setup.py

+39-50
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,38 @@ def get_content(name, splitlines=False):
2727
return content
2828

2929

30-
def checkversion(directory):
31-
"""Return GEOS version from GEOS C-API header file (geos_c.h)."""
30+
def get_geos_install_prefix():
31+
"""Return GEOS installation prefix or None if not found."""
32+
33+
env_candidate = os.environ.get("GEOS_DIR", None)
34+
if env_candidate is not None:
35+
candidates = [env_candidate]
36+
else:
37+
candidates = [os.path.expanduser("~/local"), os.path.expanduser("~"),
38+
"/usr/local", "/usr", "/opt/local", "/opt", "/sw"]
39+
40+
for prefix in candidates:
41+
libfiles = []
42+
libdirs = ["bin", "lib", "lib64"]
43+
libext = "dll" if os.name == "nt" else "so"
44+
libcode = "{0}geos_c".format("" if os.name == "nt" else "lib")
45+
libname = "{0}*.{1}*".format(libcode, libext)
46+
for libdir in libdirs:
47+
libfiles.extend(glob.glob(os.path.join(prefix, libdir, libname)))
48+
hfile = os.path.join(prefix, "include", "geos_c.h")
49+
if os.path.isfile(hfile) and libfiles:
50+
return prefix
3251

33-
version = None
34-
try:
35-
header_path = os.path.join(directory, "include", "geos_c.h")
36-
with io.open(header_path, "r", encoding="utf-8") as fd:
37-
for line in fd:
38-
if line.startswith("#define GEOS_VERSION"):
39-
version = line.split()[2]
40-
break
41-
except IOError:
42-
pass
43-
return version
52+
warnings.warn(" ".join([
53+
"Cannot find GEOS library and/or headers in standard locations",
54+
"('{0}'). Please install the corresponding packages using your",
55+
"software management system or set the environment variable",
56+
"GEOS_DIR to point to the location where GEOS is installed",
57+
"(for example, if 'geos_c.h' is in '/usr/local/include'",
58+
"and 'libgeos_c' is in '/usr/local/lib', then you need to",
59+
"set GEOS_DIR to '/usr/local'",
60+
]).format("', '".join(candidates)), RuntimeWarning)
61+
return None
4462

4563

4664
class basemap_sdist(sdist):
@@ -56,6 +74,7 @@ def finalize_options(self):
5674

5775

5876
# Initialise include and library dirs.
77+
data_files = []
5978
include_dirs = []
6079
library_dirs = []
6180
runtime_library_dirs = []
@@ -71,52 +90,22 @@ def finalize_options(self):
7190
except ImportError as err:
7291
warnings.warn("unable to locate NumPy headers", RuntimeWarning)
7392

74-
# Define GEOS install directory (from environment variable or trying to guess).
75-
geos_installdir = os.environ.get("GEOS_DIR", None)
76-
if geos_installdir is None:
77-
# Define some default locations to find GEOS.
78-
geos_search_locations = [
79-
os.path.expanduser("~/local"),
80-
os.path.expanduser("~"),
81-
"/usr/local",
82-
"/usr",
83-
"/opt/local",
84-
"/opt",
85-
"/sw"
86-
]
87-
# Loop over the default locations to see if we find something.
88-
for folder in geos_search_locations:
89-
geos_version = checkversion(folder)
90-
if geos_version is not None and geos_version >= '"3.1.1"':
91-
geos_installdir = folder
92-
break
93-
9493
# Define GEOS include, library and runtime dirs.
95-
if geos_installdir is None:
96-
warnings.warn(" ".join([
97-
"Cannot find GEOS library in standard locations ('{0}').",
98-
"Please install the corresponding packages using your",
99-
"software management system or set the environment variable",
100-
"GEOS_DIR to point to the location where GEOS is installed",
101-
"(for example, if 'geos_c.h' is in '/usr/local/include'",
102-
"and 'libgeos_c' is in '/usr/local/lib', then you need to",
103-
"set GEOS_DIR to '/usr/local'",
104-
]).format("', '".join(geos_search_locations)), RuntimeWarning)
105-
else:
106-
include_dirs.append(os.path.join(geos_installdir, "include"))
107-
library_dirs.append(os.path.join(geos_installdir, "lib"))
108-
library_dirs.append(os.path.join(geos_installdir, "lib64"))
94+
geos_install_prefix = get_geos_install_prefix()
95+
if geos_install_prefix is not None:
96+
include_dirs.append(os.path.join(geos_install_prefix, "include"))
97+
library_dirs.append(os.path.join(geos_install_prefix, "lib"))
98+
library_dirs.append(os.path.join(geos_install_prefix, "lib64"))
10999
runtime_library_dirs = library_dirs
110-
data_files = []
111100
if os.name == "nt":
112101
# On Windows:
113102
# - DLLs get installed under `bin`.
114103
# - We need to inject later the DLL in the wheel using `data_files`.
115104
# - We do not use `runtime_library_dirs` as workaround for a
116105
# `distutils` bug (http://bugs.python.org/issue2437).
117-
library_dirs.append(os.path.join(geos_installdir, "bin"))
106+
library_dirs.append(os.path.join(geos_install_prefix, "bin"))
118107
runtime_library_dirs = []
119-
dlls = glob.glob(os.path.join(geos_installdir, "*", "geos_c.dll"))
108+
dlls = glob.glob(os.path.join(geos_install_prefix, "*", "geos_c.dll"))
120109
if dlls:
121110
data_files.append(("../..", sorted(dlls)))
122111

0 commit comments

Comments
 (0)