From: Martin Mares Date: Tue, 5 Jun 2007 11:38:44 +0000 (+0200) Subject: Added command-line clients for remote submit and status. X-Git-Tag: python-dummy-working~396 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=3043ffa0cb666e509d5b07e479ead4792fbf722d;p=moe.git Added command-line clients for remote submit and status. --- diff --git a/submit/MO/Submit.pm b/submit/MO/Submit.pm index 5852b80..3491d81 100644 --- a/submit/MO/Submit.pm +++ b/submit/MO/Submit.pm @@ -16,6 +16,7 @@ sub new($) { "Key" => "client-key.pem", "Cert" => "client-cert.pem", "CACert" => "ca-cert.pem", + "Trace" => 0, "user" => "testuser", "sk" => undef, "error" => undef, @@ -24,15 +25,21 @@ sub new($) { return bless $self; } +sub DESTROY($) { + my $self = shift @_; + $self->disconnect; +} + sub log($$) { my ($self, $msg) = @_; - print STDERR "LOG: $msg\n"; + print STDERR "LOG: $msg\n" if $self->{"Trace"}; } sub err($$) { my ($self, $msg) = @_; - print STDERR "ERROR: $msg\n"; + print STDERR "ERROR: $msg\n" if $self->{"Trace"}; $self->{"error"} = $msg; + $self->disconnect; } sub is_connected($) { @@ -51,7 +58,7 @@ sub disconnect($) { sub connect($) { my $self = shift @_; - !defined $self->{"sk"} or close $self->{"sk"}; + $self->disconnect; $self->log("Connecting to submit server"); my $sk = new IO::Socket::INET( PeerAddr => $self->{"Server"}, @@ -97,7 +104,6 @@ sub connect($) { my $err = $reply->get("-"); if (defined $err) { $self->err("Cannot log in: $err"); - $self->disconnect; return undef; } @@ -109,6 +115,10 @@ sub request($$) { my ($self, $obj) = @_; my $sk = $self->{"sk"}; $obj->write($sk); ### FIXME: Flushing + if ($sk->error) { + $self->err("Connection broken"); + return undef; + } print $sk "\n"; return $self->reply; } @@ -125,4 +135,24 @@ sub reply($) { } } +sub send_file($$$) { + my ($self, $fh, $size) = @_; + my $sk = $self->{"sk"}; + while ($size) { + my $l = ($size < 4096 ? $size : 4096); + my $buf = ""; + if ($fh->read($buf, $l) != $l) { + $self->err("File shrunk during upload"); + return undef; + } + $sk->write($buf, $l); + if ($sk->error) { + $self->err("Connection broken"); + return undef; + } + $size -= $l; + } + return $self->reply; +} + 1; diff --git a/submit/remote-status b/submit/remote-status new file mode 100755 index 0000000..aa13379 --- /dev/null +++ b/submit/remote-status @@ -0,0 +1,63 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +BEGIN { + defined $ENV{"MO_ROOT"} or die "Please set MO_ROOT to the contest root directory first.\n"; +} +use lib $ENV{"MO_ROOT"} . "/lib/perl5"; +use lib "."; ### FIXME + +use MO::Submit; +use Sherlock::Object; +use POSIX; + +@ARGV == 0 or die "Usage: remote-status\n"; + +my $conn = new MO::Submit; +$conn->connect or die $conn->{"error"} . "\n"; + +sub or_die($) { + my $r = shift @_; + if (!defined $r) { die $conn->{"error"} . "\n"; } + my $err = $r->get("-"); + if ($err) { die "$err\n"; } +} + +my $r = new Sherlock::Object("!" => "STATUS"); +$r = $conn->request($r); +or_die($r); +#$r->write_indented(*STDOUT); + +my %tasks = map { $_->get("T") => $_ } $r->getarray("(T"); +foreach my $task (sort keys %tasks) { + my $t = $tasks{$task}; + my %parts = map { $_->get("P") => $_ } $t->getarray("(P"); + my @pp = keys %parts; + if (@pp != 1) { + @pp = sort { $a <=> $b } @pp; # We expect that the parts are numeric + } + foreach my $part (@pp) { + my $p = $parts{$part}; + my $name = $task; + $part eq $task or $name .= "/$part"; + printf "%-16s", $name; + + my $current_ver = $p->get("V"); + my $printed = 0; + foreach my $v ($p->getarray("(V")) { + if ($v && $v->get("V") == $current_ver) { + my $time = strftime("%H:%M:%S", localtime $v->get("T")); + print "OK (", + "$part.", $v->get("X"), ", ", + $v->get("L"), " bytes, ", + $v->get("S"), " $time)\n"; + $printed = 1; + } + } + $printed or print "---\n"; + } +} + +$conn->disconnect; diff --git a/submit/remote-submit b/submit/remote-submit new file mode 100755 index 0000000..543ed7a --- /dev/null +++ b/submit/remote-submit @@ -0,0 +1,50 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +BEGIN { + defined $ENV{"MO_ROOT"} or die "Please set MO_ROOT to the contest root directory first.\n"; +} +use lib $ENV{"MO_ROOT"} . "/lib/perl5"; +use lib "."; ### FIXME + +use MO::Submit; +use Sherlock::Object; +use File::stat; + +@ARGV == 2 || @ARGV == 3 or die "Usage: remote-submit [] \n"; +my $task = $ARGV[0]; +my $part = $task; +if (@ARGV == 3) { + $part = $ARGV[1]; + shift @ARGV; +} +my $file = $ARGV[1]; +my ($ext) = ($file =~ /\.([^.]+)$/) or die "Unable to determine filename extension\n"; + +open F, $file or die "Unable to open $file: $!\n"; +my $s = stat(*F) or die; +my $size = $s->size; + +my $conn = new MO::Submit; +$conn->connect or die $conn->{"error"} . "\n"; + +sub or_die($) { + my $r = shift @_; + if (!defined $r) { die $conn->{"error"} . "\n"; } + my $err = $r->get("-"); + if ($err) { die "$err\n"; } +} + +my $r = new Sherlock::Object("!" => "SUBMIT", "T" => $task, "P" => $part, "X" => $ext, "S" => $size); +$r = $conn->request($r); +or_die($r); + +$r = $conn->send_file(\*F, $size); +or_die($r); + +print "Submitted OK.\n"; + +$conn->disconnect; +close F;