]>
Commit | Line | Data |
---|---|---|
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/encode_json/; | |
11 | use Term::ANSIColor qw/RED RESET/; | |
12 | use Getopt::Long qw/:config require_order/; | |
13 | ||
14 | use constant LEVEL_VALUES => { | |
15 | beginner => 100, | |
16 | easy => 250, | |
17 | medium => 500, | |
18 | hard => 1000, | |
19 | }; | |
20 | ||
21 | ################################################## | |
22 | ||
23 | my $dsn = $ENV{GRUNTMASTER_DSN} // 'dbi:Pg:'; | |
24 | my $db = Gruntmaster::Data->connect($dsn); | |
25 | ||
26 | sub cmd_help{ | |
27 | exec perldoc => $0 | |
28 | } | |
29 | ||
30 | sub 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 | ||
95 | sub 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 | ||
103 | sub cmd_get{ | |
104 | my ($id, $col) = @_; | |
105 | say $db->problem($id)->get_column($col) | |
106 | } | |
107 | ||
108 | sub 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 => read_file $file}); | |
116 | } | |
117 | ||
118 | sub cmd_list{ | |
119 | local $, = "\n"; | |
120 | say map {$_->id} $db->problems->all | |
121 | } | |
122 | ||
123 | sub cmd_rm{ | |
124 | my ($id) = @_; | |
125 | $db->problem($id)->delete; | |
126 | } | |
127 | ||
128 | sub cmd_show{ | |
129 | my %columns = $db->problem(shift)->get_columns; | |
130 | print <<END | |
131 | Name: $columns{name} | |
132 | Author: $columns{author} | |
133 | Statement written by: $columns{writer} | |
134 | Owner: $columns{owner} | |
135 | Level: $columns{level} | |
136 | Output limit: $columns{olimit} | |
137 | Time limit: $columns{timeout} | |
138 | Test count: $columns{testcnt} | |
139 | Generator: $columns{generator} | |
140 | Runner: $columns{runner} | |
141 | Judge: $columns{judge} | |
142 | Private: $columns{private} | |
143 | END | |
144 | } | |
145 | ||
146 | ################################################## | |
147 | ||
148 | my $cmd = 'cmd_' . shift; | |
149 | cmd_help unless exists $main::{$cmd}; | |
150 | no strict 'refs'; | |
151 | $cmd->(@ARGV); | |
152 | ||
153 | 1; | |
154 | __END__ | |
155 | ||
156 | =encoding utf-8 | |
157 | ||
158 | =head1 NAME | |
159 | ||
160 | gruntmaster-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 | ||
174 | gruntmaster-problem is a tool for managing problems. | |
175 | ||
176 | =over | |
177 | ||
178 | =item B<list> | |
179 | ||
180 | Prints the list of problems in the selected contest. | |
181 | ||
182 | =item B<show> I<id> | |
183 | ||
184 | Prints detailed information about problem I<id>. | |
185 | ||
186 | =item B<add> I<id> | |
187 | ||
188 | Adds a new problem with id I<id>. | |
189 | ||
190 | =item B<rm> I<id> | |
191 | ||
192 | Removes the problem with id I<id>. | |
193 | ||
194 | =item B<set> I<id> I<key> I<value> | |
195 | ||
196 | Sets the I<key> configuration option of problem I<id> to I<value>. | |
197 | ||
198 | =item B<get> I<id> I<key> | |
199 | ||
200 | Get the value of the I<key> configuration option of problem I<id> | |
201 | ||
202 | =item B<edit> I<id> I<key> | |
203 | ||
204 | 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. | |
205 | ||
206 | =item B<set> --file I<id> I<key> I<file> | |
207 | ||
208 | Sets 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 | ||
214 | Marius Gavrilescu E<lt>marius@ieval.roE<gt> | |
215 | ||
216 | =head1 COPYRIGHT AND LICENSE | |
217 | ||
218 | Copyright (C) 2014 by Marius Gavrilescu | |
219 | ||
220 | This library is free software; you can redistribute it and/or modify | |
221 | it under the same terms as Perl itself, either Perl version 5.18.1 or, | |
222 | at your option, any later version of Perl 5 you may have available. | |
223 | ||
224 | ||
225 | =cut |