-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtasks.py
292 lines (246 loc) · 5.51 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
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
from enum import Enum, unique
from typing import Optional
from invoke import task
from invoke.context import Context
PACKAGE_NAME = "hotbox"
VERSION_FILE = f"{PACKAGE_NAME}/__init__.py"
SOURCES = " ".join(["hotbox", "tests", "tasks.py"])
@task
def clean(ctx: Context) -> None:
"""clean
Remove all build, test, coverage and Python artifacts.
"""
ctx.run(
" ".join(
[
"rm -rf",
".coverage",
".mypy_cache",
".pytest_cache",
".ruff_cache",
".tox",
"*.egg",
"*.egg-info",
"build",
"coverage.xml",
"dist",
"htmlcov",
"site",
]
),
pty=True,
echo=True,
)
@task
def install(ctx: Context) -> None:
"""install
Install dependencies.
"""
ctx.run(
"pip install --upgrade pip",
pty=True,
echo=True,
)
ctx.run(
"pip install -e '.[dev]'",
pty=True,
echo=True,
)
ctx.run(
"pre-commit install",
pty=True,
echo=True,
)
ctx.run(
"pre-commit autoupdate",
pty=True,
echo=True,
)
ctx.run(
"if command -v pyenv 1>/dev/null 2>&1; then pyenv rehash; fi",
pty=True,
echo=True,
)
@task
def lint(ctx: Context) -> None:
"""lint
Check typing and formatting.
"""
ctx.run(
f"mypy {SOURCES}",
pty=True,
echo=True,
)
ctx.run(
f"black {SOURCES} --check",
pty=True,
echo=True,
)
ctx.run(
f"ruff {SOURCES}",
pty=True,
echo=True,
)
@task
def format(ctx: Context) -> None:
"""format
Format the code.
"""
ctx.run(
f"black {SOURCES}",
pty=True,
echo=True,
)
ctx.run(
f"ruff {SOURCES} --fix",
pty=True,
echo=True,
)
@task
def test(ctx: Context) -> None:
"""test
Run the tests.
"""
ctx.run(
" ".join(
[
"pytest",
"-o",
"console_output_style=progress",
"--asyncio-mode=auto",
"--disable-warnings",
"--cov=hotbox",
"--cov=tests",
"--cov-report=term-missing",
"--cov-report=xml",
"--cov-report=html",
"--cov-fail-under=100",
]
),
pty=True,
echo=True,
)
@task
def build(ctx: Context) -> None:
"""build
Build the package.
"""
ctx.run(
"pip install --upgrade build",
pty=True,
echo=True,
)
ctx.run(
"python -m build",
pty=True,
echo=True,
)
@task
def publish(ctx: Context) -> None:
"""publish
Publish the package.
"""
ctx.run(
"pip install --upgrade twine",
pty=True,
echo=True,
)
ctx.run(
"twine upload dist/*",
pty=True,
echo=True,
)
@task
def all(ctx: Context) -> None:
"""all
Run all the tasks that matter for local dev.
"""
clean(ctx)
install(ctx)
format(ctx)
lint(ctx)
test(ctx)
@task
def docs_build(ctx: Context) -> None:
"""docs_build
Build the docs.
"""
ctx.run(
"mkdocs build",
pty=True,
echo=True,
)
ctx.run(
"cp ./docs/index.md ./README.md",
pty=True,
echo=True,
)
ctx.run(
"git add ./docs README.md",
pty=True,
echo=True,
)
ctx.run(
"git commit -S -m '📚 Updated docs with docs-build.'",
pty=True,
echo=True,
)
@task
def docs_serve(ctx: Context) -> None:
"""docs_serve
Serve the docs.
"""
ctx.run(
"mkdocs serve --dev-addr 127.0.0.1:8008",
pty=True,
echo=True,
)
@task
def run_uvicorn(ctx: Context) -> None:
"""run_uvicorn
Run the uvicorn server.
"""
ctx.run(
"uvicorn hotbox.api:api --reload --port 8000",
pty=True,
echo=True,
)
@unique
class BumpType(Enum):
MAJOR = "major"
MINOR = "minor"
def _bump_version(version: str, bump: Optional[BumpType] = None) -> str:
"""Bump a version string.
Args:
version (str): The version string to bump.
bump (str): The type of bump to perform.
Returns:
str: The bumped version string.
"""
from packaging.version import Version
v = Version(version)
if bump == BumpType.MAJOR:
v = Version(f"{v.major + 1}.0.0")
elif bump == BumpType.MINOR:
v = Version(f"{v.major}.{v.minor + 1}.0")
else:
v = Version(f"{v.major}.{v.minor}.{v.micro + 1}")
return str(v)
@task(aliases=["uv"])
def update_version_number(ctx: Context, part: Optional[BumpType] = None) -> None:
"""update version number
Specify the part of the version number to bump. The default is to bump the
micro version number. Other options are major and minor.
"""
from hotbox import __version__
print(f"Current version: {__version__}")
new_version = _bump_version(__version__, part)
with open(VERSION_FILE, "r") as f:
lines = f.readlines()
with open(VERSION_FILE, "w") as f:
for line in lines:
if line.startswith("__version__"):
f.write(f'__version__ = "{new_version}"\n')
else:
f.write(line)
print(f"New version: {new_version}")