File indexing completed on 2024-05-12 16:28:27
0001 #! /usr/bin/perl -w 0002 use strict; 0003 0004 # This script runs a commnand on a list of input arguments that are supplied on 0005 # the standard input. 0006 # Graphical output is handled in a Xephyr window. 0007 # Output of the command is given in a file results.txt 0008 # If the argument list contains 'FILE' the argument will be put there instead of 0009 # at the end. 0010 0011 # Examples: 0012 # find -name '*.odt' | \ 0013 # massTester.pl --withgui --timeout 3 words --export-pdf --export-filename dummy.pdf 0014 # 0015 # find -name '*.odt' | \ 0016 # massTester.pl --withgui --timeout 3 koconverter --batch FILE out.odt 0017 0018 # Author: Jos van den Oever 0019 0020 my $timeout = 3; # seconds 0021 my $withgui = 0; 0022 my $DISPLAY = ":2"; 0023 my $outputfile = "results.txt"; 0024 0025 # run the given command and return the exit status 0026 # the first argument is the timeout in seconds 0027 # the rest of the arguments form the command 0028 sub testCommand { 0029 my $timeout = shift; 0030 my @command = @_; 0031 my $pid = fork; 0032 my $result = "undefined"; 0033 eval { 0034 local $SIG{ALRM} = sub { die "alarm\n" }; 0035 alarm $timeout; 0036 0037 if (!$pid) { 0038 exec @command; 0039 exit; 0040 } 0041 waitpid $pid, 0; 0042 $result = $?; 0043 alarm 0; 0044 }; 0045 if ($@ eq "alarm\n") { 0046 kill 9, $pid; 0047 $result = "timeout"; 0048 } 0049 return $result; 0050 } 0051 0052 sub makeCommand { 0053 my @command; 0054 my $file = shift; 0055 my $foundFILE = 0; 0056 foreach (@_) { 0057 if ($_ eq "FILE") { 0058 $foundFILE = 1; 0059 push @command, $file; 0060 } else { 0061 push @command, $_; 0062 } 0063 } 0064 if ($foundFILE == 0) { 0065 push @command, $file; 0066 } 0067 return @command; 0068 } 0069 0070 # read the value for the current argument in the input array 0071 sub readArgumentValue { 0072 my $argv = shift; 0073 my $name = @$argv[0]; 0074 die "Provide value for $name.\n" unless defined @$argv[1]; 0075 shift @$argv; 0076 return @$argv[0]; 0077 } 0078 0079 # parse arguments 0080 while ($ARGV[0] && $ARGV[0] =~ m/^--/) { 0081 if ($ARGV[0] eq "--timeout") { 0082 $timeout = readArgumentValue \@ARGV; 0083 } elsif ($ARGV[0] eq "--withgui") { 0084 $withgui = 1; 0085 } else { 0086 die "Unknown argument $ARGV[0].\n"; 0087 } 0088 shift; 0089 } 0090 die "No command supplied.\n" unless $#ARGV >= 0; 0091 0092 my @command = @ARGV; 0093 0094 # start Xephyr 0095 my $xephyrpid; 0096 if ($withgui == 1) { 0097 if (!($xephyrpid = fork)) { 0098 exec "Xephyr", "-noreset", $DISPLAY; 0099 exit; 0100 } 0101 } 0102 $ENV{'DISPLAY'} = $DISPLAY; 0103 $ENV{'KDE_DEBUG'} = "0"; 0104 my @files = <STDIN>; 0105 0106 my %result; 0107 foreach (@files) { 0108 my $file = $_; 0109 chomp $file; 0110 my @cmd = makeCommand $file, @command; 0111 $result{$file} = testCommand $timeout, @cmd; 0112 } 0113 0114 open FH, "> $outputfile"; 0115 foreach (keys %result) { 0116 print FH $_."\t".$result{$_}."\n"; 0117 } 0118 close FH; 0119 0120 # close Xephyr 0121 if ($withgui == 1) { 0122 kill 15, $xephyrpid; 0123 waitpid $xephyrpid, 0; 0124 }