forked from mkdocstrings/mkdocstrings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduties.py
316 lines (256 loc) · 9.47 KB
/
duties.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
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
"""Development tasks."""
from __future__ import annotations
import os
import sys
from pathlib import Path
from typing import TYPE_CHECKING, Any
from duty import duty
from duty.callables import black, blacken_docs, coverage, lazy, mkdocs, mypy, pytest, ruff, safety
if sys.version_info < (3, 8):
from importlib_metadata import version as pkgversion
else:
from importlib.metadata import version as pkgversion
if TYPE_CHECKING:
from duty.context import Context
PY_SRC_PATHS = (Path(_) for _ in ("src", "tests", "duties.py", "scripts"))
PY_SRC_LIST = tuple(str(_) for _ in PY_SRC_PATHS)
PY_SRC = " ".join(PY_SRC_LIST)
CI = os.environ.get("CI", "0") in {"1", "true", "yes", ""}
WINDOWS = os.name == "nt"
PTY = not WINDOWS and not CI
MULTIRUN = os.environ.get("PDM_MULTIRUN", "0") == "1"
def pyprefix(title: str) -> str: # noqa: D103
if MULTIRUN:
prefix = f"(python{sys.version_info.major}.{sys.version_info.minor})"
return f"{prefix:14}{title}"
return title
def merge(d1: Any, d2: Any) -> Any: # noqa: D103
basic_types = (int, float, str, bool, complex)
if isinstance(d1, dict) and isinstance(d2, dict):
for key, value in d2.items():
if key in d1:
if isinstance(d1[key], basic_types):
d1[key] = value
else:
d1[key] = merge(d1[key], value)
else:
d1[key] = value
return d1
if isinstance(d1, list) and isinstance(d2, list):
return d1 + d2
return d2
def mkdocs_config() -> str: # noqa: D103
from mkdocs import utils
# patch YAML loader to merge arrays
utils.merge = merge
if "+insiders" in pkgversion("mkdocs-material"):
return "mkdocs.insiders.yml"
return "mkdocs.yml"
@duty
def changelog(ctx: Context) -> None:
"""Update the changelog in-place with latest commits.
Parameters:
ctx: The context instance (passed automatically).
"""
from git_changelog.cli import build_and_render
git_changelog = lazy(build_and_render, name="git_changelog")
ctx.run(
git_changelog(
repository=".",
output="CHANGELOG.md",
convention="angular",
template="keepachangelog",
parse_trailers=True,
parse_refs=False,
sections=["build", "deps", "feat", "fix", "refactor"],
bump_latest=True,
in_place=True,
),
title="Updating changelog",
)
@duty(pre=["check_quality", "check_types", "check_docs", "check_dependencies", "check-api"])
def check(ctx: Context) -> None: # noqa: ARG001
"""Check it all!
Parameters:
ctx: The context instance (passed automatically).
"""
@duty
def check_quality(ctx: Context) -> None:
"""Check the code quality.
Parameters:
ctx: The context instance (passed automatically).
"""
ctx.run(
ruff.check(*PY_SRC_LIST, config="config/ruff.toml"),
title=pyprefix("Checking code quality"),
command=f"ruff check --config config/ruff.toml {PY_SRC}",
)
@duty
def check_dependencies(ctx: Context) -> None:
"""Check for vulnerabilities in dependencies.
Parameters:
ctx: The context instance (passed automatically).
"""
# retrieve the list of dependencies
requirements = ctx.run(
["pdm", "export", "-f", "requirements", "--without-hashes"],
title="Exporting dependencies as requirements",
allow_overrides=False,
)
ctx.run(
safety.check(requirements),
title="Checking dependencies",
command="pdm export -f requirements --without-hashes | safety check --stdin",
)
@duty
def check_docs(ctx: Context) -> None:
"""Check if the documentation builds correctly.
Parameters:
ctx: The context instance (passed automatically).
"""
Path("htmlcov").mkdir(parents=True, exist_ok=True)
Path("htmlcov/index.html").touch(exist_ok=True)
config = mkdocs_config()
ctx.run(
mkdocs.build(strict=True, config_file=config, verbose=True),
title=pyprefix("Building documentation"),
command=f"mkdocs build -vsf {config}",
)
@duty
def check_types(ctx: Context) -> None:
"""Check that the code is correctly typed.
Parameters:
ctx: The context instance (passed automatically).
"""
os.environ["MYPYPATH"] = "src"
ctx.run(
mypy.run(*PY_SRC_LIST, config_file="config/mypy.ini"),
title=pyprefix("Type-checking"),
command=f"mypy --config-file config/mypy.ini {PY_SRC}",
)
@duty
def check_api(ctx: Context) -> None:
"""Check for API breaking changes.
Parameters:
ctx: The context instance (passed automatically).
"""
from griffe.cli import check as g_check
griffe_check = lazy(g_check, name="griffe.check")
ctx.run(
griffe_check("mkdocstrings", search_paths=["src"], color=True),
title="Checking for API breaking changes",
command="griffe check -ssrc mkdocstrings",
nofail=True,
)
@duty(silent=True)
def clean(ctx: Context) -> None:
"""Delete temporary files.
Parameters:
ctx: The context instance (passed automatically).
"""
ctx.run("rm -rf .coverage*")
ctx.run("rm -rf .mypy_cache")
ctx.run("rm -rf .pytest_cache")
ctx.run("rm -rf tests/.pytest_cache")
ctx.run("rm -rf build")
ctx.run("rm -rf dist")
ctx.run("rm -rf htmlcov")
ctx.run("rm -rf pip-wheel-metadata")
ctx.run("rm -rf site")
ctx.run("find . -type d -name __pycache__ | xargs rm -rf")
ctx.run("find . -name '*.rej' -delete")
@duty
def docs(ctx: Context, host: str = "127.0.0.1", port: int = 8000) -> None:
"""Serve the documentation (localhost:8000).
Parameters:
ctx: The context instance (passed automatically).
host: The host to serve the docs from.
port: The port to serve the docs on.
"""
ctx.run(
mkdocs.serve(dev_addr=f"{host}:{port}", config_file=mkdocs_config()),
title="Serving documentation",
capture=False,
)
@duty
def docs_deploy(ctx: Context) -> None:
"""Deploy the documentation on GitHub pages.
Parameters:
ctx: The context instance (passed automatically).
"""
os.environ["DEPLOY"] = "true"
config_file = mkdocs_config()
if config_file == "mkdocs.yml":
ctx.run(lambda: False, title="Not deploying docs without Material for MkDocs Insiders!")
origin = ctx.run("git config --get remote.origin.url", silent=True)
if "pawamoy-insiders/mkdocstrings" in origin:
ctx.run("git remote add org-pages git@github.com:mkdocstrings/mkdocstrings.github.io", silent=True, nofail=True)
ctx.run(
mkdocs.gh_deploy(config_file=config_file, remote_name="org-pages", force=True),
title="Deploying documentation",
)
else:
ctx.run(
lambda: False,
title="Not deploying docs from public repository (do that from insiders instead!)",
nofail=True,
)
@duty
def format(ctx: Context) -> None:
"""Run formatting tools on the code.
Parameters:
ctx: The context instance (passed automatically).
"""
ctx.run(
ruff.check(*PY_SRC_LIST, config="config/ruff.toml", fix_only=True, exit_zero=True),
title="Auto-fixing code",
)
ctx.run(black.run(*PY_SRC_LIST, config="config/black.toml"), title="Formatting code")
ctx.run(
blacken_docs.run(*PY_SRC_LIST, "docs", exts=["py", "md"], line_length=120),
title="Formatting docs",
nofail=True,
)
@duty(post=["docs-deploy"])
def release(ctx: Context, version: str) -> None:
"""Release a new Python package.
Parameters:
ctx: The context instance (passed automatically).
version: The new version number to use.
"""
origin = ctx.run("git config --get remote.origin.url", silent=True)
if "pawamoy-insiders/mkdocstrings" in origin:
ctx.run(
lambda: False,
title="Not releasing from insiders repository (do that from public repo instead!)",
)
ctx.run("git add pyproject.toml CHANGELOG.md", title="Staging files", pty=PTY)
ctx.run(["git", "commit", "-m", f"chore: Prepare release {version}"], title="Committing changes", pty=PTY)
ctx.run(f"git tag {version}", title="Tagging commit", pty=PTY)
ctx.run("git push", title="Pushing commits", pty=False)
ctx.run("git push --tags", title="Pushing tags", pty=False)
ctx.run("pdm build", title="Building dist/wheel", pty=PTY)
ctx.run("twine upload --skip-existing dist/*", title="Publishing version", pty=PTY)
@duty(silent=True, aliases=["coverage"])
def cov(ctx: Context) -> None:
"""Report coverage as text and HTML.
Parameters:
ctx: The context instance (passed automatically).
"""
ctx.run(coverage.combine, nofail=True)
ctx.run(coverage.report(rcfile="config/coverage.ini"), capture=False)
ctx.run(coverage.html(rcfile="config/coverage.ini"))
@duty
def test(ctx: Context, match: str = "") -> None:
"""Run the test suite.
Parameters:
ctx: The context instance (passed automatically).
match: A pytest expression to filter selected tests.
"""
py_version = f"{sys.version_info.major}{sys.version_info.minor}"
os.environ["COVERAGE_FILE"] = f".coverage.{py_version}"
ctx.run(
pytest.run("-n", "auto", "tests", config_file="config/pytest.ini", select=match, color="yes"),
title=pyprefix("Running tests"),
command=f"pytest -c config/pytest.ini -n auto -k{match!r} --color=yes tests",
)