Exemplo n.º 1
0
 def _RunAdbCmd(cls,
                args,
                timeout=None,
                retries=None,
                device_serial=None,
                check_error=True):
     status, output = cmd_helper.GetCmdStatusAndOutputWithTimeout(
         cls._BuildAdbCmd(args, device_serial),
         timeout_retry.CurrentTimeoutThread().GetRemainingTime())
     if status != 0:
         raise device_errors.AdbCommandFailedError(args, output, status,
                                                   device_serial)
     # This catches some errors, including when the device drops offline;
     # unfortunately adb is very inconsistent with error reporting so many
     # command failures present differently.
     if check_error and output.startswith('error:'):
         raise device_errors.AdbCommandFailedError(args, output)
     return output
Exemplo n.º 2
0
 def _RunAdbCmd(cls,
                arg_list,
                timeout=None,
                retries=None,
                check_error=True):
     cmd = [constants.GetAdbPath()] + arg_list
     exit_code, output = cmd_helper.GetCmdStatusAndOutputWithTimeout(
         cmd,
         timeout_retry.CurrentTimeoutThread().GetRemainingTime())
     if exit_code != 0:
         raise device_errors.AdbCommandFailedError(
             cmd, 'returned non-zero exit code %d and output %r' %
             (exit_code, output))
     # This catches some errors, including when the device drops offline;
     # unfortunately adb is very inconsistent with error reporting so many
     # command failures present differently.
     if check_error and output[:len('error:')] == 'error:':
         raise device_errors.AdbCommandFailedError(arg_list, output)
     return output
Exemplo n.º 3
0
    def Root(self, timeout=_DEFAULT_TIMEOUT, retries=_DEFAULT_RETRIES):
        """Restarts the adbd daemon with root permissions, if possible.

    Args:
      timeout: (optional) Timeout per try in seconds.
      retries: (optional) Number of retries to attempt.
    """
        output = self._DeviceAdbCmd(['root'], timeout, retries)
        if 'cannot' in output:
            raise device_errors.AdbCommandFailedError(['root'], output)
Exemplo n.º 4
0
    def Shell(self,
              command,
              expect_rc=0,
              timeout=_DEFAULT_TIMEOUT,
              retries=_DEFAULT_RETRIES):
        """Runs a shell command on the device.

    Args:
      command: The shell command to run.
      expect_rc: (optional) Check that the command's return code matches this
        value. Default is 0. If set to None the test is skipped.
      timeout: (optional) Timeout per try in seconds.
      retries: (optional) Number of retries to attempt.

    Returns:
      The output of the shell command as a string.

    Raises:
      device_errors.AdbCommandFailedError: If the return code doesn't match
        |expect_rc|.
    """
        if expect_rc is None:
            actual_command = command
        else:
            actual_command = '%s; echo %%$?;' % command.rstrip()
        output = self._DeviceAdbCmd(['shell', actual_command],
                                    timeout,
                                    retries,
                                    check_error=False)
        if expect_rc is not None:
            output_end = output.rfind('%')
            if output_end < 0:
                # causes the string for rc to become empty and also raise a ValueError
                output_end = len(output)

            try:
                rc = int(output[output_end + 1:])
            except ValueError:
                raise device_errors.AdbCommandFailedError(
                    ['shell'],
                    'command %r on device produced output %r where no'
                    ' valid return code was found' % (actual_command, output),
                    self._device_serial)

            output = output[:output_end]
            if rc != expect_rc:
                raise device_errors.AdbShellCommandFailedError(
                    command, rc, output, self._device_serial)
        return output
Exemplo n.º 5
0
  def Pull(self, remote, local, timeout=60*5, retries=_DEFAULT_RETRIES):
    """Pulls a file from the device to the host.

    Args:
      remote: Path on the device filesystem.
      local: Path on the host filesystem.
      timeout: (optional) Timeout per try in seconds.
      retries: (optional) Number of retries to attempt.
    """
    cmd = ['pull', remote, local]
    self._RunDeviceAdbCmd(cmd, timeout, retries)
    try:
      _VerifyLocalFileExists(local)
    except IOError:
      raise device_errors.AdbCommandFailedError(
          cmd, 'File not found on host: %s' % local, device_serial=str(self))
Exemplo n.º 6
0
  def Uninstall(self, package, keep_data=False, timeout=_DEFAULT_TIMEOUT,
                retries=_DEFAULT_RETRIES):
    """Remove the app |package| from the device.

    Args:
      package: The package to uninstall.
      keep_data: (optional) If set keep the data and cache directories.
      timeout: (optional) Timeout per try in seconds.
      retries: (optional) Number of retries to attempt.
    """
    cmd = ['uninstall']
    if keep_data:
      cmd.append('-k')
    cmd.append(package)
    output = self._DeviceAdbCmd(cmd, timeout, retries)
    if 'Failure' in output:
      raise device_errors.AdbCommandFailedError(cmd, output)
Exemplo n.º 7
0
    def _RunShellCommandImpl(self,
                             cmd,
                             check_return=False,
                             as_root=False,
                             timeout=None):
        """Implementation of RunShellCommand.

    This is split from RunShellCommand to allow other DeviceUtils methods to
    call RunShellCommand without spawning a new timeout thread.

    TODO(jbudorick) Remove the timeout parameter once this is no longer
    implemented via AndroidCommands.

    Args:
      cmd: Same as for |RunShellCommand|.
      check_return: Same as for |RunShellCommand|.
      as_root: Same as for |RunShellCommand|.
      timeout: timeout in seconds

    Raises:
      Same as for |RunShellCommand|.

    Returns:
      Same as for |RunShellCommand|.
    """
        if isinstance(cmd, list):
            cmd = ' '.join(cmd)
        if as_root and not self.HasRoot():
            cmd = 'su -c %s' % cmd
        if check_return:
            code, output = self.old_interface.GetShellCommandStatusAndOutput(
                cmd, timeout_time=timeout)
            if int(code) != 0:
                raise device_errors.AdbCommandFailedError(
                    cmd.split(),
                    'Nonzero exit code (%d)' % code,
                    device=str(self))
        else:
            output = self.old_interface.RunShellCommand(cmd,
                                                        timeout_time=timeout)
        return output
Exemplo n.º 8
0
    def InstallMultiple(self,
                        apk_paths,
                        forward_lock=False,
                        reinstall=False,
                        sd_card=False,
                        allow_downgrade=False,
                        partial=False,
                        timeout=60 * 2,
                        retries=_DEFAULT_RETRIES):
        """Install an apk with splits on the device.

    Args:
      apk_paths: Host path to the APK file.
      forward_lock: (optional) If set forward-locks the app.
      reinstall: (optional) If set reinstalls the app, keeping its data.
      sd_card: (optional) If set installs on the SD card.
      timeout: (optional) Timeout per try in seconds.
      retries: (optional) Number of retries to attempt.
      allow_downgrade: (optional) Allow versionCode downgrade.
      partial: (optional) Package ID if apk_paths doesn't include all .apks.
    """
        for path in apk_paths:
            _VerifyLocalFileExists(path)
        cmd = ['install-multiple']
        if forward_lock:
            cmd.append('-l')
        if reinstall:
            cmd.append('-r')
        if sd_card:
            cmd.append('-s')
        if allow_downgrade:
            cmd.append('-d')
        if partial:
            cmd.extend(('-p', partial))
        cmd.extend(apk_paths)
        output = self._RunDeviceAdbCmd(cmd, timeout, retries)
        if 'Success' not in output:
            raise device_errors.AdbCommandFailedError(
                cmd, output, device_serial=self._device_serial)
Exemplo n.º 9
0
  def Install(self, apk_path, forward_lock=False, reinstall=False,
              sd_card=False, timeout=60*2, retries=_DEFAULT_RETRIES):
    """Install an apk on the device.

    Args:
      apk_path: Host path to the APK file.
      forward_lock: (optional) If set forward-locks the app.
      reinstall: (optional) If set reinstalls the app, keeping its data.
      sd_card: (optional) If set installs on the SD card.
      timeout: (optional) Timeout per try in seconds.
      retries: (optional) Number of retries to attempt.
    """
    _VerifyLocalFileExists(apk_path)
    cmd = ['install']
    if forward_lock:
      cmd.append('-l')
    if reinstall:
      cmd.append('-r')
    if sd_card:
      cmd.append('-s')
    cmd.append(apk_path)
    output = self._DeviceAdbCmd(cmd, timeout, retries)
    if 'Success' not in output:
      raise device_errors.AdbCommandFailedError(cmd, output)
Exemplo n.º 10
0
 def action(cmd):
   if exists_result:
     return ''
   else:
     raise device_errors.AdbCommandFailedError(
         cmd, 'File not found', 1, str(self.adb))