This repository has been archived by the owner on Aug 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
230 lines (207 loc) · 7.23 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
"""
Installation script for pytao.
Usage:
python setup.py install
"""
from setuptools import setup, Extension
from distutils.util import convert_path, get_platform
from distutils import sysconfig
import sys
import os
# setuptools.Extension automatically converts all '.pyx' extensions to '.c'
# extensions if detecting that neither Cython nor Pyrex is available. Early
# versions of setuptools don't know about Cython. Since we don't use Pyrex
# in this module, this leads to problems in the two cases where Cython is
# available and Pyrex is not or vice versa. Therefore, setuptools.Extension
# needs to be patched to match our needs:
try:
# Use Cython if available:
from Cython.Build import cythonize
except ImportError:
# Otherwise, always use the distributed .c instead of the .pyx file:
def cythonize(extensions):
def pyx_to_c(source):
return source[:-4]+'.c' if source.endswith('.pyx') else source
for ext in extensions:
ext.sources = list(map(pyx_to_c, ext.sources))
missing_sources = [s for s in ext.sources if not os.path.exists(s)]
if missing_sources:
raise OSError(('Missing source file: {0[0]!r}. '
'Install Cython to resolve this problem.')
.format(missing_sources))
return extensions
else:
orig_Extension = Extension
class Extension(orig_Extension):
"""Extension that *never* replaces '.pyx' by '.c' (using Cython)."""
def __init__(self, name, sources, *args, **kwargs):
orig_Extension.__init__(self, name, sources, *args, **kwargs)
self.sources = sources
def main(argv):
"""Execute setup."""
fix_distutils_sysconfig_mingw()
setup_args = get_setup_args(sys.argv)
setup(**setup_args)
def read_file(path):
"""Read a file in binary mode."""
with open(convert_path(path), 'rb') as f:
return f.read()
def exec_file(path):
"""Execute a python file and return the `globals` dictionary."""
namespace = {}
exec(read_file(path), namespace, namespace)
return namespace
def get_long_description():
"""Compose a long description for PyPI."""
long_description = None
try:
long_description = read_file('README.rst').decode('utf-8')
long_description += '\n' + read_file('COPYING.rst').decode('utf-8')
long_description += '\n' + read_file('CHANGES.rst').decode('utf-8')
except (IOError, UnicodeDecodeError):
pass
return long_description
def fix_distutils_sysconfig_mingw():
"""
When using windows and MinGW, in distutils.sysconfig the compiler (CC) is
not initialized at all, see http://bugs.python.org/issue2437. The
following manual fix for this problem may cause other issues, but it's a
good shot.
"""
if sysconfig.get_config_var('CC') is None:
sysconfig._config_vars['CC'] = 'gcc'
def get_setup_args(argv):
extension_args = get_extension_args(argv)
long_description = get_long_description()
metadata = exec_file('pytao/__init__.py')
return dict(
name=metadata['__title__'],
version=metadata['__version__'],
description=metadata['__summary__'],
long_description=long_description,
author=metadata['__author__'],
author_email=metadata['__author_email__'],
url=metadata['__uri__'],
classifiers=metadata['__classifiers__'],
packages = [
"pytao",
],
ext_modules = cythonize([
Extension('pytao.tao_pipe',
sources=["pytao/tao_pipe.pyx"],
**extension_args),
]),
install_requires=[
'setuptools',
'minrpc==0.0.7',
],
package_data={
'pytao': [
'COPYING.txt'
]
}
)
def remove_arg(args, opt):
"""
Remove all occurences of ``--PARAM=VALUE`` or ``--PARAM VALUE`` from
``args`` and return the corresponding values.
"""
iterargs = iter(args)
result = []
remain = []
for arg in iterargs:
if arg == opt:
result.append(next(iterargs))
elif arg.startswith(opt + '='):
result.append(arg.split('=', 1)[1])
else:
remain.append(arg)
args[:] = remain
return result
def get_extension_args(argv):
"""Get arguments for C-extension (include pathes, libraries, etc)."""
# Let's just use the default system headers:
include_dirs = []
library_dirs = []
# Parse command line option: --bmad-dir=/path/to/bmad_installation. We could
# use build_ext.user_options instead, but then the --bmad-dir argument can
# be passed only to the 'build_ext' command, not to 'build' or 'install',
# which is a minor nuisance.
base_dir = os.environ['DIST_BASE_DIR']
fallback = [os.path.join(base_dir, 'production'),
os.path.join(base_dir, 'debug')]
fallback = list(filter(os.path.isdir, fallback))
bmad_dir, = remove_arg(argv, '--bmad-dir') or [fallback[0]]
bmad_dir = prefix = os.path.expanduser(bmad_dir)
lib_path_candidates = [os.path.join(prefix, 'lib'),
os.path.join(prefix, 'lib64')]
include_dirs.append(os.path.join(prefix, 'include'))
library_dirs.extend(filter(os.path.isdir, lib_path_candidates))
plot_lib = os.environ.get('ACC_PLOT_PACKAGE')
if plot_lib == 'pgplot':
plot_libs = ['pgplot']
ext_plot_libs = ['X11']
elif plot_lib == 'plplot':
plot_libs = [
'plplotcxx',
'plplotf95',
'plplot',
'csirocsa',
'qsastime',
]
ext_plot_libs = [
'pangocairo-1.0',
'cairo',
'X11',
]
elif plot_lib == 'none':
plot_libs = []
ext_plot_libs = []
else:
raise RuntimeError(
"Unknown plot mode. $ACC_PLOT_PACKAGE must be 'plplot', 'pgplot' or 'none'.")
# order matters:
internal_libs = [
'tao',
'bmad',
'sim_utils',
'forest',
'xrlf03',
'xrl',
'xsif',
'fgsl',
'gsl',
'gslcblas',
'recipes_f-90_LEPP',
] + plot_libs + [
'lapack95',
'lapack',
'blas',
]
external_libs = ext_plot_libs + [
'readline',
'gfortran',
]
# required libraries
if get_platform() == "win32" or get_platform() == "win-amd64":
# TODO: actually try this on windows
pass
# NOTE: pass 'extra_link_args' in favor of 'libraries' in order to be able
# to insert 'static/dynamic' hints.
# NOTE: Apple's clang linker doesn't support mixing static/dynamic linking
# using `-Bdynamic`, `-Bstatic` hints, therefore we have to specify the
# full pathname:
link_args = []
link_args += [os.path.join(bmad_dir, 'lib', 'lib'+lib+'.a') for lib in internal_libs]
link_args += ['-l'+lib for lib in external_libs]
# Common arguments for the Cython extensions:
return dict(
libraries=[],
include_dirs=include_dirs,
library_dirs=library_dirs,
runtime_library_dirs=library_dirs,
extra_link_args=link_args,
extra_compile_args=['-std=gnu99'],
)
if __name__ == '__main__':
main(sys.argv)