Mechanize.pm

filter: Statementstmt 91.6% 55 / 60 Branchbran 40.0% 4 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 86.4% 70 / 81 CC 8 SCAR 16.1
CC
8
Cov
84%
__ANON__
11.0
__ANON__
2.0
_create_session
1.0
CRAP 5.0
« Mechanize/ConnectParams.pm ↑ Summary Mechanize/Session.pm »
1 package Net::SSH::Mechanize;
2 #use AnyEvent::Log;
3 #use Coro;
4 16 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Net::SSH::Mechanize::ConnectParams;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Net::SSH::Mechanize::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use AnyEvent::Subprocess;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 #use Scalar::Util qw(refaddr);
10 16 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 our @CARP_NOT = qw(AnyEvent AnyEvent::Subprocess Coro::AnyEvent);
12  
13 # Stop our carp errors from being reported within AnyEvent::Coro
14 16 @Coro::AnyEvent::CARP_NOT = qw(AnyEvent::CondVar);
15  
16 16 our $VERSION = '0.1.3'; # VERSION
17  
18 #$AnyEvent::Log::FILTER->level("fatal");
19  
20  
21 16 my @connection_params = qw(host user port password);
22  
23 # An object which defines a connection.
24 16 has 'connection_params' => (
25 isa => 'Net::SSH::Mechanize::ConnectParams',
26 is => 'ro',
27 handles => \@connection_params,
28 );
29  
30  
31 has 'session' => (
32 isa => 'Net::SSH::Mechanize::Session',
33 is => 'ro',
34 lazy => 1,
35 69 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 69 shift->_create_session;
37 },
38 16 handles => [qw(login login_async capture capture_async sudo_capture sudo_capture_async logout)],
39 );
40  
41 # The log-in timeout limit in seconds
42 16 has 'login_timeout' => (
43 is => 'rw',
44 isa => 'Int',
45 default => 30,
46 );
47  
48  
49 # This wrapper exists to map @connection_params into a
50 # Net::SSH::Mechanize::ConnectParams instance, if one is not supplied
51 # explicitly.
52 114 ▶around 'BUILDARGS' => sub {
Subroutine __ANON__: called CC=7 SCAR=24.0
53 114 my $orig = shift;
54 114 my $self = shift;
55  
56 114 my $params = $self->$orig(@_);
57  
58 # check for connection_params paramter
59 114 my $cp;
60 114 ▶ if (exists $params->{connection_params}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{'connection_params'}) { } 114 0 114
61 # Prevent duplication of parameters - if we have a connection_params
62 # parameter, forbid the shortcut alternatives.
63 114 foreach my $param (@connection_params) {
64 croak "Cannot specify both $param and connection_params parameters"
65 456 ▶ if exists $params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params->{$param} 0 456 456
66 }
67  
68 114 $cp = $params->{connection_params};
69 114 ▶ $cp = Net::SSH::Mechanize::ConnectParams->new($cp)
Branch: 1 branch
BranchTrueFalseTotal
if ref $cp eq "HASH" 0 114 114
70 if ref $cp eq 'HASH';
71 }
72 else {
73 # Splice the short-cut @connection_params out of %$params and into %cp_params
74 0 my %cp_params;
75 0 foreach my $param (@connection_params) {
76 0 ▶ next unless exists $params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 0 0 0
77 0 $cp_params{$param} = delete $params->{$param};
78 }
79  
80 # Try and construct a ConnectParams instance
81 0 $cp = Net::SSH::Mechanize::ConnectParams->new(%cp_params);
82 }
83  
84 return {
85 114 %$params,
86 connection_params => $cp,
87 };
88 16 };
89  
90  
91 ######################################################################
92 # public methods
93  
94  
95  
96 69 ▶sub _create_session {
Subroutine _create_session: called CC=1 SCAR=0.0
97 69 my $self = shift;
98  
99 # We do this funny stuff with $session and $job so that the on_completion
100 # callback can tell the session it should clean up
101 69 my $session;
102 my $job = AnyEvent::Subprocess->new(
103 run_class => 'Net::SSH::Mechanize::Session',
104 delegates => [
105 'Pty',
106 'CompletionCondvar',
107 [Handle => {
108 name => 'stderr',
109 direction => 'r',
110 replace => \*STDERR,
111 }],
112 ],
113 2 ▶ on_completion => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
114 2 my $done = shift;
115
116 # printf "xx completing child PID %d _error_event %s is %s \n",
117 # $session->child_pid, $session->_error_event, $session->_error_event->ready? "ready":"unready"; #DB
118 2 my $stderr = $done->delegate('stderr');
119 2 my $errtext = $stderr->rbuf;
120 2 ▶ my $msg = sprintf "child PID %d terminated unexpectedly with exit value %d",
Branch: 1 branch
BranchTrueFalseTotal
$errtext ? : 0 2 2
121 $session->child_pid, $done->exit_value, $errtext? "\n$errtext" : '';
122 2 $session->_error_event->send($msg);
123 2 undef $session;
124 },
125 12 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
126 12 my $cmd = shift->{cmd};
127 12 exec @$cmd;
128 },
129 69 );
130 69 $session = $job->run({cmd => [$self->connection_params->ssh_cmd]});
131
132 # Tack this on afterwards, mainly to supply the password. We
133 # can't add it to the constructor above because of the design of
134 # AnyEvent::Subprocess.
135 57 $session->connection_params($self->connection_params);
136  
137 # And set the login_timeout
138 57 $session->login_timeout($self->login_timeout);
139  
140 # turn off terminal echo
141 57 $session->delegate('pty')->handle->fh->set_raw;
142  
143 # Rebless $session into a subclass of AnyEvent::Subprocess::Running
144 # which just supplies extra methods we need.
145 # bless $session, 'Net::SSH::Mechanize::Session';
146  
147 57 return $session;
148 }
149  
150  
151  
152 16 __PACKAGE__->meta->make_immutable;
153 16 1;
154  
155 __END__
« Mechanize/ConnectParams.pm ↑ Summary Mechanize/Session.pm »