lib/Android/ElectricSheep/Automator/ADB.pm

filter: Statementstmt 26.0% 25 / 96 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 25.8% 8 / 31 Podpod 95.6% 22 / 23 totaltotal 28.5% 55 / 193 CC 19 SCAR 31.9
CC
19
Cov
19%
run
90.0
new
30.0
devices
30.0
CRAP 24.3
« lib/Android/ElectricSheep/Automator/XMLParsers.pm ↑ Summary script/electric-sheep-close-app.pl »
1 package Android::ElectricSheep::Automator::ADB;
2  
3 1 ▶use 5.014000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.009';
8  
9 1 ▶use Android::ElectricSheep::Automator::ADB::Device;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use IPC::Open2;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 # we already have a run() 'method'
14 # which clashes with IPC::Run's run() sub
15 # fingers crossed: don't import IPC::Run::run()
16 # and use fully qualified name when calling it.
17 1 ▶use IPC::Run qw/timeout/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
20 0 my ($class, %args) = @_;
21 0 ▶ $args{path} //= $ENV{ADB};
Condition0%0/3
$args{"path"} //= $ENV{"ADB"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"path"} //= $ENV{"ADB"}
$args{"path"} $ENV{"ADB"}
Truth table0%0/3
A //= B
A= $args{"path"}B= $ENV{"ADB"}
ABresult
1X1
011
000
22 0 ▶ $args{path} //= 'adb';
Condition0%0/2
$args{"path"} //= "adb"
l!l
00
MC/DC0%0/1
$args{"path"} //= "adb"
$args{"path"}
Truth table0%0/2
A
A= $args{"path"}
Aresult
11
00
23 0 ▶ $args{verbosity} //= 0;
Condition0%0/2
$args{"verbosity"} //= 0
l!l
00
MC/DC0%0/1
$args{"verbosity"} //= 0
$args{"verbosity"}
Truth table0%0/2
A
A= $args{"verbosity"}
Aresult
11
00
24 0 ▶ $args{args} //= [];
Condition0%0/2
$args{"args"} //= []
l!l
00
MC/DC0%0/1
$args{"args"} //= []
$args{"args"}
Truth table0%0/2
A
A= $args{"args"}
Aresult
11
00
25 0 bless \%args, $class
26 }
27  
28 0 ▶sub run2 {
Subroutine run2: not called CC=3 SCAR=24.8
Pod undocumented
29 0 my ($self, @args) = @_;
30 0 my ($out, $in);
31 0 ▶ my @dev_args = $self->{device_serial} ? ('-s', $self->{device_serial}) : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->{'device_serial'} ? : 0 0 0
32 0 my $pid = open2 $out, $in, $self->{path}, @{$self->{args}}, @args;
33 0 my $result = read_file $out;
34 0 close $out;
35 0 close $in;
36 0 ▶ waitpid $pid, 0 or croak "$!";
Branch: 1 branch
BranchTrueFalseTotal
waitpid $pid, 0 or croak("$!") 0 0 0
37 0 $result;
38 }
39  
40 # returns an arrayref of
41 # [statuscode, stdout, stderr]
42 # on success statuscode is 0 and stdout/stderr may or may not have something
43 # on failure statuscode is 1 and stdout/stderr may or may not have something
44 # the '$result' returned by original run2() is now the stdout,
45 # the 2nd item in the returned arrayref
46 0 ▶sub run {
Subroutine run: not called CC=9 SCAR=45.0
Pod documented
47 0 my ($self, @args) = @_;
48 0 my ($out, $in, $err);
49 0 ▶ my @dev_args = $self->{device_serial} ? ('-s', $self->{device_serial}) : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->{'device_serial'} ? : 0 0 0
50 0 my @cmd = ($self->{path}, @{$self->{args}}, @args);
51 0 ▶ if( $self->{verbosity} > 0 ){ print STDOUT __PACKAGE__.'::run()'." : executing command : ".join(' ', @cmd)."\n" }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbosity"} > 0) 0 0 0
52 # check if undef is passed in @cmd, IPC does not like that
53 0 for (@cmd){
54 0 ▶ if( ! defined $_ ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_) 0 0 0
55 0 my $errstr = __PACKAGE__.'::run()'." : error, command contains undef values (Perl's undef) which is not allowed, most likely a cockup with creating the command array: @cmd";
56 0 carp $errstr;
57 0 return [1, "", $errstr]
58 }
59 }
60 0 my $res = eval {
61 0 IPC::Run::run(
62 \@cmd,
63 \$in, \$out, \$err,
64 # AHP: on timeout it throws an exception matching
65 # /^IPC::Run: .*timed out/
66 # or specify your own exception name (see doc)
67 # I can't find what unit the timeout interval is!
68 IPC::Run::timeout(1000)
69 )
70 };
71 # WARNING: adb on error sometimes returns non-zero exit code
72 # but sometimes it exits normally with zero but there is
73 # an error which you can find in the STDOUT/STDERR.
74 # Unfortunately this is not a consistent message.
75 # For example:
76 # adb shell xyz
77 # will set $? to 127
78 # but, e.g. this:
79 # adb shell input keycombination 1 2
80 # will exit with $?=0 and print some error message!
81 # Searching for 'Error:' in STDERR is ok?
82 # TODO: this needs to be dealt with here.
83 0 my $exit_code = $? >> 8;
84 #if( (! $res) || $@ || ($err=~/\bError\: /) ){
85 0 ▶ if( ($exit_code > 0) || $@ || ($err=~/\bError\: /) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code > 0 or $@ or $err =~ /\bError\: /u) 0 0 0
Condition0%0/6
$exit_code > 0 or $@ or $err =~ /\bError\: /u
l!l&&r!l&&!r
000
$exit_code > 0 or $@
l!l&&r!l&&!r
000
MC/DC0%0/3
$exit_code > 0 or $@ or $err =~ /\bError\: /u
$exit_code > 0 $@ $err =~ /\bError\: /u
Truth table0%0/4
A or B or C
A= $exit_code > 0B= $@C= $err =~ /\bError\: /u
ABCresult
1XX1
01X1
0011
0000
86 0 ▶ carp "STDERR:\n${err}\nOTHER INFO: $@\n\n"
Branch: 1 branch
BranchTrueFalseTotal
$@ =~ /IPC::Run: .*timed out/u ? : 0 0 0
87 .(($@=~/IPC::Run: .*timed out/)
88 ?"\nWARNING: it looks like a timeout has occured.\n\n"
89 :""
90 )
91 .__PACKAGE__.'::run()'." : error, failed to execute command (see above for stderr), exit code was '${exit_code}': ".join(' ', @cmd)
92 ; # end carp
93 0 return [1, $out, $err, $exit_code];
94 }
95 0 return [0, $out, $err, $exit_code];
96 }
97  
98 0 ▶sub start_server { shift->run('start-server') }
Subroutine start_server: not called CC=1 SCAR=6.9
Pod documented
99 0 ▶sub kill_server { shift->run('kill-server') }
Subroutine kill_server: not called CC=1 SCAR=6.9
Pod documented
100  
101 0 ▶sub connect { shift->run('connect', @_) }
Subroutine connect: not called CC=1 SCAR=6.9
Pod documented
102 0 ▶sub disconnect { shift->run('disconnect', @_) }
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod documented
103  
104 0 ▶sub devices {
Subroutine devices: not called CC=5 SCAR=34.0
Pod documented
105 0 my $ret = shift->run('devices', '-l');
106 0 ▶ if( $ret->[0] != 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ret->[0] != 0) 0 0 0
107 0 print STDERR __PACKAGE__.'::devices()'." : error, failed to enquire devices:\n".$ret->[1]."\n".$ret->[2];
108 return undef
109 0 }
110 0 my @devices = grep { ! /^List of devices/ }
111 0 grep { / / }
112 split '\n', $ret->[1]; # stdout contains the devices one in each line
113 0 my @result;
114 0 for (@devices) {
115 0 ▶ next if /^List of devices/;
Branch: 1 branch
BranchTrueFalseTotal
if /^List of devices/u 0 0 0
116 0 ▶ next unless / /;
Branch: 1 branch
BranchTrueFalseTotal
unless / /u 0 0 0
117 0 push @result, Android::ElectricSheep::Automator::ADB::Device->new(split)
118 }
119 @result
120 0 }
121  
122 0 ▶sub set_device {
Subroutine set_device: not called CC=1 SCAR=6.9
Pod documented
123 0 my ($self, $device) = @_;
124 0 $self->{device_serial} = $device->serial;
125 }
126  
127 0 ▶sub wait_for_device { shift->run('wait-for-device') }
Subroutine wait_for_device: not called CC=1 SCAR=6.9
Pod documented
128 0 ▶sub get_state { shift->run('get-state') }
Subroutine get_state: not called CC=1 SCAR=6.9
Pod documented
129 0 ▶sub get_serialno { shift->run('get-serialno') }
Subroutine get_serialno: not called CC=1 SCAR=6.9
Pod documented
130 0 ▶sub get_devpath { shift->run('get-devpath') }
Subroutine get_devpath: not called CC=1 SCAR=6.9
Pod documented
131 0 ▶sub remount { shift->run('remount') }
Subroutine remount: not called CC=1 SCAR=6.9
Pod documented
132 0 ▶sub reboot { shift->run('reboot', @_) }
Subroutine reboot: not called CC=1 SCAR=6.9
Pod documented
133 0 ▶sub reboot_bootloader { shift->run('reboot-bootloader') }
Subroutine reboot_bootloader: not called CC=1 SCAR=6.9
Pod documented
134 0 ▶sub root { shift->run('root') }
Subroutine root: not called CC=1 SCAR=6.9
Pod documented
135 0 ▶sub usb { shift->run('usb') }
Subroutine usb: not called CC=1 SCAR=6.9
Pod documented
136 0 ▶sub tcpip { shift->run('tcpip', @_) }
Subroutine tcpip: not called CC=1 SCAR=6.9
Pod documented
137  
138 0 ▶sub push {
Subroutine push: not called CC=1 SCAR=6.9
Pod documented
139 0 my ($self, $local, $remote) = @_;
140 0 $self->run(push => $local, $remote)
141 }
142  
143 0 ▶sub pull {
Subroutine pull: not called CC=1 SCAR=6.9
Pod documented
144 0 my ($self, $remote, $local) = @_;
145 0 $self->run(pull => $remote, $local)
146 }
147  
148 0 ▶sub pull_archive {
Subroutine pull_archive: not called CC=1 SCAR=6.9
Pod documented
149 0 my ($self, $remote, $local) = @_;
150 0 $self->run(pull => '-a', $remote, $local)
151 }
152  
153 0 ▶sub shell { shift->run(shell => @_) }
Subroutine shell: not called CC=1 SCAR=6.9
Pod documented
154  
155 1 1;
156 __END__
« lib/Android/ElectricSheep/Automator/XMLParsers.pm ↑ Summary script/electric-sheep-close-app.pl »