-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
150 lines (120 loc) · 4.37 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
"""Tasks for maintaining the project.
Execute 'invoke --list' for guidance on using Invoke
"""
import platform
import webbrowser
from pathlib import Path
from invoke import task
from invoke.context import Context
from invoke.runners import Result
ROOT_DIR = Path(__file__).parent
DOCS_DIR = ROOT_DIR.joinpath("docs")
DOCS_BUILD_DIR = DOCS_DIR.joinpath("_build")
DOCS_INDEX = DOCS_BUILD_DIR.joinpath("index.html")
COVERAGE_FILE = ROOT_DIR.joinpath(".coverage")
COVERAGE_DIR = ROOT_DIR.joinpath("htmlcov")
COVERAGE_REPORT = COVERAGE_DIR.joinpath("index.html")
SOURCE_DIR = ROOT_DIR.joinpath("dike")
TEST_DIR = ROOT_DIR.joinpath("tests")
PYTHON_TARGETS = [
SOURCE_DIR,
TEST_DIR,
Path(__file__),
]
PYTHON_TARGETS_STR = " ".join([str(p) for p in PYTHON_TARGETS])
def _run(c: Context, command: str) -> Result:
return c.run(command, pty=platform.system() != "Windows")
@task()
def clean_build(c):
# type: (Context) -> None
"""Clean up files from package building."""
_run(c, "rm -fr build/")
_run(c, "rm -fr dist/")
_run(c, "rm -fr .eggs/")
_run(c, "find . -name '*.egg-info' -exec rm -fr {} +")
_run(c, "find . -name '*.egg' -exec rm -f {} +")
@task()
def clean_python(c):
# type: (Context) -> None
"""Clean up python file artifacts."""
_run(c, "find . -name '*.pyc' -exec rm -f {} +")
_run(c, "find . -name '*.pyo' -exec rm -f {} +")
_run(c, "find . -name '*~' -exec rm -f {} +")
_run(c, "find . -name '__pycache__' -exec rm -fr {} +")
@task()
def clean_tests(c):
# type: (Context) -> None
"""Clean up files from testing."""
_run(c, f"rm -f {COVERAGE_FILE}")
_run(c, f"rm -fr {COVERAGE_DIR}")
_run(c, "rm -fr .pytest_cache")
@task()
def clean_docs(c):
# type: (Context) -> None
"""Clean up files from documentation builds."""
_run(c, f"rm -fr {DOCS_BUILD_DIR}")
_run(c, f"rm -f {DOCS_DIR}/modules.rst {DOCS_DIR}/{{cookiecutter.project_slug}}.rst")
@task(pre=[clean_build, clean_python, clean_tests, clean_docs])
def clean(c):
# type: (Context) -> None
"""Run all clean sub-tasks."""
@task()
def hooks(c):
# type: (Context) -> None
"""Run pre-commit hooks."""
_run(c, "pre-commit run --all-files")
@task(name="format", help={"check": "Checks if source is formatted without applying changes"})
def format_(c, check=False):
# type: (Context, bool) -> None
"""Format code."""
isort_options = ["--check-only", "--diff"] if check else []
_run(c, f"uv run isort {' '.join(isort_options)} {PYTHON_TARGETS_STR}")
black_options = ["--diff", "--check"] if check else ["--quiet"]
_run(c, f"uv run black {' '.join(black_options)} {PYTHON_TARGETS_STR}")
@task()
def tests(c):
# type: (Context) -> None
"""Run tests."""
pytest_options = ["--xdoctest", "--cov", "--cov-report=", "--cov-fail-under=0"]
_run(c, f"uv run pytest {' '.join(pytest_options)} {TEST_DIR} {SOURCE_DIR}")
@task(
help={
"fmt": "Build a local report: report, html, json, annotate, html, xml.",
"open_browser": "Open the coverage report in the web browser (requires --fmt html)",
}
)
def coverage(c, fmt="report", open_browser=False):
# type: (Context, str, bool) -> None
"""Create coverage report."""
if any(Path().glob(".coverage.*")):
_run(c, "uv run coverage combine")
_run(c, f"uv run coverage {fmt} -i")
if fmt == "html" and open_browser:
webbrowser.open(COVERAGE_REPORT.as_uri())
@task()
def docs(c):
# type: (Context) -> None
"""Build documentation."""
_run(c, "uv run mkdocs build")
@task(pre=[clean, hooks, docs, tests, coverage])
def check(c):
# type: (Context) -> None
"""Run all checks together."""
@task(
help={
"part": "Part of the version to be bumped.",
"dry_run": "Don't write any files, just pretend. (default: False)",
"allow_dirty": "Normally, bumpversion will abort if the working directory is "
"dirty to protect yourself from releasing unversioned files and/or "
"overwriting unsaved changes. Use this option to override this check.",
}
)
def version(c, part, dry_run=False, allow_dirty=False):
# type: (Context, str, bool, bool) -> None
"""Bump version."""
bump_options = []
if dry_run:
bump_options.append("--dry-run")
if allow_dirty:
bump_options.append("--allow-dirty")
_run(c, f"uv run bump2version {' '.join(bump_options)} {part}")