-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathtasks.py
More file actions
784 lines (617 loc) · 24.4 KB
/
tasks.py
File metadata and controls
784 lines (617 loc) · 24.4 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
import os
import re
import shlex
import shutil
import subprocess
import tarfile
from pathlib import Path
from sys import platform
from invoke import exceptions, task
ROOT = os.path.dirname(os.path.realpath(__file__))
LOCALE_DIR = os.path.realpath(os.path.abspath("foundation_cms/locale"))
# Python commands's outputs are not rendering properly. Setting pty for *Nix system and
# "PYTHONUNBUFFERED" env var for Windows at True.
if platform == "win32":
PLATFORM_ARG = dict(env={"PYTHONUNBUFFERED": "True"})
else:
PLATFORM_ARG = dict(pty=True)
# The command for locale string abstraction is long and elaborate,
# so we build it here rather so that we don't clutter up the tasks.
locale_abstraction_instructions = " ".join(
[
"makemessages",
"--all",
"--keep-pot",
"--no-wrap",
"--ignore=foundation_cms/legacy_apps/wagtailcustomization/*",
"--ignore=foundation_cms/settings/base.py",
"--ignore=foundation_cms/legacy_apps/wagtailpages/templates/wagtailpages/pages/dear_internet_page.html",
"--ignore=foundation_cms/templates/patterns/pages/core/welcome_page.html",
"--ignore=dockerpythonvenv/*",
]
)
locale_abstraction_instructions_js = " ".join(
[
"makemessages",
"-d djangojs",
"--all",
"--extension js,jsx",
"--keep-pot",
"--no-wrap",
"--ignore=node_modules",
"--ignore=dockerpythonvenv/*",
"--ignore=foundation_cms/legacy_apps/static/compiled",
"--ignore=cypress",
]
)
def create_env_file(env_file):
"""Create or update an .env to work with a docker environment"""
with open(env_file) as f:
env_vars = f.read()
# We also need to make sure to use the correct db values based on our docker settings.
username = dbname = "postgres"
with open("docker-compose.yml") as d:
docker_compose = d.read()
username = re.search("POSTGRES_USER=(.*)", docker_compose).group(1) or username
dbname = re.search("POSTGRES_DB=(.*)", docker_compose).group(1) or dbname
# Update the DATABASE_URL env
new_db_url = f"DATABASE_URL=postgresql://{username}@postgres:5432/{dbname}"
old_db_url = re.search("DATABASE_URL=.*", env_vars)
env_vars = env_vars.replace(old_db_url.group(0), new_db_url)
# update the ALLOWED_HOSTS
new_hosts = "ALLOWED_HOSTS=*"
old_hosts = re.search("ALLOWED_HOSTS=.*", env_vars)
env_vars = env_vars.replace(old_hosts.group(0), new_hosts)
# create the new env file
with open(".env", "w") as f:
f.write(env_vars)
# Project setup and update
def l10n_block_inventory(ctx, stop=False):
"""
Update the block inventory.
To stop the containers after the command has run, pass `stop=True`.
"""
print("* Updating block information")
manage(ctx, "block_inventory", stop=stop)
@task(aliases=["create-super-user"])
def createsuperuser(ctx, stop=False):
"""
Create a superuser with username and password 'admin'.
To stop the containers after the command is run, pass the `--stop` flag.
"""
manage(ctx, "create_admin", stop=stop)
def initialize_database(ctx, slow=False):
"""
Initialize the database.
To stop all containers after each management command, pass `slow=True`.
"""
print("* Applying database migrations.")
migrate(ctx, stop=slow)
# print("* Creating fake data")
# manage(ctx, "load_fake_data", stop=slow)
print("* Creating redesign data")
manage(ctx, "load_redesign_data", stop=slow)
print("* Creating fake legacy data")
manage(ctx, "legacy_load_fake_data", stop=slow)
print("* Sync locales")
manage(ctx, "sync_locale_trees", stop=slow)
l10n_block_inventory(ctx, stop=slow)
createsuperuser(ctx, stop=slow)
@task(aliases=["docker-new-db"])
def new_db(ctx, slow=False, no_seed_data=False):
"""
Delete your database and create a new one with fake data.
If you are experiencing 'too many clients' errors while running this command, try
to pass the `--slow` flag. This will make sure that the containers are stopped
between the management commands and prevent that issue.
Pass `--no-seed-data` to run migrations only, skipping all content/fake data loading.
This is used by `inv generate-schema-snapshot` to produce a clean snapshot.
"""
print("* Starting the postgres service")
ctx.run("docker-compose up -d postgres")
print("* Delete the database")
ctx.run("docker-compose run --rm postgres dropdb --if-exists wagtail -hpostgres -Ufoundation")
print("* Create the database")
ctx.run("docker-compose run --rm postgres createdb wagtail -hpostgres -Ufoundation")
if no_seed_data:
print("* Applying migrations (no seed data)...")
migrate(ctx, stop=slow)
else:
initialize_database(ctx, slow=slow)
print("Stop postgres service")
ctx.run("docker-compose down")
@task(aliases=["docker-catchup", "catchup"])
def catch_up(ctx):
"""Rebuild images, install dependencies, and apply migrations"""
print("* Stopping services first")
ctx.run("docker-compose down")
print("* Rebuilding images and install dependencies")
# The docker image build will install node and python dependencies.
ctx.run("docker-compose build")
print("* Applying database migrations.")
migrate(ctx)
print("* Updating block information.")
l10n_block_inventory(ctx)
print("\n* Start your dev server with:\n inv start or docker-compose up")
@task(aliases=["new-env", "docker-new-env"])
def setup(ctx):
"""Get a new dev environment and a new database with fake data"""
with ctx.cd(ROOT):
print("* Setting default environment variables")
if os.path.isfile(".env"):
print("* Stripping quotes and making sure your DATABASE_URL and ALLOWED_HOSTS are properly setup")
create_env_file(".env")
else:
print("* Creating a new .env")
create_env_file("env.default")
print("* Stopping project's containers and delete volumes if necessary")
ctx.run("docker-compose down --volumes")
print("* Building Docker images")
ctx.run("docker-compose build")
initialize_database(ctx)
print("\n* Start your dev server with:\n inv start or docker-compose up.")
@task(aliases=["start", "docker-start"])
def start_dev(ctx):
"""Start the dev server"""
with ctx.cd(ROOT):
ctx.run("docker-compose up")
@task(aliases=["start-lean", "docker-start-lean"])
def start_lean_dev(ctx):
"""Start the dev server without rebuilding frontend assets for a faster start up."""
print("Starting the dev server without rebuilding frontend assets...")
print("WARNING: Frontend assets may be outdated or missing if they haven't been built yet.")
ctx.run("docker-compose -f docker-compose.yml -f docker-compose-lean.yml up")
@task
def sh(c, service="backend"):
"""
Run bash in a local container
"""
subprocess.run(["docker-compose", "exec", service, "bash"])
# Javascript shorthands
@task(aliases=["docker-yarn"])
def yarn(ctx, command):
"""Shorthand to yarn. inv docker-yarn \"[COMMAND] [ARG]\" """
with ctx.cd(ROOT):
# Tell user to use yarn_install instead if command includes 'install' or 'ci'
if "install" in command or "ci" in command:
print("Please use 'inv yarn-install' instead.")
return
ctx.run(f"docker-compose run --rm backend yarn {command}")
@task(aliases=["docker-yarn-exec"])
def yarn_exec(ctx, command):
"""Run yarn in running container, e.g for yarn install."""
with ctx.cd(ROOT):
# Using 'exec' instead of 'run --rm' as /node_modules is not mounted.
# To make this persistent, use 'exec' to run in the running container.
try:
ctx.run(f"docker-compose exec --user=root backend yarn {command}")
except exceptions.UnexpectedExit:
print("This command requires a running container.\n")
print("Please run 'inv start' or 'inv start-lean' in a separate terminal window first.")
@task(aliases=["docker-yarn-install"])
def yarn_install(ctx):
"""Install Node dependencies"""
with ctx.cd(ROOT):
yarn_exec(ctx, "install")
@task(aliases=["copy-stage-db"])
def copy_staging_database(ctx):
with ctx.cd(ROOT):
ctx.run("node copy-db.js")
@task(aliases=["new-snapshot"])
def generate_schema_snapshot(ctx):
"""
Build a clean local DB (migrations only, no seed content), dump it in custom format,
and save it as a .dump file ready for pg_restore on review apps.
WARNING: This will destroy and recreate your local database.
Upload the resulting file to S3 manually afterward.
"""
from datetime import date
filename = f"schema-{date.today().isoformat()}.dump"
answer = input("This will destroy and rebuild your local database. Continue? [y/N] ").strip().lower()
if answer != "y":
print("Aborted.")
return
new_db(ctx, no_seed_data=True)
print(f"* Dumping database to {filename}...")
ctx.run(f"docker-compose up -d postgres")
ctx.run(
f"docker-compose run --rm postgres pg_dump -Fc --no-owner --no-acl -hpostgres -Ufoundation wagtail > {filename}"
)
ctx.run("docker-compose down")
print(f"Snapshot saved to ./{filename}")
print(f"Upload to S3 with: aws s3 cp {filename} s3://<bucket>/snapshots/{filename}")
print(f"You might also need to update heroku config variables to reference today's snapshot.")
print(f"Snapshot complete, initializing content for local database.")
initialize_database(ctx, slow=False)
@task(aliases=["copy-prod-db"])
def copy_production_database(ctx):
with ctx.cd(ROOT):
ctx.run("node copy-db.js --prod")
# Python shorthands
@task
def pyrun(ctx, command, stop=False):
"""
Shorthand to commands with the activated Python virutalenv.
To stop the containers after the command has been run, pass the `--stop` flag.
"""
with ctx.cd(ROOT):
ctx.run(
f'docker-compose run --rm backend bash -c "source ./dockerpythonvenv/bin/activate && {command}"',
**PLATFORM_ARG,
)
if stop:
ctx.run("docker-compose stop")
@task(aliases=["docker-manage"])
def manage(ctx, command, stop=False):
"""
Shorthand to manage.py.
inv docker-manage \"[COMMAND] [ARG]\"
To stop the containers after the command has been run, pass the `--stop` flag.
"""
command = f"python ./manage.py {command}"
pyrun(ctx, command, stop=stop)
@task(aliases=["np-fake-articles"])
def load_np_fake_articles(
ctx,
count=70,
keyword="NP_PAGINATION_TEST",
seed=None,
delete=False,
update_index=False,
stop=False,
):
"""
Generate fake Nothing Personal articles for testing search pagination.
Example:
- inv load-np-fake-articles --count=70 --keyword=NP_PAGINATION_TEST --delete
"""
parts = [
"load_np_fake_articles",
f"--count {int(count)}",
f"--keyword {shlex.quote(str(keyword))}",
]
if seed is not None and str(seed) != "":
parts.append(f"--seed {int(seed)}")
if delete:
parts.append("--delete")
if update_index:
parts.append("--update-index")
manage(ctx, " ".join(parts), stop=stop)
@task(aliases=["docker-djcheck"])
def djcheck(ctx, stop=False):
"""
Django system check framework.
To stop the containers after the command has run, pass the `--stop` flag.
"""
print("Running system check framework...")
manage(ctx, "check", stop=stop)
@task(aliases=["docker-migrate"])
def migrate(ctx, stop=False):
"""
Update the database schema.
To stop the containers after the command has run, pass the `--stop` flag.
"""
manage(ctx, "migrate --no-input", stop=stop)
@task(aliases=["docker-makemigrations"])
def makemigrations(ctx, args=""):
"""
Creates new migration(s) for apps. Optional: --args=""
"""
manage(ctx, f"makemigrations {args}")
@task(aliases=["docker-makemigrations-dryrun"])
def makemigrations_dryrun(ctx, args=""):
"""
Show new migration(s) for apps without creating them. Optional: --args=""
"""
manage(ctx, f"makemigrations {args} --dry-run")
# Tests
@task(aliases=["docker-test"])
def test(ctx):
"""Run tests."""
djcheck(ctx)
makemigrations_dryrun(ctx, args="--check")
test_python(ctx)
@task(aliases=["docker-test-python"])
def test_python(ctx, file="", n="auto", verbose=False):
"""
Run python tests.
Example calls:
- test_python(ctx)
- test_python(ctx, file="test_something.py")
- test_python(ctx, n=4, verbose=True)
Parameters:
- ctx: Context object (provided by Invoke)
- file: Optional string representing the path to a specific test file to run.
- n: Optional integer or string 'auto' representing the number of parallel tests to run.
Default is 'auto' which allows pytest to automatically determine the optimal number.
- verbose: Optional boolean flag indicating whether to print verbose output during testing. Default is False.
"""
parallel = f"-n {n}" if n != "1" else ""
v = "-v" if verbose else ""
# Don't run coverage if a file is specified
cov = "" if file else "--cov=foundation_cms/legacy_apps --cov-report=term-missing"
command = f"pytest {v} {parallel} {file} --reuse-db {cov}"
pyrun(ctx, command)
# Linting
@task
def lint(ctx):
"""Run linting."""
lint_html(ctx)
lint_css(ctx)
lint_js(ctx)
lint_python(ctx)
@task
def lint_html(ctx):
"""Run HTML linting."""
# Skipping djlint format checking because it has consistency issues and issues with blocktrans.
# This should change when formatting is moved to a version using and AST.
# See also: https://github.com/Riverside-Healthcare/djLint/issues/493
# djlint_check(ctx)
#
# Use djhtml indent checking until format checking with djlint becomes possible.
djhtml_check(ctx)
djlint_lint(ctx)
@task
def lint_css(ctx):
"""Run CSS linting."""
yarn(ctx, "run lint:css")
@task
def lint_js(ctx):
"""Run JavaScript linting."""
yarn(ctx, "run lint:js")
@task
def lint_python(ctx):
"""Run Python linting."""
flake8(ctx)
isort_check(ctx)
black_check(ctx)
# Formatting
@task
def format(ctx):
"""Run formatters."""
format_html(ctx)
format_css(ctx)
format_js(ctx)
format_python(ctx)
@task
def format_html(ctx):
"""Run HTML formatting."""
# Skipping djlint formatting because it has consistency issues and issues with blocktrans.
# This should change when formatting is moved to a version using and AST.
# See also: https://github.com/Riverside-Healthcare/djLint/issues/493
# djlint_format(ctx)
#
# Indent HTML until full formatting with djlint becomes possible
djhtml_format(ctx)
@task
def format_css(ctx):
"""Run css formatting."""
yarn(ctx, "run fix:css")
@task
def format_js(ctx):
"""Run javascript formatting."""
yarn(ctx, "run fix:js")
@task
def format_python(ctx):
"""Run python formatting."""
isort(ctx)
black(ctx)
# Tooling
@task(help={"args": "Override the arguments passed to black."})
def black(ctx, args=None):
"""Run black code formatter."""
args = args or "."
pyrun(ctx, command=f"black {args}")
@task
def black_check(ctx):
"""Run black code formatter in check mode."""
black(ctx, ". --check")
@task(help={"args": "Override the arguments passed to djhtml."})
def djhtml(ctx, args=None):
"""Run djhtml code indenter."""
args = args or "-h"
pyrun(ctx, command=f"djhtml {args}")
@task
def djhtml_check(ctx):
"""Run djhtml code indenter in check mode."""
djhtml(ctx, args="-c maintenance/ foundation_cms/")
@task
def djhtml_format(ctx):
"""Run djhtml code indenter in formatting mode."""
djhtml(ctx, args="maintenance/ foundation_cms/")
@task(help={"args": "Override the arguments passed to djlint."})
def djlint(ctx, args=None):
"""Run djlint code formatter and linter."""
args = args or "."
pyrun(ctx, command=f"djlint {args}")
@task
def djlint_check(ctx):
"""Run djlint in format checking mode."""
djlint(ctx, ". --check")
@task
def djlint_format(ctx):
"""Run djlint formatting mode."""
djlint(ctx, ". --reformat --quiet")
@task
def djlint_lint(ctx):
"""Run djlint in linting mode."""
djlint(ctx, ". --lint")
@task
def flake8(ctx):
"""Run flake8."""
pyrun(ctx, "flake8 ./foundation_cms")
@task(help={"args": "Override the arguments passed to isort."})
def isort(ctx, args=None):
"""Run isort code formatter."""
args = args or "."
pyrun(ctx, command=f"isort {args}")
@task
def isort_check(ctx):
"""Run isort code formatter in check mode."""
isort(ctx, ". --check-only")
@task(help={"args": "Override the arguments passed to mypy."})
def mypy(ctx, args=None):
"""Run mypy type checking on the project."""
args = args or "foundation_cms"
pyrun(ctx, command=f"mypy {args}")
# Pip-tools
def _pip_compile_workaround(requirement_file, additional_commands=""):
"""
A workaround to fix 'Device or resource busy' error when running
pip-compile in docker container where the output file is a mounted volume.
This is because pip-compile tries to replace the output file, and you can't replace
mount points. However, you can write to them. So we work around this by using an
unmounted .tmp file as intermediary, and then write the changes to the mounted file.
"""
output_file = requirement_file.replace(".in", ".txt")
temp_file = output_file + ".tmp"
return (
f"cp {output_file} {temp_file}&&pip-compile {requirement_file}"
+ " "
+ additional_commands
+ f" --output-file={temp_file}&&cp {temp_file} {output_file}&&rm {temp_file}"
)
@task(aliases=["docker-pip-compile"], optional=["command"])
def pip_compile(ctx, filename="requirements.in", command=""):
"""Shorthand to pip-tools. inv pip-compile \"[filename]\" \"[COMMAND] [ARG]\" """
with ctx.cd(ROOT):
ctx.run(
f"""docker-compose run --rm backend bash -c '{_pip_compile_workaround(filename, command)}'""",
**PLATFORM_ARG,
)
@task(aliases=["docker-pip-compile-lock"])
def pip_compile_lock(ctx):
"""Lock prod and dev dependencies"""
with ctx.cd(ROOT):
# Running in separate steps as the dev-requirements.in needs to read requirements.txt
command = _pip_compile_workaround("requirements.in") + "&&"
command = command + _pip_compile_workaround("dev-requirements.in")
ctx.run(
f"""docker-compose run --rm backend bash -c '{command}'""",
**PLATFORM_ARG,
)
@task(aliases=["docker-pip-sync"])
def pip_sync(ctx):
"""Sync your python virtualenv"""
with ctx.cd(ROOT):
try:
ctx.run(
# Using 'exec' instead of 'run --rm' as /dockerpythonvenv is not mounted.
# To make this persistent, use 'exec' to run in the running container.
"docker-compose exec backend ./dockerpythonvenv/bin/pip-sync requirements.txt dev-requirements.txt",
**PLATFORM_ARG,
)
except exceptions.UnexpectedExit:
print("This command requires a running container.\n")
print("Please run 'inv start' or 'inv start-lean' in a separate terminal window first.")
# Translation
@task(aliases=["docker-makemessages"])
def makemessages(ctx):
"""Extract all template messages in .po files for localization"""
ctx.run("./translation-management.sh import")
manage(ctx, locale_abstraction_instructions)
manage(ctx, locale_abstraction_instructions_js)
ctx.run("./translation-management.sh export")
# Translation Alternative Command
LOCALE_FOLDERS = [
"legacy_apps/locale/",
"legacy_apps/templates/pages/buyersguide/about/locale/",
"legacy_apps/wagtailpages/templates/wagtailpages/pages/locale/",
"legacy_apps/wagtailpages/templates/wagtailpages/pages/youtube-regrets-2021/locale/",
"legacy_apps/wagtailpages/templates/wagtailpages/pages/youtube-regrets-2022/locale/",
"legacy_apps/mozfest/locale/",
]
@task(aliases=["docker-msgmerge"])
def msgmerge(ctx):
"""Run msgmerge for all .pot/.po files found in FOLDERS across all locales."""
ctx.run("./translation-management.sh import")
manage(ctx, locale_abstraction_instructions)
manage(ctx, locale_abstraction_instructions_js)
locales = ["en", "de", "es", "fr", "fy-NL", "nl", "pl", "pt-BR", "sw"]
js_pot_path = "foundation_cms/legacy_apps/locale/djangojs.pot"
for folder in LOCALE_FOLDERS:
pot_path = f"foundation_cms/{folder}django.pot"
if not os.path.exists(pot_path):
print(f"Skipping: no .pot at {pot_path}")
continue
for locale in locales:
locale_dir = locale.replace("-", "_")
po_path = f"foundation_cms/{folder}{locale_dir}/LC_MESSAGES/django.po"
try:
pyrun(ctx, f"msgmerge --update --no-wrap --no-fuzzy-matching --backup=none {po_path} {pot_path}")
except Exception:
print(f"PO file for '{locale}' not found in {folder}. Consider initializing.")
if os.path.exists(js_pot_path):
for locale in locales:
locale_dir = locale.replace("-", "_")
js_po_path = f"foundation_cms/legacy_apps/locale/{locale_dir}/LC_MESSAGES/djangojs.po"
try:
pyrun(ctx, f"msgmerge --update --no-wrap --no-fuzzy-matching --backup=none {js_po_path} {js_pot_path}")
except Exception:
print(f"JS PO file for '{locale}' not found.")
ctx.run("./translation-management.sh export")
@task(aliases=["docker-compilemessages"])
def compilemessages(ctx):
"""Compile the latest translations"""
with ctx.cd(ROOT):
ctx.run(
"docker-compose run --rm -w /app backend "
"./dockerpythonvenv/bin/python manage.py compilemessages --ignore=dockerpythonvenv/*",
**PLATFORM_ARG,
)
@task(aliases=["staging-to-review-app"])
def staging_db_to_review_app(ctx, review_app_name):
"""
Copy Staging DB to a specific Review App. inv staging-to-review-app \"[REVIEW_APP_NAME]\"
"""
from copy_staging_db_to_review_app import main
main(ctx, review_app_name)
@task(name="get-ffmpeg")
def compile_ffmpeg(ctx, output_dir="compiled-ffmpeg"):
"""
Copy ffmpeg and ffprobe from a running dev container and archive it.
If ../heroku-ffmpeg-static-builds exists, copy the tarball there too.
Version is inferred from the built binary.
Afterward, cleanup local copies of binaries and archive.
"""
root = Path(__file__).resolve().parent
output_path = root / output_dir
output_path.mkdir(parents=True, exist_ok=True)
static_builds_dir = root.parent / "heroku-ffmpeg-static-builds"
print("Copying ffmpeg and ffprobe from running dev container...")
try:
container_id = subprocess.check_output(["docker-compose", "ps", "-q", "backend"], text=True).strip()
if not container_id:
print("No running 'backend' container found. Please run 'inv start' first.")
return
except subprocess.CalledProcessError:
print("Failed to find Docker container. Is Docker running?")
return
for binary in ("ffmpeg", "ffprobe"):
subprocess.run(["docker", "cp", f"{container_id}:/usr/bin/{binary}", str(output_path / binary)], check=True)
version_output = subprocess.check_output(
["docker", "exec", container_id, "/usr/bin/ffmpeg", "-version"], text=True
)
version = version_output.splitlines()[0].split()[2] if version_output else "unknown"
print(f"Detected FFmpeg version: {version}")
archive_path = output_path / f"ffmpeg-{version}-webp.tar.xz"
print("Creating tarball...")
with tarfile.open(archive_path, "w:xz") as tar:
for binary in ("ffmpeg", "ffprobe"):
tar.add(output_path / binary, arcname=binary)
if static_builds_dir.exists():
shutil.copy2(archive_path, static_builds_dir / archive_path.name)
print(f"Copied tarball to: {static_builds_dir / archive_path.name}")
else:
print("Skipped copying: ../heroku-ffmpeg-static-builds does not exist.")
print("Clone it from https://github.com/MozillaFoundation/heroku-ffmpeg-static-builds")
print("Cleaning up local files...")
for file in [output_path / "ffmpeg", output_path / "ffprobe", archive_path]:
try:
file.unlink()
print(f"Deleted {file}")
except FileNotFoundError:
print(f"File already removed: {file}")
except Exception as e:
print(f"Error deleting {file}: {e}")
print("Done.")