From: Petru Trimbitas Date: Fri, 15 Apr 2022 18:26:23 +0000 (+0300) Subject: Add javascript X-Git-Url: http://git.ieval.ro/?p=gruntmaster-daemon.git;a=commitdiff_plain;h=HEAD;hp=12c4881b366d0b5dcb673fe7f30a6970b39926ba Add javascript --- diff --git a/Makefile.PL b/Makefile.PL index 46c5cb5..adc109a 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -18,6 +18,7 @@ WriteMakefile( Hash::Merge 0 List::Util 0 Log::Log4perl 0 + Test::HTTP::LocalServer 0 Test::More 0 YAML::Tiny 0/, }, diff --git a/README b/README deleted file mode 100644 index 1b0e7ab..0000000 --- a/README +++ /dev/null @@ -1,40 +0,0 @@ -Gruntmaster-Daemon version 0.001 -================================ - -The README is used to introduce the module and provide instructions on -how to install the module, any machine dependencies it may have (for -example C compilers and installed libraries) and any other information -that should be provided before the module is installed. - -A README file is required for CPAN modules since CPAN extracts the -README file from a module distribution so that people browsing the -archive can use it get an idea of the modules uses. It is usually a -good idea to provide version information here so that people can -decide whether fixes for the module are worth downloading. - -INSTALLATION - -To install this module type the following: - - perl Makefile.PL - make - make test - make install - -DEPENDENCIES - -This module requires these other modules and libraries: - - blah blah blah - -COPYRIGHT AND LICENCE - -Put the correct copyright and licence information here. - -Copyright (C) 2013 by Marius Gavrilescu - -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself, either Perl version 5.18.1 or, -at your option, any later version of Perl 5 you may have available. - - diff --git a/gruntmaster-compile b/gruntmaster-compile index 2538cde..3599621 100755 --- a/gruntmaster-compile +++ b/gruntmaster-compile @@ -41,11 +41,12 @@ given ($format){ exit } - when ([qw/GOLFSCRIPT JULIA PERL PHP PYTHON PYTHON3 RUBY SBCL/]){ + when ([qw/GOLFSCRIPT JAVASCRIPT JULIA PERL PHP PYTHON PYTHON3 RUBY SBCL/]){ open IN, '<', $name; open OUT, '>', $basename; print OUT "#!/usr/bin/golfscript\n" if $_ eq 'GOLFSCRIPT'; print OUT "#!/usr/bin/julia -O\n" if $_ eq 'JULIA'; + print OUT "#!/usr/bin/node\n" if $_ eq 'JAVASCRIPT'; print OUT "#!/usr/bin/perl\n" if $_ eq 'PERL'; print OUT "#!/usr/bin/php -d ONLINE_JUDGE=true\n" if $_ eq 'PHP'; print OUT "#!/usr/bin/python2.7\n" if $_ eq 'PYTHON'; diff --git a/lib/Gruntmaster/Daemon.pm b/lib/Gruntmaster/Daemon.pm index 7a9dea6..9597572 100644 --- a/lib/Gruntmaster/Daemon.pm +++ b/lib/Gruntmaster/Daemon.pm @@ -8,6 +8,7 @@ our $VERSION = '5999.000_005'; use Gruntmaster::Daemon::Constants qw/ERR/; use Gruntmaster::Daemon::Format qw/prepare_files stopvms/; +use Gruntmaster::SendResults qw/send_results_request/; use File::Slurp qw/read_file/; use File::Temp qw/tempdir/; @@ -26,6 +27,7 @@ use constant FORMAT_EXTENSION => { GOLFSCRIPT => 'gs', HASKELL => 'hs', JAVA => 'java', + JAVASCRIPT => 'js', JULIA => 'jl', MONO => 'cs', OBERON => 'm', @@ -110,6 +112,7 @@ sub process{ $meta->{errors} = $errors; get_logger->info('Job result: ' . $meta->{result_text}); + send_results_request($meta->{job_id}, $meta->{result}, $meta->{result_text}); } sub process_job { @@ -129,8 +132,8 @@ sub process_job { my $timeout_override = db()->query('SELECT timeout FROM limits WHERE problem=? AND format=?', $job->{problem}, $job->{format})->list; $meta->{timeout} = $timeout_override if defined $timeout_override; $meta->{tests} = decode_json $pb->{tests} if $meta->{runner} eq 'File'; - $job->{contest} &&= contest_entry($job->{contest}); - delete $meta->{precnt} unless $job->{contest} && $job->{contest}{started} && !$job->{contest}{finished}; ## no critic (ProhibitNegativeExpressionsInUnlessAndUntilConditions) + my $contest = $job->{contest} && contest_entry($job->{contest}); + delete $meta->{precnt} unless $contest && $contest->{started} && !$contest->{finished}; ## no critic (ProhibitNegativeExpressionsInUnlessAndUntilConditions) $meta->{testcnt} = $meta->{precnt} if $meta->{precnt}; $meta->{files}{ver} = { @@ -138,6 +141,7 @@ sub process_job { format => $pb->{verformat}, content => $pb->{versource}, } if $pb->{verformat}; + $meta->{job_id} = $job->{id}; process $meta; diff --git a/lib/Gruntmaster/SendResults.pm b/lib/Gruntmaster/SendResults.pm new file mode 100644 index 0000000..e01a9a6 --- /dev/null +++ b/lib/Gruntmaster/SendResults.pm @@ -0,0 +1,36 @@ +package Gruntmaster::SendResults; + +use 5.014000; +use strict; +use warnings; +use parent qw/Exporter/; + +use HTTP::Request; +use JSON qw/encode_json decode_json/; +use LWP::UserAgent; + +our $VERSION = '5999.000_005'; +our @EXPORT_OK = qw/send_results_request/; + +sub send_results_request { + my ($job_id, $result, $result_text) = @_; + + my $url = $ENV{REMOTE_ADDRESS}; + my $header = [ + 'Content-Type' => 'application/json' + ]; + my $data = { + gm_id => $job_id, + result => $result, + result_text => $result_text + }; + + my $encoded_data = encode_json($data); + my $request = HTTP::Request->new(POST => $url, $header, $encoded_data); + my $ua = LWP::UserAgent->new(); + my $response = $ua->request($request); + + return $response; +} + +1; diff --git a/t/00-compile.t b/t/00-compile.t index 28dd755..eaa82ca 100644 --- a/t/00-compile.t +++ b/t/00-compile.t @@ -1,7 +1,11 @@ -#!/usr/bin/perl -w use v5.14; use strict; use warnings; +use FindBin; + use Test::More tests => 1; -BEGIN { use_ok('Gruntmaster::Daemon') }; +BEGIN { + $ENV{PATH} = "$FindBin::Bin/../blib/script:" . $ENV{PATH}; + use_ok('Gruntmaster::Daemon') +}; diff --git a/t/01-jobs.t b/t/01-jobs.t index 3f00b3d..c37e081 100644 --- a/t/01-jobs.t +++ b/t/01-jobs.t @@ -1,4 +1,3 @@ -#!/usr/bin/perl -w use v5.14; use strict; use warnings; @@ -10,6 +9,7 @@ use File::Basename qw/fileparse/; use File::Slurp qw/read_file/; use File::Temp qw/tempdir/; use File::Which qw/which/; +use FindBin; use Hash::Merge qw/merge/; use List::Util qw/sum/; use Log::Log4perl; @@ -26,6 +26,7 @@ use constant COMPILER => { GOLFSCRIPT golfscript MONO gmcs JAVA javac + JAVASCRIPT node JULIA julia PASCAL fpc GOLANG go @@ -40,7 +41,7 @@ use constant COMPILER => { OCAML ocaml SBCL sbcl/}; -my %needs_fork = map { $_ => 1 } qw/GOLANG GOLFSCRIPT GCCGO JAVA JULIA RUBY/; +my %needs_fork = map { $_ => 1 } qw/GOLANG GOLFSCRIPT GCCGO JAVA JAVASCRIPT JULIA RUBY SBCL/; my $loglevel = $ENV{TEST_LOG_LEVEL} // ($ENV{TEST_VERBOSE} ? 'TRACE' : 'OFF'); my $log_conf = <init(\$log_conf); $ENV{PATH} = getcwd . ':' . $ENV{PATH}; +$ENV{PATH} = "$FindBin::Bin/../blib/script:" . $ENV{PATH}; umask 0022; sub check_job{ diff --git a/t/02-send-results-request.t b/t/02-send-results-request.t new file mode 100644 index 0000000..6d6eff8 --- /dev/null +++ b/t/02-send-results-request.t @@ -0,0 +1,67 @@ +#!/usr/bin/perl -w +use v5.14; +use strict; +use warnings; + +use Gruntmaster::Daemon qw/process/; + +use Cwd qw/getcwd/; +use File::Slurp qw/read_file/; +use File::Temp qw/tempdir/; +use HTTP::Tiny; +use Hash::Merge qw/merge/; +use JSON qw/encode_json decode_json/; +use Log::Log4perl; +use Test::HTTP::LocalServer; +use Test::More tests => 3; +use YAML::Tiny qw/LoadFile/; + +my $server = Test::HTTP::LocalServer->spawn; +my $response = HTTP::Tiny->new->get( $server->url ); +$ENV{REMOTE_ADDRESS} = $server->url; + +my $loglevel = $ENV{TEST_LOG_LEVEL} // ($ENV{TEST_VERBOSE} ? 'TRACE' : 'OFF'); +my $log_conf = <init(\$log_conf); + +$ENV{PATH} = getcwd . ':' . $ENV{PATH}; +my $tempdir = tempdir "gruntmasterd-testingXXXX", TMPDIR => 1, CLEANUP => 1; +chmod 0777, $tempdir; + +sub process_meta() { + my $pbmeta = LoadFile "t/problems/aplusb/meta.yml"; + for (1 .. $pbmeta->{testcnt}) { + $pbmeta->{infile}[$_ - 1] = read_file "t/problems/aplusb/$_.in" + if $pbmeta->{generator} eq 'File'; + $pbmeta->{okfile}[$_ - 1] = read_file "t/problems/aplusb/$_.ok" + if $pbmeta->{runner} && $pbmeta->{runner} eq 'File'; + } + my $source = ; + my $meta = LoadFile "$source/meta.yml"; + $meta->{files}{prog}{content} = read_file "$source/$meta->{files}{prog}{name}"; + $meta = merge $meta, $pbmeta; + my $savedcwd = getcwd; + chdir $tempdir; + $meta->{job_id} = 1; + Gruntmaster::Daemon::process($meta); + chdir $savedcwd; +} + +sub send_request() { + process_meta(); + + my $requests = $server->get_log; + is $requests =~ qr/"result_text":"40 points"/, 1; + is $requests =~ qr/"result":10/, 1; + is $requests =~ qr/"gm_id":1/, 1; +} + +send_request(); + +$server->stop; diff --git a/t/problems/hello/tests/javascript/meta.yml b/t/problems/hello/tests/javascript/meta.yml new file mode 100644 index 0000000..c7be177 --- /dev/null +++ b/t/problems/hello/tests/javascript/meta.yml @@ -0,0 +1,7 @@ +test_name: Javascript +test_description: Hello world in Javascript +problem: hello +files: + prog: + format: JAVASCRIPT + name: prog.js diff --git a/t/problems/hello/tests/javascript/prog.js b/t/problems/hello/tests/javascript/prog.js new file mode 100644 index 0000000..7e2c877 --- /dev/null +++ b/t/problems/hello/tests/javascript/prog.js @@ -0,0 +1 @@ +console.log('Hello World!')