~ubuntu-branches/debian/sid/calligraplan/sid

« back to all changes in this revision

Viewing changes to devtools/scripts/massTester.pl

  • Committer: Package Import Robot
  • Author(s): Pino Toscano
  • Date: 2018-02-01 18:20:19 UTC
  • Revision ID: package-import@ubuntu.com-20180201182019-1qo7qaim5wejm5k9
Tags: upstream-3.1.0
ImportĀ upstreamĀ versionĀ 3.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/perl -w
 
2
use strict;
 
3
 
 
4
# This script runs a commnand on a list of input arguments that are supplied on
 
5
# the standard input.
 
6
# Graphical output is handled in a Xephyr window.
 
7
# Output of the command is given in a file results.txt
 
8
# If the argument list contains 'FILE' the argument will be put there instead of
 
9
# at the end.
 
10
 
 
11
# Examples:
 
12
# find -name '*.odt' | \
 
13
#     massTester.pl --withgui --timeout 3 words --export-pdf --export-filename dummy.pdf
 
14
#
 
15
# find -name '*.odt' | \
 
16
#     massTester.pl --withgui --timeout 3 koconverter --batch FILE out.odt
 
17
 
 
18
# Author: Jos van den Oever
 
19
 
 
20
my $timeout = 3; # seconds
 
21
my $withgui = 0;
 
22
my $DISPLAY = ":2";
 
23
my $outputfile = "results.txt";
 
24
 
 
25
# run the given command and return the exit status
 
26
# the first argument is the timeout in seconds
 
27
# the rest of the arguments form the command
 
28
sub testCommand {
 
29
        my $timeout = shift;
 
30
        my @command = @_;
 
31
        my $pid = fork;
 
32
        my $result = "undefined";
 
33
        eval {
 
34
                local $SIG{ALRM} = sub { die "alarm\n" };
 
35
                alarm $timeout;
 
36
 
 
37
                if (!$pid) {
 
38
                        exec @command;
 
39
                        exit;
 
40
                }
 
41
                waitpid $pid, 0;
 
42
                $result = $?;
 
43
                alarm 0;
 
44
        };
 
45
        if ($@ eq "alarm\n") {
 
46
                kill 9, $pid;
 
47
                $result = "timeout";
 
48
        }
 
49
        return $result;
 
50
}
 
51
 
 
52
sub makeCommand {
 
53
        my @command;
 
54
        my $file = shift;
 
55
        my $foundFILE = 0;
 
56
        foreach (@_) {
 
57
                if ($_ eq "FILE") {
 
58
                        $foundFILE = 1;
 
59
                        push @command, $file;
 
60
                } else {
 
61
                        push @command, $_;
 
62
                }
 
63
        }
 
64
        if ($foundFILE == 0) {
 
65
                push @command, $file;
 
66
        }
 
67
        return @command;
 
68
}
 
69
 
 
70
# read the value for the current argument in the input array
 
71
sub readArgumentValue {
 
72
        my $argv = shift;
 
73
        my $name = @$argv[0];
 
74
        die "Provide value for $name.\n" unless defined @$argv[1];
 
75
        shift @$argv;
 
76
        return @$argv[0];
 
77
}
 
78
 
 
79
# parse arguments
 
80
while ($ARGV[0] && $ARGV[0] =~ m/^--/) {
 
81
        if ($ARGV[0] eq "--timeout") {
 
82
                $timeout = readArgumentValue \@ARGV;
 
83
        } elsif ($ARGV[0] eq "--withgui") {
 
84
                $withgui = 1;
 
85
        } else {
 
86
                die "Unknown argument $ARGV[0].\n";
 
87
        }
 
88
        shift;
 
89
}
 
90
die "No command supplied.\n" unless $#ARGV >= 0;
 
91
 
 
92
my @command = @ARGV;
 
93
 
 
94
# start Xephyr
 
95
my $xephyrpid;
 
96
if ($withgui == 1) {
 
97
        if (!($xephyrpid = fork)) {
 
98
                exec "Xephyr", "-noreset", $DISPLAY;
 
99
                exit;
 
100
        }
 
101
}
 
102
$ENV{'DISPLAY'} = $DISPLAY;
 
103
$ENV{'KDE_DEBUG'} = "0";
 
104
my @files = <STDIN>;
 
105
 
 
106
my %result;
 
107
foreach (@files) {
 
108
        my $file = $_;
 
109
        chomp $file;
 
110
        my @cmd = makeCommand $file, @command;
 
111
        $result{$file} = testCommand $timeout, @cmd;
 
112
}
 
113
 
 
114
open FH, "> $outputfile";
 
115
foreach (keys %result) {
 
116
        print FH $_."\t".$result{$_}."\n";
 
117
}
 
118
close FH;
 
119
 
 
120
# close Xephyr
 
121
if ($withgui == 1) {
 
122
        kill 15, $xephyrpid;
 
123
        waitpid $xephyrpid, 0;
 
124
}