-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtasks.py
More file actions
484 lines (399 loc) · 15.3 KB
/
Copy pathtasks.py
File metadata and controls
484 lines (399 loc) · 15.3 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import shlex
import glob
import shutil
from invoke import task, exceptions
from rich import print
import platform
from pathlib import Path
_HOST_SYSTEM = platform.system()
_SUPPORTED_SYSTEMS = (
"Windows",
"Linux",
"Darwin",
)
_PACKAGE_NAME = "mypackage"
def _task_screen_log(message: str, bold: bool = True, color: str = "blue") -> None:
"""
Convenient function to display a message on terminal during task executions.
"""
rich_delimiters = f"bold {color}" if bold else f"{color}"
print(f"[{rich_delimiters}]{message}[/{rich_delimiters}]")
@task(
help={
"verbose": "Run pytest in verbose mode",
"color": "Colorize pytest output",
"check_coverage": "Display coverage summary table after running the tests",
"generate_report": "Generate pytest report and save it as a xml file (named pytest.xml)",
"generate_cov_xml": "Generate coverage report and save it as a xml file (named coverage.xml)",
"cov_append": "Append coverage output to existing coverage file",
},
)
def tests_ipynb(
ctx,
verbose=True,
color=True,
check_coverage=False,
generate_cov_xml=False,
generate_report=False,
cov_append=False,
):
"""
Run notebooks as regression tests for each cell.
"""
task_output_message = "Running notebooks as tests"
_task_screen_log(task_output_message)
base_command = "pytest -ra -q --nbval notebooks/supported"
if verbose:
base_command += " -v"
if color:
base_command += " --color=yes"
check_coverage_msg = ""
if check_coverage:
check_coverage_msg += f"--cov={_PACKAGE_NAME}"
base_command += f" {check_coverage_msg}"
generate_report_cmd = ""
if generate_report:
generate_report_cmd = "--junitxml=pytest.xml"
if check_coverage:
base_command += f" {generate_report_cmd}"
else:
base_command += f" --cov={_PACKAGE_NAME} {generate_report_cmd}"
generate_cov_xml_cmd = ""
if generate_cov_xml:
generate_cov_xml_cmd = "--cov-report xml:coverage.xml"
base_command += f" {generate_cov_xml_cmd}"
if generate_report or generate_cov_xml:
base_command += " --cov-report=term-missing:skip-covered"
if cov_append:
base_command += " --cov-append"
host_system = _HOST_SYSTEM
if host_system not in _SUPPORTED_SYSTEMS:
raise exceptions.Exit(f"{_PACKAGE_NAME} is running on unsupported operating system", code=1)
_task_screen_log(f"Running: {base_command}", color="yellow", bold=False)
pty_flag = True if host_system != "Windows" else False
ctx.run(base_command, pty=pty_flag)
@task(
help={
"numprocess": "Num of processes to run pytest in parallel",
"verbose": "Run pytest in verbose mode",
"color": "Colorize pytest output",
"check_coverage": "Display coverage summary table after running the tests",
"generate_report": "Generate pytest report and save it as a xml file (named pytest.xml)",
"generate_cov_xml": "Generate coverage report and save it as a xml file (named coverage.xml)",
"record_output": "Record all the pytest CLI output to pytest-coverage.txt file",
},
optional=["numprocess"],
)
def tests(
ctx,
numprocess=-1,
verbose=True,
color=True,
check_coverage=False,
generate_cov_xml=False,
generate_report=False,
record_output=False,
# ipynb=True,
):
"""
Run tests with pytest.
"""
task_output_message = "Running the tests"
_task_screen_log(task_output_message)
base_command = "pytest -ra -q"
if verbose:
base_command += " -v"
if color:
base_command += " --color=yes"
if numprocess != 1:
base_command += " -n"
if numprocess == -1:
base_command += " auto"
elif numprocess > 1:
base_command += f" {int(numprocess)}"
else:
_task_screen_log(
"Warning: there is no negative number of processes. Setting to 1 (serial).",
color="yellow",
)
base_command += " 1"
check_coverage_msg = ""
if check_coverage:
check_coverage_msg += f"--cov={_PACKAGE_NAME}"
base_command += f" {check_coverage_msg}"
generate_report_cmd = ""
if generate_report:
generate_report_cmd = "--junitxml=pytest.xml"
if check_coverage:
base_command += f" {generate_report_cmd}"
else:
base_command += f" --cov={_PACKAGE_NAME} {generate_report_cmd}"
generate_cov_xml_cmd = ""
if generate_cov_xml:
generate_cov_xml_cmd = "--cov-report xml:coverage.xml"
base_command += f" {generate_cov_xml_cmd}"
if generate_report or generate_cov_xml:
base_command += " --cov-report=term-missing:skip-covered"
if record_output:
base_command += " | tee pytest-coverage.txt"
host_system = _HOST_SYSTEM
if host_system not in _SUPPORTED_SYSTEMS:
raise exceptions.Exit(f"{_PACKAGE_NAME} is running on unsupported operating system", code=1)
_task_screen_log(f"Running: {base_command}", color="yellow", bold=False)
pty_flag = True if host_system != "Windows" else False
ctx.run(base_command, pty=pty_flag)
@task
def diff_coverage(ctx):
"""
Run diff_cover to verify if all new code is covered. Needs a coverage.xml file.
"""
task_output_message = "Check if diff code is covered"
_task_screen_log(task_output_message)
base_command = "diff-cover coverage.xml --config-file pyproject.toml"
_task_screen_log(f"Running: {base_command}", color="yellow", bold=False)
host_system = _HOST_SYSTEM
if host_system not in _SUPPORTED_SYSTEMS:
raise exceptions.Exit(f"{_PACKAGE_NAME} is running on unsupported operating system", code=1)
pty_flag = True if host_system != "Windows" else False
ctx.run(base_command, pty=pty_flag)
@task(
help={
"color": "Display output with colors",
"pretty": "Enable better and colorful mypy output",
"verbose": "Run mypy in verbose mode",
"files": "Files to be checked with mypy",
}
)
def type_check(ctx, pretty=False, verbose=False, color=True, files=""):
"""
Run mypy on mypackage to check for typing issues.
"""
task_output_message = f"Running typing check on {_PACKAGE_NAME}"
_task_screen_log(task_output_message)
base_command = "mypy"
if pretty:
base_command += " --pretty"
if verbose:
base_command += " --verbose"
if color:
base_command += " --color-output"
if files != "":
base_command += f" {files}"
_task_screen_log(f"Running: {base_command}", color="yellow", bold=False)
host_system = _HOST_SYSTEM
if host_system not in _SUPPORTED_SYSTEMS:
raise exceptions.Exit(f"{_PACKAGE_NAME} is running on unsupported operating system", code=1)
pty_flag = True if host_system != "Windows" else False
ctx.run(base_command, pty=pty_flag)
@task(help={"overwrite": "Reinstall git hooks overwriting the previous installation."})
def hooks(ctx, overwrite=False):
"""
Configure pre-commit in the local git.
"""
task_output_message = "Installing pre-commit hooks"
_task_screen_log(task_output_message)
base_command = "pre-commit install"
if overwrite:
base_command += " --overwrite"
_task_screen_log(f"Running: {base_command}", color="yellow", bold=False)
ctx.run(base_command)
@task(
pre=[hooks],
help={
"all_files": "Run git hooks in all files (may take some time)",
"files": "Run git hooks in a given set of files",
"verbose": "Run git hooks in verbose mode",
"from_ref": "(for usage with `--to-ref`) -- this option represents the original ref in a `from_ref...to_ref` diff expression. For `pre-push` hooks, this represents the branch you are pushing to. For `post-checkout` hooks, this represents the branch that was previously checked out.",
"to_ref": "(for usage with `--from-ref`) -- this option represents the destination ref in a `from_ref...to_ref` diff expression. For `pre-push` hooks, this represents the branch being pushed. For `post-checkout` hooks, this represents the branch that is now checked out.",
},
)
def run_hooks(ctx, all_files=False, verbose=False, files="", from_ref="", to_ref=""):
"""
Run all the installed git hooks in all.
"""
task_output_message = "Run installed git hooks"
_task_screen_log(task_output_message)
base_command = "pre-commit run"
if all_files:
base_command += " --all-files"
if verbose:
base_command += " --verbose"
if files != "":
base_command += f" --files '{files}'"
if from_ref != "":
base_command += f" --from-ref {from_ref}"
if to_ref != "":
base_command += f" --to-ref {to_ref}"
_task_screen_log(f"Running: {base_command}", color="yellow", bold=False)
host_system = _HOST_SYSTEM
if host_system not in _SUPPORTED_SYSTEMS:
raise exceptions.Exit(f"{_PACKAGE_NAME} is running on unsupported operating system", code=1)
pty_flag = True if host_system != "Windows" else False
ctx.run(base_command, pty=pty_flag)
@task
def dev_install(ctx):
"""
Install the package in the environment. Warning: use when developing with virtualenv only.
"""
task_output_message = "Installing the package in the active environment"
_task_screen_log(task_output_message)
base_command = 'pip install -e ".[dev]"'
host_system = _HOST_SYSTEM
if host_system not in _SUPPORTED_SYSTEMS:
raise exceptions.Exit(f"{_PACKAGE_NAME} is running on unsupported operating system", code=1)
pty_flag = True if host_system != "Windows" else False
ctx.run(base_command, pty=pty_flag)
@task
def docs_install(ctx):
"""
Install the docs dependencies in the venv. Warning: use when developing with virtualenv only.
"""
task_output_message = "Installing the docs dependencies in the active environment"
_task_screen_log(task_output_message)
base_command = 'pip install -e ".[docs]"'
host_system = _HOST_SYSTEM
if host_system not in _SUPPORTED_SYSTEMS:
raise exceptions.Exit(f"{_PACKAGE_NAME} is running on unsupported operating system", code=1)
pty_flag = True if host_system != "Windows" else False
ctx.run(base_command, pty=pty_flag)
@task(
help={
"dry": "Show what would be removed without actually deleting",
}
)
def clean(ctx, dry=False):
"""
Remove build/config dirs:
- mypackage.egg-info
- dist
- build
- *_cache
- site
"""
patterns = [
"*.egg-info",
"dist",
"build",
"*_cache",
"site",
]
# Collect all matching dirs
exclude_root = ".venv" # we need to skip changes in .venv
to_remove = []
for pat in patterns:
for d in Path(".").rglob(pat):
if not d.is_dir():
continue
# skip .venv
if exclude_root in d.parts:
continue
to_remove.append(d)
if not to_remove:
_task_screen_log("Nothing to clean.", color="yellow")
return
for d in to_remove:
_task_screen_log(f"{'Would remove:' if dry else 'Removing: '}{d}", color="yellow")
if not dry:
shutil.rmtree(d)
_task_screen_log(
f"\n{len(to_remove)} director{'y' if len(to_remove) == 1 else 'ies'} {'would be removed' if dry else 'removed'}.",
color="yellow",
)
@task(
help={
"verbose": "Build docs with verbose mode",
"clean": "Clean build of the docs, rebuilding the files",
"quiet": "Silence warnings",
"include-ipynb": "Collect and add supported notebooks as docs",
}
)
def build_docs(ctx, verbose=False, clean=True, quiet=False, include_ipynb=False):
"""
Builds the docs file to be deployed.
Warning: use when developing with virtualenv only.
"""
task_output_message = "Building the docs file"
_task_screen_log(task_output_message)
base_command = "mkdocs build"
if clean:
base_command += " --clean"
else:
base_command += " --dirty"
if verbose:
base_command += " --verbose"
if quiet:
base_command += " --quiet"
# We need to move or create symlinks to ipynbs files, moving them to
# docs, otherwise mkdocs will be unable to reach them
if include_ipynb:
ipynbs_path = Path("notebooks/supported")
target_ipynbs_dir = Path("docs/notebooks")
if not ipynbs_path.exists():
raise exceptions.Exit(f"Notebook source not found: {ipynbs_path}", 1)
# Ensure parent exists
target_ipynbs_dir.parent.mkdir(parents=True, exist_ok=True)
# Blow away any old copy
if target_ipynbs_dir.exists():
shutil.rmtree(target_ipynbs_dir)
# Copy everything under notebooks/supported → docs/notebooks
shutil.copytree(ipynbs_path, target_ipynbs_dir)
_task_screen_log(
f"Included notebooks from {ipynbs_path} → {target_ipynbs_dir}", color="yellow"
)
host_system = _HOST_SYSTEM
if host_system not in _SUPPORTED_SYSTEMS:
raise exceptions.Exit(f"{_PACKAGE_NAME} is running on unsupported operating system", code=1)
pty_flag = True if host_system != "Windows" else False
ctx.run(base_command, pty=pty_flag)
@task
def deploy_docs_local(ctx):
"""
Deploy MkDocs pages locally.
"""
task_output_message = "Deploying Docs locally"
_task_screen_log(task_output_message)
base_command = "mkdocs serve"
host_system = _HOST_SYSTEM
if host_system not in _SUPPORTED_SYSTEMS:
raise exceptions.Exit(f"{_PACKAGE_NAME} is running on unsupported operating system", code=1)
pty_flag = True if host_system != "Windows" else False
ctx.run(base_command, pty=pty_flag, echo=True)
@task(pre=[docs_install])
def deploy_docs_gh(ctx):
"""
Deploy MkDocs pages to Github.
"""
task_output_message = "Deploying Docs to Github"
_task_screen_log(task_output_message)
base_command = "mkdocs gh-deploy"
host_system = _HOST_SYSTEM
if host_system not in _SUPPORTED_SYSTEMS:
raise exceptions.Exit(f"{_PACKAGE_NAME} is running on unsupported operating system", code=1)
pty_flag = True if host_system != "Windows" else False
ctx.run(base_command, pty=pty_flag, echo=True)
@task(
help={
"src": "Glob pattern(s) or list of .ipynb paths to pair (default: notebooks/*.ipynb)",
"dry": "Preview only (no files will be changed)",
}
)
def pair_ipynbs(ctx, src="notebooks/**/*.ipynb", dry=False):
"""
Pair Jupyter notebooks with Python scripts (percent‐format).
"""
_task_screen_log("Pairing notebooks with Python scripts")
# Gather files
if isinstance(src, str):
raw = glob.glob(src, recursive=True)
else:
raw = list(src)
notebooks = [Path(p) for p in raw if Path(p).suffix == ".ipynb"]
if not notebooks:
raise exceptions.Exit(f"No notebooks found for {src}", 1)
# Run pairing
for nb in notebooks:
print(f"{'Would pair:' if dry else 'Pairing:'} {nb}")
if not dry:
cmd = ["jupytext", "--set-formats", "ipynb,py:percent", str(nb)]
ctx.run(" ".join(shlex.quote(x) for x in cmd), pty=(_HOST_SYSTEM != "Windows"))
print(f"{len(notebooks)} notebook(s) {'would be paired' if dry else 'paired'}.")