Commit | Line | Data |
---|---|---|
ddceb393 MG |
1 | package Gruntmaster::Daemon::Format; |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6 | use parent qw/Exporter/; | |
9577371b | 7 | no if $] > 5.017011, warnings => 'experimental::smartmatch'; |
ddceb393 | 8 | |
fdb99417 | 9 | use Expect::Simple; |
ddceb393 | 10 | use File::Basename qw/fileparse/; |
c40a2dd0 | 11 | use File::Slurp qw/read_file write_file/; |
ddceb393 | 12 | use List::MoreUtils qw/natatime/; |
f5e29130 | 13 | use Log::Log4perl qw/get_logger/; |
fdb99417 MG |
14 | use POSIX qw/mkfifo/; |
15 | use String::ShellQuote qw/shell_quote/; | |
c40a2dd0 | 16 | use Try::Tiny; |
ddceb393 | 17 | |
99d37110 | 18 | our $VERSION = '5999.000_004'; |
fdb99417 | 19 | our @EXPORT_OK = qw/prepare_files stopvms/; |
ddceb393 MG |
20 | |
21 | ################################################## | |
22 | ||
fdb99417 MG |
23 | our (%vm); |
24 | ||
25 | sub runvm { | |
26 | my ($name) = @_; | |
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", | |
33 | Prompt => '# ', | |
34 | DisconnectCmd => 'exit', | |
35 | RawPty => 1, | |
36 | Timeout => 10, | |
37 | }); | |
38 | } | |
39 | ||
40 | sub stopvms { %vm = () } | |
41 | ||
c40a2dd0 | 42 | sub execlist { |
fdb99417 MG |
43 | my ($vm, @args) = @_; |
44 | my $er = "exec-result-$vm"; | |
45 | if ($vm{$vm}) { | |
46 | my $cmd = ">$er " . shell_quote 'gruntmaster-exec', @args; | |
47 | get_logger->trace("Running in VM $vm: $cmd"); | |
48 | $vm{$vm}->send($cmd); | |
c40a2dd0 | 49 | } else { |
99d37110 | 50 | my $ret = fork // die "Cannot fork\n"; |
fdb99417 MG |
51 | if ($ret) { |
52 | waitpid $ret, 0; | |
53 | } else { | |
99d37110 | 54 | open STDOUT, '>', $er or die "Cannot open $er\n"; |
e61f1a82 | 55 | get_logger->trace("Running: gruntmaster-exec @args"); |
fdb99417 MG |
56 | exec 'gruntmaster-exec', @args; |
57 | } | |
c40a2dd0 | 58 | } |
fdb99417 MG |
59 | |
60 | die "gruntmaster-exec died\n" if -z $er; | |
61 | my ($excode, $exmsg) = read_file $er; | |
62 | unlink $er; | |
99d37110 | 63 | chomp ($excode, $exmsg); ## no critic (ProhibitParensWithBuiltins) |
fdb99417 | 64 | get_logger->trace("Exec result: $excode $exmsg"); |
99d37110 | 65 | die [$excode, $exmsg] if $excode > 0; ## no critic (RequireCarping) |
c40a2dd0 MG |
66 | } |
67 | ||
ddceb393 MG |
68 | sub mkrun{ |
69 | my $format = shift; | |
70 | sub{ | |
d16a999c | 71 | local *__ANON__ = 'mkrun_runner'; |
ddceb393 | 72 | my ($name, %args) = @_; |
d16a999c | 73 | get_logger->trace("Running $name..."); |
99d37110 | 74 | my $basename = fileparse $name, qr/[.][^.]*/s; |
4e08f696 | 75 | my @args = ('--nobody'); |
c40a2dd0 MG |
76 | push @args, '--timeout', $args{timeout} if $args{timeout}; |
77 | push @args, '--mlimit', $args{mlimit} if $args{mlimit}; | |
78 | push @args, '--olimit', $args{olimit} if $args{olimit}; | |
79 | my @fds = exists $args{fds} ? @{$args{fds}} : (); | |
80 | my $it = natatime 2, @fds; | |
81 | while (my ($fd, $file) = $it->()) { | |
82 | push @args, "--fd=$fd $file"; | |
ddceb393 | 83 | } |
c2af0332 | 84 | execlist $basename, @args, "./$basename" |
ddceb393 MG |
85 | } |
86 | } | |
87 | ||
ad77b7d3 MG |
88 | sub prepare{ |
89 | my ($name, $format) = @_; | |
ad77b7d3 MG |
90 | get_logger->trace("Preparing file $name..."); |
91 | ||
c40a2dd0 | 92 | try { |
fdb99417 | 93 | execlist prog => '--fd=1 >>errors', '--fd=2 >>errors', 'gruntmaster-compile', $format, $name; |
c40a2dd0 MG |
94 | } catch { |
95 | die "Compile error\n" | |
96 | } finally { | |
97 | $Gruntmaster::Daemon::errors .= read_file 'errors'; | |
98 | $Gruntmaster::Daemon::errors .= "\n"; | |
99 | unlink 'errors'; | |
100 | }; | |
ad77b7d3 MG |
101 | } |
102 | ||
103 | sub prepare_files{ | |
104 | my $meta = shift; | |
fdb99417 | 105 | runvm $_ for keys %{$meta->{files}}; |
ad77b7d3 | 106 | |
759d3ca5 | 107 | for my $file (values %{$meta->{files}}) { |
ad77b7d3 MG |
108 | my ($format, $name, $content) = @{$file}{qw/format name content/}; |
109 | ||
110 | $file->{run} = mkrun($format); | |
111 | write_file $name, $content; | |
112 | prepare $name, $format; | |
113 | } | |
114 | } | |
115 | ||
bc372959 MG |
116 | 1; |
117 | __END__ | |
118 | ||
119 | =encoding utf-8 | |
120 | ||
121 | =head1 NAME | |
122 | ||
123 | Gruntmaster::Daemon::Format - Utility functions for handling source files | |
124 | ||
125 | =head1 SYNOPSIS | |
126 | ||
127 | use Gruntmaster::Daemon::Format qw/prepare_files/; | |
128 | prepare_files { files => { | |
129 | prog => { | |
130 | name => 'prog.pl', | |
131 | format => 'PERL', | |
132 | content => 'print "Hello, world!"' | |
133 | }, | |
134 | ver => { | |
135 | name => 'ver.cpp', | |
136 | format => 'CPP', | |
137 | content => ... | |
138 | }, | |
139 | }}; | |
140 | ||
141 | =head1 DESCRIPTION | |
142 | ||
143 | Gruntmaster::Daemon::Format exports utility functions for handling source files. | |
144 | ||
145 | =over | |
146 | ||
147 | =item B<prepare_files> I<$meta> | |
148 | ||
149 | Compiles all the source files in C<< $meta->{files} >>. | |
150 | ||
151 | =back | |
152 | ||
153 | =head1 AUTHOR | |
154 | ||
155 | Marius Gavrilescu E<lt>marius@ieval.roE<gt> | |
156 | ||
157 | =head1 COPYRIGHT AND LICENSE | |
158 | ||
159 | Copyright (C) 2014 by Marius Gavrilescu | |
160 | ||
161 | This library is free software: you can redistribute it and/or modify | |
162 | it under the terms of the GNU Affero General Public License as published by | |
163 | the Free Software Foundation, either version 3 of the License, or | |
164 | (at your option) any later version. | |
165 | ||
166 | ||
167 | =cut |