Repository navigation
Expand file tree
/
Copy pathtasks.py
More file actions
386 lines (303 loc) Β· 12.3 KB
/
Copy pathtasks.py
File metadata and controls
386 lines (303 loc) Β· 12.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
r"""Define some tasks that are executed with invoke."""
from __future__ import annotations
import logging
from typing import TYPE_CHECKING
from invoke.tasks import task
if TYPE_CHECKING:
from invoke.context import Context
logging.basicConfig(level=logging.INFO, format="%(message)s")
logger = logging.getLogger(__name__)
NAME = "hya"
SOURCE = f"src/{NAME}"
TESTS = "tests"
UNIT_TESTS = f"{TESTS}/unit"
INTEGRATION_TESTS = f"{TESTS}/integration"
FUNCTIONAL_TESTS = f"{TESTS}/functional"
PYTHON_VERSION = "3.13"
@task
def check_format(c: Context) -> None:
r"""Check code format with black.
This task verifies that all Python code follows Black's formatting rules
without modifying any files. Use the 'format' task to automatically fix
formatting issues.
Args:
c: The invoke context.
"""
logger.info("π¨ Checking code format with black...")
c.run("black --check .", pty=True)
logger.info("β
Code format check passed")
@task
def check_lint(c: Context) -> None:
r"""Check code linting with ruff.
This task runs Ruff linter to identify code quality issues, potential bugs,
and style violations. Output is formatted for GitHub Actions compatibility.
Args:
c: The invoke context.
"""
logger.info("π Checking code linting with ruff...")
c.run("ruff check --output-format=github .", pty=True)
logger.info("β
Linting check passed")
@task
def check_types(c: Context) -> None:
r"""Check type hints with pyright.
This task uses Pyright to perform static type checking on the source code,
ensuring type safety and catching potential type-related errors.
Args:
c: The invoke context.
"""
logger.info("π¬ Checking type hints with pyright...")
c.run(f"pyright --verifytypes {NAME} --ignoreexternal", pty=True)
logger.info("β
Type check passed")
@task
def create_venv(c: Context) -> None:
r"""Create a virtual environment and install invoke.
This task creates a fresh virtual environment using uv with the specified
Python version, activates it, and installs the invoke package for task
management.
Args:
c: The invoke context.
Note:
The virtual environment will be created in the .venv directory and any
existing environment will be cleared.
"""
logger.info(f"π Creating virtual environment with Python {PYTHON_VERSION}...")
c.run(f"uv venv --python {PYTHON_VERSION} --clear", pty=True)
c.run("source .venv/bin/activate", pty=True)
logger.info("π¦ Installing invoke...")
c.run("make install-invoke", pty=True)
logger.info("β
Virtual environment created successfully")
@task
def doctest_src(c: Context) -> None:
r"""Run doctests on source code and validate markdown code examples.
This function performs two types of validation:
1. Runs doctests on Python source code files using xdoctest
2. Validates code examples embedded in markdown files (via check_markdown.sh
which internally uses doctest)
Args:
c: The invoke context.
"""
logger.info("π Running doctests on source code...")
c.run(f"python -m pytest --xdoctest {SOURCE}", pty=True)
logger.info("π Validating markdown code examples...")
c.run("dev/check_markdown.sh", pty=True)
logger.info("β
Doctest validation complete")
@task
def docformat(c: Context) -> None:
r"""Format docstrings in source code.
This task uses docformatter to automatically format all docstrings in the
source code according to PEP 257 conventions and project-specific settings
defined in pyproject.toml.
Args:
c: The invoke context.
Note:
This modifies files in place. Ensure your work is committed before
running this task.
"""
logger.info("π Formatting docstrings...")
c.run(f"docformatter --config ./pyproject.toml --in-place {SOURCE}", pty=True)
logger.info("β
Docstring formatting complete")
@task
def format_shell(c: Context) -> None:
r"""Format and validate shell scripts.
This task performs two operations on all shell scripts:
1. Runs shellcheck to identify potential bugs and style issues
2. Formats scripts using shfmt for consistent style
Args:
c: The invoke context.
Raises:
SystemExit: If shellcheck or shfmt fails.
"""
logger.info("π Running shellcheck on shell scripts...")
c.run("shellcheck -- **/*.sh", pty=True)
logger.info("β
Shellcheck passed\n")
logger.info("π§ Running shfmt to format shell scripts...")
c.run("shfmt -l -w -- **/*.sh", pty=True)
logger.info("β
Shell formatting complete")
@task
def install(
c: Context, optional_deps: bool = True, dev_deps: bool = True, docs_deps: bool = False
) -> None:
r"""Install project dependencies and the package in editable mode.
This task synchronizes dependencies from the lock file and installs the
package in editable mode, allowing you to test changes without reinstalling.
Args:
c: The invoke context.
optional_deps: If True, install all optional dependencies defined in
the project extras. Default is True.
dev_deps: If True, install development dependencies (testing, linting,
formatting tools). Default is True.
docs_deps: If True, install documentation generation dependencies
(mkdocs, themes, plugins). Default is False.
Example:
# Install with all dependencies except docs
invoke install
# Install only core and dev dependencies
invoke install --no-optional-deps
# Install everything including docs dependencies
invoke install --docs-deps
"""
logger.info("π¦ Installing project dependencies...")
cmd = ["uv sync --frozen"]
if optional_deps:
cmd.append("--all-extras")
if dev_deps:
cmd.append("--group dev")
if docs_deps:
cmd.append("--group docs")
c.run(" ".join(cmd), pty=True)
logger.info("π§ Installing package in editable mode...")
c.run("uv pip install -e .", pty=True)
logger.info("β
Installation complete")
@task
def update(c: Context) -> None:
r"""Update dependencies and pre-commit hooks to their latest
versions.
This task performs a comprehensive update of the project's tooling:
1. Updates all locked dependencies to their latest compatible versions
2. Upgrades all uv tools to their latest versions
3. Updates pre-commit hook versions
4. Reinstalls the project with updated documentation dependencies
Args:
c: The invoke context.
Warning:
This may introduce breaking changes. Review the changes and run tests
after updating.
"""
logger.info("π Updating dependencies...")
c.run("uv sync --upgrade", pty=True)
logger.info("π οΈ Upgrading uv tools...")
c.run("uv tool upgrade --all", pty=True)
logger.info("πͺ Updating pre-commit hooks...")
c.run("pre-commit autoupdate", pty=True)
logger.info("π¦ Reinstalling with docs dependencies...")
install(c, docs_deps=True)
logger.info("β
Update complete")
@task
def all_test(c: Context, cov: bool = False) -> None:
r"""Run all tests (unit and integration).
This task executes the complete test suite including unit tests, integration
tests, and doctests with a 10-second timeout per test.
Args:
c: The invoke context.
cov: If True, generate coverage reports in HTML, XML, and terminal
formats. Default is False.
Example:
# Run all tests without coverage
invoke all-test
# Run all tests with coverage reports
invoke all-test --cov
"""
logger.info("π§ͺ Running all tests (unit and integration)...")
cmd = ["python -m pytest --xdoctest --timeout 10"]
if cov:
cmd.append(f"--cov-report html --cov-report xml --cov-report term --cov={NAME}")
logger.info("π Coverage reports will be generated")
cmd.append(f"{TESTS}")
c.run(" ".join(cmd), pty=True)
logger.info("β
All tests complete")
@task
def unit_test(c: Context, cov: bool = False) -> None:
r"""Run unit tests.
This task executes only the unit tests (fast, isolated tests) with doctests
and a 10-second timeout per test. Unit tests are located in the tests/unit
directory.
Args:
c: The invoke context.
cov: If True, generate coverage reports in HTML, XML, and terminal
formats. Default is False.
Example:
# Run unit tests without coverage
invoke unit-test
# Run unit tests with coverage reports
invoke unit-test --cov
"""
logger.info("π§ͺ Running unit tests...")
cmd = ["python -m pytest --xdoctest --timeout 10"]
if cov:
cmd.append(f"--cov-report html --cov-report xml --cov-report term --cov={NAME}")
logger.info("π Coverage reports will be generated")
cmd.append(f"{UNIT_TESTS}")
c.run(" ".join(cmd), pty=True)
logger.info("β
Unit tests complete")
@task
def integration_test(c: Context, cov: bool = False) -> None:
r"""Run integration tests.
Args:
c: The invoke context.
cov: If True, generate coverage reports.
"""
logger.info("π§ͺ Running integration tests...")
cmd = ["python -m pytest --xdoctest --timeout 60"]
if cov:
cmd.append(
f"--cov-report html --cov-report xml --cov-report term --cov-append --cov={NAME}"
)
logger.info("π Coverage reports will be generated (appending)")
cmd.append(f"{INTEGRATION_TESTS}")
c.run(" ".join(cmd), pty=True)
logger.info("β
Integration tests complete")
@task
def show_installed_packages(c: Context) -> None:
r"""Show the installed packages.
Args:
c: The invoke context.
"""
logger.info("π¦ Listing installed packages...")
c.run("uv pip list", pty=True)
@task
def show_python_config(c: Context) -> None:
r"""Show the python configuration.
Args:
c: The invoke context.
"""
logger.info("π Python configuration:")
c.run("uv python list --only-installed", pty=True)
c.run("uv python find", pty=True)
c.run("which python", pty=True)
@task
def publish_pypi(c: Context) -> None:
r"""Publish the package to PyPI.
Args:
c: The invoke context.
"""
logger.info("π¦ Building package...")
c.run("uv build", pty=True)
logger.info("π Verifying package installation...")
c.run(
f'uv run --with {NAME} --refresh-package {NAME} --no-project -- python -c "import {NAME}"',
pty=True,
)
logger.info("π Publishing to PyPI...")
c.run("uv publish --token ${PYPI_TOKEN}", pty=True)
logger.info("β
Package published successfully")
@task
def publish_doc_dev(c: Context) -> None:
r"""Publish development (e.g. unstable) docs."""
logger.info("π Publishing development documentation...")
logger.info("ποΈ Deleting previous 'main' version if it exists...")
c.run("mike delete --config-file docs/mkdocs.yml main", pty=True, warn=True)
logger.info("π Deploying 'main' and 'dev' aliases...")
c.run("mike deploy --config-file docs/mkdocs.yml --push --update-aliases main dev", pty=True)
logger.info("β
Development documentation published")
@task
def publish_doc_latest(c: Context) -> None:
r"""Publish latest (e.g. stable) docs."""
from feu.git import get_last_version_tag_name # noqa: PLC0415
from packaging.version import Version # noqa: PLC0415
logger.info("π Publishing latest documentation...")
try:
version = Version(get_last_version_tag_name())
tag = f"{version.major}.{version.minor}"
logger.info(f"π Using version tag: {tag}")
except RuntimeError:
tag = "0.0"
logger.warning("β οΈ No version tag found, using default: 0.0")
logger.info(f"ποΈ Deleting previous '{tag}' version if it exists...")
c.run(f"mike delete --config-file docs/mkdocs.yml {tag}", pty=True, warn=True)
logger.info(f"π Deploying '{tag}' and 'latest' aliases...")
c.run(
f"mike deploy --config-file docs/mkdocs.yml --push --update-aliases {tag} latest", pty=True
)
logger.info("π― Setting 'latest' as default...")
c.run("mike set-default --config-file docs/mkdocs.yml --push --allow-empty latest", pty=True)
logger.info("β
Latest documentation published")