Android-ElectricSheep-Automator

 view release on metacpan or  search on metacpan

lib/Android/ElectricSheep/Automator/ADB.pm  view on Meta::CPAN

sub run2 {
	my ($self, @args) = @_;
	my ($out, $in);
	my @dev_args = $self->{device_serial} ? ('-s', $self->{device_serial}) : ();
	my $pid = open2 $out, $in, $self->{path}, @{$self->{args}}, @args;
	my $result = read_file $out;
	close $out;
	close $in;
	waitpid $pid, 0 or croak "$!";
	$result;
}

# returns an arrayref of
#   [statuscode, stdout, stderr]
# on success statuscode is 0 and stdout/stderr may or may not have something
# on failure statuscode is 1 and stdout/stderr may or may not have something
# the '$result' returned by original run2() is now the stdout,
# the 2nd item in the returned arrayref
sub run {
	my ($self, @args) = @_;
	my ($out, $in, $err);
	my @dev_args = $self->{device_serial} ? ('-s', $self->{device_serial}) : ();
	my @cmd = ($self->{path}, @{$self->{args}}, @args);
	if( $self->{verbosity} > 0 ){ print STDOUT __PACKAGE__.'::run()'." : executing command : ".join(' ', @cmd)."\n" }
	# check if undef is passed in @cmd, IPC does not like that
	for (@cmd){
		if( ! defined $_ ){
			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";
			carp $errstr;
			return [1, "", $errstr]
		}
	}
	my $res = eval {
	  IPC::Run::run(
		\@cmd,
		\$in, \$out, \$err,
		# AHP: on timeout it throws an exception matching
		#   /^IPC::Run: .*timed out/
		# or specify your own exception name (see doc)
		# I can't find what unit the timeout interval is!
		IPC::Run::timeout(1000)
	  )
	};
	# WARNING: adb on error sometimes returns non-zero exit code
	# but sometimes it exits normally with zero but there is
	# an error which you can find in the STDOUT/STDERR.
	# Unfortunately this is not a consistent message.
	# For example:
	#   adb shell xyz
	# will set $? to 127
	# but, e.g. this:
	#   adb shell input keycombination 1 2
	# will exit with $?=0 and print some error message!
	# Searching for 'Error:' in STDERR is ok?
	# TODO: this needs to be dealt with here.
	my $exit_code = $? >> 8;
	#if( (! $res) || $@ || ($err=~/\bError\: /) ){
	if( ($exit_code > 0) || $@ || ($err=~/\bError\: /) ){
		carp "STDERR:\n${err}\nOTHER INFO: $@\n\n"
			.(($@=~/IPC::Run: .*timed out/)
			  ?"\nWARNING: it looks like a timeout has occured.\n\n"
			  :""
			 )
			.__PACKAGE__.'::run()'." : error, failed to execute command (see above for stderr), exit code was '${exit_code}': ".join(' ', @cmd)
		; # end carp
		return [1, $out, $err, $exit_code];
	}
	return [0, $out, $err, $exit_code];
}

sub start_server { shift->run('start-server') }
sub kill_server  { shift->run('kill-server') }

sub connect      { shift->run('connect', @_) }
sub disconnect   { shift->run('disconnect', @_) }

sub devices      {
	my $ret = shift->run('devices', '-l');
	if( $ret->[0] != 0 ){
		print STDERR __PACKAGE__.'::devices()'." : error, failed to enquire devices:\n".$ret->[1]."\n".$ret->[2];
		return undef
	}
	my @devices = grep { ! /^List of devices/ }
		      grep { / / }
		      split '\n', $ret->[1]; # stdout contains the devices one in each line
	my @result;
	for (@devices) {
		next if /^List of devices/;
		next unless / /;
		push @result, Android::ElectricSheep::Automator::ADB::Device->new(split)
	}
	@result
}

sub set_device {
	my ($self, $device) = @_;
	$self->{device_serial} = $device->serial;
}

sub wait_for_device   { shift->run('wait-for-device') }
sub get_state         { shift->run('get-state') }
sub get_serialno      { shift->run('get-serialno') }
sub get_devpath       { shift->run('get-devpath') }
sub remount           { shift->run('remount') }
sub reboot            { shift->run('reboot', @_) }
sub reboot_bootloader { shift->run('reboot-bootloader') }
sub root              { shift->run('root') }
sub usb               { shift->run('usb') }
sub tcpip             { shift->run('tcpip', @_) }

sub push {
	my ($self, $local, $remote) = @_;
	$self->run(push => $local, $remote)
}

sub pull {
	my ($self, $remote, $local) = @_;
	$self->run(pull => $remote, $local)
}

sub pull_archive {



( run in 0.734 second using v1.01-cache-2.11-cpan-39bf76dae61 )