forked from memeLab/Jandig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks.py
140 lines (107 loc) · 3.31 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
from invoke import task
import os
import sys
python = sys.executable
directory = os.path.dirname(__file__)
sys.path.append('src')
#
# 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, postgres=False):
cmd = f'python3 /src/manage.py {cmd}'
ctx.run(cmd, pty=True, env=default_env(postgres))
def default_env(postgres):
os.environ['DEV_DB'] = 'True' if not postgres else 'False'
e = os.environ
return e
@task
def run(ctx, ssl=False, gunicorn=False, postgres=False):
"""
Run development server
"""
if gunicorn:
ctx.run('gunicorn --reload --worker-connections=10000 --workers=4 --log-level debug --bind 0.0.0.0:8000 config.wsgi', env={"DEV_DB":"False"})
else:
manage(ctx, "runserver 0.0.0.0:8000", postgres)
@task
def db(ctx, make=False, postgres=False):
"""
Run migrations
"""
if make:
manage(ctx, "makemigrations", postgres)
manage(ctx, "migrate", postgres)
else:
manage(ctx, "migrate", postgres)
@task
def collect(ctx):
"""
Collect static files
"""
manage(ctx, "collectstatic --no-input --clear")
@task
def install_deps(ctx):
"""
Install all dependencies
"""
ctx.run('pip3 install -r src/requirements.txt')
@task
def docker(ctx, build=False):
command = 'sudo docker-compose -f docker/docker-compose.yml up'
if build:
command += ' --build'
ctx.run(command)
@task
def build_base(ctx, publish=False):
"""
Build base docker images
"""
command = './src/etc/scripts/build-base.sh'
if publish:
command += ' publish'
ctx.run(command)
@task
def init_production(ctx):
"""
Init production environment
"""
command = './src/etc/scripts/init-production.sh'
ctx.run(command)
#
# Translations
#
@task
def i18n(ctx, compile=False, edit=False, lang='pt_BR', keep_pot=False):
"""
Extract messages for translation.
"""
if edit:
ctx.run(f'poedit locale/{lang}/LC_MESSAGES/django.po')
elif compile:
ctx.run(f'{python} etc/scripts/compilemessages.py')
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')
@task
def docs(ctx):
ctx.run('sphinx-build docs/ build/')