-
Notifications
You must be signed in to change notification settings - Fork 12
/
setup.py
420 lines (338 loc) · 12.2 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/env python
"""
setup.py file for OApackage
"""
import logging
import os
import platform
import re
import subprocess
import sys
# from distutils.command.build import build as distutils_build
from os import path
import setuptools.command.build_ext
from setuptools import Extension, find_packages, setup
if sys.version_info.minor > 10 or sys.version_info.major > 3:
from setuptools.command.build import build as setuptools_build
else:
from distutils.command.build import build as setuptools_build
from setuptools.command.install import install as setuptools_install
from setuptools.command.test import test as TestCommand
try:
import numpy as np
except ImportError:
raise RuntimeError("numpy cannot be imported. numpy must be installed prior to installing OApackage")
npinclude = np.get_include()
setup_directory = path.abspath(path.dirname(__file__))
# %%
def checkZlib(verbose=0):
"""Check whether zlib is available
Code adapted from http://stackoverflow.com/questions/28843765/setup-py-check-if-non-python-library-dependency-exists
"""
ret_val = True
return True
try:
import distutils.ccompiler
import distutils.sysconfig
import tempfile
from distutils.errors import CompileError, LinkError
# create test file...
c_code = """
#include <zlib.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
printf("Hello zlib test...\\n");
return 0;
}
"""
tmp_dir = tempfile.mkdtemp(prefix="helloztest_")
bin_file_name = os.path.join(tmp_dir, "helloz")
file_name = bin_file_name + ".c"
with open(file_name, "w") as fp:
fp.write(c_code)
# compile and link code
compiler = distutils.ccompiler.new_compiler()
assert isinstance(compiler, distutils.ccompiler.CCompiler)
distutils.sysconfig.customize_compiler(compiler)
libraries = ["z"]
ret_val = True
try:
if verbose:
print("checkZlib: compile and link")
compiler.link_executable(
compiler.compile([file_name]),
bin_file_name,
libraries=libraries,
)
except CompileError:
if verbose:
print("checkZlib: compile error in %s, zlib not available" % file_name)
ret_val = False
except LinkError:
if verbose:
print("checkZlib: link error in %s, zlib not available" % file_name)
ret_val = False
except Exception as e:
if verbose:
print("checkZlib: unknown error in %s, zlib not available" % file_name)
logging.exception(e)
ret_val = False
except Exception:
ret_val = False
return ret_val
# %%
def get_version_info(verbose=0):
"""Extract version information from source code"""
GIT_REVISION = None
if os.path.exists("src/version.h"):
with open("src/version.h") as f:
ln = f.readline()
m = re.search('.* "(.*)"', ln)
FULLVERSION = m.group(1)
else:
FULLVERSION = "0.0"
if verbose:
print("get_version_info: %s" % FULLVERSION)
return FULLVERSION, GIT_REVISION
try:
from distutils.spawn import find_executable
from distutils.version import LooseVersion
def get_swig_executable(swig_minimum_version="4.0", verbose=0):
"""Get SWIG executable"""
# stolen from https://github.com/FEniCS/ffc/blob/master/setup.py
swig_executable = None
swig_version = None
swig_valid = False
for executable in ["swig", "swig3.0"]:
swig_executable = find_executable(executable)
if swig_executable is not None:
# Check that SWIG version is ok
output = subprocess.check_output([swig_executable, "-version"]).decode("utf-8")
swig_version = re.findall(r"SWIG Version ([0-9.]+)", output)[0]
if LooseVersion(swig_version) >= LooseVersion(swig_minimum_version):
swig_valid = True
break
if verbose:
print(f"Found SWIG: {swig_executable} (version {swig_version})")
return swig_executable, swig_version, swig_valid
swig_executable, swig_version, swig_valid = get_swig_executable()
print(f"swig_version {swig_version}, swig_executable {swig_executable}")
except BaseException:
def get_swig_executable():
return None, None, False
swig_valid = False
# %% Test suite
class OATest(TestCommand):
"""Run a limited set of tests for the package"""
user_options = [("pytest-args=", "a", "Arguments to pass to py.test")]
def initialize_options(self):
TestCommand.initialize_options(self)
self.pytest_args = []
def finalize_options(self):
TestCommand.finalize_options(self)
# New setuptools don't need this anymore, thus the try block.
try:
self.test_args = []
self.test_suite = True
except AttributeError:
pass
def run_tests(self):
print("## oapackage test: load package")
# import here, cause outside the eggs aren't loaded
import oapackage
import oapackage.oahelper
import oapackage.scanf
print("## oapackage test: oalib version %s" % oapackage.version())
print("## oapackage test: package compile options\n%s\n" % oapackage.oalib.compile_information())
oapackage.oalib.test_array_manipulation(verbose=0)
oapackage.oalib.test_conference_candidate_generators(verbose=0)
errno = 0
sys.exit(errno)
# %% Define sources of the package
oadev = 0
srcs = [
"arraytools.cpp",
"arrayproperties.cpp",
"pareto.cpp",
"nonroot.cpp",
"mathtools.cpp",
"oaoptions.cpp",
"tools.cpp",
"md5.cpp",
"strength.cpp",
"graphtools.cpp",
"conference.cpp",
"unittests.cpp",
"Deff.cpp",
"evenodd.cpp",
"lmc.cpp",
"extend.cpp",
]
srcs = ["src/" + ff for ff in srcs]
if os.path.exists("dev/oadevelop.cpp"):
oadev = 1
print("Building development code")
srcs = ["dev/oadevelop.cpp"] + srcs
sources = srcs + ["src/bitarray/bit_array.cpp"]
oaheaders = [cppfile.replace(".cpp", ".h") for cppfile in srcs] + [os.path.join("src", "version.h")]
for nauty_file in "nauty.c nautinv.c nautil.c naurng.c naugraph.c schreier.c naugroup.c".split(" "):
sources += [os.path.join("src", "nauty", nauty_file)]
nautyheaders = [
os.path.join("src", "nauty", headerfile)
for headerfile in [
"gtools.h",
"naugroup.h",
"nautinv.h",
"naurng.h",
"naugraph.h",
"nausparse.h",
"nautil.h",
"nauty.h",
"schreier.h",
]
]
sources = sources
swig_opts = []
compile_options = []
sources = ["oalib.i"] + sorted(sources)
if oadev:
swig_opts += ["-c++", "-doxygen", "-w503,401,362,509,389", "-Isrc/", "-Idev/"]
compile_options += ["-DSWIGCODE", "-DFULLPACKAGE", "-DOADEV", "-Idev/"]
swig_opts += ["-DSWIGCODE", "-DFULLPACKAGE", "-DOADEV"]
else:
swig_opts += ["-c++", "-doxygen", "-w503,401,362,302,389,446,509,305", "-Isrc/"]
compile_options += ["-DSWIGCODE", "-DFULLPACKAGE"]
swig_opts += ["-DSWIGCODE", "-DFULLPACKAGE"]
# add nauty files
swig_opts += ["-Isrc/nauty/"]
compile_options += ["-Isrc/nauty/"]
# specifically for clang
# compile_options += ["-std=c++11"]
if platform.system() == "Windows":
compile_options += ["-DWIN32", "-D_WIN32"]
swig_opts += ["-DWIN32", "-D_WIN32"]
rtd = os.environ.get("READTHEDOCS", False)
print(f"Readthedocs environment: {rtd}")
if "VSC_SCRATCH" in os.environ.keys():
# we are running on the VSC cluster
zlibdir = os.environ.get("EBROOTZLIB") # SOFTROOTZLIB
libraries = ["z"]
library_dirs = [zlibdir + "/lib"]
include_dirs = [".", "src", npinclude, zlibdir + "/include"]
else:
libraries = []
library_dirs = []
include_dirs = [".", "src", npinclude]
oalib_module = Extension(
"_oalib",
sources=sources,
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=libraries,
swig_opts=swig_opts,
)
compile_options += ["-DNOOMP"]
swig_opts += ["-DNOOMP"]
oalib_module.extra_compile_args = compile_options
if checkZlib(verbose=1):
if platform.system() == "Windows":
pass
else:
zlibflag = "-DUSEZLIB"
oalib_module.extra_compile_args += [zlibflag]
swig_opts += [zlibflag]
oalib_module.extra_link_args += ["-lz"]
else:
zlibflag = "-DNOZLIB"
oalib_module.extra_compile_args += [zlibflag]
swig_opts += [zlibflag]
if os.name == "nt":
oalib_module.extra_compile_args += []
else:
oalib_module.extra_compile_args += [
"-O3",
"-Wno-unknown-pragmas",
"-Wno-sign-compare",
"-Wno-return-type",
"-Wno-unused-variable",
"-Wno-unused-result",
"-fPIC",
]
oalib_module.extra_compile_args += [
"-Wno-date-time",
]
if platform.node() == "marmot" or platform.node() == "goffer" or platform.node() == "woelmuis":
# openmp version of code
oalib_module.extra_compile_args += ["-fopenmp", "-DDOOPENMP"]
oalib_module.extra_link_args += ["-fopenmp"]
print("find_packages: %s" % find_packages())
data_files = []
scripts = ["misc/scripts/example_oapackage_python.py"]
packages = find_packages()
# fix from:
# http://stackoverflow.com/questions/12491328/python-distutils-not-include-the-swig-generated-module
if not swig_valid:
raise Exception("could not find a recent version if SWIG")
ext_modules = [oalib_module]
# see: http://stackoverflow.com/questions/12491328/python-distutils-not-include-the-swig-generated-module
class CustomBuild(setuptools_build):
def run(self):
self.run_command("build_ext")
setuptools_build.run(self)
class CustomInstall(setuptools_install):
def run(self):
self.run_command("build_ext")
setuptools_install.run(self)
class BuildExtSwig3(setuptools.command.build_ext.build_ext):
def find_swig(self):
swig_executable, _, _ = get_swig_executable()
return swig_executable
def readme():
with open("README.md") as f:
return f.read()
long_description = readme()
version = get_version_info()[0]
# print("OApackage: version %s" % version)
setup(
name="OApackage",
cmdclass={"test": OATest, "install": CustomInstall, "build": CustomBuild, "build_ext": BuildExtSwig3},
version=version,
author="Pieter Eendebak",
description="Package to generate and analyse orthogonal arrays, conference designs and optimal designs",
long_description=long_description,
long_description_content_type="text/markdown",
author_email="pieter.eendebak@gmail.com",
license="BSD",
url="http://www.pietereendebak.nl/oapackage/index.html",
keywords=["orthogonal arrays, design of experiments, conference designs, isomorphism testing"],
ext_modules=ext_modules,
py_modules=["oalib"],
packages=packages,
data_files=data_files,
scripts=scripts,
tests_require=[
"numpy>=1.24",
"nose",
"coverage",
"matplotlib",
"mock",
"python-dateutil",
"types-python-dateutil",
],
zip_safe=False,
install_requires=["numpy>=1.24", "python-dateutil", "matplotlib"],
extras_require={
"doc": ["sphinx", "sphinxcontrib.bibtex", "sphinxcontrib.napoleon", "breathe"],
},
requires=["numpy", "matplotlib"],
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: Science/Research",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"License :: OSI Approved :: BSD License",
],
)