816f919917e42a3c9c1a1cca412928d85963c672
[gruntmaster-data.git] / gruntmaster-problem
1 #!/usr/bin/perl -w
2 use v5.14;
3
4 use Gruntmaster::Data;
5
6 use File::Temp qw/tempfile/;
7
8 use IO::Prompter [ -style => 'bold', '-stdio', '-verbatim' ];
9 use File::Slurp qw/read_file write_file/;
10 use JSON qw/decode_json encode_json/;
11 use List::Util qw/max min/;
12 use Term::ANSIColor qw/RED RESET/;
13 use Getopt::Long qw/:config require_order/;
14
15 use constant LEVEL_VALUES => {
16 beginner => 100,
17 easy => 250,
18 medium => 500,
19 hard => 1000,
20 };
21
22 ##################################################
23
24 my $dsn = $ENV{GRUNTMASTER_DSN} // 'dbi:Pg:';
25 my $db = Gruntmaster::Data->connect($dsn);
26
27 sub cmd_help{
28 exec perldoc => $0
29 }
30
31 sub cmd_add{
32 my $id = shift;
33 my $name = prompt 'Problem name';
34 my $private = prompt('Private?', '-yn') eq 'y';
35 my $contest = prompt 'Contest';
36 my $author = prompt 'Problem author (full name)';
37 my $writer = prompt 'Problem statement writer (full name)';
38 my $owner = prompt 'Problem owner (username)';
39 my $level = prompt 'Problem level', -menu => "beginner\neasy\nmedium\nhard";
40 my $value = LEVEL_VALUES->{$level};
41 my $statement = read_file prompt 'File with problem statement', -complete => 'filenames';
42 my $generator = prompt 'Generator', -menu => "File\nRun\nUndef";
43 my $runner = prompt 'Runner', -menu => "File\nVerifier\nInteractive";
44 my $judge = prompt 'Judge', -menu => "Absolute\nPoints";
45 my $testcnt = prompt 'Test count', '-i';
46
47 my $timeout = prompt 'Time limit (seconds)', '-n';
48 my $olimit = prompt 'Output limit (bytes)', '-i';
49 say 'Memory limits are broken, so I won\'t ask you for one';
50
51 my (@tests, $gensource, $genformat, $versource, $verformat);
52
53 if ($generator eq 'Run') {
54 $gensource = read_file prompt, '[Generator::Run] Generator file name', -complete => 'filenames';
55 $genformat = prompt '[Generator::Run] Generator format', -menu => [qw/C CPP MONO JAVA PASCAL PERL PYTHON/];
56 }
57
58 if ($runner eq 'File') {
59 my $default = $judge eq 'Points' ? 10 : 'Ok';
60 $tests[$_ - 1] = prompt "[Runner::File] Score for test ${_} [$default]", -default => $default for 1 .. $testcnt;
61 }
62
63 if ($runner eq 'Verifier' || $runner eq 'Interactive') {
64 say RED, 'WARNING: Runner::Interactive is experimental', RESET if $runner eq 'Interactive';
65 $versource = prompt "[Runner::$runner] Verifier file name", -complete => 'filenames';
66 $verformat = prompt "[Runner::$runner] Verifier format", -menu => [qw/C CPP MONO JAVA PASCAL PERL PYTHON/];
67 }
68
69 my %options = (
70 id => $id,
71 name => $name,
72 level => $level,
73 value => $value,
74 statement => $statement,
75 author => $author,
76 writer => $writer,
77 owner => $owner,
78 generator => $generator,
79 runner => $runner,
80 judge => $judge,
81 testcnt => $testcnt,
82 );
83 $options{private} = $private if $private;
84 $options{timeout} = $timeout if $timeout;
85 $options{olimit} = $olimit if $olimit;
86 $options{tests} = encode_json \@tests if @tests;
87 $options{gensource} = $gensource if $gensource;
88 $options{genformat} = $genformat if $genformat;
89 $options{versource} = $versource if $versource;
90 $options{verformat} = $verformat if $verformat;
91 $db->problems->create (\%options);
92
93 $db->contest_problems->create({problem => $id, contest => $contest}) if $contest;
94 }
95
96 sub cmd_set{
97 my $file;
98 GetOptions ( 'file!' => \$file );
99 my ($id, %values) = @ARGV;
100 %values = map { $_ => scalar read_file $values{$_} } keys %values if $file;
101 $db->problem($id)->update(\%values);
102 }
103
104 sub cmd_get{
105 my ($id, $col) = @_;
106 say $db->problem($id)->get_column($col)
107 }
108
109 sub cmd_edit{
110 my ($id, $col) = @_;
111 my ($fh, $file) = tempfile 'gruntmaster-problem-editXXXX', TMPDIR => 1, UNLINK => 1;
112 write_file $fh, $db->problem($id)->get_column($col);
113 close $fh;
114 my $editor = $ENV{EDITOR} // 'editor';
115 system $editor, $file;
116 $db->problem($id)->update({$col => scalar read_file $file}) or die "$!";
117 }
118
119 sub cmd_list{
120 local $, = "\n";
121 say map {$_->id} $db->problems->all
122 }
123
124 sub cmd_rm{
125 my ($id) = @_;
126 $db->problem($id)->delete;
127 }
128
129 sub cmd_show{
130 my %columns = $db->problem(shift)->get_columns;
131 print <<END
132 Name: $columns{name}
133 Author: $columns{author}
134 Statement written by: $columns{writer}
135 Owner: $columns{owner}
136 Level: $columns{level}
137 Output limit: $columns{olimit}
138 Time limit: $columns{timeout}
139 Test count: $columns{testcnt}
140 Generator: $columns{generator}
141 Runner: $columns{runner}
142 Judge: $columns{judge}
143 Private: $columns{private}
144 END
145 }
146
147 sub cmd_check {
148 my ($id) = @_;
149 my @jobs = $db->jobs->search({problem => $id, reference => { '!=', undef }})->all;
150 say 'Rerunning ' . @jobs . ' reference jobs...';
151 $_->rerun for @jobs;
152 sleep 1 while $db->jobs->search({problem => $id, result_text => undef})->count;
153
154 my (@good_times, @fail_times, $fail);
155
156 for (@jobs) {
157 $_->discard_changes;
158 my @times = map { $_->{time} } @{decode_json $_->results};
159 push @good_times, @times if $_->reference == 0;
160 push @fail_times, @times if $_->reference == 3;
161 if ($_->result == $_->reference) {
162 say 'Job ' . $_->id . ' OK'
163 } else {
164 say 'Job ' . $_->id . ' got ' . $_->result . ' instead of ' . $_->reference;
165 $fail = 1;
166 }
167 }
168
169 say 'Max time for AC: ' . max @good_times if @good_times;
170 say 'Min time for TLE: ' . min @fail_times if @fail_times;
171 say $fail ? 'Test failed' : 'Test successful';
172 exit $fail if $fail;
173 }
174
175 ##################################################
176
177 my $cmd = 'cmd_' . shift;
178 cmd_help unless exists $main::{$cmd};
179 no strict 'refs';
180 $cmd->(@ARGV);
181
182 1;
183 __END__
184
185 =encoding utf-8
186
187 =head1 NAME
188
189 gruntmaster-problem - shell interface to Gruntmaster 6000 problems
190
191 =head1 SYNOPSIS
192
193 gruntmaster-problem add problem_id
194 gruntmaster-problem list
195 gruntmaster-problem rm problem_id
196 gruntmaster-problem show problem_id
197 gruntmaster-problem set [--file] problem_id key value
198 gruntmaster-problem get problem_id key
199 gruntmaster-problem edit problem_id key
200 gruntmaster-problem check problem_id
201
202 =head1 DESCRIPTION
203
204 gruntmaster-problem is a tool for managing problems.
205
206 =over
207
208 =item B<list>
209
210 Prints the list of problems in the selected contest.
211
212 =item B<show> I<id>
213
214 Prints detailed information about problem I<id>.
215
216 =item B<add> I<id>
217
218 Adds a new problem with id I<id>.
219
220 =item B<rm> I<id>
221
222 Removes the problem with id I<id>.
223
224 =item B<set> I<id> I<key> I<value>
225
226 Sets the I<key> configuration option of problem I<id> to I<value>.
227
228 =item B<get> I<id> I<key>
229
230 Get the value of the I<key> configuration option of problem I<id>
231
232 =item B<edit> I<id> I<key>
233
234 Opens an editor with the value of the I<key> configuration option. After the editor exits, the option is updated to the value of the file.
235
236 =item B<set> --file I<id> I<key> I<file>
237
238 Sets the I<key> configuration option of problem I<id> to the contents of the file I<file>.
239
240 =item B<check> I<id>
241
242 Rerun all reference jobs for problem I<id> and check their results.
243
244 =back
245
246 =head1 AUTHOR
247
248 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
249
250 =head1 COPYRIGHT AND LICENSE
251
252 Copyright (C) 2014 by Marius Gavrilescu
253
254 This library is free software; you can redistribute it and/or modify
255 it under the same terms as Perl itself, either Perl version 5.18.1 or,
256 at your option, any later version of Perl 5 you may have available.
257
258
259 =cut
This page took 0.038357 seconds and 3 git commands to generate.