forked from ro-i/tumcsbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.sh
executable file
·173 lines (145 loc) · 3.21 KB
/
manage.sh
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
#!/bin/sh
set -e
_command_exists_or_exit () {
if ! type "$1" > /dev/null; then
echo "$1 not found"
exit 1
fi
}
_enter_venv () {
if ! $virtualenv; then
return
fi
# enter virtual environment
. "${dest_dir}/venv/bin/activate"
}
clean_func () {
if ! $virtualenv; then
return
fi
# remove virtual environment
rm -rf "${dest_dir}/venv"
}
database_func () {
db="${dest_dir}/tumcsbot.db"
if [ -e "$db" ]; then
echo "Database ${db} already exists."
return
fi
touch "$db"
chmod 0600 "$db"
}
env_func () {
if $virtualenv; then
# create virtual environment
python3 -m venv "${dest_dir}/venv"
fi
_enter_venv
# install dependecies
pip3 install -r "${dest_dir}/requirements.txt"
printf '\n\n%s\n\n' '########################################'
printf '%s' 'TODO for you: Please install the zuliprc for this bot.'
printf '\n\n%s\n\n\n' '########################################'
}
init_func () {
database_func
env_func
}
migrations_func () {
"${dest_dir}/src/migrate.py" "${dest_dir}/tumcsbot.db" "${dest_dir}/migrations.sql"
}
mypy_func () {
_enter_venv
mypy --strict "${dest_dir}/src"
}
run_func () {
_enter_venv
upgrade_requirements_func
# execute bot
exec "${dest_dir}/src/main.py" "$@" "${dest_dir}/zuliprc" "${dest_dir}/tumcsbot.db"
}
static_analysis_func () {
_enter_venv
# Disable some checks.
# todo: reanable C0301
pylint --overgeneral-exceptions=builtins.BaseException \
--min-similarity-lines=25 \
--ignore-long-lines='\".*\"' \
--no-docstring-rgx='.*' \
--good-names-rgxs='[a-z],[a-z][a-z]' \
--disable=C0103,C0114,W0201,W0702,C0301,R0903,R1702,R0912,R0915,R0914,R0904,C0302 \
--exit-zero \
"${dest_dir}/src"
mypy_func
}
test_func () {
_enter_venv
pytest
}
upgrade_requirements_func () {
# upgrade all requirements
pip3 install -U -r "${dest_dir}/requirements.txt"
}
usage () {
echo -e "usage: manage.sh -m CMD DIR [ARGS]...\n\n\t-v\trun in virtualenv mode"
}
# whether the script runs in virtualenv mode or not
virtualenv=false
while getopts "v" opt; do
case "$opt" in
'v')
virtualenv=true
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND - 1))
cmd="$1"
# Always use absolute file names.
dest_dir=$(realpath "$2")
shift 2
if ! [ -d "$dest_dir" ]; then
if [ -d "$dest_dir" ]; then
echo "error: ${dest_dir} is not a directory"
fi
mkdir "$dest_dir"
fi
case "$cmd" in
'clean')
clean_func "$@"
;;
'database')
database_func "$@"
;;
'env')
env_func "$@"
;;
'init')
init_func "$@"
;;
'migrations')
migrations_func "$@"
;;
'mypy')
mypy_func "$@"
;;
'run')
run_func "$@"
;;
'static_analysis')
static_analysis_func "$@"
;;
'tests')
test_func "$@"
;;
'upgrade_requirements')
upgrade_requirements_func "$@"
;;
*)
echo "command not found: $cmd"
exit 1
;;
esac