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