]>
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 | |
3e7fd903 | 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 { |
fdb99417 MG |
50 | my $ret = fork // die 'Cannot fork'; |
51 | if ($ret) { | |
52 | waitpid $ret, 0; | |
53 | } else { | |
54 | open STDOUT, ">$er"; | |
55 | exec 'gruntmaster-exec', @args; | |
56 | } | |
c40a2dd0 | 57 | } |
fdb99417 MG |
58 | |
59 | die "gruntmaster-exec died\n" if -z $er; | |
60 | my ($excode, $exmsg) = read_file $er; | |
61 | unlink $er; | |
62 | chomp ($excode, $exmsg); | |
63 | get_logger->trace("Exec result: $excode $exmsg"); | |
64 | die [$excode, $exmsg] if $excode > 0; | |
c40a2dd0 MG |
65 | } |
66 | ||
ddceb393 MG |
67 | sub command_and_args{ |
68 | my ($format, $basename) = @_; | |
9577371b MG |
69 | |
70 | given($format) { | |
5a4392d5 | 71 | "./$basename" when [qw/C CPP GCCGO GOLANG HASKELL PASCAL/]; |
9577371b | 72 | "./$basename.exe" when 'MONO'; |
cdd811c0 MG |
73 | java => $basename when 'JAVA'; |
74 | perl => $basename when 'PERL'; | |
75 | python => $basename when 'PYTHON'; | |
c40a2dd0 | 76 | default { die "Don't know how to execute format $format\n" } |
9577371b | 77 | } |
ddceb393 MG |
78 | } |
79 | ||
80 | sub mkrun{ | |
81 | my $format = shift; | |
82 | sub{ | |
d16a999c | 83 | local *__ANON__ = 'mkrun_runner'; |
ddceb393 | 84 | my ($name, %args) = @_; |
d16a999c | 85 | get_logger->trace("Running $name..."); |
ddceb393 | 86 | my $basename = fileparse $name, qr/\.[^.]*/; |
c40a2dd0 MG |
87 | my @args; |
88 | push @args, '--timeout', $args{timeout} if $args{timeout}; | |
89 | push @args, '--mlimit', $args{mlimit} if $args{mlimit}; | |
90 | push @args, '--olimit', $args{olimit} if $args{olimit}; | |
91 | my @fds = exists $args{fds} ? @{$args{fds}} : (); | |
92 | my $it = natatime 2, @fds; | |
93 | while (my ($fd, $file) = $it->()) { | |
94 | push @args, "--fd=$fd $file"; | |
ddceb393 | 95 | } |
fdb99417 | 96 | execlist $basename, @args, command_and_args($format, $basename); |
ddceb393 MG |
97 | } |
98 | } | |
99 | ||
ad77b7d3 MG |
100 | sub prepare{ |
101 | my ($name, $format) = @_; | |
ad77b7d3 MG |
102 | get_logger->trace("Preparing file $name..."); |
103 | ||
c40a2dd0 | 104 | try { |
fdb99417 | 105 | execlist prog => '--fd=1 >>errors', '--fd=2 >>errors', 'gruntmaster-compile', $format, $name; |
c40a2dd0 MG |
106 | } catch { |
107 | die "Compile error\n" | |
108 | } finally { | |
109 | $Gruntmaster::Daemon::errors .= read_file 'errors'; | |
110 | $Gruntmaster::Daemon::errors .= "\n"; | |
111 | unlink 'errors'; | |
112 | }; | |
ad77b7d3 MG |
113 | } |
114 | ||
115 | sub prepare_files{ | |
116 | my $meta = shift; | |
fdb99417 | 117 | runvm $_ for keys %{$meta->{files}}; |
ad77b7d3 | 118 | |
759d3ca5 | 119 | for my $file (values %{$meta->{files}}) { |
ad77b7d3 MG |
120 | my ($format, $name, $content) = @{$file}{qw/format name content/}; |
121 | ||
122 | $file->{run} = mkrun($format); | |
123 | write_file $name, $content; | |
124 | prepare $name, $format; | |
125 | } | |
126 | } | |
127 | ||
bc372959 MG |
128 | 1; |
129 | __END__ | |
130 | ||
131 | =encoding utf-8 | |
132 | ||
133 | =head1 NAME | |
134 | ||
135 | Gruntmaster::Daemon::Format - Utility functions for handling source files | |
136 | ||
137 | =head1 SYNOPSIS | |
138 | ||
139 | use Gruntmaster::Daemon::Format qw/prepare_files/; | |
140 | prepare_files { files => { | |
141 | prog => { | |
142 | name => 'prog.pl', | |
143 | format => 'PERL', | |
144 | content => 'print "Hello, world!"' | |
145 | }, | |
146 | ver => { | |
147 | name => 'ver.cpp', | |
148 | format => 'CPP', | |
149 | content => ... | |
150 | }, | |
151 | }}; | |
152 | ||
153 | =head1 DESCRIPTION | |
154 | ||
155 | Gruntmaster::Daemon::Format exports utility functions for handling source files. | |
156 | ||
157 | =over | |
158 | ||
159 | =item B<prepare_files> I<$meta> | |
160 | ||
161 | Compiles all the source files in C<< $meta->{files} >>. | |
162 | ||
163 | =back | |
164 | ||
165 | =head1 AUTHOR | |
166 | ||
167 | Marius Gavrilescu E<lt>marius@ieval.roE<gt> | |
168 | ||
169 | =head1 COPYRIGHT AND LICENSE | |
170 | ||
171 | Copyright (C) 2014 by Marius Gavrilescu | |
172 | ||
173 | This library is free software: you can redistribute it and/or modify | |
174 | it under the terms of the GNU Affero General Public License as published by | |
175 | the Free Software Foundation, either version 3 of the License, or | |
176 | (at your option) any later version. | |
177 | ||
178 | ||
179 | =cut |