]>
Commit | Line | Data |
---|---|---|
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 Expect::Simple; | |
10 | use File::Basename qw/fileparse/; | |
11 | use File::Slurp qw/read_file write_file/; | |
12 | use List::MoreUtils qw/natatime/; | |
13 | use Log::Log4perl qw/get_logger/; | |
14 | use POSIX qw/mkfifo/; | |
15 | use String::ShellQuote qw/shell_quote/; | |
16 | use Try::Tiny; | |
17 | ||
18 | our $VERSION = '5999.000_004'; | |
19 | our @EXPORT_OK = qw/prepare_files stopvms/; | |
20 | ||
21 | ################################################## | |
22 | ||
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 | ||
42 | sub execlist { | |
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); | |
49 | } else { | |
50 | my $ret = fork // die "Cannot fork\n"; | |
51 | if ($ret) { | |
52 | waitpid $ret, 0; | |
53 | } else { | |
54 | open STDOUT, '>', $er or die "Cannot open $er\n"; | |
55 | get_logger->trace("Running: gruntmaster-exec @args"); | |
56 | exec 'gruntmaster-exec', @args; | |
57 | } | |
58 | } | |
59 | ||
60 | die "gruntmaster-exec died\n" if -z $er; | |
61 | my ($excode, $exmsg) = read_file $er; | |
62 | unlink $er; | |
63 | chomp ($excode, $exmsg); ## no critic (ProhibitParensWithBuiltins) | |
64 | get_logger->trace("Exec result: $excode $exmsg"); | |
65 | die [$excode, $exmsg] if $excode > 0; ## no critic (RequireCarping) | |
66 | } | |
67 | ||
68 | sub mkrun{ | |
69 | my $format = shift; | |
70 | sub{ | |
71 | local *__ANON__ = 'mkrun_runner'; | |
72 | my ($name, %args) = @_; | |
73 | get_logger->trace("Running $name..."); | |
74 | my $basename = fileparse $name, qr/[.][^.]*/s; | |
75 | my @args = ('--nobody'); | |
76 | push @args, '--no-close' if $ENV{TEST_VERBOSE}; | |
77 | push @args, '--timeout', $args{timeout} if $args{timeout}; | |
78 | push @args, '--mlimit', $args{mlimit} if $args{mlimit}; | |
79 | push @args, '--olimit', $args{olimit} if $args{olimit}; | |
80 | my @fds = exists $args{fds} ? @{$args{fds}} : (); | |
81 | my $it = natatime 2, @fds; | |
82 | while (my ($fd, $file) = $it->()) { | |
83 | push @args, "--fd=$fd $file"; | |
84 | } | |
85 | execlist $basename, @args, "./$basename" | |
86 | } | |
87 | } | |
88 | ||
89 | sub prepare{ | |
90 | my ($name, $format) = @_; | |
91 | get_logger->trace("Preparing file $name..."); | |
92 | ||
93 | try { | |
94 | execlist prog => '--fd=1 >>errors', '--fd=2 >>errors', 'gruntmaster-compile', $format, $name; | |
95 | } catch { | |
96 | die "Compile error\n" | |
97 | } finally { | |
98 | $Gruntmaster::Daemon::errors .= read_file 'errors'; | |
99 | $Gruntmaster::Daemon::errors .= "\n"; | |
100 | unlink 'errors'; | |
101 | }; | |
102 | } | |
103 | ||
104 | sub prepare_files{ | |
105 | my $meta = shift; | |
106 | runvm $_ for keys %{$meta->{files}}; | |
107 | ||
108 | for my $file (values %{$meta->{files}}) { | |
109 | my ($format, $name, $content) = @{$file}{qw/format name content/}; | |
110 | ||
111 | $file->{run} = mkrun($format); | |
112 | write_file $name, $content; | |
113 | prepare $name, $format; | |
114 | } | |
115 | } | |
116 | ||
117 | 1; | |
118 | __END__ | |
119 | ||
120 | =encoding utf-8 | |
121 | ||
122 | =head1 NAME | |
123 | ||
124 | Gruntmaster::Daemon::Format - Utility functions for handling source files | |
125 | ||
126 | =head1 SYNOPSIS | |
127 | ||
128 | use Gruntmaster::Daemon::Format qw/prepare_files/; | |
129 | prepare_files { files => { | |
130 | prog => { | |
131 | name => 'prog.pl', | |
132 | format => 'PERL', | |
133 | content => 'print "Hello, world!"' | |
134 | }, | |
135 | ver => { | |
136 | name => 'ver.cpp', | |
137 | format => 'CPP', | |
138 | content => ... | |
139 | }, | |
140 | }}; | |
141 | ||
142 | =head1 DESCRIPTION | |
143 | ||
144 | Gruntmaster::Daemon::Format exports utility functions for handling source files. | |
145 | ||
146 | =over | |
147 | ||
148 | =item B<prepare_files> I<$meta> | |
149 | ||
150 | Compiles all the source files in C<< $meta->{files} >>. | |
151 | ||
152 | =back | |
153 | ||
154 | =head1 AUTHOR | |
155 | ||
156 | Marius Gavrilescu E<lt>marius@ieval.roE<gt> | |
157 | ||
158 | =head1 COPYRIGHT AND LICENSE | |
159 | ||
160 | Copyright (C) 2014 by Marius Gavrilescu | |
161 | ||
162 | This library is free software: you can redistribute it and/or modify | |
163 | it under the terms of the GNU Affero General Public License as published by | |
164 | the Free Software Foundation, either version 3 of the License, or | |
165 | (at your option) any later version. | |
166 | ||
167 | ||
168 | =cut |