| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """This module wraps Android's adb tool. | 5 """This module wraps Android's adb tool. |
| 6 | 6 |
| 7 This is a thin wrapper around the adb interface. Any additional complexity | 7 This is a thin wrapper around the adb interface. Any additional complexity |
| 8 should be delegated to a higher level (ex. DeviceUtils). | 8 should be delegated to a higher level (ex. DeviceUtils). |
| 9 """ | 9 """ |
| 10 | 10 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 | 49 |
| 50 Args: | 50 Args: |
| 51 device_serial: The device serial number as a string. | 51 device_serial: The device serial number as a string. |
| 52 """ | 52 """ |
| 53 if not device_serial: | 53 if not device_serial: |
| 54 raise ValueError('A device serial must be specified') | 54 raise ValueError('A device serial must be specified') |
| 55 self._device_serial = str(device_serial) | 55 self._device_serial = str(device_serial) |
| 56 | 56 |
| 57 # pylint: disable=unused-argument | 57 # pylint: disable=unused-argument |
| 58 @classmethod | 58 @classmethod |
| 59 def _BuildAdbCmd(cls, args, device_serial): |
| 60 cmd = [constants.GetAdbPath()] |
| 61 if device_serial is not None: |
| 62 cmd.extend(['-s', device_serial]) |
| 63 cmd.extend(args) |
| 64 return cmd |
| 65 # pylint: enable=unused-argument |
| 66 |
| 67 # pylint: disable=unused-argument |
| 68 @classmethod |
| 59 @decorators.WithTimeoutAndRetries | 69 @decorators.WithTimeoutAndRetries |
| 60 def _RunAdbCmd(cls, args, timeout=None, retries=None, device_serial=None, | 70 def _RunAdbCmd(cls, args, timeout=None, retries=None, device_serial=None, |
| 61 check_error=True): | 71 check_error=True): |
| 62 cmd = [constants.GetAdbPath()] | |
| 63 if device_serial is not None: | |
| 64 cmd.extend(['-s', device_serial]) | |
| 65 cmd.extend(args) | |
| 66 status, output = cmd_helper.GetCmdStatusAndOutputWithTimeout( | 72 status, output = cmd_helper.GetCmdStatusAndOutputWithTimeout( |
| 67 cmd, timeout_retry.CurrentTimeoutThread().GetRemainingTime()) | 73 cls._BuildAdbCmd(args, device_serial), |
| 74 timeout_retry.CurrentTimeoutThread().GetRemainingTime()) |
| 68 if status != 0: | 75 if status != 0: |
| 69 raise device_errors.AdbCommandFailedError( | 76 raise device_errors.AdbCommandFailedError( |
| 70 args, output, status, device_serial) | 77 args, output, status, device_serial) |
| 71 # This catches some errors, including when the device drops offline; | 78 # This catches some errors, including when the device drops offline; |
| 72 # unfortunately adb is very inconsistent with error reporting so many | 79 # unfortunately adb is very inconsistent with error reporting so many |
| 73 # command failures present differently. | 80 # command failures present differently. |
| 74 if check_error and output.startswith('error:'): | 81 if check_error and output.startswith('error:'): |
| 75 raise device_errors.AdbCommandFailedError(args, output) | 82 raise device_errors.AdbCommandFailedError(args, output) |
| 76 return output | 83 return output |
| 77 # pylint: enable=unused-argument | 84 # pylint: enable=unused-argument |
| 78 | 85 |
| 79 def _RunDeviceAdbCmd(self, args, timeout, retries, check_error=True): | 86 def _RunDeviceAdbCmd(self, args, timeout, retries, check_error=True): |
| 80 """Runs an adb command on the device associated with this object. | 87 """Runs an adb command on the device associated with this object. |
| 81 | 88 |
| 82 Args: | 89 Args: |
| 83 args: A list of arguments to adb. | 90 args: A list of arguments to adb. |
| 84 timeout: Timeout in seconds. | 91 timeout: Timeout in seconds. |
| 85 retries: Number of retries. | 92 retries: Number of retries. |
| 86 check_error: Check that the command doesn't return an error message. This | 93 check_error: Check that the command doesn't return an error message. This |
| 87 does NOT check the exit status of shell commands. | 94 does NOT check the exit status of shell commands. |
| 88 | 95 |
| 89 Returns: | 96 Returns: |
| 90 The output of the command. | 97 The output of the command. |
| 91 """ | 98 """ |
| 92 return self._RunAdbCmd(args, timeout=timeout, retries=retries, | 99 return self._RunAdbCmd(args, timeout=timeout, retries=retries, |
| 93 device_serial=self._device_serial, | 100 device_serial=self._device_serial, |
| 94 check_error=check_error) | 101 check_error=check_error) |
| 95 | 102 |
| 103 def _IterRunDeviceAdbCmd(self, args, timeout): |
| 104 """Runs an adb command and returns an iterator over its output lines. |
| 105 |
| 106 Args: |
| 107 args: A list of arguments to adb. |
| 108 timeout: Timeout in seconds. |
| 109 |
| 110 Yields: |
| 111 The output of the command line by line. |
| 112 """ |
| 113 return cmd_helper.IterCmdOutputLines( |
| 114 self._BuildAdbCmd(args, self._device_serial), timeout=timeout) |
| 115 |
| 96 def __eq__(self, other): | 116 def __eq__(self, other): |
| 97 """Consider instances equal if they refer to the same device. | 117 """Consider instances equal if they refer to the same device. |
| 98 | 118 |
| 99 Args: | 119 Args: |
| 100 other: The instance to compare equality with. | 120 other: The instance to compare equality with. |
| 101 | 121 |
| 102 Returns: | 122 Returns: |
| 103 True if the instances are considered equal, false otherwise. | 123 True if the instances are considered equal, false otherwise. |
| 104 """ | 124 """ |
| 105 return self._device_serial == str(other) | 125 return self._device_serial == str(other) |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 cmd = ['ls', path] | 255 cmd = ['ls', path] |
| 236 lines = self._RunDeviceAdbCmd( | 256 lines = self._RunDeviceAdbCmd( |
| 237 cmd, timeout=timeout, retries=retries).splitlines() | 257 cmd, timeout=timeout, retries=retries).splitlines() |
| 238 if lines: | 258 if lines: |
| 239 return [ParseLine(line) for line in lines] | 259 return [ParseLine(line) for line in lines] |
| 240 else: | 260 else: |
| 241 raise device_errors.AdbCommandFailedError( | 261 raise device_errors.AdbCommandFailedError( |
| 242 cmd, 'path does not specify an accessible directory in the device', | 262 cmd, 'path does not specify an accessible directory in the device', |
| 243 device_serial=self._device_serial) | 263 device_serial=self._device_serial) |
| 244 | 264 |
| 245 def Logcat(self, filter_spec=None, timeout=_DEFAULT_TIMEOUT, | 265 def Logcat(self, filter_spec=None, timeout=None): |
| 246 retries=_DEFAULT_RETRIES): | 266 """Get an iterator over the logcat output. |
| 247 """Get the logcat output. | |
| 248 | 267 |
| 249 Args: | 268 Args: |
| 250 filter_spec: (optional) Spec to filter the logcat. | 269 filter_spec: (optional) Spec to filter the logcat. |
| 251 timeout: (optional) Timeout per try in seconds. | 270 timeout: (optional) Timeout per try in seconds. |
| 252 retries: (optional) Number of retries to attempt. | |
| 253 | 271 |
| 254 Returns: | 272 Yields: |
| 255 logcat output as a string. | 273 logcat output line by line. |
| 256 """ | 274 """ |
| 257 cmd = ['logcat'] | 275 cmd = ['logcat'] |
| 258 if filter_spec is not None: | 276 if filter_spec is not None: |
| 259 cmd.append(filter_spec) | 277 cmd.append(filter_spec) |
| 260 return self._RunDeviceAdbCmd(cmd, timeout, retries, check_error=False) | 278 return self._IterRunDeviceAdbCmd(cmd, timeout) |
| 261 | 279 |
| 262 def Forward(self, local, remote, timeout=_DEFAULT_TIMEOUT, | 280 def Forward(self, local, remote, timeout=_DEFAULT_TIMEOUT, |
| 263 retries=_DEFAULT_RETRIES): | 281 retries=_DEFAULT_RETRIES): |
| 264 """Forward socket connections from the local socket to the remote socket. | 282 """Forward socket connections from the local socket to the remote socket. |
| 265 | 283 |
| 266 Sockets are specified by one of: | 284 Sockets are specified by one of: |
| 267 tcp:<port> | 285 tcp:<port> |
| 268 localabstract:<unix domain socket name> | 286 localabstract:<unix domain socket name> |
| 269 localreserved:<unix domain socket name> | 287 localreserved:<unix domain socket name> |
| 270 localfilesystem:<unix domain socket name> | 288 localfilesystem:<unix domain socket name> |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 """Restarts the adbd daemon with root permissions, if possible. | 456 """Restarts the adbd daemon with root permissions, if possible. |
| 439 | 457 |
| 440 Args: | 458 Args: |
| 441 timeout: (optional) Timeout per try in seconds. | 459 timeout: (optional) Timeout per try in seconds. |
| 442 retries: (optional) Number of retries to attempt. | 460 retries: (optional) Number of retries to attempt. |
| 443 """ | 461 """ |
| 444 output = self._RunDeviceAdbCmd(['root'], timeout, retries) | 462 output = self._RunDeviceAdbCmd(['root'], timeout, retries) |
| 445 if 'cannot' in output: | 463 if 'cannot' in output: |
| 446 raise device_errors.AdbCommandFailedError( | 464 raise device_errors.AdbCommandFailedError( |
| 447 ['root'], output, device_serial=self._device_serial) | 465 ['root'], output, device_serial=self._device_serial) |
| OLD | NEW |