-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtasks.py
230 lines (204 loc) · 4.7 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
from enum import Enum, unique
from invoke import task
from invoke.context import Context
PACKAGE_NAME = "spacy_to_hf"
VERSION_FILE = f"{PACKAGE_NAME}/__init__.py"
SOURCES = " ".join(["spacy_to_hf", "tests", "tasks.py"])
@task
def clean(ctx: Context) -> None:
"""clean
Remove all build, test, coverage and Python artifacts.
"""
ctx.run(
" ".join(
[
"rm -rf",
"build",
"dist",
"*.egg-info",
".pytest_cache",
".mypy_cache",
".coverage",
"htmlcov",
".ruff_cache",
"coverage.xml",
]
),
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(
"python -m spacy download en_core_web_sm",
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"ruff format {SOURCES} --check",
pty=True,
echo=True,
)
ctx.run(
f"ruff check {SOURCES}",
pty=True,
echo=True,
)
ctx.run(
f"mypy {SOURCES}",
pty=True,
echo=True,
)
@task
def format(ctx: Context) -> None:
"""format
Format the code.
"""
ctx.run(
f"ruff format {SOURCES}",
pty=True,
echo=True,
)
ctx.run(
f"ruff check {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",
"--disable-warnings",
"--cov=spacy_to_hf",
"--cov=tests",
"--cov-report=term-missing",
"--cov-report=xml",
"--cov-report=html",
"--cov-fail-under=95",
]
),
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)
@unique
class BumpType(Enum):
MAJOR = "major"
MINOR = "minor"
MICRO = "micro"
def _bump_version(version: str, bump: BumpType) -> 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")
elif bump == BumpType.MICRO:
v = Version(f"{v.major}.{v.minor}.{v.micro + 1}")
else:
raise ValueError(f"Invalid bump type: {bump}")
return str(v)
@task
def update_version_number(ctx: Context, part: BumpType = BumpType.MICRO) -> 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 spacy_to_hf 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}")