-
Notifications
You must be signed in to change notification settings - Fork 45
/
setup.py
271 lines (218 loc) · 8.62 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
#!/usr/bin/env python
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# NOTE: The configuration for the package, including the name, version, and
# other information are set in the setup.cfg file.
import sys
# First provide helpful messages if contributors try and run legacy commands
# for tests or docs.
TEST_HELP = """
Note: running tests is no longer done using 'python setup.py test'. Instead
you will need to run:
tox -e test
If you don't already have tox installed, you can install it with:
pip install tox
If you only want to run part of the test suite, you can also use pytest
directly with::
pip install -e .[test]
pytest
For more information, see:
http://docs.astropy.org/en/latest/development/testguide.html#running-tests
"""
if 'test' in sys.argv:
print(TEST_HELP)
sys.exit(1)
DOCS_HELP = """
Note: building the documentation is no longer done using
'python setup.py build_docs'. Instead you will need to run:
tox -e build_docs
If you don't already have tox installed, you can install it with:
pip install tox
You can also build the documentation with Sphinx directly using::
pip install -e .[docs]
cd docs
make html
For more information, see:
http://docs.astropy.org/en/latest/install.html#builddocs
"""
if 'build_docs' in sys.argv or 'build_sphinx' in sys.argv:
print(DOCS_HELP)
sys.exit(1)
import os
import importlib
from distutils.command.clean import clean
from setuptools.command.install import install
from setuptools import setup, Command, Extension
from subprocess import check_call, CalledProcessError
try:
from ConfigParser import ConfigParser
except ImportError:
from configparser import ConfigParser
conf = ConfigParser()
conf.read(['setup.cfg'])
# Get some values from the setup.cfg
metadata = dict(conf.items('metadata'))
PACKAGENAME = metadata.get('name', 'imexam')
package_data = {PACKAGENAME: []}
ext = []
cmdclass = {}
if not sys.platform.startswith('win'):
try:
from Cython.Distutils import build_ext
from Cython.Build import cythonize
use_cython = True
print("Cython found")
CYTHON_SOURCE = "wrappers/xpa.pyx"
except ImportError:
from distutils.command import build_ext
use_cython = False
print("Cython not found")
CYTHON_SOURCE = "wrappers/xpa.c"
XPALIB_DIR = "./cextern/xpa/"
XPA_LIBNAME = "imexamxpa"
CONF_H_NAME = os.path.join(XPALIB_DIR, "conf.h")
# We only need to compile with these
XPA_FILES = """acl.c
client.c
clipboard.c
command.c
find.c
port.c
remote.c
tcp.c
timedconn.c
word.c
xalloc.c
xlaunch.c
xpa.c
xpaio.c
""".split()
package_data[PACKAGENAME].extend(XPA_FILES)
suffix_lib = importlib.machinery.EXTENSION_SUFFIXES[0]
package_data[PACKAGENAME].append(XPA_LIBNAME+suffix_lib)
XPA_SOURCES = [os.path.join(XPALIB_DIR, c) for c in XPA_FILES]
XPALIB_DEFINES = [("HAVE_CONFIG_H", "1")]
XPA_SOURCES.append(CYTHON_SOURCE)
xpa_module = Extension("imexam."+XPA_LIBNAME,
sources=XPA_SOURCES,
include_dirs=[XPALIB_DIR],
define_macros=XPALIB_DEFINES,
depends=[CONF_H_NAME],
)
if use_cython:
ext = cythonize(xpa_module)
current_env = sys.prefix
class my_clean(Command):
""" This is a full on clean, including docs"""
user_options = []
def initialize_options(self):
self.build_base = None
self.build_lib = None
self.build_temp = None
self.build_scripts = None
self.bdist_base = None
self.all = None
def finalize_options(self):
self.set_undefined_options('build',
('build_base', 'build_base'),
('build_lib', 'build_lib'),
('build_scripts', 'build_scripts'),
('build_temp', 'build_temp'),
)
self.set_undefined_options('bdist',
('bdist_base', 'bdist_base'))
def run(self):
try:
if os.access(XPALIB_DIR + "Makefile", os.F_OK):
check_call(["make", "clean"], cwd=XPALIB_DIR)
except CalledProcessError as e:
print(e)
exit(1)
if os.access(CONF_H_NAME, os.F_OK):
os.remove(CONF_H_NAME)
os.remove("wrappers/xpa.c")
xpa_bins = ["xpaaccess",
"xpaget",
"xpainfo",
"xpamb",
"xpans",
"xpaset",
]
for file in xpa_bins:
myfile = current_env + "/bin/" + file
if os.access(myfile, os.F_OK):
print(f"removing {myfile}")
os.remove(myfile)
if os.access(XPA_LIBNAME+suffix_lib, os.F_OK):
os.remove(XPA_LIBNAME+suffix_lib)
try:
check_call(["make", "clean"], cwd="./docs")
except CalledProcessError as e:
print(e)
clean.run(self)
class InstallWithRemake(install):
"""Configure, build, and install the aXe C code."""
user_options = install.user_options +\
[('noremake', None, "Don't rebuild the C executables [default True]")]
def initialize_options(self):
super().initialize_options()
self.noremake = None
self.remake = True
def finalize_options(self):
super().finalize_options()
self.inplace = 1
if self.noremake:
if not os.access(XPALIB_DIR + "Makefile", os.F_OK):
raise FileNotFoundError("Makefile doesn't exist, let imexam build")
else:
self.remake = False
def run(self):
if self.remake:
try:
check_call(["sh", "./configure",
"--prefix="+current_env], cwd=XPALIB_DIR)
check_call(["make", "install"], cwd=XPALIB_DIR)
except CalledProcessError as e:
print(e)
exit(1)
install.run(self)
class BuildExtWithConfigure(build_ext):
"""Configure, build, and install the aXe C code."""
def initialize_options(self):
super().initialize_options()
self.inplace = 1
def build_extensions(self):
super().build_extensions()
def run(self):
try:
check_call(["sh", "./configure",
"--prefix="+current_env], cwd=XPALIB_DIR)
check_call(["make", "clean"], cwd=XPALIB_DIR)
check_call(["make", "install"], cwd=XPALIB_DIR)
except CalledProcessError as e:
print(e)
exit(1)
build_ext.run(self)
cmdclass.update({'install': InstallWithRemake,
'clean': my_clean,
'build_ext': BuildExtWithConfigure,
})
else:
ext = [xpa_module]
VERSION_TEMPLATE = """
# Note that we need to fall back to the hard-coded version if either
# setuptools_scm can't be imported or setuptools_scm can't determine the
# version, so we catch the generic 'Exception'.
try:
from setuptools_scm import get_version
version = get_version(root='..', relative_to=__file__)
except Exception:
version = '{version}'
""".lstrip()
# Import these after the above checks to ensure they are printed even if
# extensions_helpers is not installed
from setuptools import setup # noqa
setup(use_scm_version={'write_to': os.path.join('imexam', 'version.py'),
'write_to_template': VERSION_TEMPLATE},
cmdclass=cmdclass,
package_data=package_data,
ext_modules=ext)