-
Notifications
You must be signed in to change notification settings - Fork 63
/
setup.py
244 lines (226 loc) · 8.5 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
# -*- coding: utf-8 -*-
# Copyright (c) 2010-2016, MIT Probabilistic Computing Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# If some modules are not found, we use others, so no need to warn:
# pylint: disable=import-error
try:
from setuptools import setup
from setuptools.command.build_py import build_py
from setuptools.command.sdist import sdist
from setuptools.command.test import test
except ImportError:
from distutils.core import setup
from distutils.cmd import Command
from distutils.command.build_py import build_py
from distutils.command.sdist import sdist
class test(Command):
def __init__(self, *args, **kwargs):
Command.__init__(self, *args, **kwargs)
def initialize_options(self): pass
def finalize_options(self): pass
def run(self): self.run_tests()
def run_tests(self): Command.run_tests(self)
def set_undefined_options(self, opt, val):
Command.set_undefined_options(self, opt, val)
def get_version():
import re
import subprocess
# git describe a commit using the most recent tag reachable from it.
# Release tags start with v* (XXX what about other tags starting with v?)
# and are of the form `v1.1.2`.
#
# The output `desc` will be of the form v1.1.2-2-gb92bef6[-dirty]:
# - verpart v1.1.2
# - revpart 2
# - localpart gb92bef6[-dirty]
desc = subprocess.check_output([
'git', 'describe', '--dirty', '--long', '--match', 'v*',
])
match = re.match(r'^v([^-]*)-([0-9]+)-(.*)$', desc)
assert match is not None
verpart, revpart, localpart = match.groups()
# Create a post version.
if revpart > '0' or 'dirty' in localpart:
# Local part may be g0123abcd or g0123abcd-dirty.
# Hyphens not kosher here, so replace by dots.
localpart = localpart.replace('-', '.')
full_version = '%s.post%s+%s' % (verpart, revpart, localpart)
# Create a release version.
else:
full_version = verpart
# Strip the local part if there is one, to appease pkg_resources,
# which handles only PEP 386, not PEP 440.
if '+' in full_version:
pkg_version = full_version[:full_version.find('+')]
else:
pkg_version = full_version
# Sanity-check the result. XXX Consider checking the full PEP 386
# and PEP 440 regular expressions here?
assert '-' not in full_version, '%r' % (full_version,)
assert '-' not in pkg_version, '%r' % (pkg_version,)
assert '+' not in pkg_version, '%r' % (pkg_version,)
return pkg_version, full_version
pkg_version, full_version = get_version()
def write_version_py(path):
try:
with open(path, 'rb') as f:
version_old = f.read()
except IOError:
version_old = None
version_new = '__version__ = %r\n' % (full_version,)
if version_old != version_new:
print 'writing %s' % (path,)
with open(path, 'wb') as f:
f.write(version_new)
def sha256_file(pathname):
import hashlib
sha256 = hashlib.sha256()
with open(pathname, 'rb') as source_file:
for block in iter(lambda: source_file.read(65536), ''):
sha256.update(block)
return sha256
def uptodate(path_in, path_out, path_sha256):
import errno
try:
sha256_in = sha256_file(path_in).hexdigest()
sha256_out = sha256_file(path_out).hexdigest()
expected = bytes('%s\n%s\n' % (sha256_in, sha256_out))
with open(path_sha256, 'rb') as file_sha256:
actual = file_sha256.read(len(expected))
if actual != expected or file_sha256.read(1) != '':
return False
except (IOError, OSError) as e:
if e.errno != errno.ENOENT:
raise
return False
return True
def commit(path_in, path_out, path_sha256):
import os
with open(path_sha256 + '.tmp', 'wb') as file_sha256:
file_sha256.write('%s\n' % (sha256_file(path_in).hexdigest(),))
file_sha256.write('%s\n' % (sha256_file(path_out).hexdigest(),))
os.rename(path_sha256 + '.tmp', path_sha256)
def generate_parser(lemonade, path_y):
import distutils.spawn
import os.path
import sys
root = os.path.dirname(os.path.abspath(__file__))
lemonade = os.path.join(root, *lemonade.split('/'))
base, ext = os.path.splitext(path_y)
assert ext == '.y'
path_py = base + '.py'
path_sha256 = base + '.sha256'
if uptodate(path_y, path_py, path_sha256):
return
print 'generating %s -> %s' % (path_y, path_py)
distutils.spawn.spawn([
'/usr/bin/env', 'PYTHONPATH=' + lemonade,
sys.executable,
lemonade + '/bin/lemonade',
'-s', # Write statistics to stdout.
path_y,
])
commit(path_y, path_py, path_sha256)
class local_build_py(build_py):
def run(self):
write_version_py(version_py)
for grammar in grammars:
generate_parser(lemonade, grammar)
build_py.run(self)
# Make sure the VERSION file in the sdist is exactly specified, even
# if it is a development version, so that we do not need to run git to
# discover it -- which won't work because there's no .git directory in
# the sdist.
class local_sdist(sdist):
def make_release_tree(self, base_dir, files):
import os
sdist.make_release_tree(self, base_dir, files)
version_file = os.path.join(base_dir, 'VERSION')
print('updating %s' % (version_file,))
# Write to temporary file first and rename over permanent not
# just to avoid atomicity issues (not likely an issue since if
# interrupted the whole sdist directory is only partially
# written) but because the upstream sdist may have made a hard
# link, so overwriting in place will edit the source tree.
with open(version_file + '.tmp', 'wb') as f:
f.write('%s\n' % (pkg_version,))
os.rename(version_file + '.tmp', version_file)
class local_test(test):
description = "Run check.sh"
user_options = [('fail=', None, 'Use check.sh.')] # for distutils
def __init__(self, *args, **kwargs):
test.__init__(self, *args, **kwargs)
self.test_suite = "not None"
def run_tests(self):
import subprocess
subprocess.check_call(["./check.sh"])
print "Using ./check.sh directly gives you more options for testing."
# XXX These should be attributes of `setup', but helpful distutils
# doesn't pass them through when it doesn't know about them a priori.
version_py = 'src/version.py'
lemonade = 'external/lemonade/dist'
grammars = [
'src/grammar.y',
'src/backends/cgpm_alter/grammar.y',
'src/backends/cgpm_analyze/grammar.y',
'src/backends/cgpm_schema/grammar.y',
]
setup(
name='bayeslite',
version=pkg_version,
description='BQL database built on SQLite3',
url='http://probcomp.csail.mit.edu/bayesdb',
author='MIT Probabilistic Computing Project',
author_email='[email protected]',
license='Apache License, Version 2.0',
tests_require=[
'pandas',
'pexpect',
'pytest',
'scipy',
],
packages=[
'bayeslite',
'bayeslite.backends',
'bayeslite.backends.cgpm_alter',
'bayeslite.backends.cgpm_analyze',
'bayeslite.backends.cgpm_schema',
'bayeslite.plex',
'bayeslite.shell',
'bayeslite.tests',
'bayeslite.weakprng',
],
package_dir={
'bayeslite': 'src',
'bayeslite.plex': 'external/plex/dist/Plex',
'bayeslite.shell': 'shell/src',
'bayeslite.tests': 'tests',
'bayeslite.weakprng': 'external/weakprng/dist',
},
# Not in this release, perhaps later.
#scripts=['shell/scripts/bayeslite'],
test_suite = "not None", # Without it, run_tests is not called.
cmdclass={
'build_py': local_build_py,
'sdist': local_sdist,
'test': local_test,
},
package_data={
'bayeslite.backends': ['*.schema.json'],
'bayeslite.tests': [
'dha.csv',
'satellites.csv',
],
},
)