-
Notifications
You must be signed in to change notification settings - Fork 42
/
setup.py
265 lines (205 loc) · 6.45 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
#!/usr/bin/env python3
import os
import sys
from setuptools import setup, Extension, Command
from setuptools.command.build_py import build_py
import shutil
import time
op = os.path
packname = 'beat'
version = '1.1'
try:
import numpy
except ImportError:
class numpy():
def __init__(self):
pass
@classmethod
def get_include(self):
return
project_root = op.dirname(op.realpath(__file__))
REQUIREMENTS_FILE = op.join(project_root, 'requirements.txt')
with open(REQUIREMENTS_FILE) as f:
install_reqs = f.read().splitlines()
class NotInAGitRepos(Exception):
pass
def git_infos():
'''Query git about sha1 of last commit and check if there are local \
modifications.'''
from subprocess import Popen, PIPE
import re
def q(c):
return Popen(c, stdout=PIPE).communicate()[0]
if not op.exists('.git'):
raise NotInAGitRepos()
sha1 = q(['git', 'log', '--pretty=oneline', '-n1']).split()[0]
sha1 = re.sub(br'[^0-9a-f]', '', sha1)
sha1 = str(sha1.decode('ascii'))
sstatus = q(['git', 'status'])
local_modifications = bool(re.search(br'^#\s+modified:', sstatus,
flags=re.M))
return sha1, local_modifications
def make_info_module(packname, version):
'''Put version and revision information into file beat/info.py.'''
sha1, local_modifications = None, None
combi = '%s-%s' % (packname, version)
try:
sha1, local_modifications = git_infos()
combi += '-%s' % sha1
if local_modifications:
combi += '-modified'
except (OSError, NotInAGitRepos):
pass
datestr = time.strftime('%Y-%m-%d_%H:%M:%S')
combi += '-%s' % datestr
s = '''# This module is automatically created from setup.py
project_root = %s
git_sha1 = %s
local_modifications = %s
version = %s
long_version = %s # noqa
installed_date = %s
''' % tuple([repr(x) for x in (
project_root, sha1, local_modifications, version, combi, datestr)])
try:
f = open(op.join('beat', 'info.py'), 'w')
f.write(s)
f.close()
except Exception:
pass
def bash_completions_dir():
from subprocess import Popen, PIPE
def q(c):
return Popen(c, stdout=PIPE).communicate()[0]
try:
d = q(['pkg-config', 'bash-completion', '--variable=completionsdir'])
return d.strip().decode('utf-8')
except Exception:
return None
def find_beat_installs():
found = []
seen = set()
orig_sys_path = sys.path
for p in sys.path:
ap = op.abspath(p)
if ap == op.abspath('.'):
continue
if ap in seen:
continue
seen.add(ap)
sys.path = [p]
try:
import beat
dpath = op.dirname(op.abspath(beat.__file__))
x = (beat.installed_date, p, dpath,
beat.long_version)
found.append(x)
del sys.modules['beat']
del sys.modules['beat.info']
except (ImportError, AttributeError):
pass
sys.path = orig_sys_path
return found
def print_installs(found, file):
print(
'\nsys.path configuration is: \n %s\n' % '\n '.join(sys.path),
file=file)
dates = sorted([xx[0] for xx in found])
i = 1
for (installed_date, p, installed_path, long_version) in found:
oldnew = ''
if len(dates) >= 2:
if installed_date == dates[0]:
oldnew = ' (oldest)'
if installed_date == dates[-1]:
oldnew = ' (newest)'
print('''BEAT installation #%i:
date installed: %s%s
version: %s
path: %s
''' % (i, installed_date, oldnew, long_version, installed_path), file=file)
i += 1
class Uninstall(Command):
description = 'delete installations of BEAT known to the invoked ' \
'Python interpreter'''
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
found = find_beat_installs()
print_installs(found, sys.stdout)
if found:
print('''
Use the following commands to remove the BEAT installation(s) known to the
currently running Python interpreter:
sudo rm -rf build''')
for _, _, install_path, _ in found:
print(' sudo rm -rf "%s"' % install_path)
print()
else:
print('''
No BEAT installations found with the currently running Python interpreter.
''')
class custom_build_py(build_py):
def run(self):
make_info_module(packname, version)
build_py.run(self)
bd_dir = bash_completions_dir()
if bd_dir:
try:
shutil.copy('extras/beat', bd_dir)
print('Installing beat bash_completion to "%s"' % bd_dir)
except Exception:
print(
'Could not install beat bash_completion to "%s" '
'(continuing without)'
% bd_dir)
subpackages = [
'beat.fast_sweeping',
'beat.voronoi',
'beat.sampler',
'beat.models',
'beat.ffi',
'beat.apps']
setup(
cmdclass={
'build_py': custom_build_py,
'uninstall': Uninstall},
name='beat',
description='Bayesian Earthquake Analysis Tool',
version=version,
author='Hannes Vasyuara-Bathke',
author_email='[email protected]',
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Science/Research',
'Topic :: Scientific/Engineering',
'Topic :: Scientific/Engineering :: Physics',
'Programming Language :: Python :: 3.5',
'Programming Language :: C',
'Operating System :: POSIX',
'Operating System :: MacOS',
],
install_requires=install_reqs,
packages=['beat'] + subpackages,
package_dir={'beat': 'beat'},
entry_points={
'console_scripts':
['beat = beat.apps.beat:main',
'beatdown = beat.apps.beatdown:main']
},
package_data={'beat': []},
ext_modules=[
Extension(
'fast_sweep_ext',
sources=[op.join('beat/fast_sweeping', 'fast_sweep_ext.c')],
include_dirs=[numpy.get_include()]),
Extension(
'voronoi_ext',
extra_compile_args=['-lm'],
sources=[op.join('beat/voronoi', 'voronoi_ext.c')],
include_dirs=[numpy.get_include()])
]
)