-
Notifications
You must be signed in to change notification settings - Fork 86
/
grade_utils
81 lines (70 loc) · 1.58 KB
/
grade_utils
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
#!/bin/bash
# Utilities for making grade-scripts shorter
set -o pipefail # see https://unix.stackexchange.com/a/267031/16569
exec 2>&1 # redirect stderr to stdout: https://unix.stackexchange.com/q/505581/16569
TIMEOUT=10 # 10 seconds - default timeout
typeset -F SECONDS
SECONDS=0
echo "--- $SECONDS seconds"
run_in_folder_if_exists() {
if [ -d "$1" ]
then
GRADEFILES=`ls -p | grep -v /`
cp $GRADEFILES $1/
call cd $1
source $0
rm $GRADEFILES
cd ..
exit 0
fi
}
# A function that echoes and executes a command.
call() {
echo "! ${@/eval/}"
$@
}
# A function that echoes and executes a command.
# If it fails, it sets grade=0.
# If it succeeds, it sets grade=100.
grade_command() {
grade=100
call timeout $TIMEOUT $@
exitcode=$?
if [ $exitcode -ne 0 ]
then
printf "*** Command failed. Exit code: $exitcode ***\n"
grade=0
fi
echo "--- $SECONDS seconds"
}
# A function that echoes and executes a command.
# If it fails, it sets grade=0.
# If it succeeds, it sets grade=[last line of output]
grade_program() {
program=$1
echo "! timeout $TIMEOUT ./$program"
timeout $TIMEOUT ./$program
exitcode=$?
if [ $exitcode -ne 0 ]
then
printf "*** Program failed. Exit code: $exitcode ***\n"
grade=0
else
grade=$( { timeout $TIMEOUT ./$program || echo 0; } | tail -n1 )
fi
echo "--- $SECONDS seconds"
}
# from here: https://stackoverflow.com/a/1115909/827927
swap() {
local TMPFILE=tmp.$$
if [ -f $1 ] && [ -f $2 ]
then
mv $1 $TMPFILE && mv $2 $1 && mv $TMPFILE $2
elif [ -f $1 ]
then
mv $1 $2
elif [ -f $2 ]
then
mv $2 $1
fi
}