From: Marius Gavrilescu Date: Fri, 21 Mar 2014 06:44:55 +0000 (+0200) Subject: Update indentation X-Git-Tag: 5999.000_003~25 X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=a722431b0f35babda9d7da134824caf76ad75458;p=gruntmaster-daemon.git Update indentation --- diff --git a/lib/Gruntmaster/Daemon.pm b/lib/Gruntmaster/Daemon.pm index a9a6d99..f2c4a11 100644 --- a/lib/Gruntmaster/Daemon.pm +++ b/lib/Gruntmaster/Daemon.pm @@ -26,19 +26,19 @@ my $ua = LWP::UserAgent->new; my @purge_hosts = exists $ENV{PURGE_HOSTS} ? split ' ', $ENV{PURGE_HOSTS} : (); sub safe_can_nodie { - my ($type, $sub, $name) = @_; + my ($type, $sub, $name) = @_; - return unless $name =~ /^\w+$/; - no strict 'refs'; - my $pkg = __PACKAGE__ . "::${type}::${name}"; - eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@"); - $pkg->can($sub); + return unless $name =~ /^\w+$/; + no strict 'refs'; + my $pkg = __PACKAGE__ . "::${type}::${name}"; + eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@"); + $pkg->can($sub); } sub safe_can { - my ($type, $sub, $name) = @_; + my ($type, $sub, $name) = @_; - safe_can_nodie @_ or get_logger->logdie("No such \l$type: '$name'"); + safe_can_nodie @_ or get_logger->logdie("No such \l$type: '$name'"); } sub purge { @@ -49,96 +49,96 @@ sub purge { } sub process{ - my $job = shift; - - my @results; - my @full_results = (); - my $meta = {}; - our $errors = ''; - try { - $meta = job_inmeta $job; - if (job_problem $job) { - local $_ = job_problem $job; - my $pbmeta = problem_meta; - my %files = exists $meta->{files} ? %{$meta->{files}} : (); - $meta = { - %$meta, - problem => $_, - (defined problem_generator() ? (generator => problem_generator) : ()), - (defined problem_runner() ? (runner => problem_runner) : ()), - (defined problem_judge() ? (judge => problem_judge) : ()), - (defined problem_testcnt() ? (testcnt => problem_testcnt) : ()), - (defined problem_timeout() ? (timeout => problem_timeout) : ()), - (defined problem_olimit() ? (olimit => problem_olimit) : ()), - %$pbmeta - }; - $meta->{files} = {%files, %{$pbmeta->{files}}} if exists $pbmeta->{files}; - } + my $job = shift; + + my @results; + my @full_results = (); + my $meta = {}; + our $errors = ''; + try { + $meta = job_inmeta $job; + if (job_problem $job) { + local $_ = job_problem $job; + my $pbmeta = problem_meta; + my %files = exists $meta->{files} ? %{$meta->{files}} : (); + $meta = { + %$meta, + problem => $_, + (defined problem_generator() ? (generator => problem_generator) : ()), + (defined problem_runner() ? (runner => problem_runner) : ()), + (defined problem_judge() ? (judge => problem_judge) : ()), + (defined problem_testcnt() ? (testcnt => problem_testcnt) : ()), + (defined problem_timeout() ? (timeout => problem_timeout) : ()), + (defined problem_olimit() ? (olimit => problem_olimit) : ()), + %$pbmeta + }; + $meta->{files} = {%files, %{$pbmeta->{files}}} if exists $pbmeta->{files}; + } - prepare_files $meta; - chomp $errors; - - my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_"} qw/files generator runner judge testcnt/; - - $generator = safe_can Generator => generate => $generator; - $runner = safe_can Runner => run => $runner; - $judge = safe_can Judge => judge => $judge; - - for my $test (1 .. $testcnt) { - my $start_time = time; - my $result; - try { - $generator->($test, $meta); - $result = $runner->($test, $meta); - } catch { - $result = $_; - unless (ref $result) { - chomp $result; - $result = [ERR, $result]; + prepare_files $meta; + chomp $errors; + + my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_"} qw/files generator runner judge testcnt/; + + $generator = safe_can Generator => generate => $generator; + $runner = safe_can Runner => run => $runner; + $judge = safe_can Judge => judge => $judge; + + for my $test (1 .. $testcnt) { + my $start_time = time; + my $result; + try { + $generator->($test, $meta); + $result = $runner->($test, $meta); + } catch { + $result = $_; + unless (ref $result) { + chomp $result; + $result = [ERR, $result]; + } + }; + + if (ref $result) { + get_logger->trace("Test $test result is " . $result->[1]); + push @full_results, {id => $test, result => $result->[0], result_text => $result->[1], time => time - $start_time} + } else { + get_logger->trace("Test $test result is $result"); + push @full_results, {id => $test, result => 0, result_text => $result, time => time - $start_time} + } + push @results, $result; + last if $meta->{judge} eq 'Absolute' && ref $result } - }; - - if (ref $result) { - get_logger->trace("Test $test result is " . $result->[1]); - push @full_results, {id => $test, result => $result->[0], result_text => $result->[1], time => time - $start_time} - } else { - get_logger->trace("Test $test result is $result"); - push @full_results, {id => $test, result => 0, result_text => $result, time => time - $start_time} - } - push @results, $result; - last if $meta->{judge} eq 'Absolute' && ref $result - } - my %results = $judge->(@results); - $meta->{$_} = $results{$_} for keys %results; - } catch { - s,(.*) at .*,$1,; - chomp; - $meta->{result} = -1; - $meta->{result_text} = $_; - }; - - get_logger->info("Job result: " . $meta->{result_text}); - set_job_result $job, $meta->{result}; - set_job_result_text $job, $meta->{result_text}; - set_job_results $job, \@full_results if scalar @full_results; - set_job_errors $job, $errors; - - my $log = $Gruntmaster::Data::contest ? "ct/$Gruntmaster::Data::contest/log" : 'log'; - - PUBLISH gensrc => ($Gruntmaster::Data::contest // '') . ".$job"; - PUBLISH genpage => "$log/job/$job.html"; - PUBLISH genpage => "$log/index.html"; - PUBLISH genpage => "$log/st.html"; - my $page = ($job + PAGE_SIZE - 1) / PAGE_SIZE; - PUBLISH genpage => "$log/@{[$page - 1]}.html"; - PUBLISH genpage => "$log/$page.html"; - PUBLISH genpage => "$log/@{[$page + 1]}.html"; - - purge "/$log/job/$job"; - purge "/$log/"; - purge "/$log/st"; - purge "/$log/$_" for $page - 1, $page, $page + 1; + my %results = $judge->(@results); + $meta->{$_} = $results{$_} for keys %results; + } catch { + s,(.*) at .*,$1,; + chomp; + $meta->{result} = -1; + $meta->{result_text} = $_; + }; + + get_logger->info("Job result: " . $meta->{result_text}); + set_job_result $job, $meta->{result}; + set_job_result_text $job, $meta->{result_text}; + set_job_results $job, \@full_results if scalar @full_results; + set_job_errors $job, $errors; + + my $log = $Gruntmaster::Data::contest ? "ct/$Gruntmaster::Data::contest/log" : 'log'; + + PUBLISH gensrc => ($Gruntmaster::Data::contest // '') . ".$job"; + PUBLISH genpage => "$log/job/$job.html"; + PUBLISH genpage => "$log/index.html"; + PUBLISH genpage => "$log/st.html"; + my $page = ($job + PAGE_SIZE - 1) / PAGE_SIZE; + PUBLISH genpage => "$log/@{[$page - 1]}.html"; + PUBLISH genpage => "$log/$page.html"; + PUBLISH genpage => "$log/@{[$page + 1]}.html"; + + purge "/$log/job/$job"; + purge "/$log/"; + purge "/$log/st"; + purge "/$log/$_" for $page - 1, $page, $page + 1; } sub got_job{ @@ -156,11 +156,11 @@ sub got_job{ } sub run{ - Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf'); - get_logger->info("gruntmasterd $VERSION started"); - chdir tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1; - SUBSCRIBE jobs => \&got_job; - WAIT_FOR_MESSAGES 86400 while 1 + Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf'); + get_logger->info("gruntmasterd $VERSION started"); + chdir tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1; + SUBSCRIBE jobs => \&got_job; + WAIT_FOR_MESSAGES 86400 while 1 } 1; diff --git a/lib/Gruntmaster/Daemon/Constants.pm b/lib/Gruntmaster/Daemon/Constants.pm index e41218a..eb52456 100644 --- a/lib/Gruntmaster/Daemon/Constants.pm +++ b/lib/Gruntmaster/Daemon/Constants.pm @@ -8,19 +8,19 @@ use parent qw/Exporter/; our $VERSION = "5999.000_002"; use constant +{ - # Accepted - AC => 0, - - # Internal server error - ERR => -1, - - # All other errors - WA => 1, - NZX => 2, - TLE => 3, - OLE => 4, - DIED => 5, - REJ => 10, + # Accepted + AC => 0, + + # Internal server error + ERR => -1, + + # All other errors + WA => 1, + NZX => 2, + TLE => 3, + OLE => 4, + DIED => 5, + REJ => 10, }; our @EXPORT_OK = qw/AC ERR WA NZX TLE OLE DIED REJ/; diff --git a/lib/Gruntmaster/Daemon/Generator/File.pm b/lib/Gruntmaster/Daemon/Generator/File.pm index 132b902..acadf58 100644 --- a/lib/Gruntmaster/Daemon/Generator/File.pm +++ b/lib/Gruntmaster/Daemon/Generator/File.pm @@ -13,14 +13,14 @@ our $VERSION = "5999.000_002"; ################################################## sub generate{ - my ($test, $meta) = @_; - get_logger->trace("Generating test $test ..."); - if (exists $meta->{infile}) { - write_file 'input', $meta->{infile}[$test - 1] - } else { - my $ct = defined $Gruntmaster::Data::contest ? "ct/$Gruntmaster::Data::contest" : ''; - copy "/var/lib/gruntmasterd/$ct/pb/$meta->{problem}/$test.in", 'input' - } + my ($test, $meta) = @_; + get_logger->trace("Generating test $test ..."); + if (exists $meta->{infile}) { + write_file 'input', $meta->{infile}[$test - 1] + } else { + my $ct = defined $Gruntmaster::Data::contest ? "ct/$Gruntmaster::Data::contest" : ''; + copy "/var/lib/gruntmasterd/$ct/pb/$meta->{problem}/$test.in", 'input' + } } 1; diff --git a/lib/Gruntmaster/Daemon/Generator/Run.pm b/lib/Gruntmaster/Daemon/Generator/Run.pm index 33240af..d971e43 100644 --- a/lib/Gruntmaster/Daemon/Generator/Run.pm +++ b/lib/Gruntmaster/Daemon/Generator/Run.pm @@ -11,10 +11,10 @@ our $VERSION = '5999.000_002'; ################################################## sub generate{ - my ($test, $meta) = @_; - my $gen = $meta->{files}{gen}; - get_logger->trace("Generating test $test..."); - $gen->{run}->($gen->{name}, args => [ $test ], fds => [qw/1 >input/]); + my ($test, $meta) = @_; + my $gen = $meta->{files}{gen}; + get_logger->trace("Generating test $test..."); + $gen->{run}->($gen->{name}, args => [ $test ], fds => [qw/1 >input/]); } 1; diff --git a/lib/Gruntmaster/Daemon/Generator/Undef.pm b/lib/Gruntmaster/Daemon/Generator/Undef.pm index d9285a8..5df3762 100644 --- a/lib/Gruntmaster/Daemon/Generator/Undef.pm +++ b/lib/Gruntmaster/Daemon/Generator/Undef.pm @@ -11,7 +11,7 @@ our $VERSION = "5999.000_002"; ################################################## sub generate{ - get_logger->trace("Pretending to generate test $_[0]..."); + get_logger->trace("Pretending to generate test $_[0]..."); } 1; diff --git a/lib/Gruntmaster/Daemon/Judge/Points.pm b/lib/Gruntmaster/Daemon/Judge/Points.pm index 85ba209..b153948 100644 --- a/lib/Gruntmaster/Daemon/Judge/Points.pm +++ b/lib/Gruntmaster/Daemon/Judge/Points.pm @@ -13,10 +13,10 @@ our $VERSION = '5999.000_002'; ################################################## sub judge{ - no warnings qw/numeric/; - get_logger->trace("Judging results: @_"); - my $points = sum 0, grep { !ref } @_; - $points == 100 ? (result => AC, result_text => 'Accepted') : (result => REJ, result_text => "$points points", points => $points) + no warnings qw/numeric/; + get_logger->trace("Judging results: @_"); + my $points = sum 0, grep { !ref } @_; + $points == 100 ? (result => AC, result_text => 'Accepted') : (result => REJ, result_text => "$points points", points => $points) } 1; diff --git a/lib/Gruntmaster/Daemon/Runner/File.pm b/lib/Gruntmaster/Daemon/Runner/File.pm index 3f4c863..bfa9a05 100644 --- a/lib/Gruntmaster/Daemon/Runner/File.pm +++ b/lib/Gruntmaster/Daemon/Runner/File.pm @@ -13,27 +13,27 @@ our $VERSION = "5999.000_002"; ################################################## sub run{ - my ($test, $meta) = @_; - get_logger->trace("Running on test $test..."); - $meta->{files}{prog}{run}->($meta->{files}{prog}{name}, fds => [qw/0 input 1 >output/], map {defined $meta->{$_} ? ($_ => $meta->{$_}) : () } qw/timeout olimit mlimit/); - my $out = slurp 'output'; - my $ok; - if (exists $meta->{okfile}) { - $ok = $meta->{okfile}[$test - 1] - } else { - my $ct = defined $Gruntmaster::Data::contest ? "ct/$Gruntmaster::Data::contest" : ''; - $ok = slurp "/var/lib/gruntmasterd/$ct/pb/$meta->{problem}/$test.ok" - } - - $out =~ s/^\s+//; - $ok =~ s/^\s+//; - $out =~ s/\s+/ /; - $ok =~ s/\s+/ /; - $out =~ s/\s+$//; - $ok =~ s/\s+$//; - - die [WA, "Wrong answer"] if $out ne $ok; - $meta->{tests}[$test - 1] // 0 + my ($test, $meta) = @_; + get_logger->trace("Running on test $test..."); + $meta->{files}{prog}{run}->($meta->{files}{prog}{name}, fds => [qw/0 input 1 >output/], map {defined $meta->{$_} ? ($_ => $meta->{$_}) : () } qw/timeout olimit mlimit/); + my $out = slurp 'output'; + my $ok; + if (exists $meta->{okfile}) { + $ok = $meta->{okfile}[$test - 1] + } else { + my $ct = defined $Gruntmaster::Data::contest ? "ct/$Gruntmaster::Data::contest" : ''; + $ok = slurp "/var/lib/gruntmasterd/$ct/pb/$meta->{problem}/$test.ok" + } + + $out =~ s/^\s+//; + $ok =~ s/^\s+//; + $out =~ s/\s+/ /; + $ok =~ s/\s+/ /; + $out =~ s/\s+$//; + $ok =~ s/\s+$//; + + die [WA, "Wrong answer"] if $out ne $ok; + $meta->{tests}[$test - 1] // 0 } 1; diff --git a/lib/Gruntmaster/Daemon/Runner/Interactive.pm b/lib/Gruntmaster/Daemon/Runner/Interactive.pm index ec07aeb..03c3911 100644 --- a/lib/Gruntmaster/Daemon/Runner/Interactive.pm +++ b/lib/Gruntmaster/Daemon/Runner/Interactive.pm @@ -15,27 +15,27 @@ our $VERSION = '5999.000_002'; ################################################## sub run{ - my ($test, $meta) = @_; - get_logger->trace("Running on test $test..."); - - mkfifo 'fifo1', 0600 or die $! unless -e 'fifo1'; - mkfifo 'fifo2', 0600 or die $! unless -e 'fifo2'; - - my $ret = fork // get_logger->logdie("Fork failed: $!"); - if ($ret) { - $meta->{files}{prog}{run}->($meta->{files}{prog}{name}, fds => [qw/0 fifo1 1 >fifo2/], map {defined $meta->{$_} ? ($_ => $meta->{$_}) : () } qw/timeout mlimit/); - waitpid $ret, 0; - die [WA, "Wrong Answer"] if $?; - } else { - try { - $meta->{files}{int}{run}->($meta->{files}{int}{name}, fds => [qw/1 >fifo1 0 fifo2 4 >result/]); - } catch { - exit 1; - }; - exit - } - - scalar slurp 'result' + my ($test, $meta) = @_; + get_logger->trace("Running on test $test..."); + + mkfifo 'fifo1', 0600 or die $! unless -e 'fifo1'; + mkfifo 'fifo2', 0600 or die $! unless -e 'fifo2'; + + my $ret = fork // get_logger->logdie("Fork failed: $!"); + if ($ret) { + $meta->{files}{prog}{run}->($meta->{files}{prog}{name}, fds => [qw/0 fifo1 1 >fifo2/], map {defined $meta->{$_} ? ($_ => $meta->{$_}) : () } qw/timeout mlimit/); + waitpid $ret, 0; + die [WA, "Wrong Answer"] if $?; + } else { + try { + $meta->{files}{int}{run}->($meta->{files}{int}{name}, fds => [qw/1 >fifo1 0 fifo2 4 >result/]); + } catch { + exit 1; + }; + exit + } + + scalar slurp 'result' } 1; diff --git a/lib/Gruntmaster/Daemon/Runner/Verifier.pm b/lib/Gruntmaster/Daemon/Runner/Verifier.pm index 7eae047..10f0287 100644 --- a/lib/Gruntmaster/Daemon/Runner/Verifier.pm +++ b/lib/Gruntmaster/Daemon/Runner/Verifier.pm @@ -14,16 +14,16 @@ our $VERSION = '5999.000_002'; ################################################## sub run{ - my ($test, $meta) = @_; - get_logger->trace("Running on test $test..."); - $meta->{files}{prog}{run}->($meta->{files}{prog}{name}, fds => [qw/0 input 1 >output/], map {defined $meta->{$_} ? ($_ => $meta->{$_}) : () } qw/timeout olimit mlimit/); - - try { - $meta->{files}{ver}{run}->($meta->{files}{ver}{name}, fds => [qw/0 input 3 output 1 >result/], args => [$test]); - } catch { - die [WA, "Wrong answer"] - }; - scalar slurp 'result'; + my ($test, $meta) = @_; + get_logger->trace("Running on test $test..."); + $meta->{files}{prog}{run}->($meta->{files}{prog}{name}, fds => [qw/0 input 1 >output/], map {defined $meta->{$_} ? ($_ => $meta->{$_}) : () } qw/timeout olimit mlimit/); + + try { + $meta->{files}{ver}{run}->($meta->{files}{ver}{name}, fds => [qw/0 input 3 output 1 >result/], args => [$test]); + } catch { + die [WA, "Wrong answer"] + }; + scalar slurp 'result'; } 1;