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