This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
tasks.py
219 lines (178 loc) · 6.02 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
"""
## Invoke tasks without needing a Makefile.
Configuration can found at invoke.yaml
https://github.com/pyinvoke/invoke
```sh
# See all available tasks
invoke --list
# Invoke a task
invoke hooks
```
:copyright: (c) 2021 by Leo Gallucci.
:license: Apache 2.0, see LICENSE for more details.
"""
import os
from invoke import run
from invoke import task
from invoke.exceptions import UnexpectedExit
# Ignoring all missing MyPy types until https://github.com/pyinvoke/invoke/issues/248
@task
def setup(c): # type: ignore
"""Set this project up for Development."""
cmd = "poetry env info --path"
venv_path = None
try:
venv_path = c.run(cmd).stdout.strip()
except UnexpectedExit:
print("failed: %s", cmd)
c.run("poetry install --no-root")
venv_path = c.run(cmd).stdout.strip()
c.run("poetry debug info")
if not os.path.exists(".venv"):
c.run(f'ln -fs "{venv_path}" .venv')
c.run("ls -lah --color=yes .venv")
c.run("poetry run pre-commit install")
@task
def clean_build(c): # type: ignore
"""Remove build artifacts."""
c.run("rm -rf build dist .eggs")
c.run("find . -name '*.egg-info' -exec rm -rf {} +")
c.run("find . -name '*.egg' -exec rm -f {} +")
@task
def clean_pyc(c): # type: ignore
"""Remove Python file artifacts."""
c.run("find . -name '*.pyc' -exec rm -f {} +")
c.run("find . -name '*.pyo' -exec rm -f {} +")
c.run("find . -name '*~' -exec rm -f {} +")
c.run("find . -name '__pycache__' -exec rm -rf {} +")
@task
def clean_test(c): # type: ignore
"""Remove test and coverage artifacts."""
c.run("rm -rf .coverage .mypy_cache .pytest_cache .tox")
c.run("rm -rf pylint-report.txt ut-report.xml")
@task
def clean_coverage(c): # type: ignore
"""Cleanup test coverage files."""
c.run("poetry run coverage erase")
@task(pre=[clean_build, clean_pyc, clean_test, clean_coverage])
def clean(c): # type: ignore
"""Remove all build, test, coverage and Python artifacts."""
pass
@task(aliases=["upgrade"])
def update(c): # type: ignore
"""Upgrade/update packages and hooks."""
c.run("poetry update")
c.run("pre-commit autoupdate --freeze")
@task(aliases=["pylint"])
def lint(c): # type: ignore
"""Lint files and save report."""
c.run(
"""
poetry run pylint --rcfile pyproject.toml -j 0 \
iometrics > pylint-report.txt
""".strip()
)
@task(aliases=["tests"])
def test(c): # type: ignore
"""Run tests quickly with the default Python."""
c.run("PYTHONPATH=./iometrics poetry run pytest -v")
@task
def pre_checks(c): # type: ignore
"""Check checks without pytest/pylint."""
# https://pre-commit.com/#usage-in-continuous-integration
c.run("SKIP=pytest,pylint poetry run pre-commit run --origin HEAD --source origin/HEAD")
@task(pre=[clean_coverage])
def coverage(c): # type: ignore
"""Check code coverage quickly with the default Python."""
c.run(
"""
poetry run coverage run --branch --source=iometrics \
-m pytest --junitxml=ut-report.xml tests/
""".strip()
)
c.run("poetry run pylint --rcfile pyproject.toml iometrics -r n")
@task(pre=[clean_coverage])
def enforce_coverage_render_tests(c): # type: ignore
"""Check code coverage above threshold and generate reports."""
c.run(
"""
poetry run pytest --cov=iometrics --cov-fail-under=80 --cov-branch \
--junit-xml xunit-reports/xunit-result-ut.xml \
--cov-report xml:coverage-reports/coverage-ut.xml
""".strip()
)
@task
def linter_and_qc(c): # type: ignore
"""Run linter, keep exit code."""
pylint_output_file_name = "pylint-report.txt"
try:
c.run(
"""
poetry run pylint --rcfile pyproject.toml \
-j 0 iometrics > {pylint_output_file_name}
""".strip()
)
except UnexpectedExit:
raise
finally:
recipes = ",".join(
[
"master-dashboard",
"pr-issues-as-comments",
"pr-quality-gate",
"pr-vs-master-cov",
]
)
c.run(
f"""
echo recipes="{recipes}" \
reportPath="{pylint_output_file_name}"
""".strip()
)
@task
def build_and_upload_documentation(c): # type: ignore
"""Build and upload project's documentation."""
def docs_have_changed() -> bool:
"""Check if documentation changed using git diff."""
try:
run("git diff --exit-code --quiet ${GITHUB_REF##*/} docs")
except UnexpectedExit:
# git diff returns exit code 1 if there were changes
return True
else:
return False
def disable_indexing_for_PRs() -> None:
with open("site/robots.txt", "w") as robots_file:
print(
"""
User-agent: *
Disallow: /pr
""".strip(),
file=robots_file,
)
docs_name = os.environ.get("DOCS_NAME")
# https://stackoverflow.com/a/62436027/511069
pr_number = os.getenv("CI_PULL_REQUEST_NUMBER", False)
url = f"https://{docs_name}.tbd.com/pr/${pr_number}/index.html"
if pr_number:
# pull request
c.run("mkdir -p /workspace/site")
c.run(f"poetry run mkdocs build --site-dir /workspace/site/pr/{pr_number}")
if docs_have_changed():
c.run(
f"""
git gh-status --url "{url}" \
"custom/guild-python/docs" \
"success" \
"Documentation generated."
""".strip()
)
else:
c.run("poetry run mkdocs build --site-dir /workspace/site")
disable_indexing_for_PRs()
else:
print("No PR number, no need to re-generate docs at /pr/")
@task(aliases=["check", "checks", "all-checks", "lint-all", "all"])
def hooks(c): # type: ignore
"""Run all pre-commit hooks on all files."""
c.run("poetry run pre-commit run --all-files")