-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduties.py
151 lines (114 loc) · 3.87 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
"""Project Duties."""
from __future__ import annotations
import os
from typing import TYPE_CHECKING, Tuple
from duty import duty
from duty.callables import mypy
from git_changelog.cli import build_and_render
if TYPE_CHECKING:
from duty.context import Context
from git_changelog import Changelog
CI = os.environ.get("CI", "0") in {"1", "true", "yes", ""}
WINDOWS = os.name == "nt"
PTY = not WINDOWS and not CI
MODULE_NAME = "recipe_scrapers_sage"
def _changelog() -> Tuple[Changelog, str]:
"""Update changelog in-place.
Returns:
Tuple[Changelog, str]: changelog object and contents
"""
return build_and_render(
repository=".",
output="CHANGELOG.md",
convention="conventional",
template="keepachangelog",
parse_trailers=True,
parse_refs=False,
bump="auto",
in_place=True,
)
@duty(aliases=["format"])
def fmt(ctx: Context):
"""Format source code.
Args:
ctx (Context): the context instance (passed automatically).
"""
ctx.run("isort --ca --profile=black .", title="Sorting imports (isort)")
ctx.run("black .", title="Formatting code (black)")
@duty(aliases=["check_deps"])
def check_dependencies(ctx: Context):
"""Check for vulnerabilities in dependencies.
Args:
ctx (Context): the context instance (passed automatically).
"""
ctx.run(
"poetry export --only main | safety check --stdin",
title="Dependency checking (safety)",
)
@duty
def check_types(ctx: Context):
"""Check that the code is correctly typed.
Args:
ctx (Context): the context instance (passed automatically).
"""
ctx.run(mypy.run(MODULE_NAME), title="Type checking (mypy)", pty=PTY)
@duty
def ruff(ctx: Context):
"""Run ruff code linting.
Args:
ctx (Context): the context instance (passed automatically).
"""
ctx.run("ruff check .", title="Code linting (ruff)")
@duty
def check_api(ctx: Context) -> None:
"""Check for API breaking changes.
Args:
ctx (Context): the context instance (passed automatically).
"""
from griffe.cli import check as g_check
ctx.run(
lambda: g_check(MODULE_NAME),
title="Checking for API breaking changes (griffe)",
nofail=True,
)
@duty(pre=["check_types", "ruff"])
def check(ctx: Context):
"""Check it all!
Args:
ctx (Context): the context instance (passed automatically).
"""
@duty
def test(ctx: Context):
"""Run the test suite.
Args:
ctx (Context): the context instance (passed automatically).
"""
ctx.run(f"pytest --cov={MODULE_NAME} --cov-report=xml", title="Testing (pytest)")
@duty
def changelog(ctx: Context):
"""Update the changelog in-place with latest commits.
Args:
ctx (Context): the context instance (passed automatically).
"""
ctx.run(_changelog, title="Generating changelog (git-changelog)")
@duty()
def release(ctx: Context, version: str | None = None):
"""Release a new Python package.
Args:
ctx (Context): The context instance (passed automatically).
version (str, optional): The new version number to use. Defaults to None.
"""
if version is None:
res: Tuple[Changelog, str] = _changelog()
version: str = res[0].versions_list[0].planned_tag
ctx.run(f"poetry version {version}", title="Bumping version (poetry)")
ctx.run("git add pyproject.toml CHANGELOG.md", title="Staging files (git)")
ctx.run(
["git", "commit", "-m", f"chore: Prepare release {version}"],
title="Committing changes (git)",
pty=PTY,
)
ctx.run("poetry publish --build", title="Publish package (poetry)")
ctx.run(f"git tag {version}", title="Tagging commit (git)", pty=PTY)
ctx.run("git push", title="Pushing commits (git)", pty=False)
ctx.run("git push --tags", title="Pushing tags (git)", pty=False)