-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup.py
284 lines (238 loc) · 8.5 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import codecs
import os
import pathlib
import platform
import shutil
from collections import namedtuple
from setuptools import Extension, find_packages, setup
from setuptools.command.build_ext import build_ext as build_ext_orig
from meshkernel.version import __backend_version__
author_dict = {
"Julian Hofer": "[email protected]",
"Prisca van der Sluis": "[email protected]",
"Luca Carniato": "[email protected]",
}
__author__ = ", ".join(author_dict.keys())
__author_email__ = ", ".join(s for _, s in author_dict.items())
def read(rel_path: str) -> str:
"""Used to read a text file
Args:
rel_path (str): Relative path to the file
Returns:
str: File content
"""
here = os.path.abspath(os.path.dirname(__file__))
with codecs.open(os.path.join(here, rel_path), "r") as fp:
return fp.read()
def get_version(rel_path: str) -> str:
"""Get the version string
Args:
rel_path (str): Relative path to the file
Raises:
RuntimeError: Raised if the version string could not be found
Returns:
str: The version string
"""
for line in read(rel_path).splitlines():
if line.startswith("__version__"):
delim = '"' if '"' in line else "'"
return line.split(delim)[1]
raise RuntimeError("Unable to find version string.")
LibraryMeta = namedtuple("LibraryMeta", "system, name")
LibraryMeta.__doc__ = """A namedtuple that contains the library meta.
It has 2 fields:
system - System (OS) name
name - Library file name"""
def get_library_meta() -> LibraryMeta:
"""Get the filename of the MeshKernel library
Raises:
OSError: If the operating system is not supported
Returns:
LibraryMeta: A namedtuple containing the library meta
"""
system = platform.system()
if system == "Windows":
name = "MeshKernelApi.dll"
elif system == "Linux":
name = "libMeshKernelApi.so"
elif system == "Darwin":
name = "libMeshKernelApi.dylib"
else:
if not system:
system = "Unknown OS"
raise OSError("Unsupported operating system: {}".format(system))
return LibraryMeta(system, name)
try:
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
class bdist_wheel(_bdist_wheel):
"""Class describing our wheel.
Basically it says that it is not a pure Python package,
but it also does not contain any Python source and
therefore works for all Python versions
"""
def finalize_options(self):
_bdist_wheel.finalize_options(self)
# Mark us as not a pure python package
self.root_is_pure = False
def get_tag(self):
# We don't contain any python source
return "py3", "none", _bdist_wheel.get_tag(self)[2]
except ImportError:
bdist_wheel = None
class CMakeExtension(Extension):
"""Class for building a native cmake extension (C++)"""
def __init__(self, repository):
"""Constructor of CMakeExtension class
Args:
repository (str): The git repository of the extension to build
"""
name = repository.split("/")[-1]
super().__init__(name, sources=[])
self.repository = repository
class build_ext(build_ext_orig):
"""Class for building an extension using cmake"""
def run(self):
for ext in self.extensions:
self.build_cmake(ext)
super().run()
def build_cmake(self, ext):
cwd = str(pathlib.Path().absolute())
build_temp = pathlib.Path(self.build_temp)
build_temp.mkdir(parents=True, exist_ok=True)
extdir = pathlib.Path(self.get_ext_fullpath(ext.name))
extdir.mkdir(parents=True, exist_ok=True)
os.chdir(str(build_temp))
if not os.path.isdir(ext.name):
# clone repository
self.spawn(["git", "clone", ext.repository])
# switch to main, release or feature branch
branch = os.getenv("BACK_END_BRANCH")
try:
if branch == "release":
release_branch = "release/v" + __backend_version__
self.spawn(
[
"git",
"-C",
"./MeshKernel",
"switch",
"-C",
release_branch,
"origin/" + release_branch,
]
)
elif branch.startswith("feature/"):
self.spawn(
[
"git",
"-C",
"./MeshKernel",
"switch",
"-C",
branch,
"origin/" + branch,
]
)
else:
if branch != "master":
print(
"Invalid reference to branch origin/{}. Remaining on master branch.".format(
branch
)
)
else:
print("Remaining on master branch")
except Exception as ex:
# spawn failed because git switch command failed (remote/origin does not exist)
print(
ex,
"(Invalid reference to branch origin/{}). Remaining on master branch.".format(
branch
),
)
self.spawn(["git", "-C", "./MeshKernel", "branch"])
os.chdir(ext.name)
if not self.dry_run:
library_meta = get_library_meta()
# configure
self.spawn(
[
"cmake",
"-S",
".",
"-B",
"build",
"-DCMAKE_BUILD_TYPE=Release",
"-DENABLE_UNIT_TESTING=OFF",
]
)
# build in release mode
self.spawn(["cmake", "--build", "build", "--config", "Release", "-j4"])
if library_meta.system == "Linux" or library_meta.system == "Darwin":
meshkernel_path = str(
os.path.join(
*[
pathlib.Path().absolute(),
"build",
"libs",
"MeshKernelApi",
library_meta.name,
]
)
)
elif library_meta.system == "Windows":
meshkernel_path = str(
os.path.join(
*[
pathlib.Path().absolute(),
"build",
"libs",
"MeshKernelApi",
"Release",
library_meta.name,
]
)
)
destination = os.path.join(*[cwd, "meshkernel", library_meta.name])
shutil.copyfile(meshkernel_path, destination)
os.chdir(cwd)
long_description = read("README.md")
setup(
name="meshkernel",
description="`meshkernel` is a library which can be used to manipulate meshes.",
long_description=long_description,
long_description_content_type="text/markdown",
author=__author__,
author_email=__author_email__,
url="https://github.com/Deltares/MeshKernelPy",
license="MIT",
platforms="Windows, Linux, macOS",
install_requires=[
"numpy>=1.22",
"matplotlib>=3.6",
],
extras_require={
"tests": [
"pytest",
"pytest-cov",
"nbval",
],
"lint": [
"flake8",
"black",
"isort",
],
"docs": [
"sphinx",
"sphinx_book_theme",
"myst_nb",
],
},
python_requires=">=3.8",
package_data={"meshkernel": [get_library_meta().name]},
packages=find_packages(),
ext_modules=[CMakeExtension("https://github.com/Deltares/MeshKernel")],
cmdclass={"build_ext": build_ext, "bdist_wheel": bdist_wheel},
version=get_version("meshkernel/version.py"),
classifiers=["Topic :: Scientific/Engineering :: Mathematics"],
)