Add javascript
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon / Format.pm
1 package Gruntmaster::Daemon::Format;
2
3 use 5.014000;
4 use strict;
5 use warnings;
6 use parent qw/Exporter/;
7 no if $] > 5.017011, warnings => 'experimental::smartmatch';
8
9 use Digest::SHA qw/sha256_hex/;
10 use Expect;
11 use File::Basename qw/fileparse/;
12 use File::Copy qw/cp/;
13 use File::Slurp qw/read_file write_file/;
14 use List::MoreUtils qw/natatime/;
15 use Log::Log4perl qw/get_logger/;
16 use String::ShellQuote qw/shell_quote/;
17 use Time::HiRes qw/time/;
18 use Try::Tiny;
19
20 our $VERSION = '5999.000_005';
21 our @EXPORT_OK = qw/prepare_files stopvms/;
22
23 ##################################################
24
25 our (%vm, %pid);
26
27 sub 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
40 sub stopvms {
41 kill KILL => $_->pid for values %vm;
42 %vm = %pid = ();
43 }
44
45 sub 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
67 sub 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
85 sub 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
107 sub 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
124 sub 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
154 1;
155 __END__
156
157 =encoding utf-8
158
159 =head1 NAME
160
161 Gruntmaster::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
181 Gruntmaster::Daemon::Format exports utility functions for handling source files.
182
183 =over
184
185 =item B<prepare_files> I<$meta>
186
187 Compiles all the source files in C<< $meta->{files} >>.
188
189 =back
190
191 =head1 AUTHOR
192
193 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
194
195 =head1 COPYRIGHT AND LICENSE
196
197 Copyright (C) 2014 by Marius Gavrilescu
198
199 This library is free software: you can redistribute it and/or modify
200 it under the terms of the GNU Affero General Public License as published by
201 the 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.032874 seconds and 4 git commands to generate.