-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtasks.py
executable file
·265 lines (217 loc) · 6.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
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
# Third Party
from invoke import task
COMPOSE_PREFIX = "docker compose -f local.yml"
COMPOSE_BUILD = f"{COMPOSE_PREFIX} build {{opt}} {{service}}"
COMPOSE_RUN_OPT = f"{COMPOSE_PREFIX} run {{opt}} --rm {{service}} {{cmd}}"
COMPOSE_RUN_OPT_USER = COMPOSE_RUN_OPT.format(
opt="-u $(id -u):$(id -g) {opt}", service="{service}", cmd="{cmd}"
)
COMPOSE_RUN = COMPOSE_RUN_OPT.format(opt="", service="{service}", cmd="{cmd}")
DJANGO_RUN = COMPOSE_RUN.format(service="documentcloud_django", cmd="{cmd}")
DJANGO_RUN_USER = COMPOSE_RUN_OPT_USER.format(
opt="", service="documentcloud_django", cmd="{cmd}"
)
WEB_OPEN = "xdg-open {} > /dev/null 2>&1"
@task
def test(
c, path="documentcloud", create_db=False, ipdb=False, slow=False, warnings=False
):
"""Run the test suite"""
create_switch = "--create-db" if create_db else ""
ipdb_switch = "--pdb --pdbcls=IPython.terminal.debugger:Pdb" if ipdb else ""
slow_switch = "" if slow else '-m "not slow"'
warnings = "-e PYTHONWARNINGS=always" if warnings else ""
c.run(
COMPOSE_RUN_OPT_USER.format(
opt=f"-e DJANGO_SETTINGS_MODULE=config.settings.test {warnings}",
service="documentcloud_django",
cmd=f"pytest {create_switch} {ipdb_switch} {slow_switch} {path}",
),
pty=True,
)
@task
def testwatch(c, path="documentcloud"):
"""Run the test suite and watch for changes"""
c.run(
COMPOSE_RUN_OPT_USER.format(
opt="-e DJANGO_SETTINGS_MODULE=config.settings.test",
service="documentcloud_django",
cmd=f"ptw {path}",
),
pty=True,
)
@task
def launchall(c):
"""Run all the necessary programs in Squarelet and DocumentCloud in Linux program terminator"""
c.run('terminator -g terminator2.config -l "DocumentCloud"')
@task
def openreport(c):
"""Open the generated test reports file in a web browser (run `inv test` first)"""
c.run(
WEB_OPEN.format(
"file://$PWD/documentcloud/document/processing/tests/reports.html"
)
)
@task
def openapp(c):
"""Open the local Django app in a web browser"""
c.run(WEB_OPEN.format("http://dev.documentcloud.org"))
@task
def coverage(c):
"""Run the test suite with coverage report"""
c.run(
COMPOSE_RUN_OPT_USER.format(
opt="-e DJANGO_SETTINGS_MODULE=config.settings.test",
service="documentcloud_django",
cmd="coverage erase",
)
)
c.run(
COMPOSE_RUN_OPT_USER.format(
opt="-e DJANGO_SETTINGS_MODULE=config.settings.test",
service="documentcloud_django",
cmd='coverage run -m pytest -m "not slow" documentcloud',
)
)
c.run(
COMPOSE_RUN_OPT_USER.format(
opt="-e DJANGO_SETTINGS_MODULE=config.settings.test",
service="documentcloud_django",
cmd="coverage html",
)
)
@task
def pylint(c):
"""Run the linter"""
c.run(DJANGO_RUN.format(cmd="pylint documentcloud"))
@task
def format(c):
"""Format your code"""
c.run(
DJANGO_RUN_USER.format(
cmd="black documentcloud --exclude migrations && "
"black config/urls.py && "
"black config/settings && "
"isort -rc documentcloud && "
"isort -rc config/urls.py && "
"isort -rc config/settings"
)
)
@task
def runserver(c):
"""Run the development server"""
c.run(
COMPOSE_RUN_OPT.format(
opt="--service-ports --use-aliases", service="documentcloud_django", cmd=""
)
)
@task
def shell(c, opts=""):
"""Run an interactive python shell"""
c.run(DJANGO_RUN.format(cmd=f"python manage.py shell_plus {opts}"), pty=True)
@task
def sh(c):
"""Run an interactive shell"""
c.run(
COMPOSE_RUN_OPT.format(
opt="--use-aliases", service="documentcloud_django", cmd="sh"
),
pty=True,
)
@task
def dbshell(c, opts=""):
"""Run an interactive db shell"""
c.run(DJANGO_RUN.format(cmd=f"python manage.py dbshell {opts}"), pty=True)
@task
def celeryworker(c):
"""Run a celery worker"""
c.run(
COMPOSE_RUN_OPT.format(
opt="--use-aliases", service="documentcloud_celeryworker", cmd=""
)
)
@task
def celerybeat(c):
"""Run the celery scheduler"""
c.run(
COMPOSE_RUN_OPT.format(
opt="--use-aliases", service="documentcloud_celerybeat", cmd=""
)
)
@task
def manage(c, cmd):
"""Run a Django management command"""
c.run(DJANGO_RUN_USER.format(cmd=f"python manage.py {cmd}"), pty=True)
@task
def run(c, cmd):
"""Run a command directly on the docker instance"""
c.run(DJANGO_RUN_USER.format(cmd=cmd))
@task(name="pip-compile")
def pip_compile(c, upgrade=False, package=None):
"""Run pip compile"""
if package:
upgrade_flag = f"--upgrade-package {package}"
elif upgrade:
upgrade_flag = "--upgrade"
else:
upgrade_flag = ""
c.run(
COMPOSE_RUN_OPT_USER.format(
opt="-e PIP_TOOLS_CACHE_DIR=/tmp/pip-tools-cache",
service="documentcloud_django",
cmd=f"sh -c 'pip-compile {upgrade_flag} requirements/base.in && "
f"pip-compile {upgrade_flag} requirements/local.in && "
f"pip-compile {upgrade_flag} requirements/production.in'",
)
)
@task
def build(c, opt="", service=""):
"""Build the docker images"""
c.run(COMPOSE_BUILD.format(opt=opt, service=service))
@task
def heroku(c, staging=False):
"""Run commands on heroku"""
if staging:
app = "documentcloud-staging"
else:
app = "documentcloud-prod"
c.run(f"heroku run --app {app} python manage.py shell_plus", pty=True)
@task
def download_tesseract_data(c):
"""Download Tesseract data files. Needed to be able to do OCR locally."""
c.run("cd config/aws/lambda; ./build.sh")
@task
def deploy_lambdas(c, staging=False):
"""Deploy lambda functions on AWS"""
if staging:
stack = "info-and-image-staging"
env = "staging"
else:
stack = "processing-production"
env = "prod"
c.run(f"cd config/aws/lambda; ./deploy.sh {stack} {env}")
@task
def update_solr_config(c):
"""Update the solr config to the local docker images
Be sure to bring the container down and up again after updating
"""
template = (
"docker cp config/solr/{old_file} documentcloud-documentcloud_{container}:"
"/var/solr/data/{collection}/{new_file}"
)
for old_file, new_file in [
("managed-schema", "managed-schema"),
("solrconfig.local.xml", "solrconfig.xml"),
]:
for container, collection in [
("solr-1", "documentcloud"),
("test_solr-1", "documentcloud_test"),
]:
c.run(
template.format(
old_file=old_file,
new_file=new_file,
container=container,
collection=collection,
)
)