|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
import os |
|
3
|
|
|
import re |
|
4
|
|
|
from pathlib import Path |
|
5
|
|
|
|
|
6
|
|
|
from setuptools import Extension |
|
7
|
|
|
from setuptools import find_namespace_packages |
|
8
|
|
|
from setuptools import setup |
|
9
|
|
|
from setuptools.command.build_ext import build_ext |
|
10
|
|
|
from setuptools.dist import Distribution |
|
11
|
|
|
|
|
12
|
|
|
try: |
|
13
|
|
|
# Allow installing package without any Cython available. This |
|
14
|
|
|
# assumes you are going to include the .c files in your sdist. |
|
15
|
|
|
import Cython |
|
16
|
|
|
except ImportError: |
|
17
|
|
|
Cython = None |
|
18
|
|
|
|
|
19
|
|
|
# Enable code coverage for C code: we cannot use CFLAGS=-coverage in tox.ini, since that may mess with compiling |
|
20
|
|
|
# dependencies (e.g. numpy). Therefore, we set SETUPPY_CFLAGS=-coverage in tox.ini and copy it to CFLAGS here (after |
|
21
|
|
|
# deps have been safely installed). |
|
22
|
|
|
if "TOX_ENV_NAME" in os.environ and os.environ.get("SETUPPY_EXT_COVERAGE") == "yes": |
|
23
|
|
|
CFLAGS = os.environ["CFLAGS"] = "-DCYTHON_TRACE=1 -DCYTHON_USE_SYS_MONITORING=0" |
|
24
|
|
|
LFLAGS = os.environ["LFLAGS"] = "" |
|
25
|
|
|
else: |
|
26
|
|
|
CFLAGS = "" |
|
27
|
|
|
LFLAGS = "" |
|
28
|
|
|
|
|
29
|
|
|
allow_extensions = True |
|
30
|
|
|
# Enable this if the extensions will not build on PyPy |
|
31
|
|
|
# if '__pypy__' in sys.builtin_module_names: |
|
32
|
|
|
# print('NOTICE: C extensions disabled on PyPy (would be broken)!') |
|
33
|
|
|
# allow_extensions = False |
|
34
|
|
|
if os.environ.get("SETUPPY_FORCE_PURE"): |
|
35
|
|
|
print("NOTICE: C extensions disabled (SETUPPY_FORCE_PURE)!") |
|
36
|
|
|
allow_extensions = False |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
class OptionalBuildExt(build_ext): |
|
40
|
|
|
""" |
|
41
|
|
|
Allow the building of C extensions to fail. |
|
42
|
|
|
""" |
|
43
|
|
|
|
|
44
|
|
|
def run(self): |
|
45
|
|
|
try: |
|
46
|
|
|
super().run() |
|
47
|
|
|
except Exception as e: |
|
48
|
|
|
self._unavailable(e) |
|
49
|
|
|
self.extensions = [] # avoid copying missing files (it would fail). |
|
50
|
|
|
|
|
51
|
|
|
def _unavailable(self, e): |
|
52
|
|
|
print("*" * 80) |
|
53
|
|
|
print( |
|
54
|
|
|
"""WARNING: |
|
55
|
|
|
|
|
56
|
|
|
An optional code optimization (C extension) could not be compiled. |
|
57
|
|
|
|
|
58
|
|
|
Optimizations for this package will not be available! |
|
59
|
|
|
""" |
|
60
|
|
|
) |
|
61
|
|
|
|
|
62
|
|
|
print("CAUSE:") |
|
63
|
|
|
print("") |
|
64
|
|
|
print(" " + repr(e)) |
|
65
|
|
|
print("*" * 80) |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
class BinaryDistribution(Distribution): |
|
69
|
|
|
""" |
|
70
|
|
|
Distribution which almost always forces a binary package with platform name |
|
71
|
|
|
""" |
|
72
|
|
|
|
|
73
|
|
|
def has_ext_modules(self): |
|
74
|
|
|
return super().has_ext_modules() or not os.environ.get("SETUPPY_ALLOW_PURE") |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
def read(*names, **kwargs): |
|
78
|
|
|
with Path(__file__).parent.joinpath(*names).open(encoding=kwargs.get("encoding", "utf8")) as fh: |
|
79
|
|
|
return fh.read() |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
setup( |
|
83
|
|
|
long_description="{}\n{}".format( |
|
84
|
|
|
re.compile("^.. start-badges.*^.. end-badges", re.M | re.S).sub("", read("README.rst")), |
|
85
|
|
|
re.sub(":[a-z]+:`~?(.*?)`", r"``\1``", read("CHANGELOG.rst")), |
|
86
|
|
|
), |
|
87
|
|
|
long_description_content_type="text/x-rst", |
|
88
|
|
|
packages=find_namespace_packages("src"), |
|
89
|
|
|
package_dir={"": "src"}, |
|
90
|
|
|
py_modules=[path.stem for path in Path("src").glob("*.py")], |
|
91
|
|
|
include_package_data=True, |
|
92
|
|
|
zip_safe=False, |
|
93
|
|
|
cmdclass={"build_ext": OptionalBuildExt}, |
|
94
|
|
|
ext_modules=[ |
|
95
|
|
|
Extension( |
|
96
|
|
|
str(path.relative_to("src").with_suffix("")).replace(os.sep, "."), |
|
97
|
|
|
sources=[str(path)], |
|
98
|
|
|
extra_compile_args=CFLAGS.split(), |
|
99
|
|
|
extra_link_args=LFLAGS.split(), |
|
100
|
|
|
include_dirs=[str(path.parent)], |
|
101
|
|
|
) |
|
102
|
|
|
for path in Path("src").glob("**/*.pyx" if Cython else "**/*.c") |
|
103
|
|
|
] |
|
104
|
|
|
if allow_extensions |
|
105
|
|
|
else [], |
|
106
|
|
|
distclass=BinaryDistribution if allow_extensions else None, |
|
107
|
|
|
) |
|
108
|
|
|
|