-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathtasks.py
More file actions
293 lines (235 loc) · 8.36 KB
/
tasks.py
File metadata and controls
293 lines (235 loc) · 8.36 KB
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
293
from contextlib import contextmanager
from pathlib import Path
from tomllib import loads
from invoke import task
from rich import print
migration_04 = """CREATE TABLE alembic_version (
version_num VARCHAR(32) NOT NULL,
CONSTRAINT alembic_version_pkc PRIMARY KEY (version_num)
);
CREATE TABLE users (
id INTEGER NOT NULL,
username VARCHAR NOT NULL,
password VARCHAR NOT NULL,
email VARCHAR NOT NULL,
created_at DATETIME DEFAULT (CURRENT_TIMESTAMP) NOT NULL, updated_at DATETIME DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
PRIMARY KEY (id),
UNIQUE (email),
UNIQUE (username)
);
""" # noqa
migration_05 = """CREATE TABLE alembic_version (
version_num VARCHAR(32) NOT NULL,
CONSTRAINT alembic_version_pkc PRIMARY KEY (version_num)
);
CREATE TABLE users (
id INTEGER NOT NULL,
username VARCHAR NOT NULL,
password VARCHAR NOT NULL,
email VARCHAR NOT NULL,
created_at DATETIME DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
PRIMARY KEY (id),
UNIQUE (email),
UNIQUE (username)
);
""" # noqa
migration_10 = (
migration_05
+ """CREATE TABLE todos (
id INTEGER NOT NULL,
title VARCHAR NOT NULL,
description VARCHAR NOT NULL,
state VARCHAR(5) NOT NULL,
user_id INTEGER NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY(user_id) REFERENCES users (id)
);
""" # noqa
)
dotenv = """DATABASE_URL="postgresql+psycopg://app_user:app_password@localhost:5432/app_db"
SECRET_KEY="your-secret-key"
ALGORITHM="HS256"
ACCESS_TOKEN_EXPIRE_MINUTES=30
"""
fake_dotenv = """DATABASE_URL="sqlite:///database.db"
SECRET_KEY="your-secret-key"
ALGORITHM="HS256"
ACCESS_TOKEN_EXPIRE_MINUTES=30
"""
fake_dotenv_async = """DATABASE_URL="sqlite+aiosqlite:///database.db"
SECRET_KEY="your-secret-key"
ALGORITHM="HS256"
ACCESS_TOKEN_EXPIRE_MINUTES=30
"""
@contextmanager
def env_file(path: Path, sync=True):
with open(path / '.env', 'w', encoding='utf-8') as file:
if sync:
file.write(fake_dotenv)
else:
file.write(fake_dotenv_async)
yield
with open(path / '.env', 'w', encoding='utf-8') as file:
if sync:
file.write(fake_dotenv)
else:
file.write(fake_dotenv_async)
@task
def test_migrations(c):
code_path = Path('./codigo_das_aulas/').resolve().glob('*')
for path in sorted(code_path):
print('test_migrations: ', path)
database = path / 'database.db'
if database.exists():
database.unlink()
with c.cd(str(path)):
if int(path.parts[-1]) >= 10: # noqa
c.run('poetry install')
with env_file(path, sync=False):
c.run('alembic upgrade head')
schema = c.run('sqlite3 database.db ".schema"')
assert schema.stdout == migration_10
elif int(path.parts[-1]) == 4: # noqa
c.run('poetry install')
c.run('alembic upgrade head')
schema = c.run('sqlite3 database.db ".schema"')
assert schema.stdout == migration_04
elif int(path.parts[-1]) >= 5: # noqa
c.run('poetry install')
c.run('alembic upgrade head')
schema = c.run('sqlite3 database.db ".schema"')
assert schema.stdout == migration_05
@task
def typos_sub(c):
code_path = Path('./codigo_das_aulas/').resolve().glob('*')
for path in sorted(code_path):
print('typos_sub: ', path)
with c.cd(str(path)):
c.run('poetry run typos .')
@task
def lint_sub(c):
code_path = Path('./codigo_das_aulas/').resolve().glob('*')
for path in sorted(code_path):
print('lint_sub: ', path)
with c.cd(str(path)):
c.run('poetry run task lint')
@task
def type_check_sub(c, ci=False):
code_path = Path('./codigo_das_aulas/').resolve().glob('*')
for path in sorted(code_path):
print('type_check_sub: ', path)
with c.cd(str(path)):
if ci:
c.run('poetry install')
c.run('poetry add zuban')
print('Normal check: ', path)
c.run('poetry run zuban check --pretty fast_zero', warn=True)
print('Strict check: ', path)
c.run(
'poetry run zuban check --pretty --strict fast_zero',
warn=True
)
else:
c.run('zuban check --pretty fast_zero', warn=True)
@task
def test_act(c):
code_path = Path('./codigo_das_aulas/').resolve().glob('*')
for path in sorted(code_path):
print('test_act: ', path)
with c.cd(str(path)):
if (path / '.github').exists():
c.run('act')
@task
def test_docker_build(c, python_version='3.12'): # noqa: PT028
code_path = Path('./codigo_das_aulas/').resolve().glob('*')
for path in sorted(code_path):
print('test_docker_build: ', path)
with c.cd(str(path)):
if (path / 'Dockerfile').exists():
c.run(
f"sed -i 's/FROM python:.*$/FROM python:{python_version}/' Dockerfile" # noqa
)
if (path / 'compose.yaml').exists():
c.run('docker compose build')
@task
def test_sub(c):
code_path = Path('./codigo_das_aulas/').resolve().glob('*')
for path in sorted(code_path):
print('test_sub: ', path)
with c.cd(str(path)):
c.run('poetry install')
c.run('poetry run task test')
@task
def win_test_last_class(c):
code_path = Path('./codigo_das_aulas/13')
with c.cd(str(code_path)):
print('Current path: ', code_path)
c.run('poetry install')
c.run('poetry run task test')
@task
def win_test_migration(c):
code_path = Path('./codigo_das_aulas/13')
with c.cd(str(code_path)):
print('Current path: ', code_path)
c.run('poetry run alembic upgrade head')
@task
def command_sub(c, cmd):
code_path = Path('./codigo_das_aulas/').resolve().glob('*')
for path in sorted(code_path):
print('command_sub: ', path)
with c.cd(str(path)):
c.run(cmd)
@task
def update_sub(c):
code_path = Path('./codigo_das_aulas/').resolve().glob('*')
for path in sorted(code_path):
toml = path / 'pyproject.toml'
toml_tables = loads(toml.read_text())
toml_project = toml_tables['project']
dependencies = toml_project['dependencies']
dev_dependencies = toml_tables['dependency-groups']['dev']
print('update_sub:', path)
with c.cd(str(path)):
c.run('rm -rf .venv')
if (path / 'poetry.lock').exists():
c.run('rm poetry.lock')
for dep in sorted(dependencies):
_dep = dep.split()[0]
print(_dep, path)
if _dep in 'fastapi':
c.run('poetry add "fastapi[standard]@latest"')
elif _dep in 'pydantic':
c.run('poetry add "pydantic[email]@latest"')
elif _dep in 'pwdlib':
c.run('poetry add "pwdlib[argon2]@latest"')
elif _dep in 'psycopg':
c.run('poetry add "psycopg[binary]@latest"')
else:
c.run(f'poetry add {_dep}@latest')
for dep in dev_dependencies:
_dep = dep.split()[0]
c.run(f'poetry add --group dev {_dep}@latest')
c.run('poetry install')
@task
def test_compose(c):
code_path = Path('./codigo_das_aulas/').resolve().glob('*')
for path in sorted(code_path):
compose_file = path / 'compose.yaml'
if compose_file.exists():
print(f'Testando compose em: {path}')
with c.cd(str(path)):
c.run('docker compose up -d', warn=True)
c.run('sleep 10', warn=True)
result = c.run(
'docker compose ps --status exited',
hide=True
)
if len(result.stdout.split('\n')) > 2:
print('Alguns containers falharam ao iniciar')
print(result.stdout)
c.run('docker compose ps')
c.run('docker compose logs')
c.run('docker compose down', warn=True)
raise SystemExit(1)
print('Todos os containers iniciaram corretamente')
c.run('docker compose down', warn=True)