-
Notifications
You must be signed in to change notification settings - Fork 40
/
tasks.py
220 lines (183 loc) · 4.61 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
from enum import Enum, unique
from typing import Optional
from invoke import task
from invoke.context import Context
PACKAGE_NAME = "dalm"
VERSION_FILE = f"{PACKAGE_NAME}/__init__.py"
# TODO: do only dalm
SOURCES = " ".join(["dalm", "tasks.py", "tests"])
# TODO: Get this to 95
PYTEST_FAIL_UNDER = 0
@task
def clean(ctx: Context) -> None:
"""clean
Remove all build, test, coverage and Python artifacts.
"""
ctx.run(
" ".join(
[
"rm -rf",
".coverage",
".ipynb_checkpoints",
".mypy_cache",
".pytest_cache",
".ruff_cache",
"*.egg-info",
"*.egg",
"build",
"coverage.xml",
"dist",
"htmlcov",
"site",
]
),
pty=True,
echo=True,
)
@task
def install(ctx: Context, extras: Optional[str] = None, editable: bool = True) -> None:
"""install
Install dependencies.
Args:
ctx (Context): The invoke context.
extras (str, optional): The extras to install. Defaults to None. If None,
then the default extras (dev) are installed. Specify as a comma-separated
string.
editable (bool, optional): Whether to install in editable mode. Defaults to
True.
"""
if extras is None:
extras = "dev"
ctx.run(
"pip install --upgrade pip",
pty=True,
echo=True,
)
if editable:
cmd = f"pip install --upgrade -e '.[{extras}]'"
else:
cmd = f"pip install --upgrade '.[{extras}]'"
ctx.run(
cmd,
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.
Override the default SECRETS_ID in .env to skip loading secrets from Secrets Manager
"""
ctx.run(
f"pytest --cov-fail-under={PYTEST_FAIL_UNDER} tests",
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:
"""Deploy to pypi"""
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)
@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 dalm 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}")