-
Notifications
You must be signed in to change notification settings - Fork 8
/
setup.py
executable file
·238 lines (216 loc) · 7.27 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
#!/usr/bin/env python
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import absolute_import, division, print_function
#
# Standard imports
#
import glob
import os
import sys
import shutil
#
# setuptools' sdist command ignores MANIFEST.in
#
from distutils.command.sdist import sdist as DistutilsSdist
from setuptools import setup, find_packages, Extension
from setuptools.command.build_ext import build_ext
from setuptools.command.egg_info import egg_info
from distutils.command.clean import clean
from distutils.errors import CompileError
#
# DESI support code.
#
from desiutil.setup import DesiTest, DesiVersion, get_version
#
# Begin setup
#
setup_keywords = dict()
#
# THESE SETTINGS NEED TO BE CHANGED FOR EVERY PRODUCT.
#
setup_keywords['name'] = 'fiberassign'
setup_keywords['description'] = 'DESI Fiber Assignment Tools'
setup_keywords['author'] = 'DESI Collaboration'
setup_keywords['author_email'] = '[email protected]'
setup_keywords['license'] = 'BSD'
setup_keywords['url'] = 'https://github.com/desihub/fiberassign'
#
# END OF SETTINGS THAT NEED TO BE CHANGED.
#
pkg_version = get_version(setup_keywords['name'])
setup_keywords['version'] = pkg_version
cpp_version_file = os.path.join("src", "_version.h")
with open(cpp_version_file, "w") as f:
f.write('// Generated by setup.py -- DO NOT EDIT THIS\n')
f.write('const static std::string package_version("{}");\n\n'
.format(pkg_version))
#
# Use README.rst as long_description.
#
setup_keywords['long_description'] = ''
if os.path.exists('README.rst'):
with open('README.rst') as readme:
setup_keywords['long_description'] = readme.read()
#
# Set other keywords for the setup function. These are automated, & should
# be left alone unless you are an expert.
#
# Treat everything in bin/ except *.rst as a script to be installed.
#
if os.path.isdir('bin'):
setup_keywords['scripts'] = [fname for fname in
glob.glob(os.path.join('bin', '*'))
if not os.path.basename(fname)
.endswith('.rst')]
setup_keywords['provides'] = [setup_keywords['name']]
setup_keywords['python_requires'] = '>=3.6.0'
setup_keywords['setup_requires'] = (['wheel'], )
setup_keywords['install_requires'] = [
'numpy',
'pyyaml',
'scipy',
'matplotlib',
'astropy',
'fitsio'
]
setup_keywords['zip_safe'] = False
# setup_keywords['use_2to3'] = False
setup_keywords['packages'] = find_packages('py')
setup_keywords['package_dir'] = {'': 'py'}
setup_keywords['cmdclass'] = {'version': DesiVersion, 'test': DesiTest,
'sdist': DistutilsSdist}
test_suite_name = \
'{name}.test.{name}_test_suite.{name}_test_suite'.format(**setup_keywords)
setup_keywords['test_suite'] = test_suite_name
# Autogenerate command-line scripts.
#
# setup_keywords['entry_points'] =
# {'console_scripts':['desiInstall = desiutil.install.main:main']}
#
# Add internal data directories.
#
# setup_keywords['package_data'] = {'fiberassign': ['data/*',]}
# Add a custom clean command that removes in-tree files like the
# compiled extension.
class RealClean(clean):
def run(self):
super().run()
clean_files = [
"./build",
"./dist",
"py/fiberassign/_internal*",
"py/fiberassign/__pycache__",
"py/fiberassign/test/__pycache__",
"./*.egg-info",
"py/*.egg-info"
]
for cf in clean_files:
# Make paths absolute and relative to this path
apaths = glob.glob(os.path.abspath(cf))
for path in apaths:
if os.path.isdir(path):
shutil.rmtree(path)
elif os.path.isfile(path):
os.remove(path)
return
# These classes allow us to build a compiled extension that uses pybind11.
# For more details, see:
#
# https://github.com/pybind/python_example
#
# As of Python 3.6, CCompiler has a `has_flag` method.
# cf http://bugs.python.org/issue26689
def has_flag(compiler, flagname):
"""Return a boolean indicating whether a flag name is supported on
the specified compiler.
"""
import tempfile
devnull = None
oldstderr = None
try:
with tempfile.NamedTemporaryFile('w', suffix='.cpp') as f:
f.write('int main (int argc, char **argv) { return 0; }')
try:
devnull = open('/dev/null', 'w')
oldstderr = os.dup(sys.stderr.fileno())
os.dup2(devnull.fileno(), sys.stderr.fileno())
compiler.compile([f.name], extra_postargs=[flagname])
except CompileError:
return False
return True
finally:
if oldstderr is not None:
os.dup2(oldstderr, sys.stderr.fileno())
if devnull is not None:
devnull.close()
def cpp_flag(compiler):
"""Return the -std=c++[11/14] compiler flag.
The c++14 is prefered over c++11 (when it is available).
"""
if has_flag(compiler, '-std=c++14'):
return '-std=c++14'
elif has_flag(compiler, '-std=c++11'):
return '-std=c++11'
else:
raise RuntimeError('Unsupported compiler -- at least C++11 support '
'is needed!')
class BuildExt(build_ext):
"""A custom build extension for adding compiler-specific options."""
c_opts = {
'msvc': ['/EHsc'],
'unix': [],
}
if sys.platform.lower() == 'darwin':
c_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7']
def build_extensions(self):
ct = self.compiler.compiler_type
opts = self.c_opts.get(ct, [])
linkopts = []
if ct == 'unix':
opts.append('-DVERSION_INFO="%s"' %
self.distribution.get_version())
opts.append(cpp_flag(self.compiler))
if has_flag(self.compiler, '-fvisibility=hidden'):
opts.append('-fvisibility=hidden')
if has_flag(self.compiler, '-fopenmp'):
opts.append('-fopenmp')
linkopts.append('-fopenmp')
if sys.platform.lower() == 'darwin':
linkopts.append('-stdlib=libc++')
elif ct == 'msvc':
opts.append('/DVERSION_INFO=\\"%s\\"' %
self.distribution.get_version())
for ext in self.extensions:
ext.extra_compile_args.extend(opts)
ext.extra_link_args.extend(linkopts)
# remove -Wstrict-prototypes flag
if '-Wstrict-prototypes' in self.compiler.compiler_so:
self.compiler.compiler_so.remove("-Wstrict-prototypes")
build_ext.build_extensions(self)
ext_modules = [
Extension(
'fiberassign._internal',
[
'src/utils.cpp',
'src/hardware.cpp',
'src/tiles.cpp',
'src/targets.cpp',
'src/assign.cpp',
'src/_pyfiberassign.cpp'
],
include_dirs=[
'src',
],
language='c++'
),
]
setup_keywords['ext_modules'] = ext_modules
setup_keywords['cmdclass']['build_ext'] = BuildExt
setup_keywords['cmdclass']['clean'] = RealClean
# Add internal data directories
#
setup_keywords['package_data'] = {'fiberassign': ['data/*',],}
#
# Run setup command.
#
setup(**setup_keywords)