/
setup.py
249 lines (216 loc) · 8.77 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
#!/usr/bin/python
# Copyright (C) 2012 Alex Nitz, Andrew Miller, Josh Willis
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
setup.py file for PyCBC package
"""
import os
import fnmatch
import sys
import subprocess
import commands
from trace import fullmodname
import unittest
from distribute_setup import use_setuptools
use_setuptools()
from setuptools import setup,Command,Extension,find_packages
from distutils.command.clean import clean as _clean
from distutils.command.build import build as _build
from distutils.command.config import config as _config
from numpy import get_include as np_get_include
# Below function copied from PyCBC v1 setup.py file. Not sure whom to credit--Duncan?
def pkg_config(libraries=[],library_dirs=[],include_dirs=[],pkg_libraries=[]):
# Check that we have the packages
for pkg in pkg_libraries:
if os.system('pkg-config --exists %s 2>/dev/null' % pkg) == 0:
pass
else:
print "Could not find library {0}".format(pkg)
sys.exit(1)
# Get the pck-config flags
if len(pkg_libraries)>0 :
for token in commands.getoutput("pkg-config --libs --cflags %s" % ' '.join(pkg_libraries)).split():
if token[:2]== "-l":
libraries.append(token[2:])
if token[:2]== "-L":
library_dirs.append(token[2:])
if token[:2] == "-I":
include_dirs.append(token[2:])
return libraries,library_dirs,include_dirs
# Now use the above function to set up our extension library's needs:
ext_libraries, ext_library_dirs, ext_include_dirs = pkg_config(pkg_libraries=["lal","lalsimulation","lalinspiral"])
# Setup our swig options. We need the first two to match with how the swiglal
# wrappings are compiled, so that our module can talk to theirs. Then we must
# reassemble the include_dirs to add the "-I" so swig can find things
ext_swig_opts = ['-O','-keyword','-builtin','-outdir','pycbc']
for libpath in ext_include_dirs:
ext_swig_opts.append('-I'+str(libpath))
# Numpy include must be added separately:
ext_include_dirs += [np_get_include()]
# Define our extension module
pycbc_headers = []
lalwrap_module = Extension('_lalwrap',
sources=['include/lalwrap.i'],
depends=['include/pycbc_laltypes.i'],
swig_opts=ext_swig_opts,
include_dirs=ext_include_dirs,
library_dirs=ext_library_dirs,
libraries=ext_libraries,
extra_compile_args=['-std=c99']
)
pycbc_headers.append('include/lalwrap.i')
pycbc_headers.append('include/pycbc_laltypes.i')
testlalwrap_module = Extension('_testlalwrap',
sources=['include/testlalwrap.i'],
depends=['include/pycbc_laltypes.i'],
swig_opts=ext_swig_opts,
include_dirs=ext_include_dirs,
library_dirs=ext_library_dirs,
libraries=ext_libraries,
extra_compile_args=['-std=c99']
)
# Add swig-generated files to the list of things to clean, so they
# get regenerated each time.
class clean(_clean):
def finalize_options (self):
_clean.finalize_options(self)
self.clean_files = ['pycbc/lalwrap.py','pycbc/lalwrap.pyc','include/lalwrap_wrap.c',
'pycbc/testlalwrap.py','pycbc/testlalwrap.pyc','include/testlalwrap_wrap.c']
def run(self):
_clean.run(self)
for f in self.clean_files:
try:
os.unlink(f)
print 'removed {0}'.format(f)
except:
pass
# Override build order, so swig is handled first.
class build(_build):
# override order of build sub-commands to work around
# <http://bugs.python.org/issue7562>
sub_commands = [ ('build_clib', _build.has_c_libraries),
('build_ext', _build.has_ext_modules),
('build_py', _build.has_pure_modules),
('build_scripts', _build.has_scripts) ]
def run(self):
_build.run(self)
test_results = []
# Run all of the testing scripts
class TestBase(Command):
user_options = []
test_modules = []
def initialize_options(self):
self.scheme = None
self.build_dir = None
def finalize_options(self):
#Populate the needed variables
self.set_undefined_options('build',('build_lib', 'build_dir'))
def find_test_modules(self,pattern):
# Find all the unittests that match a given string pattern
modules= []
for path, dirs, files in os.walk("test"):
for filename in fnmatch.filter(files, pattern):
#add the test directories to the path
sys.path.append(os.path.join(path))
#save the module name for importing
modules.append(fullmodname(filename))
return modules
def run(self):
self.run_command('build')
# Get the list of cpu test modules
self.test_modules = self.find_test_modules("test*.py")
# Run from the build directory
os.environ['PYTHONPATH'] = self.build_dir + ":" + os.environ['PYTHONPATH']
test_results.append("\n" + (self.scheme + " tests ").rjust(30))
for test in self.test_modules:
test_command = 'python ' + 'test/' + test + '.py -s ' + self.scheme
a = subprocess.call(test_command,env=os.environ,shell=True)
if a != 0:
result_str = str(test).ljust(30) + ": Fail : " + str(a)
else:
result_str = str(test).ljust(30) + ": Pass"
test_results.append(result_str)
for test in test_results:
print test
class test(Command):
def has_cuda(self):
try:
import pycuda
return True
except:
return False
def has_opencl(self):
try:
import pyopencl
return True
except:
return False
sub_commands = [('test_cpu',None),('test_cuda',has_cuda),('test_opencl',has_opencl)]
user_options = []
description = "run the available tests for all compute schemes (cpu,cuda,opencl)"
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
for cmd_name in self.get_sub_commands():
self.run_command(cmd_name)
class test_cpu(TestBase):
description = "run all CPU tests"
def initialize_options(self):
TestBase.initialize_options(self)
self.scheme = 'cpu'
class test_cuda(TestBase):
description = "run CUDA tests"
def initialize_options(self):
TestBase.initialize_options(self)
self.scheme = 'cuda'
class test_opencl(TestBase):
description = "run OpenCL tests"
def initialize_options(self):
TestBase.initialize_options(self)
self.scheme = 'opencl'
cuda_install_requires = ['pycuda', 'scikits.cuda']
install_requires = ['']
class config(_config):
user_options = [('with-cuda', None,"enable cuda support")]
def initialize_options(self):
_config.initialize_options(self)
self.with_cuda=False
def finalize_options(self):
_config.finalize_options(self)
if self.with_cuda:
self.distribution.install_requires += cuda_install_requires
# do the actual work of building the package
setup (
name = 'PyCBC',
version = '0.0.1',
py_modules = ['distribute_setup'],
description = 'Gravitational wave CBC analysis toolkit',
author = 'Ligo Virgo Collaboration - PyCBC team',
url = 'https://sugwg-git.phy.syr.edu/dokuwiki/doku.php?id=pycbc:home',
cmdclass = { 'config': config,
'test' : test,
'test_cpu':test_cpu,
'test_cuda':test_cuda,
'test_opencl':test_opencl,
'clean' : clean,
'build' : build},
ext_modules = [lalwrap_module,testlalwrap_module],
install_requires = install_requires,
packages = find_packages(),
scripts = [],
)