| 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 |
| 11 import collections |
| 11 import errno | 12 import errno |
| 12 import logging | 13 import logging |
| 13 import os | 14 import os |
| 14 | 15 |
| 15 from pylib import cmd_helper | 16 from pylib import cmd_helper |
| 16 from pylib import constants | 17 from pylib import constants |
| 17 from pylib.device import decorators | 18 from pylib.device import decorators |
| 18 from pylib.device import device_errors | 19 from pylib.device import device_errors |
| 19 from pylib.utils import timeout_retry | 20 from pylib.utils import timeout_retry |
| 20 | 21 |
| 21 | 22 |
| 22 _DEFAULT_TIMEOUT = 30 | 23 _DEFAULT_TIMEOUT = 30 |
| 23 _DEFAULT_RETRIES = 2 | 24 _DEFAULT_RETRIES = 2 |
| 24 | 25 |
| 25 | 26 |
| 26 def _VerifyLocalFileExists(path): | 27 def _VerifyLocalFileExists(path): |
| 27 """Verifies a local file exists. | 28 """Verifies a local file exists. |
| 28 | 29 |
| 29 Args: | 30 Args: |
| 30 path: Path to the local file. | 31 path: Path to the local file. |
| 31 | 32 |
| 32 Raises: | 33 Raises: |
| 33 IOError: If the file doesn't exist. | 34 IOError: If the file doesn't exist. |
| 34 """ | 35 """ |
| 35 if not os.path.exists(path): | 36 if not os.path.exists(path): |
| 36 raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), path) | 37 raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), path) |
| 37 | 38 |
| 38 | 39 |
| 40 DeviceStat = collections.namedtuple('DeviceStat', |
| 41 ['st_mode', 'st_size', 'st_time']) |
| 42 |
| 43 |
| 39 class AdbWrapper(object): | 44 class AdbWrapper(object): |
| 40 """A wrapper around a local Android Debug Bridge executable.""" | 45 """A wrapper around a local Android Debug Bridge executable.""" |
| 41 | 46 |
| 42 def __init__(self, device_serial): | 47 def __init__(self, device_serial): |
| 43 """Initializes the AdbWrapper. | 48 """Initializes the AdbWrapper. |
| 44 | 49 |
| 45 Args: | 50 Args: |
| 46 device_serial: The device serial number as a string. | 51 device_serial: The device serial number as a string. |
| 47 """ | 52 """ |
| 48 if not device_serial: | 53 if not device_serial: |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 except ValueError: | 198 except ValueError: |
| 194 logging.warning('exit status of shell command %r missing.', command) | 199 logging.warning('exit status of shell command %r missing.', command) |
| 195 raise device_errors.AdbCommandFailedError( | 200 raise device_errors.AdbCommandFailedError( |
| 196 args, output, device_serial=self._device_serial) | 201 args, output, device_serial=self._device_serial) |
| 197 output = output[:output_end] | 202 output = output[:output_end] |
| 198 if status != expect_status: | 203 if status != expect_status: |
| 199 raise device_errors.AdbCommandFailedError( | 204 raise device_errors.AdbCommandFailedError( |
| 200 args, output, status, self._device_serial) | 205 args, output, status, self._device_serial) |
| 201 return output | 206 return output |
| 202 | 207 |
| 208 def Ls(self, path, timeout=_DEFAULT_TIMEOUT, retries=_DEFAULT_RETRIES): |
| 209 """List the contents of a directory on the device. |
| 210 |
| 211 Args: |
| 212 path: Path on the device filesystem. |
| 213 timeout: (optional) Timeout per try in seconds. |
| 214 retries: (optional) Number of retries to attempt. |
| 215 |
| 216 Returns: |
| 217 A list of pairs (filename, stat) for each file found in the directory, |
| 218 where the stat object has the properties: st_mode, st_size, and st_time. |
| 219 |
| 220 Raises: |
| 221 AdbCommandFailedError if |path| does not specify a valid and accessible |
| 222 directory in the device. |
| 223 """ |
| 224 def ParseLine(line): |
| 225 cols = line.split(None, 3) |
| 226 filename = cols.pop() |
| 227 stat = DeviceStat(*[int(num, base=16) for num in cols]) |
| 228 return (filename, stat) |
| 229 |
| 230 cmd = ['ls', path] |
| 231 lines = self._RunDeviceAdbCmd( |
| 232 cmd, timeout=timeout, retries=retries).splitlines() |
| 233 if lines: |
| 234 return [ParseLine(line) for line in lines] |
| 235 else: |
| 236 raise device_errors.AdbCommandFailedError( |
| 237 cmd, 'path does not specify an accessible directory in the device', |
| 238 device_serial=self._device_serial) |
| 239 |
| 203 def Logcat(self, filter_spec=None, timeout=_DEFAULT_TIMEOUT, | 240 def Logcat(self, filter_spec=None, timeout=_DEFAULT_TIMEOUT, |
| 204 retries=_DEFAULT_RETRIES): | 241 retries=_DEFAULT_RETRIES): |
| 205 """Get the logcat output. | 242 """Get the logcat output. |
| 206 | 243 |
| 207 Args: | 244 Args: |
| 208 filter_spec: (optional) Spec to filter the logcat. | 245 filter_spec: (optional) Spec to filter the logcat. |
| 209 timeout: (optional) Timeout per try in seconds. | 246 timeout: (optional) Timeout per try in seconds. |
| 210 retries: (optional) Number of retries to attempt. | 247 retries: (optional) Number of retries to attempt. |
| 211 | 248 |
| 212 Returns: | 249 Returns: |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 """Restarts the adbd daemon with root permissions, if possible. | 433 """Restarts the adbd daemon with root permissions, if possible. |
| 397 | 434 |
| 398 Args: | 435 Args: |
| 399 timeout: (optional) Timeout per try in seconds. | 436 timeout: (optional) Timeout per try in seconds. |
| 400 retries: (optional) Number of retries to attempt. | 437 retries: (optional) Number of retries to attempt. |
| 401 """ | 438 """ |
| 402 output = self._RunDeviceAdbCmd(['root'], timeout, retries) | 439 output = self._RunDeviceAdbCmd(['root'], timeout, retries) |
| 403 if 'cannot' in output: | 440 if 'cannot' in output: |
| 404 raise device_errors.AdbCommandFailedError( | 441 raise device_errors.AdbCommandFailedError( |
| 405 ['root'], output, device_serial=self._device_serial) | 442 ['root'], output, device_serial=self._device_serial) |
| OLD | NEW |