-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtasks.py
194 lines (154 loc) · 4.15 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
import os
import sys
from invoke import task
PTY = sys.platform != "win32"
ROOT = os.path.dirname(__file__)
CLEAN_PATTERNS = [
"**/*.pyc",
"**/__pycache__",
"**/.pytest_cache",
"**/,mypy_cache",
"*.egg-info",
".cache",
".nox",
".tox",
"build",
"dist",
"docs/_build",
"reports",
"site",
]
LINTERS = (
("pyproject.toml validation", "poetry check"),
("Static Analysis", "flake8 bumpr"),
("Type checking", "mypy bumpr"),
)
FORMATTERS = (
("Sort imports using isort", "isort"),
("Format code using black", "black"),
)
def color(code):
"""A simple ANSI color wrapper factory"""
return lambda t: "\033[{0}{1}\033[0;m".format(code, t)
green = color("1;32m")
red = color("1;31m")
blue = color("1;30m")
cyan = color("1;36m")
purple = color("1;35m")
white = color("1;39m")
def header(text):
"""Display an header"""
print(" ".join((blue(">>"), cyan(text))))
sys.stdout.flush()
def info(text, *args, **kwargs):
"""Display informations"""
text = text.format(*args, **kwargs)
print(" ".join((purple(">>>"), text)))
sys.stdout.flush()
def success(text):
"""Display a success message"""
print(" ".join((green("✔"), white(text))))
sys.stdout.flush()
def error(text):
"""Display an error message"""
print(red("✘ {0}".format(text)))
sys.stdout.flush()
def exit(text=None, code=-1):
if text:
error(text)
sys.exit(code)
@task
def clean(ctx):
"""Cleanup all build artifacts"""
header(clean.__doc__)
with ctx.cd(ROOT):
for pattern in CLEAN_PATTERNS:
info(pattern)
ctx.run("rm -rf {0}".format(" ".join(CLEAN_PATTERNS)))
@task
def test(ctx, report=False, verbose=False):
"""Run tests suite"""
header(test.__doc__)
cmd = ["pytest"]
if verbose:
cmd.append("-v")
if report:
cmd.append("--junitxml=reports/tests.xml")
with ctx.cd(ROOT):
ctx.run(" ".join(cmd), pty=PTY)
@task
def cover(ctx, report=False, verbose=False):
"""Run tests suite with coverage"""
header(cover.__doc__)
cmd = [
"pytest",
"--cov-report=term",
"--cov=bumpr",
]
if verbose:
cmd.append("-v")
if report:
cmd += [
"--cov-report=html:{0}/reports/coverage".format(ROOT),
"--cov-report=xml:{0}/reports/coverage.xml".format(ROOT),
"--junitxml=reports/tests.xml",
]
with ctx.cd(ROOT):
ctx.run(" ".join(cmd), pty=PTY)
@task
def lint(ctx):
"""Run linters"""
header(lint.__doc__)
with ctx.cd(ROOT):
results = {}
for name, cmd in LINTERS:
info(name)
result = results[name] = ctx.run(cmd, pty=PTY, warn=True)
if result.failed:
error(f"{name} failed")
else:
success(f"{name} succeeded")
if any(r.failed for r in results.values()):
exit("some linters failed")
success("All linters succeeded")
@task
def format(ctx):
"""Format code"""
header(format.__doc__)
with ctx.cd(ROOT):
for name, cmd in FORMATTERS:
info(name)
ctx.run(f"{cmd} *.py bumpr tests", pty=PTY, warn=True)
@task
def tox(ctx):
"""Run test in all Python versions"""
header(tox.__doc__)
ctx.run("tox", pty=PTY)
@task
def doc(ctx, serve=False):
"""Build the documentation"""
header(doc.__doc__)
with ctx.cd(ROOT):
if serve:
ctx.run("mkdocs serve")
else:
ctx.run("mkdocs build", pty=PTY)
success("Documentation available in site/")
@task
def completion(ctx):
"""Generate bash completion script"""
header(completion.__doc__)
with ctx.cd(ROOT):
ctx.run("_bumpr_COMPLETE=source bumpr > bumpr-complete.sh", pty=PTY)
success("Completion generated in bumpr-complete.sh")
@task
def dist(ctx):
"""Package for distribution"""
header(dist.__doc__)
with ctx.cd(ROOT):
ctx.run("poetry build", pty=PTY)
success("Distribution is available in dist directory")
@task(clean, lint, test, doc, dist, default=True)
def all(ctx):
"""Run all tasks (default)"""
pass