-
Notifications
You must be signed in to change notification settings - Fork 5
/
setup.py
278 lines (239 loc) · 9.22 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
272
273
274
275
276
277
278
#!/usr/bin/env python
from distutils.core import setup
from distutils.extension import Extension
from distutils.version import LooseVersion
from Cython.Build import cythonize
import Cython.Compiler.Options
Cython.Compiler.Options.fail_fast = True
from cython import __version__ as cython_version
from pkg_resources import parse_version
from os.path import join as join_path
import numpy
import sys
import os
import json
import re
from subprocess import Popen, PIPE, check_call
CYTHON_MODULES = ['microscopes.models',
'microscopes._models',
'microscopes.common._dataview',
'microscopes.common._entity_state',
'microscopes.common.recarray.dataview',
'microscopes.common.recarray._dataview',
'microscopes.common.relation.dataview',
'microscopes.common.relation._dataview',
'microscopes.common.rng',
'microscopes.common._rng',
'microscopes.common.random',
'microscopes.common.scalar_functions',
'microscopes.common._scalar_functions',
'microscopes.common.variadic.dataview',
'microscopes.common.variadic._dataview',
]
LIBRARY_DEPENDENCIES = ["microscopes_common", "protobuf",
"distributions_shared"]
def get_git_sha1():
try:
import git
required_version = '0.3.7'
if parse_version(git.__version__) < parse_version(required_version):
raise ImportError('could not import gitpython>=%s' % required_version)
except ImportError as e:
print >>sys.stderr, e
return None
repo = git.Repo(os.path.dirname(__file__))
sha1 = repo.iter_commits().next().hexsha
return sha1
def find_dependency(soname, incname):
def test(prefix):
sofile = join_path(prefix, 'lib/{}'.format(soname))
incdir = join_path(prefix, 'include/{}'.format(incname))
if os.path.isfile(sofile) and os.path.isdir(incdir):
return (join_path(prefix, 'lib'),
join_path(prefix, 'include'))
return None
if 'VIRTUAL_ENV' in os.environ:
ret = test(os.environ['VIRTUAL_ENV'])
if ret is not None:
return ret[0], ret[1]
if 'CONDA_BUILD' in os.environ:
d = os.environ.get('PREFIX', None)
if d:
ret = test(d)
if ret is not None:
return ret[0], ret[1]
if 'CONDA_DEFAULT_ENV' in os.environ:
# shell out to conda to get info
cmd = ['conda', 'info', '--json']
s = Popen(cmd, shell=False, stdout=PIPE).stdout.read()
s = json.loads(s)
if 'default_prefix' in s:
ret = test(str(s['default_prefix']))
if ret is not None:
return ret[0], ret[1]
return None, None
def find_cython_dependency(dirname):
def test(prefix):
incdir = join_path(prefix, 'cython/{}'.format(dirname))
if os.path.isdir(incdir):
return join_path(prefix, 'cython')
return None
if 'VIRTUAL_ENV' in os.environ:
ret = test(os.environ['VIRTUAL_ENV'])
if ret is not None:
return ret
if 'CONDA_BUILD' in os.environ:
d = os.environ.get('PREFIX', None)
if d:
ret = test(d)
if ret is not None:
return ret
if 'CONDA_DEFAULT_ENV' in os.environ:
# shell out to conda to get info
cmd = ['conda', 'info', '--json']
s = Popen(cmd, shell=False, stdout=PIPE).stdout.read()
s = json.loads(s)
if 'default_prefix' in s:
ret = test(str(s['default_prefix']))
if ret is not None:
return ret
return None
def is_debug_build():
return 'DEBUG' in os.environ
def is_clang():
return sys.platform.lower().startswith('darwin')
def load_dependencies(basedir):
so_ext = 'dylib' if is_clang() else 'so'
min_cython_version = '0.20.2' if is_clang() else '0.20.1'
if LooseVersion(cython_version) < LooseVersion(min_cython_version):
raise ValueError(
'cython support requires cython>={}'.format(min_cython_version))
cc = os.environ.get('CC', None)
cxx = os.environ.get('CXX', None)
distributions_lib, distributions_inc = find_dependency(
'libdistributions_shared.{}'.format(so_ext), 'distributions')
microscopes_common_lib, microscopes_common_inc = find_dependency(
'libmicroscopes_common.{}'.format(so_ext), 'microscopes')
microscopes_common_cython_inc = find_cython_dependency('microscopes')
microscopes_common_lib, microscopes_common_inc = find_dependency(
'libmicroscopes_common.{}'.format(so_ext), 'microscopes')
if 'OFFICIAL_BUILD' not in os.environ:
sha1 = get_git_sha1()
if sha1 is None:
sha1 = 'unknown'
print 'writing git hash:', sha1
githashfile = join_path(basedir, 'githash.txt')
with open(githashfile, 'w') as fp:
print >>fp, sha1
elif is_debug_build():
raise RuntimeError("OFFICIAL_BUILD and DEBUG both set")
if distributions_inc is not None:
print 'Using distributions_inc:', distributions_inc
if distributions_lib is not None:
print 'Using distributions_lib:', distributions_lib
if microscopes_common_inc is not None:
print 'Using microscopes_common_inc:', microscopes_common_inc
if microscopes_common_cython_inc is not None:
print 'Using microscopes_common_cython_inc:', microscopes_common_cython_inc
if microscopes_common_lib is not None:
print 'Using microscopes_common_lib:', microscopes_common_lib
if microscopes_common_inc is not None:
print 'Using microscopes_common_inc:', microscopes_common_inc
if microscopes_common_lib is not None:
print 'Using microscopes_common_lib:', microscopes_common_lib
if cc is not None:
print 'Using CC={}'.format(cc)
if cxx is not None:
print 'Using CXX={}'.format(cxx)
if is_debug_build():
print 'Debug build'
include_dirs = [numpy.get_include()]
if 'EXTRA_INCLUDE_PATH' in os.environ:
include_dirs.append(os.environ['EXTRA_INCLUDE_PATH'])
if distributions_inc is not None:
include_dirs.append(distributions_inc)
if microscopes_common_inc is not None:
include_dirs.append(microscopes_common_inc)
library_dirs = []
if distributions_lib is not None:
library_dirs.append(distributions_lib)
if microscopes_common_lib is not None:
library_dirs.append(microscopes_common_lib)
return include_dirs, library_dirs
def build_extra_compile_args():
extra_compile_args = [
'-std=c++0x',
'-Wno-unused-function',
]
# taken from distributions
math_opt_flags = [
'-mfpmath=sse',
'-msse4.1',
]
if not is_debug_build():
extra_compile_args.extend(math_opt_flags)
if is_clang():
extra_compile_args.extend([
'-mmacosx-version-min=10.7', # for anaconda
'-stdlib=libc++',
'-Wno-deprecated-register',
])
if is_debug_build():
extra_compile_args.append('-DDEBUG_MODE')
return extra_compile_args
def build_extra_link_args():
extra_link_args = []
if 'EXTRA_LINK_ARGS' in os.environ:
extra_link_args.append(os.environ['EXTRA_LINK_ARGS'])
return extra_link_args
def make_extension(module_name):
sources = [module_name.replace('.', '/') + '.pyx']
return Extension(
module_name,
sources=sources,
language="c++",
include_dirs=include_dirs,
libraries=LIBRARY_DEPENDENCIES,
library_dirs=library_dirs,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args)
def read_readme():
with open('README.md') as f:
return f.read()
def get_version():
version = None
with open(join_path(basedir, '__init__.py')) as fp:
for line in fp:
if re.match("_version_base\s+=\s+'\S+'$", line):
version = line.split()[-1].strip("'")
if not version:
raise RuntimeError("could not determine version")
return version
check_call(['protoc', '--python_out=.', 'microscopes/io/schema.proto'])
basedir = join_path(os.path.dirname(__file__), 'microscopes', 'common')
include_dirs, library_dirs = load_dependencies(basedir)
extra_compile_args = build_extra_compile_args()
extra_link_args = build_extra_link_args()
extensions = cythonize([make_extension(module) for module in CYTHON_MODULES])
long_description = read_readme()
version = get_version()
setup(version=version,
name='microscopes-common',
description='Non-parametric bayesian inference',
long_description=long_description,
url='https://github.com/datamicroscopes/common',
author='Stephen Tu, Eric Jonas',
maintainer='Stephen Tu',
maintainer_email='[email protected]',
packages=(
'microscopes',
'microscopes.io',
'microscopes.common',
'microscopes.common.recarray',
'microscopes.common.relation',
'microscopes.common.variadic',
'microscopes.common.vendor',
'microscopes.dbg',
'microscopes.dbg.models',
),
ext_modules=extensions)