-
Notifications
You must be signed in to change notification settings - Fork 21
/
tasks.py
72 lines (58 loc) · 2.01 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
import os
import sys
from invoke import task
python = sys.executable
directory = os.path.dirname(__file__)
sys.path.append("jandig")
#
# Call python manage.py in a more robust way
#
def robust_manage(ctx, cmd, env=None, **kwargs):
kwargs = {k.replace("_", "-"): v for k, v in kwargs.items() if v is not False}
opts = " ".join(f'--{k} {"" if v is True else v}' for k, v in kwargs.items())
cmd = f"{python} ./src/manage.py {cmd} {opts}"
env = {**os.environ, **(env or {})}
path = env.get("PYTHONPATH", ":".join(sys.path))
env.setdefault("PYTHONPATH", f"src:{path}")
print(cmd)
ctx.run(cmd, pty=True, env=env)
def manage(ctx, cmd):
cmd = f"python3 ./src/manage.py {cmd}"
ctx.run(cmd, pty=True, env=os.environ)
@task
def run(ctx, ssl=False, gunicorn=False):
"""
Run development server
"""
if gunicorn:
ctx.run(
"cd src && gunicorn --reload --worker-connections=10000 --workers=4 --log-level debug --bind 0.0.0.0:8000 config.wsgi"
)
else:
manage(ctx, "runserver 0.0.0.0:8000")
#
# Translations
#
@task
def i18n(ctx, edit=False, lang="pt_BR", keep_pot=False):
"""
Extract messages for translation.
"""
if edit:
ctx.run(f"poedit locale/{lang}/LC_MESSAGES/django.po")
else:
print("Collecting messages")
robust_manage(ctx, "makemessages", keep_pot=True, locale=lang)
print("Extract Jinja translations")
ctx.run("pybabel extract -F ./etc/babel.cfg -o ./locale/jinja2.pot .")
print("Join Django + Jinja translation files")
ctx.run(
"msgcat ./locale/django.pot ./locale/jinja2.pot --use-first -o ./locale/join.pot",
pty=True,
)
ctx.run(r"""sed -i '/"Language: \\n"/d' ./locale/join.pot""", pty=True)
print(f"Update locale {lang} with Jinja2 messages")
ctx.run(f"msgmerge ./locale/{lang}/LC_MESSAGES/django.po ./locale/join.pot -U")
if not keep_pot:
print("Cleaning up")
ctx.run("rm ./locale/*.pot")