forked from h4l/rnginline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
274 lines (202 loc) · 7.45 KB
/
Copy pathtasks.py
File metadata and controls
274 lines (202 loc) · 7.45 KB
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
from __future__ import unicode_literals, print_function
import sys
from os import path
import os
import glob
import itertools
import operator
import shutil
import pkg_resources
from invoke import ctask as task
import six
from six.moves import shlex_quote, reduce
import wheel.pep425tags
ROOT = path.relpath(path.dirname(__file__))
@task
def clean_build(ctx):
shutil.rmtree(path.join(ROOT, "build"))
@task
def clean_dist(ctx):
shutil.rmtree(path.join(ROOT, "dist"))
def _pytest_args():
# On Python 3 we run doctests in modules. The doctests are PY3 specific due
# to output formatting differences between PY2 and 3. Also, the doctests
# are just supplemental examples, not the real tests.
args = ["--pyargs", "rnginline"]
if six.PY3:
return ["--doctest-modules"] + args
return args
@task
def test(ctx, combine_coverage=False):
"""Run rnginline test suite"""
cov_args = ["--parallel-mode"] if combine_coverage is True else []
ctx.run(cmd(["coverage", "run"] + cov_args +
["-m", "pytest"] + _pytest_args()))
if not combine_coverage:
_report_coverage(ctx)
def _report_coverage(ctx):
ctx.run("coverage report")
@task
def coverage(ctx):
"""
Combine coverage of Python 2 and Python 3 test runs
This captures Python 2/3 specific code branches in coverage results.
"""
print("Combining coverage of Python 2 and 3 test runs:")
print("===============================================")
ctx.run("coverage erase")
ctx.run("tox -e py27,py34 -- --combine-coverage")
ctx.run("coverage combine")
ctx.run("coverage html")
print()
print("Combined coverage of Python 2 and 3 test runs:")
print("==============================================")
print()
_report_coverage(ctx)
@task
def pep8(ctx):
"""Lint code for PEP 8 violations"""
ctx.run("flake8 --version")
ctx.run("flake8 setup.py tasks.py rnginline")
@task
def readme(ctx):
"""Lint the README for reStructuredText syntax issues"""
ctx.run("restructuredtext-lint README.rst")
@task
def docs_test(ctx, cache_dir=None, out_dir=None):
"""
Test the doctests in the Sphinx docs. Must be run with Python 3."""
if not six.PY3:
msg = """\
error: Tried to run doc's doctests with Python 2. They must be run with
Python 3 due to the doctest module not handling formatting differences
between 2 and 3."""
raise RuntimeError(msg)
docs(ctx, builder="doctest", cache_dir=cache_dir, out_dir=out_dir,
warnings_are_errors=True)
docs(ctx, builder="html", cache_dir=cache_dir, out_dir=out_dir,
warnings_are_errors=True)
@task
def docs(ctx, builder="html", cache_dir=None, out_dir=None,
warnings_are_errors=False):
"""Build sphinx documentation"""
opts = []
if cache_dir is not None:
opts += ["-d", cache_dir]
if warnings_are_errors is True:
opts += ["-W"]
out_dir = path.join(ROOT, "docs/_build/") if out_dir is None else out_dir
ctx.run(cmd(["sphinx-build", "-b", builder] + opts +
[path.join(ROOT, "docs/"), out_dir]))
@task(clean_build, clean_dist)
def build_dists(ctx):
"""Build distribution packages"""
ctx.run("python setup.py sdist", pty=True)
ctx.run("python setup.py bdist_wheel", pty=True)
@task
def test_dists(ctx):
"""Test the build distributions from ./dist/ in isolation"""
ctx.run("tox -c tox-dist.ini", pty=True)
@task
def test_dist(ctx, dist_type):
"""Test a built distribution"""
dist_file = get_distribution(dist_type)
ctx.run(cmd("pip", "install", "--ignore-installed", dist_file), pty=True)
ctx.run(cmd(["py.test"] + _pytest_args()), pty=True)
def get_distribution(type):
type_glob = {
"sdist": "rnginline-*.tar.gz",
"wheel": "rnginline-*.whl"
}.get(type)
if type_glob is None:
raise ValueError("Unknown distribution type: {0}".format(type))
pattern = path.join(ROOT, "dist", type_glob)
dists = glob.glob(pattern)
if len(dists) != 1:
raise ValueError("Expected one find one distribution matching: {0!r} "
"but got: {1}".format(pattern, len(dists)))
return dists[0]
@task
def cache_all_requirement_wheels(ctx):
ctx.run("tox -c tox-wheelcache.ini", pty=True)
def get_platform_tag():
return wheel.pep425tags.get_abbr_impl() + wheel.pep425tags.get_impl_ver()
@task
def cache_requirement_wheels(ctx):
wheelhouse = path.join(ROOT, "wheelhouse")
all_reqs = path.join(ROOT, "requirements", "all.txt")
with open(all_reqs) as f:
reqs = list(pkg_resources.parse_requirements(f.read()))
print("Checking if wheel cache is populated...")
absent_reqs = []
for req in reqs:
print("checking {0} ... ".format(req), end="")
sys.stdout.flush()
is_cached_cmd = cmd(
"pip", "install", "--download", "/tmp/", "--use-wheel",
"--no-index", "--find-links", wheelhouse, str(req))
result = ctx.run(is_cached_cmd, warn=True, hide="both")
if result.ok:
print("present")
else:
print("ABSENT")
absent_reqs.append(req)
if absent_reqs:
print()
print("Wheel cache is not complete, populating...")
# Build wheels for all our dependencies, storing them in the wheelhouse
# dir
ctx.run(cmd([
"pip", "wheel",
# Make built wheels specific to interpreter running this.
# Required because non-wheel packages like pytest are not
# necessarily universal. e.g. pytest for python 2.6 requires
# argparse, but 2.7, 3.3, 3.4 don't.
"--build-option", "--python-tag=" + get_platform_tag(),
"--wheel-dir", wheelhouse] +
list(map(six.text_type, absent_reqs))))
print()
print("Done")
@task
def gen_requirements_all(ctx, write=False):
files = (path.join(ROOT, "requirements", f)
for f in os.listdir(path.join(ROOT, "requirements"))
if f != "all.txt")
all_requirements = reduce(operator.add, map(load_requirements, files), [])
unique_requirements = merge_requirements(all_requirements)
all = "\n".join(six.text_type(req) for req in unique_requirements)
if write is False:
print(all)
else:
with open(path.join(ROOT, "requirements", "all.txt"), "w") as f:
f.write("# Auto generated by $ inv gen_requirements_all\n")
f.write(all)
f.write("\n")
def load_requirements(file):
with open(file) as f:
return list(pkg_resources.parse_requirements(f.read()))
def merge_dupes(reqs):
merged = set(reqs)
assert len(merged) != 0
if len(merged) > 1:
raise ValueError(
"Duplicate requirement for {} with differing version/extras: {}"
.format(next(iter(merged)).key, merged))
return next(iter(merged))
def merge_requirements(reqs):
reqs = sorted(reqs, key=lambda r: r.key)
grouped = itertools.groupby(reqs, key=lambda r: r.key)
return set(merge_dupes(dupes) for (key, dupes) in grouped)
def cmd(*args):
r"""
Create a shell command string from a list of arguments.
>>> print(cmd("a", "b", "c"))
a b c
>>> print(cmd(["ls", "-l", "some dir"]))
ls -l 'some dir'
>>> print(cmd(["echo", "I'm a \"string\"."]))
echo 'I'"'"'m a "string".'
"""
if len(args) == 1 and not isinstance(args[0], six.string_types):
return cmd(*args[0])
return " ".join(shlex_quote(arg) for arg in args)