]> mj.ucw.cz Git - moe.git/blob - bin/lib
Enable dot-file globbing.
[moe.git] / bin / lib
1 # The Evaluator -- Shell Function Library
2 # (c) 2001 Martin Mares <mj@ucw.cz>
3
4 # General settings
5 shopt -s dotglob
6
7 # Logging functions.
8 # File handles used: fd1=log, fd2=progress
9
10 function log-init
11 {
12         exec >>$TDIR/log
13         HAVE_LOG=1
14 }
15
16 function pstart
17 {
18         echo >&2 -n "$@"
19 }
20
21 function pcont
22 {
23         echo >&2 -n "$@"
24 }
25
26 function pend
27 {
28         echo >&2 "$@"
29 }
30
31 function die
32 {
33         echo >&2 "$@"
34         [ -n "$HAVE_LOG" ] && echo "Fatal error: $@"
35         exit 1
36 }
37
38 # Sandbox subroutines
39
40 function box-init
41 {
42         pstart "Preparing sandbox... "
43         [ -n "$TEST_USER" ] || die "TEST_USER not set. Please fix."
44         if [ $TEST_USER == $EVAL_USER ] ; then
45                 pcont "running locally (INSECURE), "
46                 BOXDIR=box
47                 BOXCMD=bin/box
48                 mkdir -p box
49         else
50                 pcont "used account $TEST_USER, "
51                 BOXDIR=$MO_ROOT/$TEST_USER
52                 BOXCMD=bin/box-$TEST_USER
53         fi
54         [ -d $BOXDIR -a -f $BOXCMD ] || die "Sandbox set up incorrectly"
55         BOXCMD="$BOXCMD -c$BOXDIR"
56         echo "Sandbox directory: $BOXDIR"
57         echo "Sandbox command: $BOXCMD"
58         box-clean
59         pend "OK"
60 }
61
62 function box-clean
63 {
64         [ -n "$BOXCMD" ] || die "box-init not called"
65         rm -rf $BOXDIR/*
66 }
67
68 # Initialization of testing directories
69
70 function dir-init
71 {
72         pstart "Initializing... "
73         PDIR=problems/$PROBLEM
74         SDIR=solutions/$CONTESTANT/$PROBLEM
75         TDIR=testing/$CONTESTANT/$PROBLEM
76         [ -d $PDIR ] || die "Problem $PROBLEM not known"
77         [ -d $SDIR ] || die "Solution of $PROBLEM not found"
78         mkdir -p $TDIR
79         rm -rf $TDIR
80         cp -a $SDIR $TDIR
81         cat >$TDIR/log <<EOF
82 Testing solution of $PROBLEM by $CONTESTANT
83 Test started at `date`
84 Contestant's solution directory: $SDIR
85 Problem directory: $PDIR
86 Testing directory: $TDIR
87 EOF
88         >$TDIR/points
89         pend "OK"
90 }
91
92 # Locating source file in SDIR, pass name in SRCN (without path) and extension in SRCEXT
93
94 function locate-source
95 {
96         pstart "Locating source... "
97         for a in $EXTENSIONS ; do
98                 if [ -f $SDIR/$PROBLEM.$a ] ; then
99                         [ -z "$SRCN" ] || die "Multiple source files found: $SDIR/$PROBLEM.$a and $SDIR/$SRCN. Please fix."
100                         SRCN=$PROBLEM.$a
101                         SRCEXT=$a
102                 fi
103         done
104         [ -n "$SRCN" ] || die "NOT FOUND"
105         pend $SRCN
106         echo "Found source file: $SDIR/$SRCN"
107 }
108
109 # Compilation (compile SDIR/SRCN with PDIR/EXTRAS to EXE=TDIR/PROBLEM)
110
111 function compile
112 {
113         pstart "Compiling... "
114         cp -a $SDIR/$SRCN $TDIR/$SRCN
115         if [ -n "$EXTRAS" ] ; then
116                 echo "Extras: $EXTRAS"
117                 for a in $EXTRAS ; do cp $PDIR/$a $TDIR/ ; done
118         fi
119         box-clean
120         for a in $SRCN $EXTRAS ; do cp $TDIR/$a $BOXDIR/ ; done
121         SRC=$SRCN
122         EXE=$PROBLEM
123         CCMD=COMP_$SRCEXT
124         CCMD="`eval echo ${!CCMD}`"
125         COMP_SANDBOX_OPTS="`eval echo $COMP_SANDBOX_OPTS`"
126         echo "Compiler command: $CCMD"
127         echo "Compiler sandbox options: $COMP_SANDBOX_OPTS"
128
129         echo "Compiler input files:"
130         ls -Al $BOXDIR
131         if ! $BOXCMD $COMP_SANDBOX_OPTS -- $CCMD 2>$TDIR/compile.out ; then
132                 COMPILE_MSG="`cat $TDIR/compile.out`"
133                 pend "FAILED: $COMPILE_MSG"
134                 echo "$COMPILE_MSG"
135                 return 1
136         fi
137         cat $TDIR/compile.out
138         rm $TDIR/compile.out
139         echo "Compiler output files:"
140         ls -Al $BOXDIR
141         if [ ! -f $BOXDIR/$PROBLEM ] ; then
142                 pend "FAILED: Missing executable file"
143                 echo "Missing executable file"
144                 return 1
145         fi
146         EXE=$TDIR/$PROBLEM
147         cp -a $BOXDIR/$PROBLEM $EXE
148         echo "Compiled OK, result copied to $EXE"
149         pend "OK"
150 }
151
152 # Running of test program with file input/output
153
154 function test-run-with-files
155 {
156         pcont "<init> "
157         box-clean
158         echo "Executable file: $TDIR/$PROBLEM"
159         cp $TDIR/$PROBLEM $BOXDIR/
160         echo "Input: $TDIR/$PROBLEM"
161         cp $PDIR/$TEST.in $BOXDIR/$PROBLEM.in
162         echo "Input files:"
163         ls -Al $BOXDIR
164
165         pcont "<run> "
166         echo "Timeout: $TIME_LIMIT s"
167         echo "Memory: $MEM_LIMIT KB"
168         BOXOPTS="`eval echo $TEST_SANDBOX_OPTS`"
169         echo "Sandbox options: $BOXOPTS"
170         if ! $BOXCMD $BOXOPTS -- ./$PROBLEM 2>$TDIR/exec.out ; then
171                 TEST_MSG="`cat $TDIR/exec.out`"
172                 pend "$TEST_MSG"
173                 echo "$TEST_MSG"
174                 echo >$PTSFILE "0 $TEST_MSG"
175                 return 1
176         fi
177         cat $TDIR/exec.out
178         rm $TDIR/exec.out
179         echo "Output files:"
180         ls -Al $BOXDIR
181         if [ ! -s $BOXDIR/$PROBLEM.out ] ; then
182                 pend "No output file."
183                 echo "No output file."
184                 echo >$PTSFILE "0 No output."
185                 return 1
186         fi
187         cp $BOXDIR/$PROBLEM.out $TDIR/$TEST.out
188 }
189
190 # Syntax checks
191
192 function syntax-check
193 {
194         [ -n "$SYNTAX_CHECK" ] || return 0
195         pcont "<syntax> "
196         SCHECK="`eval echo $SYNTAX_CHECK`"
197         echo "Syntax check command: $SCHECK"
198         $SCHECK && return 0
199         pend "Wrong syntax."
200         echo "Wrong syntax."
201         echo >$PTSFILE "0 Wrong syntax."
202         return 1
203 }
204
205 # Output checks
206
207 function output-check
208 {
209         pcont "<check> "
210         ln $PDIR/$TEST.out $TDIR/$TEST.ok
211         OCHECK="`eval echo $OUTPUT_CHECK`"
212         echo "Output check command: $OCHECK"
213         $OCHECK && return 0
214         pend "Wrong answer."
215         echo "Wrong answer."
216         echo >$PTSFILE "0 Wrong answer."
217         return 1
218 }