Add javascript
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon / Format.pm
... / ...
CommitLineData
1package Gruntmaster::Daemon::Format;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Exporter/;
7no if $] > 5.017011, warnings => 'experimental::smartmatch';
8
9use Digest::SHA qw/sha256_hex/;
10use Expect;
11use File::Basename qw/fileparse/;
12use File::Copy qw/cp/;
13use File::Slurp qw/read_file write_file/;
14use List::MoreUtils qw/natatime/;
15use Log::Log4perl qw/get_logger/;
16use String::ShellQuote qw/shell_quote/;
17use Time::HiRes qw/time/;
18use Try::Tiny;
19
20our $VERSION = '5999.000_005';
21our @EXPORT_OK = qw/prepare_files stopvms/;
22
23##################################################
24
25our (%vm, %pid);
26
27sub runvm {
28 my ($name, $arg) = @_;
29 return unless $ENV{GRUNTMASTER_VM};
30 my $cmd = $ENV{GRUNTMASTER_VM};
31 $cmd .= ' ' . $arg if $arg;
32 get_logger->trace("Starting VM $name ($cmd)");
33 $vm{$name} = Expect->new;
34 $vm{$name}->raw_pty(1);
35 $vm{$name}->log_stdout(0);
36 $vm{$name}->spawn($cmd);
37 $vm{$name}->expect(5, '# ') or get_logger->logdie("Error while starting VM $name: ". $vm{$name}->error);
38}
39
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}) {
49 warn "Cannot kill VM\n" if $kill;
50 $vm{$vm}->expect(5, '# ');
51 } else {
52 kill KILL => $pid{$vm} if $kill;
53 waitpid $pid{$vm}, 0;
54 }
55 write_file "time-stop-$vm", time;
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}
66
67sub execlist {
68 my ($vm, @args) = @_;
69 write_file "time-start-$vm", time;
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");
74 $vm{$vm}->send($cmd, "\n");
75 } else {
76 $pid{$vm} = fork // die "Cannot fork\n";
77 unless ($pid{$vm}) {
78 open STDOUT, '>', $er or die "Cannot open $er\n";
79 get_logger->trace("Running: gruntmaster-exec @args");
80 exec 'gruntmaster-exec', @args;
81 }
82 }
83}
84
85sub mkrun{
86 my $format = shift;
87 sub{
88 local *__ANON__ = 'mkrun_runner';
89 my ($name, %args) = @_;
90 get_logger->trace("Running $name...");
91 my $basename = fileparse $name, qr/[.][^.]*/s;
92 my @args = ('--sudo');
93 push @args, '--keep-stderr' if $ENV{TEST_VERBOSE};
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";
101 }
102 execlist $basename, @args, '--', "./$basename", @{$args{args}};
103 execlist_finish $basename unless $args{nonblocking}
104 }
105}
106
107sub prepare{
108 my ($name, $format) = @_;
109 get_logger->trace("Preparing file $name...");
110
111 try {
112 execlist prog => '--fd=1 >>errors', '--fd=2 >>errors', 'gruntmaster-compile', $format, $name;
113 execlist_finish 'prog';
114 } catch {
115 my $exmsg = $_->[1];
116 die "Compile error ($exmsg)\n"
117 } finally {
118 $Gruntmaster::Daemon::errors .= read_file 'errors';
119 $Gruntmaster::Daemon::errors .= "\n" if -s 'errors';
120 unlink 'errors';
121 };
122}
123
124sub prepare_files{
125 my $meta = shift;
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 }
132
133 for my $file (values %{$meta->{files}}) {
134 my ($format, $name, $content) = @{$file}{qw/format name content/};
135
136 $file->{run} = mkrun($format);
137 write_file $name, $content;
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 }
151 }
152}
153
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.011752 seconds and 5 git commands to generate.