-
-
Notifications
You must be signed in to change notification settings - Fork 529
/
dev
executable file
·490 lines (436 loc) · 16 KB
/
dev
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
#!/bin/bash
# "dev" is an utility script speeding up different development tasks and actions.
# To find out what options are available, run it without any arguments.
# Text styles
RED='\033[0;31m'
BOLD=$(tput bold)
NORMAL=$(tput sgr0)
# Required ports
# Some tasks test for those ports before continuing
port_django=${MISAGO_DEVSERVER_PORT:-8000}
port_postgresql=5432
required_ports=($port_postgresql $port_django)
# Default superuser
username="Admin"
password="password"
email="[email protected]"
# Utility functions used by action commands
error() {
echo -e "${RED}Error:${NORMAL} $1"
}
require_in_docker() {
if [[ ! $IN_MISAGO_DOCKER = 1 ]]; then
error "This command can only be ran inside the running Misago docker container."
exit 1
fi
}
wait_for_db() {
require_in_docker
export PGPASSWORD=$POSTGRES_PASSWORD
RETRIES=10
until psql -h $POSTGRES_HOST -U $POSTGRES_USER -d $POSTGRES_DB -c "select 1" > /dev/null 2>&1 || [ $RETRIES -eq 0 ]; do
echo "Waiting for PostgreSQL to start, $((RETRIES--)) remaining attempts..."
sleep 2
done
}
# Check if user has docker compose
if [[ ! $IN_MISAGO_DOCKER = 1 ]]; then
if ! docker version >/dev/null 2>&1; then
error "You need to have Docker installed running to use this tool."
echo
echo "Docker release for your system can be downloaded for free from this page:"
echo "https://www.docker.com/get-started"
echo
exit 1
elif ! docker compose version >/dev/null 2>&1; then
error "You need to have Docker Compose plugin installed to use this tool."
echo
echo "Guide for installing Docker Compose plugin on your system can be found on this page:"
echo "https://docs.docker.com/compose/install/"
echo
exit 1
fi
fi
# Commands
intro() {
echo "Usage: ./dev [arg] ..."
echo "Arguments grouped by type:"
echo
echo "Development project:"
echo
echo " ${BOLD}init${NORMAL} initialize dev database for development."
echo " ${BOLD}afterinit${NORMAL} repeat help message displayed after init command is complete."
echo " ${BOLD}clear${NORMAL} clear media and userdata dirs and destroy docker containers."
echo " ${BOLD}rebuild${NORMAL} rebuild docker containers."
echo " ${BOLD}reset${NORMAL} run clear followed by init."
echo
echo " Both init and rebuild args can be followed with any number of extra args and options that should be appended to docker compose build."
echo
echo "Testing:"
echo
echo " ${BOLD}test${NORMAL} run tests suite using pytest."
echo
echo "Translations:"
echo
echo " ${BOLD}makemessages${NORMAL} update translation files for \"en\" language."
echo " ${BOLD}makemessages lang${NORMAL} update translation files for \"lang\" language."
echo " ${BOLD}compilemessages${NORMAL} compile translation files to \"mo\" format."
echo " ${BOLD}txpull${NORMAL} pull translations from Transifex."
echo " ${BOLD}txpush${NORMAL} push new source files to Transifex."
echo " ${BOLD}txsync${NORMAL} runs entire process of syncing translations with Transifex."
echo
echo "Plugins:"
echo
echo " ${BOLD}pluginmakemessages plugin${NORMAL} update translation files for \"en\" language in \"plugin\"."
echo " ${BOLD}pluginmakemessages plugin lang${NORMAL} update translation files for \"lang\" language in \"plugin\"."
echo " ${BOLD}plugincompilemessages plugin${NORMAL} compile translation files in \"plugin\" to \"mo\" format."
echo
echo "Shortcuts:"
echo
echo " ${BOLD}manage.py${NORMAL} runs \"python manage.py\" inside docker."
echo " ${BOLD}bash${NORMAL} starts bash session inside running Misago container."
echo " ${BOLD}run${NORMAL} runs \"docker compose run --rm misago\"."
echo " ${BOLD}psql${NORMAL} runs psql connected to development database."
echo " ${BOLD}pyfmt${NORMAL} runs isort + black on python code."
echo " ${BOLD}loaddevfixture${NORMAL} populates database with pre-made data for dev."
echo " ${BOLD}fakedata${NORMAL} populates database with generated testing data."
echo " ${BOLD}fakebigdata${NORMAL} populates database with LARGE amount of geerated testing data."
echo " ${BOLD}pipcompile${NORMAL} run pip-compile to update requirements.txt"
echo
}
# Handle invalid argument
invalid_argument() {
echo -e "Invalid argument: ${RED}$1${NORMAL}"
echo "Please run this script without any arguments to see the list of available arguments."
exit 1
}
# Initialize new dev project
init() {
for port in "${required_ports[@]}"; do
nc "127.0.0.1" "$port" < /dev/null
if [[ $? = "0" ]]; then
if [[ $port = $port_django ]]; then
error "Other application appears to already be running on http://127.0.0.1:${port_django}"
elif [[ $port = $port_postgresql ]]; then
error "PostgreSQL appears to already be running on the port $port."
echo
echo "Misago runs its own PostgreSQL instance in the docker container and uses port $port to expose it to other programs."
echo "Please stop your PostgreSQL server and try again."
echo
fi
exit 1
fi
done
docker compose stop
docker compose build --pull --force-rm "${@:2}"
docker compose run --rm misago ./dev init_in_docker
}
# Initialization step that has to occur inside docker
init_in_docker() {
require_in_docker
wait_for_db
# migrate the database
python manage.py migrate
# create superuser Admin with password "password"
python manage.py createsuperuser --username $username --email $email --password $password
# display after init message
echo
echo "================================================================================"
after_init_message
}
# After-init message
after_init_message() {
echo
echo "You can now start the development server using:"
echo
echo " docker compose up"
echo
echo "Running server will be available in the browser under the http://127.0.0.1:${port_django} address."
echo
echo "Default superuser has been created with this username and password:"
echo
echo "Username: $username"
echo "Password: $password"
echo
echo "You can populate the database with additional data using the following command:"
echo
echo "./dev loaddevfixture"
echo
echo "To connect to the database use following credentials:"
echo
echo "User: misago"
echo "Password: misago"
echo "Database: misago"
echo "Host: postgres"
echo "Port: 5432"
echo
echo "Note: development server must be running for connection to be possible."
echo
}
# Bootstrap plugins at the build time
bootstrap_plugins() {
require_in_docker
if [ -d $MISAGO_PLUGINS ]; then
echo "Bootstrapping plugins..."
glob_pattern="$MISAGO_PLUGINS/*/*/misago_plugin.py"
for plugin in $glob_pattern; do
plugin_dir=$(dirname $(dirname "$plugin"))
plugin_name=$(basename $plugin_dir)
plugin_pyproject="$plugin_dir/pyproject.toml"
plugin_requirements="$plugin_dir/requirements.txt"
plugin_requirements_dev="$plugin_dir/requirements-dev.txt"
if [ -f $plugin_pyproject ]; then
echo "Installing: $plugin_pyproject"
pip install -e "$plugin_dir[dev]"
elif [ -f $plugin_requirements ]; then
echo "Installing: $plugin_requirements"
pip install -r $plugin_requirements
if [ -f $plugin_requirements_dev ]; then
echo "Installing: $plugin_requirements_dev"
pip install -r $plugin_requirements_dev
fi
elif [ -f $plugin_requirements_dev ]; then
echo "Installing: $plugin_requirements_dev"
pip install -r $plugin_requirements_dev
else
echo "Skipping plugin bootstrap for: $plugin_name"
fi
done
pip_install="$MISAGO_PLUGINS/pip-install.txt"
if [ -f $pip_install ]; then
echo "Installing: $pip_install"
pip install -r $pip_install
fi
else
echo "Skipping plugin bootstrap because plugins directory doesn't exist."
fi
}
# Clear existing dev project
clear() {
echo -e "${RED}Warning:${NORMAL} You are going to delete media files created during development and destroy docker containers."
echo
devproject_path="$(pwd)/devproject"
echo "Enter \"y\" to confirm:"
read confirmation
if [[ $confirmation = "y" ]]; then
docker compose stop
docker compose down --remove-orphans
find $devproject_path/media -mindepth 1 ! -name '.gitignore' -delete
find $devproject_path/userdata -mindepth 1 ! -name '.gitignore' -delete
else
echo "Operation canceled."
fi
}
# Reset containers
reset() {
echo -e "${RED}Warning:${NORMAL} You are going to delete media files created during development and destroy docker containers."
echo
devproject_path="$(pwd)/devproject"
echo "Enter \"y\" to confirm:"
read confirmation
if [[ $confirmation = "y" ]]; then
docker compose stop
docker compose down --remove-orphans
find $devproject_path/media -mindepth 1 ! -name '.gitignore' -delete
find $devproject_path/userdata -mindepth 1 ! -name '.gitignore' -delete
init $@
else
echo "Operation canceled."
fi
}
# Rebuild docker containers
rebuild() {
docker compose stop
docker compose build --pull --force-rm "${@:2}"
}
# Run tests suite
test() {
docker compose run --rm misago pytest "${@:2}"
}
# Make plugin's messages
plugin_makemessages() {
docker compose run --rm --no-deps misago ./dev plugin_makemessages_in_docker $1 $2
}
# Docker part of plugin's makemessages
plugin_makemessages_in_docker() {
require_in_docker
plugin_path="./plugins/$1"
if [ -d $plugin_path ]; then
cd $plugin_path
if [[ $2 ]]; then
echo "Extracting messages in the $1 plugin for $2 language:"
echo "Processing .py and .html files..."
django-admin makemessages -l $2 --no-obsolete -e html,txt,py > /dev/null
echo "Processing .js files..."
django-admin makemessages -l $2 --no-obsolete -d djangojs > /dev/null
else
echo "Extracting messages in $1 plugin for all languages:"
django-admin makemessages --all --no-obsolete -e html,txt,py > /dev/null
django-admin makemessages --all --no-obsolete -d djangojs > /dev/null
fi
else
echo "Could not find the $1 plugin!"
fi
}
# Compile plugin's messages
plugin_compilemessages() {
docker compose run --rm --no-deps misago ./dev plugin_compilemessages_in_docker $1
}
# Docker part of plugin's compile messages
plugin_compilemessages_in_docker() {
require_in_docker
plugin_path="./plugins/$1"
if [ -d $plugin_path ]; then
cd $plugin_path
django-admin compilemessages
else
echo "Could not find the $1 plugin!"
fi
}
# Make messages
makemessages() {
docker compose run --rm --no-deps misago ./dev makemessages_in_docker $1
}
# Docker part of makemessages
makemessages_in_docker() {
require_in_docker
cd ./misago
if [[ $1 ]]; then
echo "Extracting messages for $1 language:"
echo "Processing .py and .html files..."
django-admin makemessages -l $1 --no-obsolete -e html,txt,py > /dev/null
echo "Processing .js files..."
django-admin makemessages -l $1 --no-obsolete -d djangojs > /dev/null
else
echo "Extracting messages for all languages:"
django-admin makemessages --all --no-obsolete -e html,txt,py > /dev/null
django-admin makemessages --all --no-obsolete -d djangojs > /dev/null
fi
}
# Compile messages
compilemessages() {
docker compose run --rm --no-deps misago ./dev compilemessages_in_docker
}
# Docker part of compile messages
compilemessages_in_docker() {
require_in_docker
cd ./misago
django-admin compilemessages
}
# Pull translation files from transifex
txpull() {
tx pull -a
compilemessages
}
# Push translation sources to transifex
txpush() {
tx push --source
}
# Shortcut for starting bash session in running container
run_bash() {
docker exec -it misago_misago_1 bash
}
# Shortcut for docker compose run --rm misago python manage.py
run_managepy() {
docker compose run --rm misago python manage.py "${@:2}"
}
# Shortcut for docker compose run --rm misago...
docker_run() {
docker compose run --rm misago "${@:2}"
}
# Shortcut for psql
run_psql() {
docker compose run --rm misago ./dev psql_in_docker
}
# Docker part of psql shortcut
psql_in_docker() {
wait_for_db
PGPASSWORD=$POSTGRES_PASSWORD psql --username $POSTGRES_USER --host $POSTGRES_HOST $POSTGRES_DB
}
# Shortcut for creating small dev forum
create_fake_data() {
docker compose run --rm misago python manage.py createfakecategories 7
docker compose run --rm misago python manage.py createfakecategories 12 1
docker compose run --rm misago python manage.py createfakehistory 600
}
# Shortcut for creating big dev forum
create_fake_bigdata() {
docker compose run --rm misago python manage.py createfakecategories 48
docker compose run --rm misago python manage.py createfakecategories 24 1
docker compose run --rm misago python manage.py createfakehistory 2190 120
}
# Shortcut for pip compile new requirements.txt
pip_compile() {
docker compose run --rm misago pip-compile --output-file=requirements.txt requirements.in
}
# Command dispatcher
if [[ $1 ]]; then
if [[ $1 = "init" ]]; then
init $@
elif [[ $1 = "init_in_docker" ]]; then
init_in_docker
elif [[ $1 = "afterinit" ]]; then
after_init_message
elif [[ $1 = "bootstrap_plugins" ]]; then
bootstrap_plugins
elif [[ $1 = "clear" ]]; then
clear
elif [[ $1 = "reset" ]]; then
reset $@
elif [[ $1 = "rebuild" ]]; then
rebuild $@
elif [[ $1 = "test" ]]; then
test $@
elif [[ $1 = "pluginmakemessages" ]]; then
plugin_makemessages $2 $3
elif [[ $1 = "plugin_makemessages_in_docker" ]]; then
plugin_makemessages_in_docker $2 $3
elif [[ $1 = "plugincompilemessages" ]]; then
plugin_compilemessages $2
elif [[ $1 = "plugin_compilemessages_in_docker" ]]; then
plugin_compilemessages_in_docker $2
elif [[ $1 = "makemessages" ]]; then
makemessages $2
elif [[ $1 = "makemessages_in_docker" ]]; then
makemessages_in_docker $2
elif [[ $1 = "compilemessages" ]]; then
compilemessages
elif [[ $1 = "compilemessages_in_docker" ]]; then
compilemessages_in_docker
elif [[ $1 = "txpull" ]]; then
txpull
elif [[ $1 = "txpush" ]]; then
txpush
elif [[ $1 = "txsync" ]]; then
rm -rf ./misago/locale/en
makemessages en
txpush
txpull
compilemessages
elif [[ $1 = "bash" ]]; then
run_bash
elif [[ $1 = "manage.py" ]]; then
run_managepy $@
elif [[ $1 = "run" ]]; then
docker_run $@
elif [[ $1 = "psql" ]]; then
run_psql
elif [[ $1 = "psql_in_docker" ]]; then
psql_in_docker
elif [[ $1 = "pyfmt" ]]; then
isort -rc misago
black devproject misago
elif [[ $1 = "loaddevfixture" ]]; then
docker compose run --rm misago python manage.py loaddevfixture
elif [[ $1 = "fakedata" ]]; then
create_fake_data
elif [[ $1 = "fakebigdata" ]]; then
create_fake_bigdata
elif [[ $1 = "pipcompile" ]]; then
pip_compile
else
invalid_argument $1
fi
else
intro
fi