]> mj.ucw.cz Git - moe.git/blob - mop/eval/mo-score.sh
MO: Limit character set of created passwords
[moe.git] / mop / eval / mo-score.sh
1 #!/usr/bin/perl
2 # A generator of score sheets. More ugly than it deserves.
3
4 $debug = 0;
5 $detail = 0;
6 $html = 0;
7 $tex = 0;
8 $extras = 0;
9 $alt = 0;
10 $merged = 0;
11 $usage = "Usage: mo-score [--detail] [--alt] [--extras] [--html] [--tex] [--merged] [<directory>/]<task> ...";
12 while (($arg = $ARGV[0]) =~ /^--([a-z]+)$/) {
13         shift @ARGV;
14         $var = "\$$1";
15         if (!eval "defined $var") { die $usage; }
16         eval "$var = 1;";
17 }
18 @ARGV || die $usage;
19
20 print STDERR "Scanning contestants... ";
21 open (CT, "bin/mo-get-users --full |") || die "Cannot get list of contestants";
22 while (<CT>) {
23         chomp;
24         ($u,$f) = split /\t/;
25         ($u eq "somebody") && next;
26         $users{$u}=$f;
27 }
28 close CT;
29 print STDERR 0+keys %users, "\n";
30
31 print STDERR "Scanning exceptions... ";
32 if ($extras && open (EX, "exceptions")) {
33         while (<EX>) {
34                 chomp;
35                 (/^$/ || /^#/) && next;
36                 @a = split /\s+/;
37                 $u = shift @a;
38                 defined $users{$u} || die "Unknown user $u";
39                 while (@a) { $extra{$u} += shift @a; }
40         }
41         close EX;
42         print STDERR "OK\n";
43 } else { print STDERR "none\n"; }
44
45 print STDERR "Scanning task results... ";
46 %messages = ();
47 %error_codes = ();
48 foreach $u (keys %users) {
49         foreach $task (@ARGV) {
50                 my ($dir, $t) = ("testing", $task);
51                 if ($task =~ m@^(.*)/([^/]*)$@) {
52                         $dir = $1;
53                         $t = $2;
54                 }
55                 $known_tasks{$t} = 1;
56                 $tt = "$dir/$u/$t/points" . ($alt ? ".alt" : "");
57                 -f $tt || next;
58                 print STDERR "$u/$t ";
59                 open (X, $tt) || die "Unable to open $tt";
60                 while (<X>) {
61                         chomp;
62                         /^(\S+) (-?\d+)\s*(.*)/ || die "Parse error: $_";
63                         $ttest = $1;
64                         $tpts = $2;
65                         $trem = $3;
66                         $trem =~ s/\[.*//;
67                         ($ttest_merged = $ttest) =~ s/[^0-9]//g;
68                         $ttest = $ttest_merged if $merged;
69                         $known_tests{$t}{$ttest} = 1;
70                         $cmt = $tpts;
71                         if ($tpts == 0 && $trem ne "OK") {
72                                 if ($trem =~ /^Compile /) { $cmt = "CE"; }
73                                 elsif ($trem =~ /^Time limit exceeded/) { $cmt = "TO"; }
74                                 elsif ($trem =~ /^Exited with error /) { $cmt = "RE"; }
75                                 elsif ($trem =~ /^Caught fatal signal /) { $cmt = "SG"; }
76                                 elsif ($trem =~ /^([A-Za-z])\S*\s+([A-Za-z])/) {
77                                         ($cmt = "$1$2") =~ tr/a-z/A-Z/;
78                                 }
79                                 if (!defined $messages{$trem}) {
80                                         $messages{$trem} = $cmt;
81                                         if (!defined $error_codes{$cmt}) {
82                                                 $error_codes{$cmt} = $trem;
83                                         } else {
84                                                 $error_codes{$cmt} .= ", $trem";
85                                         }
86                                 }
87                         }
88                         if (!defined($results{$u}{$t}{$ttest}) || $results{$u}{$t}{$ttest} > $tpts) {
89                                 $results{$u}{$t}{$ttest} = $tpts;
90                                 $comment{$u}{$t}{$ttest} = $cmt;
91                         }
92                         if (!defined($results_merged{$u}{$t}{$ttest_merged}) || $results_merged{$u}{$t}{$ttest_merged} > $tpts) {
93                                 $results_merged{$u}{$t}{$ttest_merged} = $tpts;
94                         }
95                 }
96                 close X;
97         }
98         foreach my $t (keys %known_tasks) {
99                 $total{$u}{$t} = 0;
100                 foreach my $pts (values %{$results_merged{$u}{$t}}) { $total{$u}{$t} += $pts; }
101         }
102 }
103 print STDERR "OK\n";
104
105 print STDERR "Creating table template... ";
106 @header = ("Rank","User","Name");
107 @body = ('','$u','$users{$u}');
108 @bodysums = ();
109 @footer = ('"Total"','','');
110 if (keys %extra) {
111         push @header, "Extra";
112         push @body, '$extra{$u}';
113         $col = 0+@footer;
114         push @bodysums, $col;
115         push @footer, "sum($col)";
116 }
117 @tasks = ();
118 foreach $task (@ARGV) {
119         my $t = ($task =~ m@/([^/]*)$@) ? $1 : $task;
120         defined $known_tasks{$t} || die "Unknown task $t";
121         push @tasks, $t;
122         push @header, substr($t, 0, 4);
123         push @body, "(\$xx = \$total{\$u}{'$t'}) > 0 ? \$xx : 0";
124         $col = 0+@footer;
125         push @footer, "sum($col)";
126         push @bodysums, $col;
127         if ($detail) {
128                 foreach $s (sort { $a <=> $b } keys %{$known_tests{$t}}) {
129                         push @header, "$s";
130                         push @body, "\$comment{\$u}{'$t'}{'$s'}";
131                         $col = 0+@footer;
132                         push @footer, "sum($col)";
133                 }
134         }
135 }
136 push @header, "Total";
137 push @body, join('+', map { $_ = "\$$_" } @bodysums);
138 $col = 0+@footer;
139 push @footer, "sum($col)";
140 print STDERR "OK\n";
141
142 print STDERR "h: ", join(':',@header), "\n" if $debug;
143 print STDERR "b: ", join(':',@body), "\n" if $debug;
144 print STDERR "f: ", join(':',@footer), "\n" if $debug;
145
146 print STDERR "Filling in results... ";
147 @table = ();
148 foreach $u (keys %users) {
149         $row = [];
150         foreach my $c (@body) {
151                 $c =~ s/\$(\d+)/\$\$row[$1]/g;
152                 $x = eval $c;
153                 push @$row, (defined $x ? $x : '-');
154         }
155         print STDERR "row: ", join(':',@$row), "\n" if $debug;
156         push @table, $row;
157 }
158 print STDERR "OK\n";
159
160 print STDERR "Sorting... ";
161 $sortcol = @{$table[0]} - 1;
162 $namecol = 2;
163 @table = sort {
164         my $p, $an, $bn;
165         $p = $$b[$sortcol] <=> $$a[$sortcol];
166         ($an = $$a[$namecol]) =~ s/(\S+)\s+(\S+)/$2 $1/;
167         ($bn = $$b[$namecol]) =~ s/(\S+)\s+(\S+)/$2 $1/;
168         $p ? $p : ($an cmp $bn);
169 } @table;
170 $i=0;
171 while ($i < @table) {
172         $j = $i;
173         while ($i < @table && ${$table[$i]}[$sortcol] == ${$table[$j]}[$sortcol]) {
174                 $i++;
175         }
176         if ($i == $j+1) {
177                 ${table[$j]}[0] = "$i.";
178         } else {
179                 ${table[$j]}[0] = $j+1 . ".-" . $i . ".";
180                 $j++;
181                 while ($j < $i) { ${table[$j++]}[0] = ""; };
182         }
183 }
184 print STDERR "OK\n";
185
186 print STDERR "Attaching headers and footers... ";
187 sub sum { my $col=shift @_; my $t=0; foreach my $z (0..@table-1) { $t += ${$table[$z]}[$col]; } $t; }
188 map { $_ = eval $_; } @footer;
189 push @table, \@footer;
190 unshift @table, \@header;
191 print STDERR "OK\n";
192
193 if ($debug) {
194         foreach $r (@table) { print join(':',@$r), "\n"; }
195 } elsif ($html) {
196         print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html40/strict.dtd">', "\n";
197         print "<HTML><HEAD><TITLE>Rank list</TITLE></HEAD><BODY>\n";
198         print "<H1>Rank list</H1>\n";
199
200         my @perm;
201         &printHtmlHeader(\@perm);
202         print "<tbody>";
203
204         foreach $r (@table[1..($#table - 1)]) {
205                 &printHtmlRow(@{$r}[@perm]);
206         }
207
208         print "<tbody>";
209         &printHtmlRow(@{$table[$#table]}[@perm]);
210
211         print "</TABLE>\n";
212         if ($detail) {
213                 print "<H2>Error codes</H2><UL>\n";
214                 foreach $r (sort keys %error_codes) { print "<LI>$r: $error_codes{$r}\n"; }
215                 print "</UL>\n";
216         }
217         print "</BODY></HTML>\n";
218 } elsif ($tex) {
219         print "\\error{TeX output not supported yet!}\n";
220 } else {
221         foreach $r (@table) { print join("\t",@$r), "\n"; }
222         print "\n";
223         foreach $r (sort keys %error_codes) { print "$r: $error_codes{$r}\n"; }
224 }
225
226
227 sub printHtmlRow {
228         print "<TR>", join('',map {
229                 if ($hdr) { $_ = "<TH>$_"; }
230                 else { $_ = "<TD align=" . (/^[0-9A-Z-]+$/ ? "right" : "left") . (length($_) > 14 ? " width=150" : "") . ">$_"; }
231         } @_), "\n";
232 }
233
234
235 sub printHtmlHeader {
236
237   my ($perm) = @_;
238
239    my $colspec = "<colgroup span=3>";
240    my $hdr1;
241    my $hdr2;
242
243    @$perm = (0, 1, 2);
244    my $p = 3;
245
246    if ($detail) {
247      $hdr1 = "<th rowspan=2>Rank<th rowspan=2>User<th rowspan=2>Name";
248      $extras and $p++ and push @$perm, 3 and $hdr1.="<th rowspan=2>Extra" and $colspec.="<colgroup span=1>";         ##Extra hack
249      for my $task (@tasks) {
250
251         my $nSub = scalar(keys %{$known_tests{$task}});
252
253         $p++;
254         map { push @$perm, $p++ } (1..$nSub);
255         push @$perm, $p - $nSub - 1;
256
257         $colspec .= "<colgroup span='" . $nSub . "'>\n";
258         $colspec .= "<colgroup span='1'>\n";
259         $hdr1 .= "<th colspan='" . ($nSub + 1) . "' style='border-bottom:1px solid black;'>$task";
260         $hdr2 .= join("", map { "<th>$_" } sort {$a <=> $b} keys %{$known_tests{$task}});
261         $hdr2 .= "<th>Total";
262      }
263
264      $hdr1 .= "<th rowspan='2'>Total";
265
266    } else {  ## no detail
267
268      $hdr1 = "<th>Rank<th>User<th>Name";
269      $extras and $p++ and push @$perm, 3 and $hdr1.="<th>Extra" and $colspec.="<colgroup span=1>";                  ##Extra hack
270
271      for my $task (@tasks) {
272         push @$perm, $p++;
273         $hdr1 .= "<th>$task";
274      }
275      $hdr1 .= "<th>Total";
276      $colspec .= "<colgroup span='" . scalar (@tasks) . "'>";
277    }
278
279    push @$perm, $p++;
280
281    print "<TABLE rules=groups frame=all border='1' cellpadding='2'>\n";
282    print "$colspec<colgroup span='1'>\n";
283    print "<tr>$hdr1</tr>\n";
284    print "<tr>$hdr2</tr>\n" if $detail;
285
286 }