-
Notifications
You must be signed in to change notification settings - Fork 38
/
build.py
275 lines (202 loc) · 6.72 KB
/
build.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
from __future__ import division, print_function, unicode_literals, with_statement
import fnmatch
import os
import shlex
import sys
import os.path
from fabricate import ExecutionError, main, run, shell, autoclean
# Core Executables
# ================
# See https://github.com/AspenWeb/pando.py/issues/542
USE_PY_VENV = sys.version_info > (3, 3)
def _virt(cmd, envdir='env'):
envdir = _env(envdir)
if os.name == "nt":
return os.path.join(envdir, 'Scripts', cmd + '.exe')
else:
return os.path.join(envdir, 'bin', cmd)
def _virt_version(envdir):
v = shell(_virt('python', envdir), '-c',
'import sys; print(sys.version_info[:2])')
return eval(v)
def _env(envdir='env'):
d = __env(envdir)
# extend the PATH
path = os.path.join(d, 'Scripts' if os.name == "nt" else 'bin')
os.environ['PATH'] = path + os.pathsep + os.environ.get('PATH', '')
return d
def __env(envdir):
# http://stackoverflow.com/a/1883251
if hasattr(sys, 'real_prefix'):
# We're already inside someone else's virtualenv.
return sys.prefix
elif hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix:
# We're already inside someone else's pyvenv.
return sys.prefix
elif os.path.exists(envdir):
# We've already built our own virtualenv.
return envdir
if USE_PY_VENV:
# use built-in venv module
run(sys.executable, '-m', 'venv', envdir)
else:
# use virtualenv instead
try:
import virtualenv
except ImportError:
# install it when missing
run(sys.executable, '-m', 'pip', 'install', '--user', 'virtualenv')
run(sys.executable, '-m', 'virtualenv', envdir)
return envdir
def env():
"""set up a base virtual environment"""
_env()
def _install_tox():
# install tox if it isn't there
try:
shell('pip', 'show', 'tox')
except ExecutionError:
run('pip', 'install', 'tox')
def _deps():
shell('pip', 'install', '-r', 'requirements.txt', ignore_status=False)
def _test_deps():
_deps()
shell('pip', 'install', '-r', 'requirements_tests.txt', ignore_status=False)
def _dev(envdir='env'):
envdir = _env(envdir)
_install_tox()
run('tox', '--notest', '--skip-missing-interpreters')
return envdir
def dev():
"""set up an environment able to run pando and the tests"""
_dev()
def clean_env():
"""clean env artifacts"""
shell('rm', '-rf', 'env')
def clean():
"""clean all artifacts"""
autoclean()
delete_files('*.pyc', '.')
clean_env()
clean_sphinx()
clean_test()
clean_build()
# Docs
# ====
def _sphinx_cmd(cmd, extra_args=[], extra_pkgs=[]):
envdir = _env()
_deps()
run('pip', 'install', *(['sphinx', 'sphinx-rtd-theme'] + extra_pkgs))
builddir = 'docs/_build'
run('mkdir', '-p', builddir)
args = ['-b', 'html', '-d', builddir + '/doctrees', 'docs', builddir + '/html']
args += extra_args
run(cmd, args)
def sphinx():
"""build sphinx documents"""
_sphinx_cmd("sphinx-build")
def autosphinx():
"""run sphinx-autobuild"""
_sphinx_cmd("sphinx-autobuild", ['--watch', 'pando'], extra_pkgs=['sphinx-autobuild'])
def clean_sphinx():
"""clean sphinx artifacts"""
shell('rm', '-rf', 'docs/_build')
# Testing
# =======
def _tox(*args, **kw):
_env()
_install_tox()
kw.setdefault('silent', False)
shell('tox', '--skip-missing-interpreters', '--', *args, **kw)
def test():
"""run all tests"""
# this calls tox, and tox calls the _test target below from inside each env
_tox(ignore_status=False)
def _test(pytest_args=()):
_test_deps()
delete_files('*.pyc', 'pando', 'tests')
pytest_args = pytest_args or shlex.split(os.environ.get('PYTEST_ARGS', ''))
shell('python', '-m', 'pytest', 'tests', *pytest_args, ignore_status=False, silent=False)
shell('pyflakes', 'pando', 'tests', ignore_status=False, silent=False)
def testf():
"""run tests, stopping at the first failure"""
_tox('python', 'build.py', '_testf', ignore_status=True)
def _testf():
_test(pytest_args=['-x'])
def pyflakes():
_tox('pyflakes', 'pando', 'tests', ignore_status=False)
def pylint():
"""run lint"""
envdir = _env()
run(_virt('pip', envdir), 'install', 'pylint')
run(_virt('pylint', envdir), '--rcfile=.pylintrc',
'pando', '|', 'tee', 'pylint.out', shell=True, ignore_status=True)
def test_cov():
"""run code coverage"""
os.environ['PYTEST_ARGS'] = (
'--junitxml=testresults.xml '
'--cov-report term '
'--cov-report xml '
'--cov-report html '
'--cov pando'
)
test()
def analyse():
"""run lint and coverage"""
pylint()
test_cov()
print('done!')
def clean_test():
"""clean test artifacts"""
shell('rm', '-rf', '.tox')
shell('rm', '-rf', '.coverage', 'coverage.xml', 'testresults.xml', 'htmlcov', 'pylint.out')
# Build
# =====
def build():
"""build an egg"""
run(sys.executable, 'setup.py', 'bdist_egg')
def wheel():
"""build a wheel"""
run(sys.executable, 'setup.py', 'bdist_wheel')
def clean_build():
"""clean build artifacts"""
run('python', 'setup.py', 'clean', '-a')
run('rm', '-rf', 'dist')
# Utils
# =====
def find_files(directory, pattern):
for root, dirs, files in os.walk(directory):
for filename in fnmatch.filter(files, pattern):
yield os.path.join(root, filename)
def delete_files(pattern, *directories):
for d in directories:
for fpath in find_files(d, pattern):
os.remove(fpath)
def show_targets():
"""show the list of valid targets (this list)"""
print("Valid targets:\n")
# organize these however
targets = ['show_targets', None,
'env', 'dev', 'testf', 'test', 'pylint', 'test_cov', 'analyse', None,
'build', 'wheel', None,
'sphinx', 'autosphinx', None,
'clean', 'clean_env', 'clean_test', 'clean_build', 'clean_sphinx', None,
]
#docs = '\n'.join([" %s - %s" % (t, LOCALS[t].__doc__) for t in targets])
#print(docs)
for t in targets:
if t is not None:
print(" %s - %s" % (t, LOCALS[t].__doc__))
else:
print("")
if len(targets) < (len(LOCALS) - len(NON_TARGETS)):
missed = set(LOCALS.keys()).difference(NON_TARGETS, targets)
print("Unordered targets: " + ', '.join(sorted(missed)))
sys.exit()
LOCALS = dict(locals())
NON_TARGETS = [ 'main', 'autoclean', 'run', 'shell' ]
NON_TARGETS += list(x for x in LOCALS if x.startswith('_') or not callable(LOCALS[x] ))
if __name__ == '__main__':
main( default='show_targets'
, ignoreprefix="python" # workaround for gh190
)