Fix test failure introduced several commits ago
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon / Format.pm
CommitLineData
ddceb393
MG
1package Gruntmaster::Daemon::Format;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Exporter/;
9577371b 7no if $] > 5.017011, warnings => 'experimental::smartmatch';
ddceb393
MG
8
9use POSIX qw//;
10use File::Basename qw/fileparse/;
f5e29130 11use File::Slurp qw/write_file/;
ddceb393 12use Gruntmaster::Daemon::Constants qw/TLE OLE DIED NZX/;
ddceb393
MG
13use Time::HiRes qw/alarm/;
14use List::MoreUtils qw/natatime/;
f5e29130 15use Log::Log4perl qw/get_logger/;
ddceb393 16use IPC::Signal qw/sig_name sig_num/;
ddceb393 17
3e7fd903 18our $VERSION = "5999.000_004";
ad77b7d3 19our @EXPORT_OK = qw/prepare_files/;
ddceb393
MG
20
21##################################################
22
23sub command_and_args{
24 my ($format, $basename) = @_;
9577371b
MG
25
26 given($format) {
5a4392d5 27 "./$basename" when [qw/C CPP GCCGO GOLANG HASKELL PASCAL/];
9577371b 28 "./$basename.exe" when 'MONO';
cdd811c0
MG
29 java => $basename when 'JAVA';
30 perl => $basename when 'PERL';
31 python => $basename when 'PYTHON';
9577371b
MG
32 default { die "Don't know how to execute format $format" }
33 }
ddceb393
MG
34}
35
36sub mkrun{
37 my $format = shift;
38 sub{
d16a999c 39 local *__ANON__ = 'mkrun_runner';
ddceb393 40 my ($name, %args) = @_;
d16a999c 41 get_logger->trace("Running $name...");
ddceb393
MG
42 my $basename = fileparse $name, qr/\.[^.]*/;
43 my $ret = fork // die 'Cannot fork';
44 if ($ret) {
45 my $tle;
46 local $SIG{ALRM} = sub { kill KILL => $ret; $tle = 1};
47 alarm $args{timeout} if exists $args{timeout};
b3f845d3 48 waitpid $ret, 0;
ddceb393
MG
49 alarm 0;
50 my $sig = $? & 127;
51 my $signame = sig_name $sig;
52 die [TLE, "Time Limit Exceeded"] if $tle;
53 die [OLE, 'Output Limit Exceeded'] if $sig && $signame eq 'XFSZ';
513eadc3 54 die [DIED, "Crash (SIG$signame)"] if $sig && $signame ne 'PIPE';
0a9f55a4 55 die [NZX, "Non-zero exit status: " . ($? >> 8)] if $? >> 8;
ddceb393
MG
56 } else {
57 my @fds = exists $args{fds} ? @{$args{fds}} : ();
58 $^F = 50;
59 POSIX::close $_ for 0 .. $^F;
60 my $it = natatime 2, @fds;
61 while (my ($fd, $file) = $it->()) {
62 open my $fh, $file or die $!;
63 my $oldfd = fileno $fh;
64 if ($oldfd != $fd) {
65 POSIX::dup2 $oldfd, $fd or die $!;
66 POSIX::close $oldfd or die $!;
67 }
68 }
69 exec 'gruntmaster-exec', $args{mlimit} // 0, $args{olimit} // 0, command_and_args($format, $basename), exists $args{args} ? @{$args{args}} : ();
89edda82 70 exit 42
ddceb393
MG
71 }
72 }
73}
74
ad77b7d3
MG
75sub prepare{
76 my ($name, $format) = @_;
ad77b7d3
MG
77 get_logger->trace("Preparing file $name...");
78
42e80e2d
MG
79 $Gruntmaster::Daemon::errors .= `gruntmaster-compile $format $name 2>&1`;
80 $Gruntmaster::Daemon::errors .= "\n";
ad77b7d3
MG
81 die 'Compile error' if $?
82}
83
84sub prepare_files{
85 my $meta = shift;
86
759d3ca5 87 for my $file (values %{$meta->{files}}) {
ad77b7d3
MG
88 my ($format, $name, $content) = @{$file}{qw/format name content/};
89
90 $file->{run} = mkrun($format);
91 write_file $name, $content;
92 prepare $name, $format;
93 }
94}
95
bc372959
MG
961;
97__END__
98
99=encoding utf-8
100
101=head1 NAME
102
103Gruntmaster::Daemon::Format - Utility functions for handling source files
104
105=head1 SYNOPSIS
106
107 use Gruntmaster::Daemon::Format qw/prepare_files/;
108 prepare_files { files => {
109 prog => {
110 name => 'prog.pl',
111 format => 'PERL',
112 content => 'print "Hello, world!"'
113 },
114 ver => {
115 name => 'ver.cpp',
116 format => 'CPP',
117 content => ...
118 },
119 }};
120
121=head1 DESCRIPTION
122
123Gruntmaster::Daemon::Format exports utility functions for handling source files.
124
125=over
126
127=item B<prepare_files> I<$meta>
128
129Compiles all the source files in C<< $meta->{files} >>.
130
131=back
132
133=head1 AUTHOR
134
135Marius Gavrilescu E<lt>marius@ieval.roE<gt>
136
137=head1 COPYRIGHT AND LICENSE
138
139Copyright (C) 2014 by Marius Gavrilescu
140
141This library is free software: you can redistribute it and/or modify
142it under the terms of the GNU Affero General Public License as published by
143the Free Software Foundation, either version 3 of the License, or
144(at your option) any later version.
145
146
147=cut
This page took 0.052119 seconds and 4 git commands to generate.