-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_it_ok.sh
executable file
·282 lines (261 loc) · 8.38 KB
/
is_it_ok.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
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
#!/usr/bin/env bash
# pouziti: is_it_ok.sh xlogin01-XYZ.zip testdir [task-num]
#
# - POZOR: obsah adresare zadaneho druhym parametrem bude po dotazu VYMAZAN!
# - rozbali archiv studenta xlogin99.zip do adresare testdir a overi formalni pozadavky pro odevzdani projektu IPP
# - cislo ulohy (task-num) je nepovinny parametr (mozne hodnoty: 1 nebo 2)
# - nasledne vyzkousi spusteni
# - detaily prubehu jsou logovany do souboru is_it_ok.log v adresari testdir
# Autor: Zbynek Krivka
# Verze: 1.5.3 (2020-01-30)
# 2012-04-03 Zverejnena prvni verze
# 2012-04-09 Pridana kontrola tretiho radku (prispel Vilem Jenis) a maximalni velikosti archivu
# 2012-04-26 Oprava povolenych pripon archivu, aby to odpovidalo pozadavkum v terminu ve WIS
# 2014-02-14 Pridana moznost koncovky tbz u archivu; Podpora koncovky .php; Kontrola zkratek rozsireni
# 2014-02-25 Pridani parametru -d open_basedir="" interpretu php
# 2015-03-12 Pridana chybejici zavorka v popisu chyby tretiho paramatru
# 2015-03-22 Pridana kontrola tretiho radku i v dalsich skriptech (prispela Michaela Lukasova), kontrola neexistence __MACOSX
# 2016-03-08 Pridana kontrola ulohy CLS a odebrana uloha CST (upozornil Michal Ormoš)
# 2017-02-06 Zrusen pozadavek na metainformace na tretim radku skriptu, zakomentovan kontrolni kod
# 2017-03-04 Aktualizace prikazu pro PHP 5.6 a Python 3.6 na Merlinovi (upozornil ¼uboš Hlipala)
# 2018-03-05 Zruseno deleni projektu na ulohy s pismennymi identifikatory, nyni 1. a 2. uloha
# 2018-03-12 Kontrola existence prikazu dos2unix, nepovinny parametr task-num (umi kontrolovat ulohu 1 nebo 2), barevne vypisy
# 2019-02-12 Uprava jmen a podporovanych formatu dokumentace (pdf|md)
# 2020-01-30 aktualizace prikazu pro PHP 7.4 a Python 3.8
#
LOG="is_it_ok.log"
MAX_ARCHIVE_SIZE=1100000
COURSE="IPP"
PARSESCRIPT="parse.php"
INTERPRETSCRIPT="interpret.py"
TESTSCRIPT="test.php"
# Konstanty barev
REDCOLOR='\033[1;31m'
GREENCOLOR='\033[1;32m'
BLUECOLOR='\033[1;34m'
NOCOLOR='\033[0m' # No Color
# Funkce: vypis barevny text
function echo_color () { # $1=color $2=text [$3=-n]
COLOR=$NOCOLOR
if [[ $1 == "red" ]]; then
COLOR=$REDCOLOR
elif [[ $1 == "blue" ]]; then
COLOR=$BLUECOLOR
elif [[ $1 == "green" ]]; then
COLOR=$GREENCOLOR
fi
echo -e $3 "$COLOR$2$NOCOLOR"
}
# Funkce: patri polozka do pole? (ze seznamu hodnot $* (parametry) hleda prvni parametr v ostatnich parametrech)
function member ()
{
local -a arr=($*)
for i in $(seq 1 $#);
do
if [ "${arr[$i]}" = "$1" ];
then
return 0
fi
done
return 1
}
# Pri nedostatku parametru (povinnych) vypis napovedu
if [[ $# -lt 2 ]]; then
echo_color red "ERROR: Missing arguments or too much arguments!"
echo "Usage: $0 ARCHIVE TESTDIR [TASKNUM]"
echo " This script checks formal requirements for archive with solution of $COURSE project."
echo " ARCHIVE - the filename of archive to check"
echo " TESTDIR - temporary directory that can be deleted/removed during testing!"
echo " TASKNUM - the task number (1 or 2) - optional"
exit 2
fi
declare -i ERRORS=0
declare -a REQUIRED_SCRIPTS=( $PARSESCRIPT $INTERPRETSCRIPT $TESTSCRIPT )
declare -a NON_REQUIRED_SCRIPTS=()
# Zpracovani nepovinneho parametru task-num
declare -i TASK=0
if [[ -n $3 ]]; then
TASK=$3
if [[ $TASK -eq 1 ]]; then
REQUIRED_SCRIPTS=( $PARSESCRIPT )
NON_REQUIRED_SCRIPTS=( $INTERPRETSCRIPT $TESTSCRIPT )
elif [[ $TASK -eq 2 ]]; then
REQUIRED_SCRIPTS=( $INTERPRETSCRIPT $TESTSCRIPT )
NON_REQUIRED_SCRIPTS=( $PARSESCRIPT )
else
echo_color red "ERROR (Unsupported task number: $3 not in {1, 2})"
exit 1
fi
fi
# Extrakce archivu
function unpack_archive () {
local ext=`echo $1 | cut -d . -f 2,3`
echo -n "Archive extraction: "
RETCODE=100
if [[ "$ext" = "zip" ]]; then
unzip -o $1 >> $LOG 2>&1
RETCODE=$?
elif [[ "$ext" = "tgz" ]]; then
tar xfz $1 >> $LOG 2>&1
RETCODE=$?
elif [[ "$ext" = "tbz" || "$ext" = "tbz2" ]]; then
tar xfj $1 >> $LOG 2>&1
RETCODE=$?
fi
if [[ $RETCODE -eq 0 ]]; then
echo_color green OK
elif [[ $RETCODE -eq 100 ]]; then
echo_color red "ERROR (unsupported extension)"
exit 1
else
echo_color red "ERROR (code $RETCODE)"
exit 1
fi
}
# Priprava testdir
if [[ -d $2 ]]; then
read -p "Do you want to delete $2 directory? (y/n)" RESP
if [[ $RESP = "y" ]]; then
rm -rf $2 2>/dev/null
else
echo_color red "ERROR:" -n
echo "User cancelled rewriting of existing directory."
exit 1
fi
fi
mkdir $2 2>/dev/null
cp $1 $2 2>/dev/null
# Overeni serveru (ala Eva neni Merlin)
echo -n "Testing on Merlin: "
HN=`hostname`
if [[ $HN = "merlin.fit.vutbr.cz" ]]; then
echo_color green "Yes"
else
echo_color blue "No"
fi
# Kontrola jmena archivu
cd $2
touch $LOG
ARCHIVE=`basename $1`
NAME=`echo $ARCHIVE | cut -d . -f 1 | egrep "^x[a-z]{5}[0-9][0-9a-z]$"`
echo -n "Archive name ($ARCHIVE): "
if [[ -n $NAME ]]; then
echo_color green "OK"
else
echo_color red "ERROR (the name $NAME does not correspond to a login)"
let ERROR=ERROR+1
fi
# Kontrola velikosti archivu
echo -n "Checking size of $ARCHIVE: "
ARCHIVE_SIZE=`du --bytes $ARCHIVE | cut -f 1`
if [[ ${ARCHIVE_SIZE} -ge ${MAX_ARCHIVE_SIZE} ]]; then
echo_color red "ERROR (Too big (${ARCHIVE_SIZE} bytes > ${MAX_ARCHIVE_SIZE} bytes)"
let ERROR=ERROR+1
else
echo_color green "OK"
fi
# Extrahovat do testdir
unpack_archive ${ARCHIVE}
# Dokumentace
echo -n "Searching for readme${TASK}.(pdf|md): "
if [[ $TASK -eq 0 ]]; then
if [ -f "readme1.pdf" -o -f "readme1.md" ]; then
echo_color blue "OK (readme1 found)"
elif [ -f "readme2.pdf" -o -f "readme2.md" ]; then
echo_color blue "OK (readme2 found)"
else
echo_color red "ERROR (not found; required readme1 or readme2!)"
let ERROR=ERROR+1
fi
elif [ $TASK -eq 1 -o $TASK -eq 2 ]; then
if [ -f "readme${TASK}.pdf" -o -f "readme${TASK}.md" ]; then
echo_color green "OK"
else
echo_color red "ERROR (not found!)"
let ERROR=ERROR+1
fi
else
echo_color red "ERROR (not found!)"
fi
#fi
# Spusteni skriptu
echo "Scripts execution test (--help): "
for SCRIPT in "${REQUIRED_SCRIPTS[@]}" "${NON_REQUIRED_SCRIPTS[@]}"; do
if [[ -f $SCRIPT ]]; then
echo -n " Checking $SCRIPT: "
EXT=`echo $SCRIPT | cut -d . -f 2`
if [[ "$EXT" = "php" ]]; then
php7.4 $SCRIPT --help >> $LOG 2>&1
RETCODE=$?
elif [[ "$EXT" = "py" ]]; then
python3.8 $SCRIPT --help >> $LOG 2>&1
RETCODE=$?
else
echo_color red "INTERNAL ERROR: Unknown script extension."
exit 3
fi
if [[ $RETCODE -eq 0 ]]; then
#echo -n " $SCRIPT: "
echo_color green "OK"
else
#echo -n " $SCRIPT: "
echo_color red "ERROR (returns code $RETCODE)"
let ERROR=ERROR+1
fi
else
if [[ $TASK -eq 0 ]]; then
if [[ "$SCRIPT" = "$PARSESCRIPT" ]]; then
echo_color blue " $SCRIPT: ERROR (not found; required for 1st task only!)"
let ERROR=ERROR+1
else
echo_color blue " $SCRIPT: ERROR (not found; required for 2nd task only!)"
let ERROR=ERROR+1
fi
else
if ( member $SCRIPT ${REQUIRED_SCRIPTS[@]} ); then
echo_color red " $SCRIPT: ERROR (not found; required for task $TASK!)"
let ERROR=ERROR+1
fi
fi
fi
done
# Kontrola rozsireni
echo -n "Presence of file rozsireni (optional): "
if [[ -f rozsireni ]]; then
echo_color green "Yes"
echo -n "Unix end of lines in rozsireni: "
if command -v dos2unix >/dev/null 2>&1; then
dos2unix -n rozsireni rozsireni.lf >> $LOG 2>&1
else
tr -d '\r' < rozsireni > rozsireni.lf 2>&1
fi
diff rozsireni rozsireni.lf >> $LOG 2>&1
RETCODE=$?
if [[ $RETCODE = "0" ]]; then
UNKNOWN=`cat rozsireni | grep -v -E -e "^(STATP|FLOAT|STACK|STATI|FILES)$" | wc -l`
if [[ $UNKNOWN = "0" ]]; then
echo_color green "OK"
else
echo_color red "ERROR (Unknown bonus identifier or redundant empty line)"
let ERROR=ERROR+1
fi
else
echo_color red "ERROR (CRLFs)"
let ERROR=ERROR+1
fi
else
echo "No"
fi
# Kontrola adresare __MACOSX
if [[ -d __MACOSX ]]; then
echo_color blue "Archive ($ARCHIVE) should not contain __MACOSX directory!"
let ERROR=ERROR+1
fi
echo -n "ALL CHECKS COMPLETED"
if [[ $ERROR -eq 0 ]]; then
echo_color green " WITHOUT ERRORS!"
elif [[ $ERROR -eq 1 ]]; then
echo_color red " WITH $ERROR ERROR!"
else
echo_color red " WITH $ERROR ERRORS!"
fi