]>
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 POSIX qw//; | |
10 | use File::Basename qw/fileparse/; | |
11 | use File::Slurp qw/read_file write_file/; | |
12 | use Gruntmaster::Daemon::Constants qw/TLE OLE DIED NZX/; | |
13 | use Time::HiRes qw/alarm/; | |
14 | use List::MoreUtils qw/natatime/; | |
15 | use Log::Log4perl qw/get_logger/; | |
16 | use IPC::Signal qw/sig_name sig_num/; | |
17 | use Try::Tiny; | |
18 | ||
19 | our $VERSION = "5999.000_004"; | |
20 | our @EXPORT_OK = qw/prepare_files/; | |
21 | ||
22 | ################################################## | |
23 | ||
24 | sub execlist { | |
25 | my (@args) = @_; | |
26 | my $ret = fork // die 'Cannot fork'; | |
27 | if ($ret) { | |
28 | waitpid $ret, 0; | |
29 | die "gruntmaster-exec died\n" if -z 'exec-result'; | |
30 | my ($excode, $exmsg) = read_file 'exec-result'; | |
31 | unlink 'exec-result'; | |
32 | chomp ($excode, $exmsg); | |
33 | die [$excode, $exmsg] if $excode > 0; | |
34 | } else { | |
35 | open STDOUT, '>exec-result'; | |
36 | exec 'gruntmaster-exec', @args; | |
37 | } | |
38 | } | |
39 | ||
40 | sub command_and_args{ | |
41 | my ($format, $basename) = @_; | |
42 | ||
43 | given($format) { | |
44 | "./$basename" when [qw/C CPP GCCGO GOLANG HASKELL PASCAL/]; | |
45 | "./$basename.exe" when 'MONO'; | |
46 | java => $basename when 'JAVA'; | |
47 | perl => $basename when 'PERL'; | |
48 | python => $basename when 'PYTHON'; | |
49 | default { die "Don't know how to execute format $format\n" } | |
50 | } | |
51 | } | |
52 | ||
53 | sub mkrun{ | |
54 | my $format = shift; | |
55 | sub{ | |
56 | local *__ANON__ = 'mkrun_runner'; | |
57 | my ($name, %args) = @_; | |
58 | get_logger->trace("Running $name..."); | |
59 | my $basename = fileparse $name, qr/\.[^.]*/; | |
60 | my @args; | |
61 | push @args, '--timeout', $args{timeout} if $args{timeout}; | |
62 | push @args, '--mlimit', $args{mlimit} if $args{mlimit}; | |
63 | push @args, '--olimit', $args{olimit} if $args{olimit}; | |
64 | my @fds = exists $args{fds} ? @{$args{fds}} : (); | |
65 | my $it = natatime 2, @fds; | |
66 | while (my ($fd, $file) = $it->()) { | |
67 | push @args, "--fd=$fd $file"; | |
68 | } | |
69 | execlist @args, command_and_args($format, $basename); | |
70 | } | |
71 | } | |
72 | ||
73 | sub prepare{ | |
74 | my ($name, $format) = @_; | |
75 | get_logger->trace("Preparing file $name..."); | |
76 | ||
77 | try { | |
78 | execlist '--fd=1 >>errors', '--fd=2 >>errors', 'gruntmaster-compile', $format, $name; | |
79 | } catch { | |
80 | die "Compile error\n" | |
81 | } finally { | |
82 | $Gruntmaster::Daemon::errors .= read_file 'errors'; | |
83 | $Gruntmaster::Daemon::errors .= "\n"; | |
84 | unlink 'errors'; | |
85 | }; | |
86 | } | |
87 | ||
88 | sub prepare_files{ | |
89 | my $meta = shift; | |
90 | ||
91 | for my $file (values %{$meta->{files}}) { | |
92 | my ($format, $name, $content) = @{$file}{qw/format name content/}; | |
93 | ||
94 | $file->{run} = mkrun($format); | |
95 | write_file $name, $content; | |
96 | prepare $name, $format; | |
97 | } | |
98 | } | |
99 | ||
100 | 1; | |
101 | __END__ | |
102 | ||
103 | =encoding utf-8 | |
104 | ||
105 | =head1 NAME | |
106 | ||
107 | Gruntmaster::Daemon::Format - Utility functions for handling source files | |
108 | ||
109 | =head1 SYNOPSIS | |
110 | ||
111 | use Gruntmaster::Daemon::Format qw/prepare_files/; | |
112 | prepare_files { files => { | |
113 | prog => { | |
114 | name => 'prog.pl', | |
115 | format => 'PERL', | |
116 | content => 'print "Hello, world!"' | |
117 | }, | |
118 | ver => { | |
119 | name => 'ver.cpp', | |
120 | format => 'CPP', | |
121 | content => ... | |
122 | }, | |
123 | }}; | |
124 | ||
125 | =head1 DESCRIPTION | |
126 | ||
127 | Gruntmaster::Daemon::Format exports utility functions for handling source files. | |
128 | ||
129 | =over | |
130 | ||
131 | =item B<prepare_files> I<$meta> | |
132 | ||
133 | Compiles all the source files in C<< $meta->{files} >>. | |
134 | ||
135 | =back | |
136 | ||
137 | =head1 AUTHOR | |
138 | ||
139 | Marius Gavrilescu E<lt>marius@ieval.roE<gt> | |
140 | ||
141 | =head1 COPYRIGHT AND LICENSE | |
142 | ||
143 | Copyright (C) 2014 by Marius Gavrilescu | |
144 | ||
145 | This library is free software: you can redistribute it and/or modify | |
146 | it under the terms of the GNU Affero General Public License as published by | |
147 | the Free Software Foundation, either version 3 of the License, or | |
148 | (at your option) any later version. | |
149 | ||
150 | ||
151 | =cut |