| 1 | # $Id: /mirror/coderepos/lang/perl/Script-Remote/trunk/lib/Script/Remote.pm 57706 2008-06-05T02:27:20.427194Z daisuke $ | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | package Script::Remote; | ||||||||||||||||||||
| 4 | 1 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use Path::Class::File; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | our $DEFAULT_SSH; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | class_type 'Path::Class::File'; | |||||||||||||||||||
| 11 | coerce 'Path::Class::File' | ||||||||||||||||||||
| 12 | => from 'Str' | ||||||||||||||||||||
| 13 | 0 | ▶ | => via { Path::Class::File->new($_) } | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 14 | 1 | ; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 1 | has 'hostname' => ( | |||||||||||||||||||
| 17 | is => 'rw', | ||||||||||||||||||||
| 18 | isa => 'Str', | ||||||||||||||||||||
| 19 | required => 1 | ||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 1 | has 'username' => ( | |||||||||||||||||||
| 23 | is => 'rw', | ||||||||||||||||||||
| 24 | isa => 'Str', | ||||||||||||||||||||
| 25 | required => 1, | ||||||||||||||||||||
| 26 | default => (getpwuid($>))[0] | ||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 1 | has 'perl' => ( | |||||||||||||||||||
| 30 | is => 'rw', | ||||||||||||||||||||
| 31 | isa => 'Path::Class::File', | ||||||||||||||||||||
| 32 | coerce => 1, | ||||||||||||||||||||
| 33 | required => 1, | ||||||||||||||||||||
| 34 | default => $^X | ||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 1 | has 'no_wait' => ( | |||||||||||||||||||
| 38 | is => 'rw', | ||||||||||||||||||||
| 39 | isa => 'Bool', | ||||||||||||||||||||
| 40 | default => 0 | ||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | has 'ssh' => ( | ||||||||||||||||||||
| 44 | is => 'rw', | ||||||||||||||||||||
| 45 | isa => 'Path::Class::File', | ||||||||||||||||||||
| 46 | coerce => 1, | ||||||||||||||||||||
| 47 | required => 1, | ||||||||||||||||||||
| 48 | 0 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 49 | 0 | ▶ | $DEFAULT_SSH ||= __find_cmd("ssh", [ split(/:/, $ENV{PATH}) ]); | ||||||||||||||||||
|
Condition0%0/3
$Script::Remote::DEFAULT_SSH ||= __find_cmd("ssh", [split(/:/, $ENV{"PATH"}, 0)])
MC/DC0%0/2
$Script::Remote::DEFAULT_SSH ||= __find_cmd("ssh", [split(/:/, $ENV{"PATH"}, 0)])
$Script::Remote::DEFAULT_SSH
__find_cmd("ssh", [split(/:/, $ENV{"PATH"}, 0)])
Truth table0%0/3
A ||= B
A= $Script::Remote::DEFAULT_SSHB= __find_cmd("ssh", [split(/:/, $ENV{"PATH"}, 0)])
| |||||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | 1 | ); | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 1 | has 'script' => ( | |||||||||||||||||||
| 54 | is => 'rw', | ||||||||||||||||||||
| 55 | isa => 'Path::Class::File', | ||||||||||||||||||||
| 56 | coerce => 1, | ||||||||||||||||||||
| 57 | required => 1, | ||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 1 | has 'pid' => ( | |||||||||||||||||||
| 61 | is => 'rw', | ||||||||||||||||||||
| 62 | isa => 'Int' | ||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 1 | has 'child_out' => ( | |||||||||||||||||||
| 66 | is => 'rw' | ||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | has 'ssh_args' => ( | ||||||||||||||||||||
| 70 | is => 'rw', | ||||||||||||||||||||
| 71 | isa => 'ArrayRef', | ||||||||||||||||||||
| 72 | auto_deref => 1, | ||||||||||||||||||||
| 73 | 0 | ▶ | default => sub { +[] }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 74 | 1 | ); | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 1 | ▶ | no Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 1 | ▶ | use Data::Dump (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 79 | 1 | ▶ | use IPC::Open2 qw(open2); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 80 | 1 | ▶ | use POSIX (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 1 | my $MYSELF = ''; | |||||||||||||||||||
| 83 | 1 | open(my $fh, __FILE__); | |||||||||||||||||||
| 84 | 1 | ▶ | while (<$fh>) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 198 | $MYSELF .= $_; | |||||||||||||||||||
| 86 | 198 | ▶ | last if /^"END OF PACKAGE";/ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 1 | our $VERSION = '0.00001'; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | sub __find_cmd | ||||||||||||||||||||
| 92 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
__find_cmd: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 93 | 0 | my ($name, $paths) = @_; | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | foreach my $path (@$paths) { | |||||||||||||||||||
| 96 | 0 | my $fqpath = Path::Class::File->new($path, $name); | |||||||||||||||||||
| 97 | 0 | ▶ | if (-x $fqpath) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 0 | return $fqpath; | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | 0 | return (); | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | sub run | ||||||||||||||||||||
| 105 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
run: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 106 | 0 | my $self = shift; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 0 | my $text = $self->__wrapper_text(@_); | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 0 | my @extra = $self->ssh_args; | |||||||||||||||||||
| 111 | 0 | my $cmd = join(' ', $self->ssh, '-l', $self->username, @extra, $self->hostname, $self->perl, '--' ); | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 0 | ▶ | local $SIG{CHLD} = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 114 | 0 | ▶ | while ((my $child = waitpid(-1, &POSIX::WNOHANG)) > 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | # nothing to do. | ||||||||||||||||||||
| 116 | 0 | ▶ | if ($? != 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 0 | warn "child exited with status $?"; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | 0 | }; | |||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 0 | my ($child_out, $child_in); | |||||||||||||||||||
| 123 | 0 | ▶ | my $pid = open2( $child_out, $child_in, $cmd ) or die; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 0 | $self->pid( $pid ); | |||||||||||||||||||
| 126 | 0 | $self->child_out( $child_out ); | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 0 | print $child_in $text; | |||||||||||||||||||
| 129 | 0 | close $child_in; | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 0 | ▶ | if (! $self->no_wait) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | 0 | $self->wait_child(); | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | sub wait_child | ||||||||||||||||||||
| 137 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
wait_child: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 138 | 0 | my $self = shift; | |||||||||||||||||||
| 139 | 0 | wait(); | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 0 | ▶ | if (my $fh = $self->child_out) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | 0 | ▶ | while (<$fh>) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | 0 | print; | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | sub __wrapper_text | ||||||||||||||||||||
| 149 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
__wrapper_text: not called CC=6 SCAR=37.4
| |||||||||||||||||||||
| 150 | 0 | my $self = shift; | |||||||||||||||||||
| 151 | 0 | my %args = @_; | |||||||||||||||||||
| 152 | 0 | ▶ | my $data = $args{data} || ''; | ||||||||||||||||||
|
Condition0%0/2
$args{'data'} || ''
MC/DC0%0/1
$args{'data'} || ''
$args{'data'}
Truth table0%0/2
A
A= $args{'data'}
| |||||||||||||||||||||
| 153 | 0 | ▶ | my $variable = $args{variable} || 'DATA'; # choose one, damnit | ||||||||||||||||||
|
Condition0%0/2
$args{'variable'} || 'DATA'
MC/DC0%0/1
$args{'variable'} || 'DATA'
$args{'variable'}
Truth table0%0/2
A
A= $args{'variable'}
| |||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | # Serialize this data, if necessary (you know the rules, no globs, | ||||||||||||||||||||
| 156 | # no XS stuff -- unless they know how to serialize/deserialize) | ||||||||||||||||||||
| 157 | 0 | ▶ | if ($data) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | 0 | $data = Data::Dump::dump($data); | |||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 0 | my $script = $self->script; | |||||||||||||||||||
| 162 | 0 | my $script_text = $script->slurp; | |||||||||||||||||||
| 163 | 0 | my $date = scalar localtime; | |||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 0 | my $text = <<EOM; | |||||||||||||||||||
| 166 | #!env perl | ||||||||||||||||||||
| 167 | # Automatically generated by Script::Remote $VERSION on $date | ||||||||||||||||||||
| 168 | EOM | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | # XXX - We may use this one of these days, but not now | ||||||||||||||||||||
| 171 | 0 | my $with_copy = 0; | |||||||||||||||||||
| 172 | 0 | ▶ | if ($with_copy) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 173 | 0 | $text .= <<EOM; | |||||||||||||||||||
| 174 | { # A copy of Script::Remote that generated this script | ||||||||||||||||||||
| 175 | $MYSELF | ||||||||||||||||||||
| 176 | } | ||||||||||||||||||||
| 177 | EOM | ||||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 0 | ▶ | if ($data) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 181 | 0 | $text .= <<EOM; | |||||||||||||||||||
| 182 | # Serialized data section. Be careful with name conflicts | ||||||||||||||||||||
| 183 | my \$$variable = $data; | ||||||||||||||||||||
| 184 | EOM | ||||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 0 | $text .= <<EOM; | |||||||||||||||||||
| 188 | { | ||||||||||||||||||||
| 189 | $script_text | ||||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | EOM | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | # print $text; | ||||||||||||||||||||
| 194 | 0 | return $text; | |||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | 1 | "END OF PACKAGE"; | |||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | __END__ | ||||||||||||||||||||