File: rt-wrapper-debian

package info (click to toggle)
rtfm 2.4.2-4%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,452 kB
  • ctags: 283
  • sloc: perl: 5,682; sh: 186; makefile: 56
file content (79 lines) | stat: -rw-r--r-- 1,642 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/perl -w
use strict;

# run a command and tee its output into the RT log
# for use with rtfm maintainer scripts

use Getopt::Std;
use IPC::Open3;
use IO::Select;
use File::Basename;
use Symbol;
use POSIX ":sys_wait_h";

use lib "/usr/local/share/request-tracker3.8/lib";
use lib "/usr/share/request-tracker3.8/lib";

require RT;

RT::LoadConfig();

my $config = RT->Config;
$config->Set(LogToScreen => 'error');

RT::InitLogging();

my %opts;
getopts('n:', \%opts) or usage();

sub usage {
    print STDERR <<EOF;
Usage: $0 [-n <name>] -- <command> [argument] [...]
EOF
    exit 1;
}


my $cmd = join(" ", @ARGV);

my $cmdname = exists $opts{n} ? $opts{n} : basename $ARGV[0];

$RT::Logger->notice(qq{running $cmd as "$cmdname"\n});

my ($child_out, $child_in, $child_err) = ("","",gensym);
my $pid = open3($child_in, $child_out, $child_err, @ARGV);

my $s = IO::Select->new;
$s->add($child_out);
$s->add($child_err);

while ($s->handles and my @ready = $s->can_read(10)) {
    for my $fh (@ready) {
        my $ret = sysread($fh, my $buf, 1024);
        if (not defined $ret) {
            die("sysread failed: $!");
        }
        if ($ret == 0) {
            $s->remove($fh);
            next;
        }
        print STDERR $buf if $fh eq $child_err;
        print $buf if $fh eq $child_out;

        my $function = ($fh eq $child_err ? "error" : "notice");
        for (split(/\n/, $buf)) {
            s/^\t*/ /;
            $RT::Logger->$function("$cmdname: $_")
        }
    }
}

my $i = 0;
do {
    if (waitpid($pid, WNOHANG) > 0) {
        my $ret = $?;
        exit $ret >> 8;
    }
    sleep 1;
} while ($i++ < 5);