]> mj.ucw.cz Git - eval.git/blob - bin/lib
Pick the _last_ line of stderr as the status line and log everything.
[eval.git] / bin / lib
1 # The Evaluator -- Shell Function Library
2 # (c) 2001--2004 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         # Report an internal error
34         echo >&2 "$@"
35         [ -n "$HAVE_LOG" ] && echo "Fatal error: $@"
36         exit 2
37 }
38
39 function fatal
40 {
41         # Report a fatal error in the program being tested
42         echo >&2 "$@"
43         [ -n "$HAVE_LOG" ] && echo "Fatal error: $@"
44         exit 1
45 }
46
47 # Sandbox subroutines
48
49 function box-init
50 {
51         pstart "Preparing sandbox... "
52         if [ -z "$TEST_USER" -o "$TEST_USER" == $EVAL_USER ] ; then
53                 pcont "running locally (INSECURE), "
54                 TEST_USER=$EVAL_USER
55                 BOXDIR=box
56                 BOXCMD=bin/box
57                 mkdir -p box
58         else
59                 pcont "used account $TEST_USER, "
60                 BOXDIR=$MO_ROOT/eval/$TEST_USER
61                 BOXCMD=bin/box-$TEST_USER
62         fi
63         [ -d $BOXDIR -a -f $BOXCMD ] || die "Sandbox set up incorrectly"
64         BOXCMD="$BOXCMD -c$BOXDIR"
65         echo "Sandbox directory: $BOXDIR"
66         echo "Sandbox command: $BOXCMD"
67         box-clean
68         pend "OK"
69 }
70
71 function box-clean
72 {
73         [ -n "$BOXCMD" ] || die "box-init not called"
74         rm -rf $BOXDIR/*
75 }
76
77 # Initialization of testing directories
78
79 function dir-init
80 {
81         pstart "Initializing... "
82         HDIR=.
83         PDIR=problems/$PROBLEM
84         SDIR=solutions/$CONTESTANT/$PROBLEM
85         TDIR=testing/$CONTESTANT/$PROBLEM
86         TMPDIR=tmp
87         [ -d $PDIR ] || die "Problem $PROBLEM not known"
88         [ -d $SDIR ] || fatal "Solution of $PROBLEM not found"
89         mkdir -p $TDIR $TMPDIR
90         rm -rf $TDIR $TMPDIR
91         mkdir -p $TDIR $TMPDIR
92         cat >$TDIR/log <<EOF
93 Testing solution of $PROBLEM by $CONTESTANT
94 Test started at `date`
95 Contestant's solution directory: $SDIR
96 Problem directory: $PDIR
97 Testing directory: $TDIR
98 EOF
99         pend "OK"
100 }
101
102 # Locating source file in SDIR, pass name in SRCN (without path) and extension in SRCEXT
103
104 function locate-source
105 {
106         pstart "Finding source... "
107         SBASE=${1:-$PROBLEM}
108         for a in $EXTENSIONS ; do
109                 if [ -f $SDIR/$SBASE.$a ] ; then
110                         [ -z "$SRCN" ] || die "Multiple source files found: $SDIR/$PROBLEM.$a and $SDIR/$SRCN. Please fix."
111                         SRCN=$SBASE.$a
112                         SRCEXT=$a
113                 fi
114         done
115         [ -n "$SRCN" ] || fatal "NOT FOUND"
116         pend $SRCN
117         echo "Found source file: $SDIR/$SRCN"
118 }
119
120 # Compilation (compile SDIR/SRCN with PDIR/COMP_EXTRAS to EXE=TDIR/PROBLEM)
121
122 function compile
123 {
124         pstart "Compiling... "
125         cp -a $SDIR/$SRCN $TDIR/$SRCN
126         if [ -n "$COMP_EXTRAS" ] ; then
127                 echo "Extras: $COMP_EXTRAS"
128                 for a in $COMP_EXTRAS ; do cp $PDIR/$a $TDIR/ ; done
129         fi
130         box-clean
131         for a in $SRCN $COMP_EXTRAS ; do cp $TDIR/$a $BOXDIR/ ; done
132         SRC=$SRCN
133         EXE=$PROBLEM
134         CCMD=COMP_$SRCEXT
135         CCMD=`eval echo ${!CCMD}`
136         COMP_SANDBOX_OPTS=`eval echo $COMP_SANDBOX_OPTS`
137         echo "Compiler command: $CCMD"
138         echo "Compiler sandbox options: $COMP_SANDBOX_OPTS"
139         eval $COMP_SANDBOX_INIT
140
141         echo "Compiler input files:"
142         ls -Al $BOXDIR
143         echo "Compiler output:"
144         if ! $BOXCMD $COMP_SANDBOX_OPTS -- $CCMD 2>$TDIR/compile.out ; then
145                 COMPILE_MSG="`cat $TDIR/compile.out`"
146                 pend "FAILED: $COMPILE_MSG"
147                 echo "$COMPILE_MSG"
148                 return 1
149         fi
150         cat $TDIR/compile.out
151         rm $TDIR/compile.out
152         echo "Compiler output files:"
153         ls -Al $BOXDIR
154         if [ ! -f $BOXDIR/$PROBLEM ] ; then
155                 pend "FAILED: Missing executable file"
156                 echo "Missing executable file"
157                 return 1
158         fi
159         EXE=$TDIR/$PROBLEM
160         cp -a $BOXDIR/$PROBLEM $EXE
161         echo "Compiled OK, result copied to $EXE"
162         pend "OK"
163 }
164
165 # Running of test program according to current task type (returns exit code and TEST_MSG)
166
167 function test-run
168 {
169         test-run-$TASK_TYPE
170 }
171
172 function test-result
173 {
174         P=$1
175         M=$2
176         if [ -s $TDIR/$TEST.pts ] ; then
177                 P=`cat $TDIR/$TEST.pts`
178                 rm $TDIR/$TEST.pts
179         fi
180         echo "Verdict: $M"
181         echo "Points: $P"
182         test-verdict $P "$M"
183 }
184
185 function test-prolog
186 {
187         pcont "<init> "
188         box-clean
189         echo "Executable file: $TDIR/$PROBLEM"
190         if [ ! -x $TDIR/$PROBLEM ] ; then
191                 test-result 0 "Compile error."
192         fi
193         cp $TDIR/$PROBLEM $BOXDIR/
194         BOX_EXTRAS=
195         IN_TYPE=${IN_TYPE:-$IO_TYPE}
196         OUT_TYPE=${OUT_TYPE:-$IO_TYPE}
197         case $IN_TYPE in
198                 file)   echo "Input file: $PROBLEM.in (from $PDIR/$TEST.in)"
199                         ln $PDIR/$TEST.in $TDIR/$TEST.in
200                         cp $PDIR/$TEST.in $BOXDIR/$PROBLEM.in
201                         BOX_EXTRAS="$BOX_EXTRAS -i/dev/null"
202                         ;;
203                 stdio)  echo "Input file: <stdin> (from $PDIR/$TEST.in)"
204                         ln $PDIR/$TEST.in $TDIR/$TEST.in
205                         BOX_EXTRAS="$BOX_EXTRAS -i$TDIR/$TEST.in"
206                         ;;
207                 none)   echo "Input file: <none>"
208                         ;;
209                 *)      die "Unknown IN_TYPE $IN_TYPE"
210                         ;;
211         esac
212         case $OUT_TYPE in
213                 file)   echo "Output file: $PROBLEM.out"
214                         BOX_EXTRAS="$BOX_EXTRAS -o/dev/null"
215                         ;;
216                 stdio)  echo "Output file: <stdout>"
217                         BOX_EXTRAS="$BOX_EXTRAS -o$TDIR/$TEST.out"
218                         ;;
219                 none)   echo "Output file: <none>"
220                         ;;
221                 *)      die "Unknown OUT_TYPE $OUT_TYPE"
222                         ;;
223         esac
224         echo "Timeout: $TIME_LIMIT s"
225         echo "Memory: $MEM_LIMIT KB"
226         eval $SANDBOX_INIT
227         echo "Sandbox contents before start:"
228         ls -Al $BOXDIR
229 }
230
231 function test-epilog
232 {
233         echo "Sandbox contents after exit:"
234         ls -Al $BOXDIR
235         case ${OUT_TYPE:-$IO_TYPE} in
236                 file)   [ -f $BOXDIR/$PROBLEM.out ] || test-result 0 "No output file."
237                         cp $BOXDIR/$PROBLEM.out $TDIR/$TEST.out
238                         ;;
239                 stdio)  [ -f $TDIR/$TEST.out ] || test-result 0 "No output file."
240                         ;;
241         esac
242 }
243
244 # Running of test program with file input/output
245
246 function test-run-file
247 {
248         test-prolog
249         pcont "<run> "
250         BOXOPTS="`eval echo $TEST_SANDBOX_OPTS`$BOX_EXTRAS"
251         echo "Sandbox options: $BOXOPTS"
252         if ! $BOXCMD $BOXOPTS -- ./$PROBLEM 2>$TMPDIR/exec.out ; then
253                 cat $TMPDIR/exec.out
254                 MSG=`tail -1 $TMPDIR/exec.out`
255                 test-result 0 "$MSG"
256         fi
257         cat $TMPDIR/exec.out
258         test-epilog
259 }
260
261 # Running of interactive test programs
262
263 function test-run-interactive
264 {
265         test-prolog
266         pcont "<run> "
267         BOXOPTS="`eval echo $TEST_SANDBOX_OPTS`$BOX_EXTRAS"
268         echo "Sandbox options: $BOXOPTS"
269         ICCMD=`eval echo $IA_CHECK`
270         echo "Interactive checker: $ICCMD"
271         if ! $HDIR/bin/iwrapper $BOXCMD $BOXOPTS -- ./$PROBLEM @@ $ICCMD 2>$TMPDIR/exec.out ; then
272                 cat $TMPDIR/exec.out
273                 MSG="`tail -1 $TMPDIR/exec.out`"
274                 test-result 0 "$MSG"
275         fi
276         cat $TMPDIR/exec.out
277         test-epilog
278 }
279
280 # Syntax checks
281
282 function syntax-check
283 {
284         [ -n "$SYNTAX_CHECK" ] || return 0
285         pcont "<syntax> "
286         SCHECK=`eval echo $SYNTAX_CHECK`
287         echo "Syntax check command: $SCHECK"
288         if ! eval $SCHECK 2>$TMPDIR/exec.out ; then
289                 cat $TMPDIR/exec.out
290                 MSG=`tail -1 $TMPDIR/exec.out`
291                 if [ -z "$MSG" ] ; then MSG="Wrong syntax." ; fi
292                 test-result 0 "$MSG"
293         fi
294         cat $TMPDIR/exec.out
295 }
296
297 # Output checks
298
299 function output-check
300 {
301         [ -n "$OUTPUT_CHECK" ] || return 0
302         pcont "<check> "
303         [ -f $PDIR/$TEST.out ] && ln $PDIR/$TEST.out $TDIR/$TEST.ok
304         OCHECK=`eval echo $OUTPUT_CHECK`
305         echo "Output check command: $OCHECK"
306         if ! eval $OCHECK 2>$TMPDIR/exec.out ; then
307                 cat $TMPDIR/exec.out
308                 MSG=`tail -1 $TMPDIR/exec.out`
309                 if [ -z "$MSG" ] ; then MSG="Wrong answer." ; fi
310                 test-result 0 "$MSG"
311         fi
312         cat $TMPDIR/exec.out
313         MSG=`tail -1 $TMPDIR/exec.out`
314         if [ -z "$MSG" ] ; then MSG="OK" ; fi
315         test-result $POINTS_PER_TEST "$MSG"
316 }
317
318 # Setup of public commands
319
320 function public-setup
321 {
322         HDIR=$MO_PUBLIC
323         PDIR=$MO_PUBLIC/problems/$PROBLEM
324         SDIR=.
325         TDIR=~/.test
326         TMPDIR=~/.test
327         [ -d $PDIR ] || die "Unknown problem $PROBLEM"
328
329         pstart "Initializing... "
330         mkdir -p $TDIR
331         rm -rf $TDIR/*
332         BOXDIR=~/.box
333         mkdir -p $BOXDIR
334         rm -rf $BOXDIR/*
335         BOXCMD="$MO_PUBLIC/bin/box -c$BOXDIR"
336         exec >log
337         pend "OK  (see 'log' for details)"
338 }
339
340 # Locate output of open data problem, test case TEST
341
342 function open-locate
343 {
344         [ -f $PDIR/$TEST.in ] || die "Unknown test $TEST"
345         SRCN=$SDIR/$PROBLEM$TEST.out
346         [ -f $SRCN ] || fatal "Output file $SRCN not found"
347 }