-
Notifications
You must be signed in to change notification settings - Fork 44
/
tasks.py
160 lines (113 loc) · 3.78 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
"""
This module provides the CLI interface for invoke tasks.
All tasks can be executed from this file's directory using:
$ inv <task>
Where <task> is a function defined below with the @task decorator.
"""
from functools import partial
import os
from invoke import Exit, UnexpectedExit, run as _run, task
PACKAGE_NAME = "cacheout"
PACKAGE_SOURCE = f"src/{PACKAGE_NAME}"
TEST_TARGETS = f"{PACKAGE_SOURCE} tests"
LINT_TARGETS = f"{TEST_TARGETS} tasks.py"
EXIT_EXCEPTIONS = (Exit, UnexpectedExit, SystemExit)
# Set pyt=True to enable colored output when available.
run = partial(_run, pty=True)
@task
def black(ctx, quiet=False):
"""Autoformat code using black."""
run(f"black {LINT_TARGETS}", hide=quiet)
@task
def isort(ctx, quiet=False):
"""Autoformat Python imports."""
run(f"isort {LINT_TARGETS}", hide=quiet)
@task
def docformatter(ctx):
"""Autoformat docstrings using docformatter."""
run(
f"docformatter -r {LINT_TARGETS} "
f"--in-place --pre-summary-newline --wrap-descriptions 100 --wrap-summaries 100"
)
@task
def fmt(ctx):
"""Autoformat code and docstrings."""
print("Running docformatter")
docformatter(ctx)
print("Running isort")
isort(ctx, quiet=True)
print("Running black")
black(ctx, quiet=True)
@task
def flake8(ctx):
"""Check code for PEP8 violations using flake8."""
run(f"flake8 --format=pylint {LINT_TARGETS}")
@task
def pylint(ctx):
"""Check code for static errors using pylint."""
run(f"pylint {LINT_TARGETS}")
@task
def mypy(ctx):
"""Check code using mypy type checker."""
run(f"mypy {LINT_TARGETS}")
@task
def lint(ctx):
"""Run linters."""
linters = {"flake8": flake8, "pylint": pylint, "mypy": mypy}
failures = []
print(f"Preparing to run linters: {', '.join(linters)}\n")
for name, linter in linters.items():
print(f"Running {name}")
try:
linter(ctx)
except EXIT_EXCEPTIONS:
failures.append(name)
result = "FAILED"
else:
result = "PASSED"
print(f"{result}\n")
if failures:
failed = ", ".join(failures)
raise Exit(f"ERROR: linters failed: {failed}")
@task(help={"args": "Override default pytest arguments"})
def test(ctx, args=f"{TEST_TARGETS} --cov={PACKAGE_NAME}"):
"""Run unit tests using pytest."""
tox_env_site_packages_dir = os.getenv("TOX_ENV_SITE_PACKAGES_DIR")
if tox_env_site_packages_dir:
# Re-path package source to match tox env so that we generate proper coverage report.
tox_env_pkg_src = os.path.join(tox_env_site_packages_dir, os.path.basename(PACKAGE_SOURCE))
args = args.replace(PACKAGE_SOURCE, tox_env_pkg_src)
run(f"pytest {args}")
@task
def ci(ctx):
"""Run linters and tests."""
print("Building package")
build(ctx)
print("Building docs")
docs(ctx)
print("Checking linters")
lint(ctx)
print("Running unit tests")
test(ctx)
@task
def docs(ctx, serve=False, bind="127.0.0.1", port=8000):
"""Build docs."""
run("rm -rf docs/_build")
run("sphinx-build -q -W -b html docs docs/_build/html")
if serve:
print(f"Serving docs on {bind} port {port} (http://{bind}:{port}/) ...")
run(f"python -m http.server -b {bind} --directory docs/_build/html {port}", hide=True)
@task
def build(ctx):
"""Build Python package."""
run("rm -rf dist build docs/_build")
run("python -m build")
@task
def clean(ctx):
"""Remove temporary files related to development."""
run("find . -type f -name '*.py[cod]' -delete -o -type d -name __pycache__ -delete")
run("rm -rf .tox .coverage .cache .pytest_cache .mypy_cache **/.egg* **/*.egg* dist build")
@task(pre=[build])
def release(ctx):
"""Release Python package."""
run("twine upload dist/*")