-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
253 lines (168 loc) · 6 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
"""
Module to manage shell-oriented subprocesses and organizing executable
Python code into CLI-invokable tasks.
"""
import asyncio
from invoke import Context, task
from app.services.mongo.service import MongoDBService
from app.tests.fixtures.manager import FileFixtureManager
@task
def install(ctx: Context, group: str | None = None) -> None:
"""Installs project's dependencies.
Args:
ctx (invoke.Context): The context object representing the current invocation.
group (str): Name of poetry group. Defaults to None.
Example:
invoke install # Installs dependencies for default group.
invoke install --group dev # Installs dependencies for dev group.
"""
command = "poetry install"
if group is not None:
command += f" --with {group}"
ctx.run(command)
@task
def outdated_packages(ctx: Context) -> None:
"""Shows a list of outdated packages.
Args:
ctx (invoke.Context): The context object representing the current invocation.
Example:
invoke outdated-packages # Shows outdated packages.
"""
ctx.run("poetry show --outdated")
@task
def create_migration(ctx: Context, description: str) -> None:
"""Creates a MongoDB migration script.
Args:
ctx (invoke.Context): The context object representing the current invocation.
description (str): Migration script description.
Example:
# Creates an empty migration script.
invoke create-migration --description add_index
"""
ctx.run(f"mongodb-migrate-create --description {description}")
@task
def upgrade_migrations(_: Context, to_datetime: str | None = None) -> None:
"""Upgrades MongoDB migrations.
Args:
_ (invoke.Context): The context object representing the current invocation.
to_datetime (str | None): Defines a prefix of migration upgrade/downgrade
operations will reach.
Example:
# Upgrades MongoDB migrations.
invoke upgrade-migrations
# Upgrades MongoDB migrations till '20231231132337'.
invoke upgrade-migrations --to-datetime 20231231132337
"""
MongoDBService.run_migrations(upgrade=True, to_datetime=to_datetime)
@task
def downgrade_migrations(_: Context, to_datetime: str | None = None) -> None:
"""Downgrades MongoDB migration.
Args:
_ (invoke.Context): The context object representing the current invocation.
to_datetime (str | None): Defines a prefix of migration upgrade/downgrade
operations will reach.
Example:
# Downgrades MongoDB migrations.
invoke downgrade_migrations
# Downgrades MongoDB migrations till '20231231132337'.
invoke downgrade-migrations --to-datetime 20231231132337
"""
MongoDBService.run_migrations(upgrade=False, to_datetime=to_datetime)
@task(default=True)
def run(ctx: Context) -> None:
"""Runs a FastAPI application.
Args:
ctx (invoke.Context): The context object representing the current invocation.
Example:
invoke run # Runs the application.
"""
ctx.run("python -m app.app")
@task(pre=[upgrade_migrations])
def test(ctx: Context) -> None:
"""Runs unit tests.
Args:
ctx (invoke.Context): The context object representing the current invocation.
Example:
invoke test # Runs unit tests.
"""
ctx.run(
"pytest -v --cov-report term-missing --cov-report xml "
"--cov-report html --cov-branch --cov=app app/"
)
@task
def lint(ctx: Context, fix: bool = False) -> None:
"""Runs Ruff linter.
Args:
fix (bool): If True, enables automatically fix behaviour.
ctx (invoke.Context): The context object representing the current invocation.
Example:
invoke lint # Runs linter.
invoke lint --fix # Runs linter with automatically error fix.
"""
command = "ruff check ."
if fix:
command += " --fix"
ctx.run(command)
@task
def format(ctx: Context, fix: bool = False) -> None:
"""Runs formatter.
Args:
fix (bool): If True, enables automatically fix behaviour.
ctx (invoke.Context): The context object representing the current invocation.
Example:
invoke format # Runs formatter and only shows the list of errors.
invoke format --fix # Runs formatter and automatically fixes errors.
"""
command = "ruff format ."
if fix is False:
command += " --diff --check"
ctx.run(command)
@task
def mypy(ctx: Context) -> None:
"""Runs mypy checker.
Args:
ctx (invoke.Context): The context object representing the current invocation.
Example:
invoke mypy # Runs static type checker.
"""
ctx.run("mypy .")
@task
def check(ctx: Context) -> None:
"""Runs unit tests, linter, mypy and formatter together.
Args:
ctx (invoke.Context): The context object representing the current invocation.
Example:
invoke check # Runs linter, formatter, static type checker and unit tests.
"""
lint(ctx)
format(ctx)
mypy(ctx)
test(ctx)
@task
def fixture(_: Context) -> None:
"""Loads test data from fixture files into MongoDB collections.
Args:
_ (invoke.Context): The context object representing the current invocation.
Example:
invoke fixture # Loads data from fixtures into Mongo collections.
"""
file_fixture_manager = FileFixtureManager()
asyncio.run(file_fixture_manager.load())
@task
def build(ctx: Context) -> None:
"""Builds a new docker image for application.
Args:
ctx: The context object representing the current invocation.
Example:
invoke build # Builds new application image
"""
ctx.run("docker build -t shibumi-store:latest .")
@task
def compose(ctx: Context) -> None:
"""Runs a docker-compose.
Args:
ctx: The context object representing the current invocation.
Example:
invoke compose # Runs a docker-compose
"""
ctx.run("docker-compose up -d")