use v5.14;
use Gruntmaster::Data;
-use Gruntmaster::Page::Submit;
+
+use File::Temp qw/tempfile/;
use IO::Prompter [ -style => 'bold', '-stdio', '-verbatim' ];
-use File::Slurp qw/read_file/;
+use File::Slurp qw/read_file write_file/;
+use JSON qw/decode_json encode_json/;
+use List::Util qw/max min/;
use Term::ANSIColor qw/RED RESET/;
-use Getopt::Long qw/GetOptions/;
+use Getopt::Long qw/:config require_order/;
+
+use constant LEVEL_VALUES => {
+ beginner => 100,
+ easy => 250,
+ medium => 500,
+ hard => 1000,
+};
##################################################
-my $contest;
+my $dsn = $ENV{GRUNTMASTER_DSN} // 'dbi:Pg:';
+my $db = Gruntmaster::Data->connect($dsn);
sub cmd_help{
exec perldoc => $0
}
-sub prompt_file{
- my ($meta, $name, $prefix) = @_;
- my $filename = prompt '$prefix filename', -complete => 'filenames';
- $meta->{files}{$name}{content} = read_file $filename;
- $meta->{files}{$name}{format} = prompt '$prefix format', -menu => Gruntmaster::Page::Submit::FORMATS;
- $meta->{files}{$name}{name} = prompt "$prefix filename [$filename]", -default => $filename;
-}
-
sub cmd_add{
my $id = shift;
my $name = prompt 'Problem name';
+ my $private = prompt('Private?', '-yn') eq 'y';
+ my $contest = prompt 'Contest';
my $author = prompt 'Problem author (full name)';
+ my $writer = prompt 'Problem statement writer (full name)';
my $owner = prompt 'Problem owner (username)';
my $level = prompt 'Problem level', -menu => "beginner\neasy\nmedium\nhard";
+ my $value = LEVEL_VALUES->{$level};
my $statement = read_file prompt 'File with problem statement', -complete => 'filenames';
- my %meta;
- $meta{generator} = prompt 'Generator', -menu => "File\nRun\nUndef";
- $meta{runner} = prompt 'Runner', -menu => "File\nVerifier\nInteractive";
- $meta{judge} = prompt 'Judge', -menu => "Absolute\nPoints";
- $meta{testcnt} = prompt 'Test count', '-i';
-
- $meta{timeout} = prompt 'Time limit (seconds)', '-n';
- delete $meta{timeout} unless $meta{timeout};
- $meta{olimit} = prompt 'Output limit (bytes)', '-i';
- delete $meta{olimit} unless $meta{olimit};
- say 'Memory limits are broken, so I won\'t ask you for one';
+ my $generator = prompt 'Generator', -menu => "File\nRun\nUndef";
+ my $runner = prompt 'Runner', -menu => "File\nVerifier\nInteractive";
+ my $judge = prompt 'Judge', -menu => "Absolute\nPoints";
+ my $testcnt = prompt 'Test count', '-i';
- if ($meta{generator} eq 'File') {
- my $prefix = prompt '[Generator::File] Input file prefix';
- $meta{infile}[$_ - 1] = read_file "$prefix$_.in" for 1 .. $meta{testcnt};
- }
+ my $timeout = prompt 'Time limit (seconds)', '-n';
+ my $olimit = prompt 'Output limit (bytes)', '-i';
+ say 'Memory limits are broken, so I won\'t ask you for one';
- prompt_file \%meta, gen => '[Generator::Run] Generator' if $meta{generator} eq 'Run';
+ my (@tests, $gensource, $genformat, $versource, $verformat);
- if ($meta{runner} eq 'File') {
- my $prefix = prompt '[Runner::File] Output file prefix';
- $meta{okfile}[$_ - 1] = read_file "$prefix$_.ok" for 1 .. $meta{testcnt};
- $meta{tests}[$_ - 1] = prompt "[Runner::File] Score for test ${_} [10]", '-i', -default => 10 for 1 .. $meta{testcnt};
+ if ($generator eq 'Run') {
+ $gensource = read_file prompt, '[Generator::Run] Generator file name', -complete => 'filenames';
+ $genformat = prompt '[Generator::Run] Generator format', -menu => [qw/C CPP MONO JAVA PASCAL PERL PYTHON/];
}
- prompt_file \%meta, ver => '[Runner::Verifier] Verifier' if $meta{runner} eq 'Verifier';
+ if ($runner eq 'File') {
+ my $default = $judge eq 'Points' ? 10 : 'Ok';
+ $tests[$_ - 1] = prompt "[Runner::File] Score for test ${_} [$default]", -default => $default for 1 .. $testcnt;
+ }
- if ($meta{runner} eq 'Interactive') {
- say RED, 'WARNING: Runner::Interactive is experimental', RESET;
- prompt_file int => '[Runner::Interactive] Interactive verifier';
+ if ($runner eq 'Verifier' || $runner eq 'Interactive') {
+ say RED, 'WARNING: Runner::Interactive is experimental', RESET if $runner eq 'Interactive';
+ $versource = prompt "[Runner::$runner] Verifier file name", -complete => 'filenames';
+ $verformat = prompt "[Runner::$runner] Verifier format", -menu => [qw/C CPP MONO JAVA PASCAL PERL PYTHON/];
}
- insert_problem $id => name => $name, level => $level, statement => $statement, author => $author, owner => $owner;
- set_problem_meta $id => \%meta;
- PUBLISH genpage => $contest ? "ct/$contest/pb/index.html" : 'pb/index.html';
- PUBLISH genpage => $contest ? "ct/$contest/pb/$id.html" : "pb/$id.html";
+ my %options = (
+ id => $id,
+ name => $name,
+ level => $level,
+ value => $value,
+ statement => $statement,
+ author => $author,
+ writer => $writer,
+ owner => $owner,
+ generator => $generator,
+ runner => $runner,
+ judge => $judge,
+ testcnt => $testcnt,
+ );
+ $options{private} = $private if $private;
+ $options{timeout} = $timeout if $timeout;
+ $options{olimit} = $olimit if $olimit;
+ $options{tests} = encode_json \@tests if @tests;
+ $options{gensource} = $gensource if $gensource;
+ $options{genformat} = $genformat if $genformat;
+ $options{versource} = $versource if $versource;
+ $options{verformat} = $verformat if $verformat;
+ $db->problems->create (\%options);
+
+ $db->contest_problems->create({problem => $id, contest => $contest}) if $contest;
}
sub cmd_set{
GetOptions ( 'file!' => \$file );
my ($id, %values) = @ARGV;
%values = map { $_ => scalar read_file $values{$_} } keys %values if $file;
- edit_problem $id => %values;
- PUBLISH genpage => 'pb/index.html';
- PUBLISH genpage => "pb/$id.html";
+ $db->problem($id)->update(\%values);
+}
+
+sub cmd_get{
+ my ($id, $col) = @_;
+ say $db->problem($id)->get_column($col)
+}
+
+sub cmd_edit{
+ my ($id, $col) = @_;
+ my ($fh, $file) = tempfile 'gruntmaster-problem-editXXXX', TMPDIR => 1, UNLINK => 1;
+ write_file $fh, $db->problem($id)->get_column($col);
+ close $fh;
+ my $editor = $ENV{EDITOR} // 'editor';
+ system $editor, $file;
+ $db->problem($id)->update({$col => scalar read_file $file}) or die "$!";
}
sub cmd_list{
local $, = "\n";
- say problems;
+ say map {$_->id} $db->problems->all
}
sub cmd_rm{
- remove_problem shift;
- PUBLISH genpage => $contest ? "ct/$contest/pb/index.html" : 'pb/index.html';
+ my ($id) = @_;
+ $db->problem($id)->delete;
}
sub cmd_show{
- local $_ = shift or goto &cmd_list;
+ my %columns = $db->problem(shift)->get_columns;
+ print <<END
+Name: $columns{name}
+Author: $columns{author}
+Statement written by: $columns{writer}
+Owner: $columns{owner}
+Level: $columns{level}
+Output limit: $columns{olimit}
+Time limit: $columns{timeout}
+Test count: $columns{testcnt}
+Generator: $columns{generator}
+Runner: $columns{runner}
+Judge: $columns{judge}
+Private: $columns{private}
+END
+}
+
+sub cmd_check {
+ my ($id) = @_;
+ my @jobs = $db->jobs->search({problem => $id, reference => { '!=', undef }})->all;
+ say 'Rerunning ' . @jobs . ' reference jobs...';
+ $_->rerun for @jobs;
+ sleep 1 while $db->jobs->search({problem => $id, result_text => undef})->count;
+
+ my (@good_times, @fail_times, $fail);
+
+ for (@jobs) {
+ $_->discard_changes;
+ my @times = map { $_->{time} } @{decode_json $_->results};
+ push @good_times, @times if $_->reference == 0;
+ push @fail_times, @times if $_->reference == 3;
+ if ($_->result == $_->reference) {
+ say 'Job ' . $_->id . ' OK'
+ } else {
+ say 'Job ' . $_->id . ' got ' . $_->result . ' instead of ' . $_->reference;
+ $fail = 1;
+ }
+ }
+
+ say 'Max time for AC: ' . max @good_times if @good_times;
+ say 'Min time for TLE: ' . min @fail_times if @fail_times;
+ say $fail ? 'Test failed' : 'Test successful';
+ exit $fail if $fail;
}
##################################################
-GetOptions ( 'contest=s' => \$contest );
-local $Gruntmaster::Data::contest = $contest;
my $cmd = 'cmd_' . shift;
cmd_help unless exists $main::{$cmd};
no strict 'refs';
-$cmd->(@ARGV) if exists $main::{$cmd};
+$cmd->(@ARGV);
1;
__END__
=head1 SYNOPSIS
- gruntmaster-problem [--contest=mycontest] add problem_id
- gruntmaster-problem [--contest=mycontest] list
- gruntmaster-problem [--contest=mycontest] rm problem_id
- gruntmaster-problem [--contest=mycontest] show problem_id
- gruntmaster-problem [--contest=mycontest] set [--file] problem_id key value
+ gruntmaster-problem add problem_id
+ gruntmaster-problem list
+ gruntmaster-problem rm problem_id
+ gruntmaster-problem show problem_id
+ gruntmaster-problem set [--file] problem_id key value
+ gruntmaster-problem get problem_id key
+ gruntmaster-problem edit problem_id key
+ gruntmaster-problem check problem_id
=head1 DESCRIPTION
gruntmaster-problem is a tool for managing problems.
-Select the contest with the optional argument I<--contest>.
-
=over
=item B<list>
Sets the I<key> configuration option of problem I<id> to I<value>.
+=item B<get> I<id> I<key>
+
+Get the value of the I<key> configuration option of problem I<id>
+
+=item B<edit> I<id> I<key>
+
+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.
+
=item B<set> --file I<id> I<key> I<file>
Sets the I<key> configuration option of problem I<id> to the contents of the file I<file>.
+=item B<check> I<id>
+
+Rerun all reference jobs for problem I<id> and check their results.
+
=back
=head1 AUTHOR
Copyright (C) 2014 by Marius Gavrilescu
-This library is free software: you can redistribute it and/or modify
-it under the terms of the GNU Affero General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.18.1 or,
+at your option, any later version of Perl 5 you may have available.
=cut