1 package Gruntmaster
::Daemon
::Format
;
6 use parent qw
/Exporter/;
7 no if $] > 5.017011, warnings
=> 'experimental::smartmatch';
10 use File
::Basename qw
/fileparse/;
11 use File
::Slurp qw
/read_file write_file/;
12 use List
::MoreUtils qw
/natatime/;
13 use Log
::Log4perl qw
/get_logger/;
15 use String
::ShellQuote qw
/shell_quote/;
18 our $VERSION = '5999.000_004';
19 our @EXPORT_OK = qw
/prepare_files stopvms/;
21 ##################################################
27 return unless $ENV{GRUNTMASTER_VM
};
28 mkfifo
"$name.in", 0600;
29 mkfifo
"$name.out", 0600;
30 get_logger
->trace("Starting VM $name");
31 $vm{$name} = Expect
::Simple
->new({
32 Cmd
=> "$ENV{GRUNTMASTER_VM} $name",
34 DisconnectCmd
=> 'exit',
40 sub stopvms
{ %vm = () }
44 my $er = "exec-result-$vm";
46 my $cmd = ">$er " . shell_quote
'gruntmaster-exec', @args;
47 get_logger
->trace("Running in VM $vm: $cmd");
50 my $ret = fork // die "Cannot fork\n";
54 open STDOUT
, '>', $er or die "Cannot open $er\n";
55 get_logger
->trace("Running: gruntmaster-exec @args");
56 exec 'gruntmaster-exec', @args;
60 die "gruntmaster-exec died\n" if -z
$er;
61 my ($excode, $exmsg) = read_file
$er;
63 chomp ($excode, $exmsg); ## no critic (ProhibitParensWithBuiltins)
64 get_logger
->trace("Exec result: $excode $exmsg");
65 die [$excode, $exmsg] if $excode > 0; ## no critic (RequireCarping)
69 my ($format, $basename) = @_;
72 "./$basename" when [qw
/C CPP GCCGO GOLANG HASKELL PASCAL
/];
73 "./$basename.exe" when 'MONO';
74 java
=> $basename when 'JAVA';
75 perl
=> $basename when 'PERL';
76 python
=> $basename when 'PYTHON';
77 default { die "Don't know how to execute format $format\n" }
84 local *__ANON__
= 'mkrun_runner';
85 my ($name, %args) = @_;
86 get_logger
->trace("Running $name...");
87 my $basename = fileparse
$name, qr/[.][^.]*/s;
89 push @args, '--timeout', $args{timeout
} if $args{timeout
};
90 push @args, '--mlimit', $args{mlimit
} if $args{mlimit
};
91 push @args, '--olimit', $args{olimit
} if $args{olimit
};
92 my @fds = exists $args{fds
} ? @
{$args{fds
}} : ();
93 my $it = natatime
2, @fds;
94 while (my ($fd, $file) = $it->()) {
95 push @args, "--fd=$fd $file";
97 execlist
$basename, @args, command_and_args
($format, $basename);
102 my ($name, $format) = @_;
103 get_logger
->trace("Preparing file $name...");
106 execlist prog
=> '--fd=1 >>errors', '--fd=2 >>errors', 'gruntmaster-compile', $format, $name;
108 die "Compile error\n"
110 $Gruntmaster::Daemon
::errors
.= read_file
'errors';
111 $Gruntmaster::Daemon
::errors
.= "\n";
118 runvm
$_ for keys %{$meta->{files
}};
120 for my $file (values %{$meta->{files
}}) {
121 my ($format, $name, $content) = @
{$file}{qw
/format name content/};
123 $file->{run
} = mkrun
($format);
124 write_file
$name, $content;
125 prepare
$name, $format;
136 Gruntmaster::Daemon::Format - Utility functions for handling source files
140 use Gruntmaster::Daemon::Format qw/prepare_files/;
141 prepare_files { files => {
145 content => 'print "Hello, world!"'
156 Gruntmaster::Daemon::Format exports utility functions for handling source files.
160 =item B<prepare_files> I<$meta>
162 Compiles all the source files in C<< $meta->{files} >>.
168 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
170 =head1 COPYRIGHT AND LICENSE
172 Copyright (C) 2014 by Marius Gavrilescu
174 This library is free software: you can redistribute it and/or modify
175 it under the terms of the GNU Affero General Public License as published by
176 the Free Software Foundation, either version 3 of the License, or
177 (at your option) any later version.