-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtasks.py
117 lines (86 loc) · 2.95 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
"""Defines following commands which use Invoke[1].
invoke format
* Format the files automatically
invoke check
* Check the formatting, type hints, etc.
invoke docs
* Start sphinx build for documentation
invoke test
* Runs tests and coverage for the source tree (does not test docs build)
In addition to these, tox is an important command. Some examples (defined in
tox.ini):
tox
* run tests with multiple versions of python (if available on the system). Runs
also run checks (same as in invoke check). Note that this one first builds
sdist from source tree, then wheel from sdist, and runs all the tests agaist
the wheel.
tox --skip-build
* Otherwise same as `tox`, but skips building the wheel. Runs the tests against
a wheel that sits in the dist/ folder. Mainly for CI.
tox -e build
* Builds the sdist and wheel into dist/
[1] https://docs.pyinvoke.org/
"""
from __future__ import annotations
import os
import typing
from pathlib import Path
from colorama import Fore
from invoke import task # type: ignore
from wakepy.core import CURRENT_PLATFORM
from wakepy.core.platform import is_unix_like_foss
if typing.TYPE_CHECKING:
from invoke.runners import Result
def get_run_with_print(c):
def run_with_print(cmd: str, ignore_errors: bool = False) -> Result:
print("Running:", Fore.YELLOW, cmd, Fore.RESET)
unix_like = is_unix_like_foss(CURRENT_PLATFORM)
res: Result = c.run(
cmd,
pty=unix_like,
warn=ignore_errors,
shell=get_shell() if unix_like else None,
)
return res
return run_with_print
@task
def format(c) -> None:
run = get_run_with_print(c)
run("python -m isort .")
run("python -m black .")
run("python -m ruff check --fix .")
@task
def check(c) -> None:
run = get_run_with_print(c)
run("python -m isort --check .")
run("python -m black --check .")
run("python -m ruff check --no-fix .")
run("python -m mypy .")
@task
def docs(c) -> None:
"""Starts sphinx build with live-reload on browser."""
run = get_run_with_print(c)
# The `-a` flag ensures that *all* files (not only edited files) will get
# rebuild. You may also build just once with
# sphinx-build -b html docs/source/ docs/build
run("sphinx-autobuild docs/source/ docs/build/ -a")
@task
def test(c, pdb: bool = False) -> None:
run = get_run_with_print(c)
pdb_flag = " --pdb " if pdb else ""
res = run(
f"env -u DBUS_SESSION_BUS_ADDRESS python -m pytest -W error {pdb_flag}"
"--cov-branch --cov ./src --cov-fail-under=100",
ignore_errors=True,
)
if res.exited:
run(f"coverage html && python -m webbrowser -t htmlcov{os.sep}index.html")
check(c)
def get_shell():
bash = "/bin/bash"
usr_bash = "/usr/local/bin/bash"
if Path(bash).exists():
return bash
elif Path(usr_bash).exists():
return usr_bash
raise RuntimeError("Could not find a shell to be be used with invoke!")