-
Notifications
You must be signed in to change notification settings - Fork 11
/
dev
executable file
·296 lines (250 loc) · 9.19 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
#!/bin/bash
# Helper script for commands related to this repository.
#
# Notes:
# - Changes to template only require user to refresh browser.
# - Changes to static files require the 'static' command to be run.
# - Changes to Python code are detected by gunicorn and should take effect
# on the server after a few seconds.
set -e
export DOCKER_UID=$UID
ERROR='\033[0;31m'
SUCCESS='\033[0;32m'
CODE='\033[0;36m'
NC='\033[0m' # No Color
cmd_helps=()
defhelp() {
local command="${1?}"
local text="${2?}"
local help_str
help_str="$(printf ' %-28s %s' "$command" "$text")"
cmd_helps+=("$help_str")
}
# Print out help information
cmd_help() {
echo "Script for performing tasks related to the codeWOF repository."
echo
echo "Usage: ./dev [COMMAND]"
echo "Replace [COMMAND] with a word from the list below."
echo
echo "COMMAND list:"
for str in "${cmd_helps[@]}"; do
echo -e "$str"
done
}
defhelp help 'View all help.'
# Start development environment
cmd_start() {
echo "Creating systems..."
docker compose -f docker-compose.local.yml up -d
# Alert user that system is ready
echo -e "\n${SUCCESS}System is up!${NC}"
echo -e "Run the command ${CODE}./dev update${NC} to load content."
}
defhelp start 'Start development environment.'
# Stop development environment
cmd_end() {
echo "Stopping systems..."
docker compose -f docker-compose.local.yml down --remove-orphans
}
defhelp end 'Stop development environment.'
cmd_restart() {
docker compose -f docker-compose.local.yml restart "$@"
}
defhelp restart 'Restart container.'
# Update all content
cmd_update() {
cmd_static
echo ""
cmd_migrate
echo ""
cmd_update_data
echo ""
cmd_collect_static
echo -e "\n${SUCCESS}Content is loaded!${NC}"
echo "Open your preferred web browser to the URL 'codewof.localhost'"
}
defhelp update 'Update system ready for use.'
# Run Django makemigrations command
# Run in exising container to use existing volumes
cmd_makemigrations() {
echo "Creating database migrations..."
docker compose -f docker-compose.local.yml exec django python ./manage.py makemigrations --no-input
}
defhelp makemigrations 'Run Django makemigrations command.'
# Run Django migrate command
# Run in exising container to access new migration files
cmd_migrate() {
echo "Applying database migrations..."
docker compose -f docker-compose.local.yml exec django python ./manage.py migrate
}
defhelp migrate 'Run Django migrate command.'
### Data related commands #################################
# Run update_data command
cmd_update_data() {
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django python ./manage.py update_data
}
defhelp update_data "Update all required data."
# Run load_user_types command
cmd_load_user_types() {
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django python ./manage.py load_user_types
}
defhelp load_user_types "Load user types."
# Run load_questions command
cmd_load_questions() {
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django python ./manage.py load_questions
}
defhelp load_questions "Load questions."
# Run load_style_errors command
cmd_load_style_errors() {
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django python ./manage.py load_style_errors
}
defhelp load_style_errors "Load style errors."
# Run load_achievements command
cmd_load_achievements() {
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django python ./manage.py load_achievements
}
defhelp load_achievements "Load achievements."
cmd_createsuperuser() {
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django python ./manage.py createsuperuser
}
defhelp createsuperuser "Create superuser in Django system."
cmd_sample_data() {
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django python ./manage.py sample_data $1
}
defhelp sample_data "Add sample data to website."
cmd_send_email_reminders() {
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django python ./manage.py send_email_reminders
}
defhelp send_email_reminders "Send an email reminder to all users who opted to receive one today."
cmd_raise_backdate_flags() {
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django python ./manage.py raise_backdate_flags
}
defhelp raise_backdate_flags 'Raise a flag on all user profiles, requiring a backdate be done on them.'
cmd_remove_expired_invitations() {
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django python ./manage.py remove_expired_invitations
}
defhelp remove_expired_invitations "Remove all Invitation objects where date_expires is before the current datetime."
cmd_backdate() {
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django python ./manage.py backdate_points_and_achievements --ignore_flags
}
defhelp backdate 'Re-calculate points and achievements earned for all user profiles.'
###########################################################
# Build static files
cmd_static() {
echo "Building static files..."
docker compose -f docker-compose.local.yml run --rm node npm run generate-assets
}
defhelp static 'Build static files.'
# Collecting static files
cmd_collect_static() {
echo
echo "Collecting static files..."
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django python manage.py collectstatic --no-input --clear
}
defhelp collect_static "Collecting static files."
# Update and collect static files
cmd_update_static() {
cmd_static
echo ""
cmd_collect_static
echo ""
echo -e "\n${SUCCESS}Static files are updated!${NC}"
}
defhelp update_static 'Update static files.'
# Build Docker images
cmd_build() {
echo "Building Docker images..."
docker compose -f docker-compose.local.yml build
}
defhelp build 'Build or rebuild Docker images.'
# Run exec
cmd_exec() {
docker compose -f docker-compose.local.yml exec "$@"
}
defhelp exec "Execute command in given container."
# View Docker logs
cmd_logs() {
echo "Building Docker images..."
docker compose -f docker-compose.local.yml logs --timestamps "$@"
}
defhelp logs 'View logs.'
# Run style checks
cmd_style() {
echo "Running PEP8 style checker..."
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django flake8
pep8_status=$?
echo
echo "Running Python docstring checker..."
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django pydocstyle --count --explain
pydocstyle_status=$?
! (( pep8_status || pydocstyle_status ))
}
defhelp style 'Run style checks.'
# Run test suite
cmd_test_suite() {
echo "Running test suite..."
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django coverage run --rcfile=./.coveragerc ./manage.py test --settings=config.settings.testing --pattern "test_*.py" -v 3 --nomigrations
}
defhelp test_suite 'Run test suite with code coverage.'
# Run specific test suite
cmd_test_specific() {
echo "Running specific test suite..."
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django python ./manage.py test --settings=config.settings.testing "${1}" -v 3 --nomigrations
}
defhelp test_specific 'Run specific test suite. Pass in parameter of Python test module.'
# Display test coverage table
cmd_test_coverage() {
echo "Displaying test suite coverage..."
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django coverage xml -i
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django coverage report -m --skip-covered
}
defhelp test_coverage 'Display code coverage report.'
cmd_createsuperuser() {
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django python ./manage.py createsuperuser
}
defhelp createsuperuser "Create superuser in Django system."
# --- Testing -----------------------------------------------------------------
# For use in GitHub Actions environment
cmd_ci() {
docker network create uccser-development-stack
cmd_start
local cmd="$1"
shift
if [ -z "$cmd" ]; then
echo -e "${ERROR}ERROR: ci command requires one parameter!${NC}"
cmd_help
exit 1
fi
if silent type "cmd_$cmd"; then
"cmd_$cmd" "$@"
exit $?
else
echo -e "${ERROR}ERROR: Unknown command!${NC}"
echo "Type './dev help' for available commands."
exit 1
fi
}
cmd_test_general() {
docker compose -f docker-compose.local.yml run --rm --label traefik.enable=false django coverage run --rcfile=./.coveragerc ./manage.py test --settings=config.settings.testing --pattern "test_*.py" -v 3 --exclude-tag=resource --exclude-tag=management --nomigrations
}
# --- Core script logic -------------------------------------------------------
silent() {
"$@" > /dev/null 2>&1
}
# If no command given
if [ $# -eq 0 ]; then
echo -e "${ERROR}ERROR: This script requires a command!${NC}"
cmd_help
exit 1
fi
cmd="$1"
shift
if silent type "cmd_$cmd"; then
"cmd_$cmd" "$@"
exit $?
else
echo -e "${ERROR}ERROR: Unknown command!${NC}"
echo "Type './dev help' for available commands."
exit 1
fi