-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathtasks.py
More file actions
370 lines (305 loc) · 11.1 KB
/
tasks.py
File metadata and controls
370 lines (305 loc) · 11.1 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"""
tasks.py is a Python file used in the task automation framework Invoke.
It contains a collection of tasks, which are functions that can be executed from the command line.
To execute a task, you can run the `invoke` command followed by the task name.
For example, to build the project, you can run `invoke build`.
"""
from invoke import task
from invoke.collection import Collection
from invoke.context import Context
from invoke.exceptions import UnexpectedExit
from pathlib import Path
from typing import List, Optional
from subprocess import run
import wget
from os import environ
IS_CI = environ.get("CI", "false").lower() == "true"
COVERAGE_DEFAULT = False
def divider(text: str):
"""Print a divider with text centered."""
print(text.center(88, "-"))
def all_exist(files: List[str] = None, directories: List[str] = None) -> bool:
"""Check if all files and directories exist."""
if files:
for file in files:
file = Path(file)
if not file.exists() or not file.is_file():
return False
if directories:
for directory in directories:
directory = Path(directory)
if not directory.exists() or not directory.is_dir():
return False
return True
@task()
def docs_build(ctx: Context, ignore_warnings: bool = False):
"""Build the documentation using Sphinx."""
cmd = []
cmd += "python -m sphinx build".split()
cmd += ["--color"] # color output
cmd += ["-b", "html"] # generate html
if not ignore_warnings:
cmd += ["-W"] # warnings as errors
cmd += ["-n"] # nitpicky mode
doc_dir = Path("docs/_build")
doc_dir.mkdir(exist_ok=True, parents=True)
cmd += ["docs", doc_dir.as_posix()] # add source and output directories
try:
ctx.run(" ".join(cmd), echo=True)
print("-" * 80)
print("Documentation is built and available at:")
print(f" file://{doc_dir.resolve()}/index.html")
print("You can copy and paste this URL into your browser.")
except UnexpectedExit as err:
print("-" * 80)
print(
"Documentation build failed. Here are some tips:\n"
" - Check the Sphinx output for errors and warnings.\n"
" - Try running `invoke docs.clean` to remove cached files.\n"
" - Try running with `--ignore-warnings` to ignore warnings.\n"
" The build in CI pipelines will still fail but this might\n"
" help you fix the warnings locally.\n"
)
# Ensure error code is propagated for CI/CD pipelines
raise SystemExit(err.result.return_code)
@task
def docs_clean(ctx: Context):
"""Remove the built documentation."""
ctx.run("rm -r docs/_build")
ctx.run("rm docs/api/modules/*")
@task
def download_moa(ctx: Context):
"""Download moa.jar from the web."""
url = ctx["moa_url"]
moa_path = Path(ctx["moa_path"])
if not moa_path.exists():
moa_path.parent.mkdir(parents=True, exist_ok=True)
print(f"Downloading moa.jar from : {url}")
wget.download(url, out=moa_path.resolve().as_posix())
else:
print("Nothing todo: `moa.jar` already exists.")
@task(pre=[download_moa])
def build_stubs(ctx: Context):
"""Build Java stubs using stubgenj.
Uses https://pypi.org/project/stubgenj/ https://gitlab.cern.ch/scripting-tools/stubgenj
to generate Python stubs for Java classes. This is useful for type checking and
auto-completion in IDEs. The generated stubs are placed in the `src` directory
with the `-stubs` suffix.
"""
moa_path = Path(ctx["moa_path"])
assert moa_path.exists() and moa_path.is_file()
class_path = moa_path.resolve().as_posix()
if all_exist(
directories=[
"src/moa-stubs",
"src/com-stubs/yahoo/labs/samoa",
"src/com-stubs/github/javacliparser",
]
):
print("Nothing todo: Java stubs already exist.")
return
run(
[
"python",
"-m",
"stubgenj",
f"--classpath={class_path}",
"--output-dir=src",
# Options
"--convert-strings",
"--no-jpackage-stubs",
# Names of the packages to generate stubs for
"moa",
"com.yahoo.labs.samoa",
"com.github.javacliparser",
],
check=True,
)
@task
def clean_stubs(ctx: Context):
"""Remove the Java stubs."""
ctx.run(
"rm -r src/moa-stubs src/com-stubs || echo 'Nothing to do: Java stubs do not exist.'"
)
@task(pre=[clean_stubs])
def clean_moa(ctx: Context):
"""Remove the moa.jar file."""
moa_path = Path(ctx["moa_path"])
if moa_path.exists():
moa_path.unlink()
print("Removed moa.jar.")
else:
print("Nothing todo: `moa.jar` does not exist.")
@task(pre=[clean_stubs, clean_moa, download_moa, build_stubs])
def refresh_moa(ctx: Context):
"""Replace the moa.jar file with the appropriate version.
The appropriate version is determined by the `moa_url` variable in the `invoke.yaml` file.
This is equivalent to the following steps:
1. Remove the moa.jar file `invoke build.clean-moa`.
2. Download the moa.jar file `invoke build.download-moa`.
3. Build the Java stubs. `invoke build.java-stubs`
"""
ctx.run("python -c 'import capymoa; capymoa.about()'")
@task(pre=[clean_stubs, clean_moa])
def clean(ctx: Context):
"""Clean all build artifacts."""
@task(
help={
"parallel": "Run the notebooks in parallel.",
"overwrite": (
"Overwrite the notebooks with the executed output. Requires ``--slow``."
),
"k_pattern": "Run only the notebooks that match the pattern. Same as `pytest -k`",
"slow": (
"Run the notebooks in slow mode by setting the environment variable "
"`NB_FAST` to `false`."
),
"no_skip": "Do not skip any notebooks.",
}
)
def notebooks(
ctx: Context,
parallel: bool = False,
overwrite: bool = False,
k_pattern: Optional[str] = None,
slow: bool = False,
no_skip: bool = False,
):
"""Run the notebooks and check for errors.
Uses nbmake https://github.com/treebeardtech/nbmake to execute the notebooks
and check for errors.
Note that nbmake does not support code coverage.
The `--overwrite` flag can be used to overwrite the notebooks with the
executed output.
"""
assert not (not slow and overwrite), "You cannot use `--overwrite` with `--fast`."
env = {"COVERAGE_FILE": ".coverage.notebooks"}
# Set the environment variable to run the notebooks in fast mode.
if not slow:
environ["NB_FAST"] = "true"
timeout = 60 * 3
else:
timeout = -1
skip_notebooks = ctx["test_skip_notebooks"]
if skip_notebooks is None or no_skip:
skip_notebooks = []
print(f"Skipping notebooks: {skip_notebooks}")
cmd = [
"python -m pytest --nbmake",
"-x", # Stop after the first failure
f"--nbmake-timeout={timeout}",
"notebooks",
"--durations=5", # Show the duration of each notebook
]
cmd += ["-n=auto"] if parallel else [] # Should we run in parallel?
# Overwrite the notebooks with the executed output
cmd += ["--overwrite"] if overwrite else []
if len(skip_notebooks) > 0:
cmd += ["--deselect " + nb for nb in skip_notebooks] # Skip some notebooks
if k_pattern:
cmd += [f"-k {k_pattern}"]
ctx.run(" ".join(cmd), echo=True, env=env)
@task
def pytest(ctx: Context, parallel: bool = True, coverage: bool = COVERAGE_DEFAULT):
"""Run the tests using pytest."""
env = {"COVERAGE_FILE": ".coverage.pytest"}
cmd = [
"python -m pytest",
"--durations=5", # Show the duration of each test
"--exitfirst", # Exit instantly on first error or failed test
]
cmd += ["--cov"] if coverage else []
cmd += ["-n=auto"] if parallel else []
ctx.run(" ".join(cmd), echo=True, env=env)
@task
def doctest(ctx: Context, parallel: bool = True, coverage: bool = COVERAGE_DEFAULT):
"""Run tests defined in docstrings using pytest."""
env = {"COVERAGE_FILE": ".coverage.doctest"}
cmd = [
"python -m pytest",
"--doctest-modules", # Enable doctest tests
"--durations=5", # Show the duration of each test
"--exitfirst", # Exit instantly on first error or failed test
"src/capymoa", # Don't run tests in the `tests` directory
]
cmd += ["--cov"] if coverage else []
cmd += ["-n=auto"] if parallel else []
ctx.run(" ".join(cmd), echo=True, env=env)
@task(aliases=["cov-combine"])
def coverage_combine(ctx: Context):
"""Combine coverage data from different sources."""
cmd = ["python -m coverage combine --keep"]
covfiles = [
".coverage.pytest",
".coverage.doctest",
]
for covfile in covfiles:
if Path(covfile).exists():
cmd += [covfile]
ctx.run(" ".join(cmd), echo=True)
@task(aliases=["cov-report"], pre=[coverage_combine])
def coverage_report(ctx: Context):
"""Generate coverage report."""
ctx.run("python -m coverage html -i", echo=True)
@task(aliases=["cov-clean"])
def coverage_clean(ctx: Context):
"""Clean coverage data."""
ctx.run("python -m coverage erase", echo=True)
ctx.run("rm -rf htmlcov", echo=True)
@task
def all_tests(ctx: Context, parallel: bool = True, coverage: bool = COVERAGE_DEFAULT):
"""Run all the tests."""
divider("test.pytest")
pytest(ctx, parallel, coverage)
divider("test.doctest")
doctest(ctx, parallel, coverage)
divider("test.notebooks")
notebooks(ctx, parallel)
if coverage:
divider("test.cov-report")
coverage_combine(ctx)
coverage_report(ctx)
@task
def commit(ctx: Context):
"""Commit changes using conventional commits.
Utility wrapper around `python -m commitizen commit`.
"""
print("Running Lint Checks ...")
ctx.run("python -m ruff check")
print("Running Format Checks ...")
ctx.run("python -m ruff format --check")
ctx.run("python -m commitizen commit", pty=True)
@task
def lint(ctx: Context):
"""Lint the code using ruff."""
ctx.run("python -m ruff check --fix")
@task(aliases=["fmt"])
def format(ctx: Context):
"""Format the code using ruff."""
ctx.run("python -m ruff format", echo=True)
ctx.run("python -m ruff check --fix", echo=True)
docs = Collection("docs")
docs.add_task(docs_build, "build", default=True)
docs.add_task(docs_clean, "clean")
build = Collection("build")
build.add_task(download_moa)
build.add_task(build_stubs, "stubs")
build.add_task(clean_stubs, "clean-stubs")
build.add_task(clean_moa, "clean-moa")
build.add_task(clean)
test = Collection("test")
test.add_task(all_tests, "all", default=True)
test.add_task(notebooks, "nb")
test.add_task(pytest, "pytest")
test.add_task(doctest, "doctest")
test.add_task(coverage_combine)
test.add_task(coverage_clean)
test.add_task(coverage_report)
ns = Collection()
ns.add_collection(docs)
ns.add_collection(build)
ns.add_collection(test)
ns.add_task(commit)
ns.add_task(refresh_moa)
ns.add_task(lint)
ns.add_task(format)