forked from metabrainz/critiquebrainz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
185 lines (145 loc) · 6.05 KB
/
manage.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
import os
import subprocess
from werkzeug.serving import run_simple
from werkzeug.wsgi import DispatcherMiddleware
from brainzutils import cache
import click
from critiquebrainz import frontend, ws
from critiquebrainz.data import dump_manager
import critiquebrainz.data.utils as data_utils
import critiquebrainz.data.fixtures as _fixtures
cli = click.Group()
application = DispatcherMiddleware(frontend.create_app(), {
"/ws/1": ws.create_app()
})
# Files listed here will be monitored for changes in debug mode and will
# force a reload when modified.
OBSERVE_FILES = [
"critiquebrainz/frontend/static/build/rev-manifest.json",
]
@cli.command()
@click.option("--host", "-h", default="0.0.0.0", show_default=True)
@click.option("--port", "-p", default=8080, show_default=True)
@click.option("--debug", "-d", is_flag=True,
help="Turns debugging mode on or off. If specified, overrides "
"'DEBUG' value in the config file.")
def runserver(host, port, debug=False):
run_simple(
hostname=host,
port=port,
application=application,
use_debugger=debug,
extra_files=OBSERVE_FILES,
use_reloader=debug,
)
@cli.command()
def extract_strings():
"""Extract all strings into messages.pot.
This command should be run after any translatable strings are updated.
Otherwise updates are not going to be available on Transifex.
"""
_run_command("pybabel extract -F critiquebrainz/frontend/babel.cfg -k lazy_gettext "
"-o critiquebrainz/frontend/messages.pot critiquebrainz/frontend")
click.echo("Strings have been successfully extracted into messages.pot file.")
@cli.command()
def pull_translations():
"""Pull translations for languages defined in config from Transifex and compile them.
Before using this command make sure that you properly configured Transifex client.
More info about that is available at http://docs.transifex.com/developer/client/setup#configuration.
"""
languages = ','.join(frontend.create_app().config['SUPPORTED_LANGUAGES'])
_run_command("tx pull -f -r critiquebrainz.critiquebrainz -l %s" % languages)
@cli.command()
def update_strings():
"""Extract strings and pull translations from Transifex."""
extract_strings()
pull_translations()
@cli.command()
def compile_translations():
"""Compile translations for use."""
_run_command("pybabel compile -d critiquebrainz/frontend/translations")
click.echo("Translated strings have been compiled and ready to be used.")
@cli.command()
def clear_memcached():
with frontend.create_app().app_context():
cache.flush_all()
click.echo("Flushed everything from memcached.")
@click.option("--skip-create-db", "-s", is_flag=True,
help="Skip database creation step.")
@click.option("--test-db", "-t", is_flag=True,
help="Initialize the test database.")
@click.option("--force", "-f", is_flag=True,
help="Drop existing tables and types.")
@cli.command()
def init_db(skip_create_db=False, test_db=False, force=False):
"""Initialize the database.
* Creates the database.
* Creates all tables.
* Adds fixtures required to run the app.
"""
click.echo("Initializing the database...")
if force:
click.echo("Dropping existing tables and types...")
data_utils.drop_tables()
data_utils.drop_types()
click.echo("Done!")
if test_db:
db_uri = frontend.create_app(config_path=os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'critiquebrainz', 'test_config.py'
)).config['SQLALCHEMY_DATABASE_URI']
else:
db_uri = frontend.create_app().config['SQLALCHEMY_DATABASE_URI']
if not skip_create_db:
init_postgres(db_uri)
create_extension(db_uri)
click.echo("Creating tables... ", nl=False)
data_utils.create_all()
click.echo("Done!")
click.echo("Adding fixtures... ")
app = frontend.create_app()
with app.app_context():
_fixtures.install(*_fixtures.all_data)
click.echo("Done!")
click.echo("Initialization has been completed!")
def init_postgres(db_uri):
"""Initializes PostgreSQL database from provided URI.
New user and database will be created, if needed. It also creates uuid-ossp extension.
"""
hostname, port, db, username, password = data_utils.explode_db_uri(db_uri) # pylint: disable=unused-variable
if hostname not in ['localhost', '127.0.0.1']:
raise Exception('Cannot configure a remote database')
# Checking if user already exists
retv = subprocess.check_output('sudo -u postgres psql -t -A -c'
'"SELECT COUNT(*) FROM pg_user WHERE usename = \'%s\';"' %
username, shell=True)
if retv == '0':
exit_code = subprocess.call(
'sudo -u postgres psql -c '
'"CREATE ROLE %s PASSWORD \'%s\' NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN;"' %
(username, password),
shell=True,
)
if exit_code != 0:
raise Exception('Failed to create PostgreSQL user!')
# Checking if database exists
exit_code = subprocess.call('sudo -u postgres psql -c "\\q" %s' % db, shell=True)
if exit_code != 0:
exit_code = subprocess.call('sudo -u postgres createdb -O %s %s' % (username, db), shell=True)
if exit_code != 0:
raise Exception('Failed to create PostgreSQL database!')
def create_extension(db_uri):
host, port, db, username, password = data_utils.explode_db_uri(db_uri)
psql_cmd = "psql -h %s -p %s -U %s -W %s %s" % (host, port, username, password, db)
exit_code = subprocess.call(
'%s -t -A -c "CREATE EXTENSION IF NOT EXISTS \\"%s\\";" %s' %
(psql_cmd, 'uuid-ossp', db),
shell=True,
)
if exit_code != 0:
raise Exception('Failed to create PostgreSQL extension!')
def _run_command(command):
return subprocess.check_call(command, shell=True)
cli.add_command(dump_manager.cli, name="dump")
if __name__ == '__main__':
cli()