]> mj.ucw.cz Git - moe.git/blob - submit/contest
29b046e5d8ea5c60e2c11e86c45b1a9c5746dbe8
[moe.git] / submit / contest
1 #!/usr/bin/perl
2 # Contest UI for the MO Submitter
3 # (c) 2007 Martin Mares <mj@ucw.cz>
4
5 use strict;
6 use warnings;
7
8 BEGIN {
9         defined $ENV{"MO_ROOT"} or die "Please set MO_ROOT to the contest root directory first.\n";
10 }
11 use lib $ENV{"MO_ROOT"} . "/lib/perl5";
12 use lib $ENV{"MO_ROOT"} . "/submit/lib/perl5";
13
14 use MO::Submit;
15 use Sherlock::Object;
16 use POSIX;
17 use IO::File;
18 use File::stat;
19 use Gtk2 -init;
20
21 my $conn = new MO::Submit;
22
23 ### GUI INITIALIZATION ###
24
25 sub init_refresh();
26 sub start_refresh_timer($);
27 sub stop_refresh_timer();
28 my $force_refresh = 0;
29 sub refresh();
30
31 sub submit($);
32
33 my $busy_cursor = Gtk2::Gdk::Cursor->new('watch');
34
35 # The main window
36 my $window = Gtk2::Window->new('toplevel');
37 $window->signal_connect("delete-event" => sub { Gtk2->main_quit });
38 $window->set_title($conn->{"Contest"} . " Submitter");
39 $window->set_wmclass("submitter", "Submitter");
40 $window->set_default_size(640, 480);
41
42 # The title label
43 my $title_lab = Gtk2::Label->new;
44 $title_lab->set_markup("<span size='x-large'>" . $conn->{"Contest"} . "</span>");
45
46 # The row of buttons
47 my $b_submit = Gtk2::Button->new('Submit');
48 $b_submit->set_image(Gtk2::Image->new_from_stock('gtk-apply', 'button'));
49 $b_submit->signal_connect(clicked => sub { submit(0) });
50 $b_submit->set_sensitive(0);
51
52 my $b_check = Gtk2::Button->new('Check');
53 $b_check->set_image(Gtk2::Image->new_from_stock('gtk-find', 'button'));
54 $b_check->signal_connect(clicked => sub { submit(1) });
55 $b_check->set_sensitive(0);
56
57 my $b_refresh = Gtk2::Button->new('Refresh');
58 $b_refresh->set_image(Gtk2::Image->new_from_stock('gtk-refresh', 'button'));
59 $b_refresh->signal_connect(clicked => sub { start_refresh_timer(1) });
60
61 my $button_box = Gtk2::HBox->new;
62 $button_box->pack_start_defaults($b_submit);
63 $button_box->pack_start_defaults($b_check);
64 $button_box->pack_start_defaults($b_refresh);
65 $button_box->set_border_width(5);
66
67 # The list of tasks
68 my $task_store = Gtk2::ListStore->new('Glib::String', 'Glib::String');
69
70 my $task_view = Gtk2::TreeView->new($task_store);
71 my $task_renderer = Gtk2::CellRendererText->new;
72 my $task_col1 = Gtk2::TreeViewColumn->new_with_attributes("Task", $task_renderer, "text", 0);
73 $task_view->append_column($task_col1);
74 my $task_col2 = Gtk2::TreeViewColumn->new_with_attributes("Status", $task_renderer, "text", 1);
75 $task_view->append_column($task_col2);
76 $task_view->set_headers_visible(0);
77
78 my $task_scroll = Gtk2::ScrolledWindow->new;
79 $task_scroll->set_policy("automatic", "automatic");
80 $task_scroll->add($task_view);
81 $task_scroll->set_border_width(5);
82
83 my $task_frame = Gtk2::Frame->new("Tasks");
84 $task_frame->add($task_scroll);
85
86 my $selected_task;
87 my $task_sel = $task_view->get_selection;
88 $task_sel->set_mode('single');
89 $task_sel->signal_connect(changed => sub {
90         my $iter = $_[0]->get_selected;
91         if ($iter) {
92                 $selected_task = $task_store->get($iter, 0);
93                 $b_submit->set_sensitive(1);
94                 $b_check->set_sensitive(1);
95         } else {
96                 $selected_task = undef;
97                 print "Deselected task\n";
98                 $b_submit->set_sensitive(0);
99                 $b_check->set_sensitive(0);
100         }
101 });
102
103 my $status_bar = Gtk2::Statusbar->new;
104 my $bar_ctx = $status_bar->get_context_id('xyzzy');
105
106 my $vbox = Gtk2::VBox->new;
107 $vbox->pack_start($title_lab, 0, 0, 10);
108 $vbox->pack_start($task_frame, 1, 1, 0);
109 $vbox->pack_start($button_box, 0, 0, 0);
110 $vbox->pack_start($status_bar, 0, 0, 0);
111
112 $window->add($vbox);
113 $window->signal_connect("expose-event" => sub { init_refresh(); return 0; });
114 $window->show_all;
115
116 ### REFRESHING ###
117
118 my $last_status_id;
119
120 sub msg($) {
121         print "GUI: ", $_[0], "\n" if $conn->{"Trace"};
122 }
123
124 sub status($) {
125         msg($_[0]);
126         defined $last_status_id and $status_bar->remove($bar_ctx, $last_status_id);
127         $last_status_id = $status_bar->push($bar_ctx, shift @_);
128 }
129
130 sub busy($) {
131         status($_[0]);
132         $window->window->set_cursor($busy_cursor);
133         $window->Gtk2::Gdk::flush;
134 }
135
136 sub ready($) {
137         status($_[0]);
138         $window->window->set_cursor(undef);
139         $window->Gtk2::Gdk::flush;
140 }
141
142 my $window_inited = 0;
143 sub init_refresh()
144 {
145         if (!$window_inited) {
146                 $force_refresh = 1;
147                 start_refresh_timer(1);
148                 $window_inited = 1;
149         }
150         return 1;
151 }
152
153 my $refresh_timer_id;
154
155 sub timed_refresh()
156 {
157         refresh();
158         return 1;       # We wish to re-run the timer
159 }
160
161 sub start_refresh_timer($) {
162         my ($go) = @_;
163         stop_refresh_timer();
164         refresh() if $go;
165         $refresh_timer_id = Glib::Timeout->add($conn->{"RefreshTimer"}, \&timed_refresh);
166 }
167
168 sub stop_refresh_timer() {
169         if (defined $refresh_timer_id) {
170                 Glib::Source->remove($refresh_timer_id);
171                 $refresh_timer_id = undef;
172         }
173 }
174
175 my $task_status_object;
176 my @task_parts = ();
177 my @task_stat = ();
178
179 sub recalc_task_list() {
180         my @new_tp = ();
181         my @new_stat = ();
182         foreach my $t ($task_status_object->getarray("(T")) {
183                 my $task = $t->get("T");
184                 foreach my $p ($t->getarray("(P")) {
185                         my $part = $p->get("P");
186                         my $taskpart = ($task eq $part) ? $task : "$task/$part";
187                         push @new_tp, $taskpart;
188                         my $status = "---";
189                         my $current_ver = $p->get("V");
190                         foreach my $v ($p->getarray("(V")) {
191                                 if ($v->get("V") == $current_ver) {
192                                         my $time = strftime("%H:%M:%S", localtime $v->get("T"));
193                                         $status = "OK (" .
194                                                 "$part." . $v->get("X") . ", " .
195                                                 $v->get("L") . " bytes, " .
196                                                 $v->get("S") . " $time)";
197                                         last;
198                                 }
199                         }
200                         push @new_stat, $status;
201                 }
202         }
203
204         if (join("\n", @new_tp) ne join("\n", @task_parts)) {
205                 # The tasks have changed, repopulate the whole structure
206                 $task_store->clear;
207                 my @s = @new_stat;
208                 foreach my $taskpart (@new_tp) {
209                         my $iter = $task_store->append;
210                         $task_store->set($iter,
211                                 0, $taskpart,
212                                 1, shift @s);
213                 }
214         } else {
215                 # Update the task status
216                 my @s = @task_stat;
217                 my @ns = @new_stat;
218                 $task_store->foreach(sub {
219                         my ($obj, $path, $iter) = @_;
220                         if ($s[0] ne $ns[0]) {
221                                 $task_store->set($iter, 1, $ns[0]);
222                         }
223                         shift @s;
224                         shift @ns;
225                         return 0;
226                 });
227         }
228
229         @task_parts = @new_tp;
230         @task_stat = @new_stat;
231 }
232
233 sub refresh()
234 {
235         if (!$conn->is_connected || $force_refresh) {
236                 busy("Connecting to server...");
237                 if ($conn->connect) {
238                         ready("Connected successfully");
239                 } else {
240                         ready($conn->{"error"});
241                 }
242         }
243         if ($conn->is_connected) {
244                 busy("Updating status...");
245                 my $r = new Sherlock::Object("!" => "STATUS");
246                 $r = $conn->request($r);
247                 if (!defined $r) {
248                         ready($conn->{"error"});
249                 } elsif ($r->get("-")) {
250                         ready($r->get("-"));
251                 } else {
252                         $task_status_object = $r;
253                         recalc_task_list;
254                         $force_refresh = 0;
255                         ready("Ready");
256                 }
257         }
258         if (!$conn->is_connected && !$force_refresh) {
259                 # Retry
260                 $conn->log("Retrying");
261                 refresh();
262         }
263 }
264
265 ### SUBMITTING ###
266
267 my $subwin;
268 my $chooser;
269 my $subwin_vbox;
270 my $subwin_label;
271 my $bbutton_box;
272 my $submitting_label;
273 my $status_label;
274 my $check_only;
275 my $submit_filename;
276 my $submit_extension;
277 my %submit_fn_cache = ();
278
279 sub end_submit($) {
280         my ($close) = @_;
281         $subwin->destroy if $close;
282         start_refresh_timer(1);
283 }
284
285 sub finish_submit() {
286         my $button = Gtk2::Button->new_from_stock('gtk-close');
287         $button->signal_connect(clicked => sub { end_submit(1) });
288
289         $bbutton_box = Gtk2::HButtonBox->new;
290         $bbutton_box->pack_start_defaults($button);
291         $subwin_vbox->pack_start($bbutton_box, 0, 0, 10);
292
293         ready("Ready");
294         $subwin->show_all;
295         $subwin->window->set_cursor(undef);
296 }
297
298 sub submit_ok() {
299         $status_label->set_markup("<span size='large'>Submitted OK</span>");
300         $submitting_label->set_markup("<span size='large'>The task has been successfully submitted.</span>");
301         refresh();
302         finish_submit();
303 }
304
305 sub submit_failed($) {
306         my ($msg) = @_;
307         $status_label->set_markup("<span size='large'>Submit failed</span>");
308         $submitting_label->set_markup("<span size='large'>$msg</span>");
309         finish_submit();
310 }
311
312 sub run_submit() {
313         my ($task, $part) = split /\//, $selected_task;
314         defined $part or $part = $task;
315
316         if (defined $conn->{"History"}) {
317                 busy("Submitting locally to " . $conn->{"History"});
318                 my $err = $conn->local_submit($task, $part, $submit_extension, $submit_filename);
319                 if (defined $err) {
320                         submit_failed("Recording to local history failed\n($err)");
321                         return;
322                 }
323         }
324
325         if ($conn->is_connected) {
326                 busy("Checking server status...");
327                 my $r = new Sherlock::Object("!" => "NOP");
328                 $r = $conn->request($r);
329         }
330         if (!$conn->is_connected) {
331                 busy("Reconnecting to server...");
332                 if (!$conn->connect) {
333                         ready($conn->{"error"});
334                         submit_failed("Unable to connect to the server");
335                         return;
336                 }
337         }
338         busy("Submitting...");
339
340         my $fh = new IO::File($submit_filename);
341         if (!$fh) {
342                 submit_failed("Unable to open $submit_filename\n($!)");
343                 return;
344         }
345         my $stat = File::stat::populate($fh->stat);
346         if (!$stat) {
347                 submit_failed("Unable to stat $submit_filename\n($!)");
348                 return;
349         }
350         my $size = $stat->size;
351
352         my $r = new Sherlock::Object("!" => "SUBMIT", "T" => $task, "P" => $part, "X" => $submit_extension, "S" => $size);
353         $r = $conn->request($r);
354         if (!defined($r)) {
355                 submit_failed("Connection to the server lost");
356                 return;
357         } elsif ($r->get("-")) {
358                 submit_failed($r->get("-"));
359                 return;
360         }
361
362         $r = $conn->send_file($fh, $size);
363         if (!defined($r)) {
364                 submit_failed("Connection to the server lost");
365                 return;
366         } elsif ($r->get("-")) {
367                 submit_failed($r->get("-"));
368                 return;
369         }
370
371         close $fh;
372         submit_ok();
373 }
374
375 sub checks_failed($) {
376         my ($msg) = @_;
377
378         $status_label->set_markup("<span size='large'>Check failed</span>");
379
380         my $text_buffer = Gtk2::TextBuffer->new;
381         $text_buffer->set_text($msg);
382
383         my $text_view = Gtk2::TextView->new_with_buffer($text_buffer);
384         $text_view->set_editable(0);
385         $text_view->set_cursor_visible(0);
386
387         my $text_scroll = Gtk2::ScrolledWindow->new;
388         $text_scroll->set_policy("automatic", "automatic");
389         $text_scroll->add($text_view);
390
391         my $text_frame = Gtk2::Frame->new("Checker log");
392         $text_frame->add($text_scroll);
393
394         $submitting_label->destroy;
395         $subwin_vbox->pack_start_defaults($text_frame);
396
397         finish_submit();
398 }
399
400 sub checks_ok() {
401         if ($check_only) {
402                 $status_label->set_markup("<span size='large'>Checked successfully</span>");
403                 $submitting_label->set_markup("<span size='large'>The task has passed the checks.</span>");
404                 finish_submit();
405                 return;
406         }
407
408         ### FIXME: Record to local history here
409
410         $status_label->set_markup("<span size='large'>Submitting</span>");
411         $subwin->show_all;
412
413         # Continue when everything is displayed
414         Glib::Idle->add(sub {
415                 $window->Gtk2::Gdk::flush;
416                 run_submit();
417                 return 0;
418         });
419 }
420
421 sub run_checks() {
422         ($submit_extension) = ($submit_filename =~ /\.([^.]+)$/);
423         if (!$submit_extension) {
424                 checks_failed("The filename does not have a valid extension");
425                 return;
426         }
427         if (!$conn->{"Checks"}) {
428                 checks_ok();
429                 return;
430         }
431         my $root = $conn->{"root"};
432         my ($task, $part) = split /\//, $selected_task;
433         defined $part or $part = "";
434         my $verdict = `$root/bin/check -s "$submit_filename" $task $part 2>&1`;
435         if ($?) {
436                 checks_failed($verdict);
437         } else {
438                 checks_ok();
439         }
440 }
441
442 sub do_submit() {
443         $submit_filename = $chooser->get_filename;
444         $submit_fn_cache{$selected_task} = $submit_filename;
445         msg "Selected $submit_filename";
446         defined $submit_filename or return;
447         -f $submit_filename or return;
448
449         $chooser->destroy;
450         $bbutton_box->destroy;
451
452         $status_label->set_markup("<span size='large'>Checking</span>");
453
454         $submitting_label = Gtk2::Label->new("Please wait...");
455         $subwin_vbox->pack_start_defaults($submitting_label);
456         $subwin->show_all;
457         $subwin->window->set_cursor($busy_cursor);
458
459         # Continue when everything is displayed
460         Glib::Idle->add(sub {
461                 $window->Gtk2::Gdk::flush;
462                 run_checks();
463                 return 0;
464         });
465 }
466
467 sub submit($) {
468         $check_only = shift @_;
469
470         stop_refresh_timer();
471
472         $subwin = Gtk2::Window->new('toplevel');
473         $subwin->set_default_size(640, 480);
474         $subwin->set_modal(1);
475         $subwin->set_transient_for($window);
476         $subwin->set_destroy_with_parent(1);
477         $subwin->set_title("Submit task $selected_task");
478         $subwin->set_wmclass("submitter", "Submitter");
479         $subwin->signal_connect("delete-event" => sub { end_submit(0); return 0; });
480
481         my $bb_submit = Gtk2::Button->new($check_only ? 'Check' : 'Submit');
482         $bb_submit->set_image(Gtk2::Image->new_from_stock('gtk-apply', 'button'));
483         $bb_submit->signal_connect(clicked => \&do_submit);
484
485         my $bb_cancel = Gtk2::Button->new_from_stock('gtk-cancel');
486         $bb_cancel->signal_connect(clicked => sub { end_submit(1) });
487
488         $bbutton_box = Gtk2::HButtonBox->new;
489         $bbutton_box->pack_start_defaults($bb_submit);
490         $bbutton_box->pack_start_defaults($bb_cancel);
491         $bbutton_box->set_border_width(5);
492
493         my $subwin_label = Gtk2::Label->new;
494         $subwin_label->set_markup("<span size='x-large'>" . ($check_only ? "Checking" : "Submitting") . " $selected_task</span>");
495
496         $status_label = Gtk2::Label->new;
497         $status_label->set_markup("<span size='large'>Please select file to " . ($check_only ? "check" : "submit") . "</span>");
498
499         $chooser = Gtk2::FileChooserWidget->new("open");
500         $chooser->set_local_only(1);
501         $chooser->signal_connect("file-activated" => \&do_submit);
502         $chooser->set_filename($submit_fn_cache{$selected_task}) if defined $submit_fn_cache{$selected_task};
503
504         $subwin_vbox = Gtk2::VBox->new;
505         $subwin_vbox->pack_start($subwin_label, 0, 0, 10);
506         $subwin_vbox->pack_start($status_label, 0, 0, 10);
507         $subwin_vbox->pack_start_defaults($chooser);
508         $subwin_vbox->pack_start($bbutton_box, 0, 0, 0);
509
510         $subwin->add($subwin_vbox);
511         $subwin->show_all;
512 }
513
514 ### MAIN ###
515
516 Gtk2->main;
517 exit 0;