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