Improve Interactive runner
[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 8
cd1a16d1 9use Digest::SHA qw/sha256_hex/;
61470035 10use Expect;
ddceb393 11use File::Basename qw/fileparse/;
cd1a16d1 12use File::Copy qw/cp/;
c40a2dd0 13use File::Slurp qw/read_file write_file/;
ddceb393 14use List::MoreUtils qw/natatime/;
f5e29130 15use Log::Log4perl qw/get_logger/;
fdb99417 16use String::ShellQuote qw/shell_quote/;
c40a2dd0 17use Try::Tiny;
ddceb393 18
99d37110 19our $VERSION = '5999.000_004';
fdb99417 20our @EXPORT_OK = qw/prepare_files stopvms/;
ddceb393
MG
21
22##################################################
23
61470035 24our (%vm, %pid);
fdb99417
MG
25
26sub runvm {
0f817fa6 27 my ($name, $arg) = @_;
fdb99417 28 return unless $ENV{GRUNTMASTER_VM};
0f817fa6
MG
29 my $cmd = $ENV{GRUNTMASTER_VM};
30 $cmd .= ' ' . $arg if $arg;
31 get_logger->trace("Starting VM $name ($cmd)");
61470035
MG
32 $vm{$name} = Expect->spawn($cmd);
33 $vm{$name}->raw_pty(1);
34 $vm{$name}->expect(5, '# ') or get_logger->logdie("Error while starting VM $name: ". $vm{$name}->error);
fdb99417
MG
35}
36
61470035
MG
37sub stopvms {
38 kill KILL => $_->pid for values %vm;
39 %vm = %pid = ();
40}
41
42sub execlist_finish {
43 my ($vm, $kill) = @_;
44
45 if ($vm{$vm}) {
46 warn "Cannot kill VM" if $kill;
47 $vm{$vm}->expect(5, '# ');
48 } else {
49 kill KILL => $pid{$vm} if $kill;
50 waitpid $pid{$vm}, 0;
51 }
52 return if $kill;
53
54 my $er = "exec-result-$vm";
55 die "gruntmaster-exec died\n" if -z $er;
56 my ($excode, $exmsg) = read_file $er;
57 unlink $er;
58 chomp ($excode, $exmsg); ## no critic (ProhibitParensWithBuiltins)
59 get_logger->trace("Exec result from $vm: $excode $exmsg");
60 die [$excode, $exmsg] if $excode; ## no critic (RequireCarping)
61}
fdb99417 62
c40a2dd0 63sub execlist {
fdb99417
MG
64 my ($vm, @args) = @_;
65 my $er = "exec-result-$vm";
66 if ($vm{$vm}) {
67 my $cmd = ">$er " . shell_quote 'gruntmaster-exec', @args;
68 get_logger->trace("Running in VM $vm: $cmd");
69 $vm{$vm}->send($cmd);
c40a2dd0 70 } else {
61470035
MG
71 $pid{$vm} = fork // die "Cannot fork\n";
72 unless ($pid{$vm}) {
99d37110 73 open STDOUT, '>', $er or die "Cannot open $er\n";
e61f1a82 74 get_logger->trace("Running: gruntmaster-exec @args");
fdb99417
MG
75 exec 'gruntmaster-exec', @args;
76 }
c40a2dd0
MG
77 }
78}
79
ddceb393
MG
80sub 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...");
99d37110 86 my $basename = fileparse $name, qr/[.][^.]*/s;
42ce8ba9 87 my @args = ('--sudo');
13b1661a 88 push @args, '--keep-stderr' if $ENV{TEST_VERBOSE};
c40a2dd0
MG
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";
ddceb393 96 }
61470035
MG
97 execlist $basename, @args, '--', "./$basename", @{$args{args}};
98 execlist_finish $basename unless $args{nonblocking}
ddceb393
MG
99 }
100}
101
ad77b7d3
MG
102sub prepare{
103 my ($name, $format) = @_;
ad77b7d3
MG
104 get_logger->trace("Preparing file $name...");
105
c40a2dd0 106 try {
fdb99417 107 execlist prog => '--fd=1 >>errors', '--fd=2 >>errors', 'gruntmaster-compile', $format, $name;
61470035 108 execlist_finish 'prog';
c40a2dd0 109 } catch {
36ecc8ea
MG
110 my $exmsg = $_->[1];
111 die "Compile error ($exmsg)\n"
c40a2dd0
MG
112 } finally {
113 $Gruntmaster::Daemon::errors .= read_file 'errors';
837911b3 114 $Gruntmaster::Daemon::errors .= "\n" if -s 'errors';
c40a2dd0
MG
115 unlink 'errors';
116 };
ad77b7d3
MG
117}
118
119sub prepare_files{
120 my $meta = shift;
0f817fa6
MG
121 if ($meta->{runner} eq 'Interactive') {
122 runvm ver => '-serial unix:vm.sock,nowait,server';
123 runvm prog => '-serial unix:vm.sock,nowait';
124 } else {
125 runvm $_ for keys %{$meta->{files}};
126 }
ad77b7d3 127
759d3ca5 128 for my $file (values %{$meta->{files}}) {
ad77b7d3
MG
129 my ($format, $name, $content) = @{$file}{qw/format name content/};
130
131 $file->{run} = mkrun($format);
132 write_file $name, $content;
cd1a16d1
MG
133 if ($ENV{GRUNTMASTER_CCACHE}) {
134 my $key = lc sha256_hex($content) . '-' . $format;
135 my $cachefn = "$ENV{GRUNTMASTER_CCACHE}/$key";
136 my $exefn = fileparse $name, qr/[.][^.]*/s;
137 if (cp $cachefn, $exefn) {
138 get_logger->trace("File $name found in compilation cache")
139 } else {
140 prepare $name, $format;
141 cp $exefn, $cachefn
142 }
143 } else {
144 prepare $name, $format
145 }
ad77b7d3
MG
146 }
147}
148
bc372959
MG
1491;
150__END__
151
152=encoding utf-8
153
154=head1 NAME
155
156Gruntmaster::Daemon::Format - Utility functions for handling source files
157
158=head1 SYNOPSIS
159
160 use Gruntmaster::Daemon::Format qw/prepare_files/;
161 prepare_files { files => {
162 prog => {
163 name => 'prog.pl',
164 format => 'PERL',
165 content => 'print "Hello, world!"'
166 },
167 ver => {
168 name => 'ver.cpp',
169 format => 'CPP',
170 content => ...
171 },
172 }};
173
174=head1 DESCRIPTION
175
176Gruntmaster::Daemon::Format exports utility functions for handling source files.
177
178=over
179
180=item B<prepare_files> I<$meta>
181
182Compiles all the source files in C<< $meta->{files} >>.
183
184=back
185
186=head1 AUTHOR
187
188Marius Gavrilescu E<lt>marius@ieval.roE<gt>
189
190=head1 COPYRIGHT AND LICENSE
191
192Copyright (C) 2014 by Marius Gavrilescu
193
194This library is free software: you can redistribute it and/or modify
195it under the terms of the GNU Affero General Public License as published by
196the Free Software Foundation, either version 3 of the License, or
197(at your option) any later version.
198
199
200=cut
This page took 0.073709 seconds and 4 git commands to generate.