forked from hemmelig/AnisotroPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
executable file
·237 lines (182 loc) · 7.05 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# -*- coding: utf-8 -*-
import os
import pathlib
import re
import shutil
import sys
from distutils.ccompiler import get_default_compiler
from pkg_resources import get_build_platform
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
# Check if we are on RTD and don't build extensions if we are.
READ_THE_DOCS = os.environ.get("READTHEDOCS", None) == "True"
if READ_THE_DOCS:
try:
environ = os.environb
except AttributeError:
environ = os.environ
environ[b"CC"] = b"x86_64-linux-gnu-gcc"
environ[b"LD"] = b"x86_64-linux-gnu-ld"
environ[b"AR"] = b"x86_64-linux-gnu-ar"
# Directory of the current file
SETUP_DIRECTORY = pathlib.Path.cwd()
long_description = """A Python package for standard analysis routines in
the study of seismic anisotropy."""
def read(*parts):
"""
Build an absolute path from *parts* and and return the contents of the
resulting file.
"""
p = SETUP_DIRECTORY
for part in parts:
p /= part
with p.open("r") as f:
return f.read()
META_FILE = read("anisotropy", "__init__.py")
def find_meta(meta):
"""
Extract __*meta*__ from META_FILE.
"""
meta_match = re.search(
r"^__{meta}__ = ['\"]([^'\"]*)['\"]".format(meta=meta),
META_FILE, re.M
)
if meta_match:
return meta_match.group(1)
raise RuntimeError("Unable to find __{meta}__ string.".format(meta=meta))
def get_package_data():
package_data = {}
if not READ_THE_DOCS:
if get_build_platform() in ("win32", "win-amd64"):
package_data["anisotropy.core"] = ["anisotropy/core/src/*.dll"]
return package_data
def get_package_dir():
package_dir = {}
if get_build_platform() in ("win32", "win-amd64"):
package_dir["anisotropy.core"] = str(
pathlib.Path("anisotropy") / "core")
return package_dir
def get_extras_require():
if READ_THE_DOCS:
return {"docs": ['Sphinx >= 1.8.1', 'docutils']}
def get_include_dirs():
import numpy
include_dirs = [str(pathlib.Path.cwd() / "anisotropy" / "core" / "src"),
numpy.get_include(),
str(pathlib.Path(sys.prefix) / "include")]
if get_build_platform().startswith("freebsd"):
include_dirs.append("/usr/local/include")
return include_dirs
def get_library_dirs():
library_dirs = []
if get_build_platform() in ("win32", "win-amd64"):
library_dirs.append(str(pathlib.Path.cwd() / "anisotropy" / "core"))
library_dirs.append(str(pathlib.Path(sys.prefix) / "bin"))
library_dirs.append(str(pathlib.Path(sys.prefix) / "lib"))
if get_build_platform().startswith("freebsd"):
library_dirs.append("/usr/local/lib")
return library_dirs
def export_symbols(*parts):
"""
Required for Windows systems - functions defined in anisotropylib.def.
"""
p = SETUP_DIRECTORY
for part in parts:
p /= part
with p.open("r") as f:
lines = f.readlines()[2:]
return [s.strip() for s in lines if s.strip() != ""]
def get_extensions():
if READ_THE_DOCS:
return []
common_extension_args = {
"include_dirs": get_include_dirs(),
"library_dirs": get_library_dirs()}
sources = [str(pathlib.Path("anisotropy")
/ "core" / "src" / "anisotropy.c")]
exp_symbols = export_symbols("anisotropy/core/src/anisotropylib.def")
if get_build_platform() not in ("win32", "win-amd64"):
if get_build_platform().startswith("freebsd"):
# Clang uses libomp not libgomp
extra_link_args = ["-lm", "-lomp"]
else:
extra_link_args = ["-lm", "-lgomp"]
extra_compile_args = ["-fopenmp", "-fPIC", "-Ofast"]
else:
extra_link_args = []
extra_compile_args = ["/openmp", "/TP", "/O2"]
common_extension_args["extra_link_args"] = extra_link_args
common_extension_args["extra_compile_args"] = extra_compile_args
common_extension_args["export_symbols"] = exp_symbols
ext_modules = [Extension("anisotropy.core.src.anisotropylib",
sources=sources, **common_extension_args)]
return ext_modules
class CustomBuildExt(build_ext):
def finalize_options(self):
build_ext.finalize_options(self)
if self.compiler is None:
compiler = get_default_compiler()
else:
compiler = self.compiler
if compiler == "msvc":
# Sort linking issues with init exported symbols
def _get_export_symbols(self, ext):
return ext.export_symbols
build_ext.get_export_symbols = _get_export_symbols
def setup_package():
"""Setup package"""
if not READ_THE_DOCS:
install_requires = ["matplotlib", "numpy", "obspy", "pandas", "pyproj",
"scipy"]
else:
install_requires = ["matplotlib", "mock", "numpy", "obspy", "pandas",
"pyproj", "scipy"]
setup_args = {
"name": "anisotropy",
"version": find_meta("version"),
"description": find_meta("description"),
"long_description": long_description,
"url": "https://github.com/hemmelig/AnisotroPy",
"author": find_meta("author"),
"author_email": find_meta("email"),
"license": find_meta("license"),
"classifiers": [
"Development Status :: 3 - Alpha",
"Intended Audience :: Science/Research",
"Topic :: Scientific/Engineering",
"Natural Language :: English",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
],
"keywords": "standard routines in the study of seismic anisotropy",
"install_requires": install_requires,
"extras_require": get_extras_require(),
"zip_safe": False,
"packages": ["anisotropy",
"anisotropy.effective_modelling",
"anisotropy.materials",
"anisotropy.utils"],
"package_data": get_package_data(),
"package_dir": get_package_dir()
}
shutil.rmtree(str(SETUP_DIRECTORY / "build"), ignore_errors=True)
setup(**setup_args)
if __name__ == "__main__":
# clean --all does not remove extensions automatically
if "clean" in sys.argv and "--all" in sys.argv:
# Delete complete build directory
path = SETUP_DIRECTORY / "build"
shutil.rmtree(str(path), ignore_errors=True)
# Delete all shared libs from clib directory
path = SETUP_DIRECTORY / "anisotropy" / "core" / "src"
for filename in path.glob("*.pyd"):
filename.unlink(missing_ok=True)
for filename in path.glob("*.so"):
filename.unlink(missing_ok=True)
for filename in path.glob("*.dll"):
filename.unlink(missing_ok=True)
else:
setup_package()