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