]>
Commit | Line | Data |
---|---|---|
ddceb393 MG |
1 | package Gruntmaster::Daemon::Format; |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6 | use parent qw/Exporter/; | |
9577371b | 7 | no if $] > 5.017011, warnings => 'experimental::smartmatch'; |
ddceb393 MG |
8 | |
9 | use POSIX qw//; | |
10 | use File::Basename qw/fileparse/; | |
f5e29130 | 11 | use File::Slurp qw/write_file/; |
ddceb393 | 12 | use Gruntmaster::Daemon::Constants qw/TLE OLE DIED NZX/; |
ddceb393 MG |
13 | use Time::HiRes qw/alarm/; |
14 | use List::MoreUtils qw/natatime/; | |
f5e29130 | 15 | use Log::Log4perl qw/get_logger/; |
ddceb393 | 16 | use IPC::Signal qw/sig_name sig_num/; |
ddceb393 | 17 | |
a5522656 | 18 | our $VERSION = "5999.000_001"; |
ad77b7d3 | 19 | our @EXPORT_OK = qw/prepare_files/; |
ddceb393 MG |
20 | |
21 | ################################################## | |
22 | ||
23 | sub command_and_args{ | |
24 | my ($format, $basename) = @_; | |
9577371b MG |
25 | |
26 | given($format) { | |
27 | "./$basename" when [qw/C CPP PASCAL/]; | |
28 | "./$basename.exe" when 'MONO'; | |
29 | "java $basename" when 'JAVA'; | |
30 | "perl $basename" when 'PERL'; | |
31 | "python $basename" when 'PYTHON'; | |
32 | default { die "Don't know how to execute format $format" } | |
33 | } | |
ddceb393 MG |
34 | } |
35 | ||
36 | sub mkrun{ | |
37 | my $format = shift; | |
38 | sub{ | |
39 | my ($name, %args) = @_; | |
40 | my $basename = fileparse $name, qr/\.[^.]*/; | |
41 | my $ret = fork // die 'Cannot fork'; | |
42 | if ($ret) { | |
43 | my $tle; | |
44 | local $SIG{ALRM} = sub { kill KILL => $ret; $tle = 1}; | |
45 | alarm $args{timeout} if exists $args{timeout}; | |
46 | wait; | |
47 | alarm 0; | |
48 | my $sig = $? & 127; | |
49 | my $signame = sig_name $sig; | |
50 | die [TLE, "Time Limit Exceeded"] if $tle; | |
51 | die [OLE, 'Output Limit Exceeded'] if $sig && $signame eq 'XFSZ'; | |
52 | die [DIED, "Crash (SIG$signame)"] if $sig; | |
53 | die [NZX, "Non-zero exit status: " . ($? >> 8)] if $?; | |
54 | } else { | |
55 | my @fds = exists $args{fds} ? @{$args{fds}} : (); | |
56 | $^F = 50; | |
57 | POSIX::close $_ for 0 .. $^F; | |
58 | my $it = natatime 2, @fds; | |
59 | while (my ($fd, $file) = $it->()) { | |
60 | open my $fh, $file or die $!; | |
61 | my $oldfd = fileno $fh; | |
62 | if ($oldfd != $fd) { | |
63 | POSIX::dup2 $oldfd, $fd or die $!; | |
64 | POSIX::close $oldfd or die $!; | |
65 | } | |
66 | } | |
67 | exec 'gruntmaster-exec', $args{mlimit} // 0, $args{olimit} // 0, command_and_args($format, $basename), exists $args{args} ? @{$args{args}} : (); | |
89edda82 | 68 | exit 42 |
ddceb393 MG |
69 | } |
70 | } | |
71 | } | |
72 | ||
ad77b7d3 MG |
73 | sub prepare{ |
74 | my ($name, $format) = @_; | |
75 | our $errors; | |
ad77b7d3 MG |
76 | get_logger->trace("Preparing file $name..."); |
77 | ||
496fffaa | 78 | $errors .= `gruntmaster-compile $format $name 2>&1`; |
ad77b7d3 MG |
79 | $errors .= "\n"; |
80 | die 'Compile error' if $? | |
81 | } | |
82 | ||
83 | sub prepare_files{ | |
84 | my $meta = shift; | |
85 | ||
86 | for my $file (values $meta->{files}) { | |
87 | my ($format, $name, $content) = @{$file}{qw/format name content/}; | |
88 | ||
89 | $file->{run} = mkrun($format); | |
90 | write_file $name, $content; | |
91 | prepare $name, $format; | |
92 | } | |
93 | } | |
94 | ||
bc372959 MG |
95 | 1; |
96 | __END__ | |
97 | ||
98 | =encoding utf-8 | |
99 | ||
100 | =head1 NAME | |
101 | ||
102 | Gruntmaster::Daemon::Format - Utility functions for handling source files | |
103 | ||
104 | =head1 SYNOPSIS | |
105 | ||
106 | use Gruntmaster::Daemon::Format qw/prepare_files/; | |
107 | prepare_files { files => { | |
108 | prog => { | |
109 | name => 'prog.pl', | |
110 | format => 'PERL', | |
111 | content => 'print "Hello, world!"' | |
112 | }, | |
113 | ver => { | |
114 | name => 'ver.cpp', | |
115 | format => 'CPP', | |
116 | content => ... | |
117 | }, | |
118 | }}; | |
119 | ||
120 | =head1 DESCRIPTION | |
121 | ||
122 | Gruntmaster::Daemon::Format exports utility functions for handling source files. | |
123 | ||
124 | =over | |
125 | ||
126 | =item B<prepare_files> I<$meta> | |
127 | ||
128 | Compiles all the source files in C<< $meta->{files} >>. | |
129 | ||
130 | =back | |
131 | ||
132 | =head1 AUTHOR | |
133 | ||
134 | Marius Gavrilescu E<lt>marius@ieval.roE<gt> | |
135 | ||
136 | =head1 COPYRIGHT AND LICENSE | |
137 | ||
138 | Copyright (C) 2014 by Marius Gavrilescu | |
139 | ||
140 | This library is free software: you can redistribute it and/or modify | |
141 | it under the terms of the GNU Affero General Public License as published by | |
142 | the Free Software Foundation, either version 3 of the License, or | |
143 | (at your option) any later version. | |
144 | ||
145 | ||
146 | =cut |