-
Notifications
You must be signed in to change notification settings - Fork 72
/
tasks.py
223 lines (177 loc) · 5.98 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
from contextlib import contextmanager
from pathlib import Path
from invoke import task
from rich import print
from tomllib import loads
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_09 = 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
"""
@contextmanager
def env_file(path: Path):
with open(path / '.env', 'w', encoding='utf-8') as file:
file.write(fake_dotenv)
yield
with open(path / '.env', 'w', encoding='utf-8') as file:
file.write(dotenv)
@task
def test_migrations(c):
code_path = Path('./codigo_das_aulas/').resolve().glob('*')
for path in sorted(code_path):
print(path)
database = path / 'database.db'
if database.exists():
database.unlink()
with c.cd(str(path)):
if int(path.parts[-1]) >= 9: # noqa
c.run('poetry install')
with env_file(path):
c.run('alembic upgrade head')
schema = c.run('sqlite3 database.db ".schema"')
assert schema.stdout == migration_09
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 update_project(c):
c.run('rm -rf .venv')
toml = Path('.') / 'pyproject.toml'
toml_tables = loads(toml.read_text())
poetry_toml = toml_tables['tool']['poetry']
dependencies = poetry_toml['dependencies']
if (Path('.') / 'poetry.lock').exists():
c.run('rm poetry.lock')
for dep in sorted(dependencies):
if dep == 'python':
...
else:
c.run(f'poetry add {dep}@latest')
@task
def typos_sub(c):
code_path = Path('./codigo_das_aulas/').resolve().glob('*')
for path in sorted(code_path):
print(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(path)
with c.cd(str(path)):
c.run('poetry run task lint')
@task
def test_act(c):
code_path = Path('./codigo_das_aulas/').resolve().glob('*')
for path in sorted(code_path):
print(path)
with c.cd(str(path)):
if (path / '.github').exists():
c.run('act')
@task
def test_docker_build(c):
code_path = Path('./codigo_das_aulas/').resolve().glob('*')
for path in sorted(code_path):
print(path)
with c.cd(str(path)):
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(path)
with c.cd(str(path)):
c.run('poetry install')
c.run('poetry run task test')
@task
def command_sub(c, cmd):
code_path = Path('./codigo_das_aulas/').resolve().glob('*')
for path in sorted(code_path):
print(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())
poetry_toml = toml_tables['tool']['poetry']
dependencies = poetry_toml['dependencies']
dev_dependencies = poetry_toml['group']['dev']['dependencies']
print(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):
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"')
elif dep == 'python':
...
else:
c.run(f'poetry add {dep}@latest')
for dep in dev_dependencies:
c.run(f'poetry add --group dev {dep}@latest')
c.run('poetry install')