-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
executable file
·304 lines (255 loc) · 7.66 KB
/
tasks.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# mypy: ignore-errors
"""
Development scripts.
You need ``invoke`` installed to run them.
"""
import os
import re
import sys
import invoke
ROOT = os.path.dirname(os.path.abspath(__file__))
PACKAGE = "src/py_gql"
DEFAULT_TARGETS = (
"%s tests examples" % PACKAGE
if sys.version >= "3.6"
else "%s tests" % PACKAGE
)
VALID_VERSION_RE = re.compile(r"^\d+\.\d+\.\d+(?:\.(dev|a|b|rc)\d+)?$")
def _join(*cmd):
return " ".join(c for c in cmd if c)
@invoke.task()
def benchmark(ctx):
"""
Run benchmarks.
"""
with ctx.cd(ROOT):
ctx.run(
_join(
"py.test",
"--benchmark-only",
"--benchmark-group-by=fullname",
"tests/benchmarks",
),
echo=True,
pty=True,
)
@invoke.task(iterable=["files", "ignore"])
def test(
ctx,
coverage=False,
bail=True,
verbose=False,
grep=None,
files=None,
junit=False,
ignore=None,
parallel=False,
watch=False,
):
"""
Run test suite (using: py.test).
You should be able to run pytest directly but this provides some useful
shortcuts and defaults.
"""
ignore = ignore or []
files = ("%s tests" % PACKAGE) if not files else " ".join(files)
with ctx.cd(ROOT):
ctx.run(
_join(
"py.test",
"-c setup.cfg",
"--exitfirst" if bail else None,
(
"--cov %s --cov-config setup.cfg --no-cov-on-fail "
"--cov-report term --cov-report html --cov-report xml "
)
% PACKAGE
if coverage
else None,
"--junit-xml junit.xml" if junit else None,
"--looponfail" if watch else None,
"-vvl --full-trace" if verbose else "-q",
"-rf",
"-k %s" % grep if grep else None,
"-n auto" if parallel else None,
(
" ".join("--ignore %s" % i for i in ignore)
if ignore
else None
),
files,
),
echo=True,
pty=True,
)
@invoke.task(iterable=["files"])
def flake8(ctx, files=None, junit=False):
files = DEFAULT_TARGETS if not files else " ".join(files)
try:
ctx.run(
_join(
"flake8",
"--output-file flake8.txt --tee" if junit else None,
files,
),
echo=True,
)
except invoke.exceptions.UnexpectedExit:
raise
finally:
if junit:
ctx.run("flake8_junit flake8.txt flake8.junit.xml", echo=True)
@invoke.task(aliases=["typecheck"], iterable=["files"])
def mypy(ctx, files=None, junit=False):
files = DEFAULT_TARGETS if not files else " ".join(files)
ctx.run(
_join("mypy", "--junit-xml mypy.junit.xml" if junit else None, files),
echo=True,
)
@invoke.task(aliases=["format"], iterable=["files"])
def fmt(ctx, files=None):
"""
Run formatters.
"""
with ctx.cd(ROOT):
ctx.run(
_join(
"isort",
(
"-rc %s setup.py tasks.py" % DEFAULT_TARGETS
if not files
else " ".join(files)
),
),
echo=True,
)
ctx.run(
_join(
"black",
(
"%s setup.py tasks.py" % DEFAULT_TARGETS
if not files
else " ".join(files)
),
),
echo=True,
)
@invoke.task(pre=[flake8, mypy, test])
def check(ctx):
"""
Run all checks (formatting, lint, typecheck and tests).
"""
with ctx.cd(ROOT):
pass
@invoke.task
def docs(ctx, clean_=True, strict=False, verbose=False):
"""
Generate documentation.
"""
with ctx.cd(os.path.join(ROOT, "docs")):
if clean_:
ctx.run("rm -rf _build", echo=True)
ctx.run(
_join(
"sphinx-build",
"-v" if verbose else "",
"-W" if strict else None,
"-b html",
'"." "_build"',
),
pty=True,
echo=True,
)
@invoke.task
def build(ctx, cythonize_module=False):
"""
Build source distribution and wheel.
"""
with ctx.cd(ROOT):
ctx.run("rm -rf dist", echo=True)
ctx.run(
_join(
"PY_GQL_USE_CYTHON=1" if cythonize_module else None,
"python",
"setup.py",
"sdist",
"bdist_wheel",
),
echo=True,
)
@invoke.task(iterable=["python"])
def build_manylinux_wheels(ctx, python, cythonize_module=True, all_=False):
"""
Build and extract a manylinux wheel using the official docker image.
See https://github.com/pypa/manylinux for more information.
"""
if not python and not all_:
raise invoke.exceptions.Exit("Must define at least one Python version.")
if all_:
python_versions = "35,36,37,38"
else:
python_versions = ",".join(python)
with ctx.cd(ROOT):
ctx.run(
_join(
"docker",
"run",
"--rm",
"-v $(pwd):/workspace",
"-w /workspace",
"-e PYTHON_VERSIONS=%s" % python_versions,
"-e PY_GQL_USE_CYTHON=1" if cythonize_module else None,
"quay.io/pypa/manylinux2010_x86_64",
"bash -c /workspace/scripts/build-manylinux-wheels.sh",
),
echo=True,
)
@invoke.task
def generate_checksums(ctx):
with ctx.cd(os.path.join(ROOT, "dist")):
ctx.run("rm -rf checksums.txt")
ctx.run(
'find . -name "*%s*" -type f -exec sha256sum "{}" + >| checksums.txt'
% PACKAGE,
echo=True,
)
@invoke.task
def update_version(ctx, version, force=False, push=False):
"""
Update version and create relevant git tag.
"""
with ctx.cd(ROOT):
if not VALID_VERSION_RE.match(version):
raise invoke.exceptions.Exit(
"Invalid version format, must match /%s/."
% VALID_VERSION_RE.pattern
)
pkg = {}
with open(os.path.join(PACKAGE, "_pkg.py")) as f:
exec(f.read(), {}, pkg)
local_version = pkg["__version__"]
if (not force) and local_version >= version:
raise invoke.exceptions.Exit(
"Must increment the version (current %s)." % local_version
)
with open(os.path.join(PACKAGE, "_pkg.py")) as f:
new_file = f.read().replace(local_version, version)
with open(os.path.join(PACKAGE, "_pkg.py"), "w") as f:
f.write(new_file)
modified = ctx.run("git ls-files -m", hide=True)
if (not force) and modified.stdout.strip() != "%s/_pkg.py" % PACKAGE:
raise invoke.exceptions.Exit(
"There are still modified files in your directory. "
"Commit or stash them."
)
ctx.run("git add %s/_pkg.py" % PACKAGE)
ctx.run("git commit -m v%s" % version)
ctx.run("git tag v%s" % version)
if push:
ctx.run("git push && git push --tags")
ns = invoke.Collection.from_module(sys.modules[__name__])
# Support calling a standalone CLI tool as long as invoke is installed.
if __name__ == "__main__":
invoke.Program(namespace=ns).run()