Chromium Code Reviews| 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 collections |
| 12 import errno | 12 import errno |
| 13 import logging | 13 import logging |
| 14 import os | 14 import os |
| 15 import re | |
| 15 | 16 |
| 16 from pylib import cmd_helper | 17 from pylib import cmd_helper |
| 17 from pylib import constants | 18 from pylib import constants |
| 18 from pylib.device import decorators | 19 from pylib.device import decorators |
| 19 from pylib.device import device_errors | 20 from pylib.device import device_errors |
| 20 from pylib.device import device_filter | |
| 21 from pylib.utils import timeout_retry | 21 from pylib.utils import timeout_retry |
| 22 | 22 |
| 23 | 23 |
| 24 _DEFAULT_TIMEOUT = 30 | 24 _DEFAULT_TIMEOUT = 30 |
| 25 _DEFAULT_RETRIES = 2 | 25 _DEFAULT_RETRIES = 2 |
| 26 | 26 |
| 27 _EMULATOR_RE = re.compile(r'^emulator-[0-9]+$') | |
| 28 | |
| 27 | 29 |
| 28 def _VerifyLocalFileExists(path): | 30 def _VerifyLocalFileExists(path): |
| 29 """Verifies a local file exists. | 31 """Verifies a local file exists. |
| 30 | 32 |
| 31 Args: | 33 Args: |
| 32 path: Path to the local file. | 34 path: Path to the local file. |
| 33 | 35 |
| 34 Raises: | 36 Raises: |
| 35 IOError: If the file doesn't exist. | 37 IOError: If the file doesn't exist. |
| 36 """ | 38 """ |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 def KillServer(cls, timeout=_DEFAULT_TIMEOUT, retries=_DEFAULT_RETRIES): | 155 def KillServer(cls, timeout=_DEFAULT_TIMEOUT, retries=_DEFAULT_RETRIES): |
| 154 cls._RunAdbCmd(['kill-server'], timeout=timeout, retries=retries) | 156 cls._RunAdbCmd(['kill-server'], timeout=timeout, retries=retries) |
| 155 | 157 |
| 156 @classmethod | 158 @classmethod |
| 157 def StartServer(cls, timeout=_DEFAULT_TIMEOUT, retries=_DEFAULT_RETRIES): | 159 def StartServer(cls, timeout=_DEFAULT_TIMEOUT, retries=_DEFAULT_RETRIES): |
| 158 # CPU affinity is used to reduce adb instability http://crbug.com/268450 | 160 # CPU affinity is used to reduce adb instability http://crbug.com/268450 |
| 159 cls._RunAdbCmd(['start-server'], timeout=timeout, retries=retries, | 161 cls._RunAdbCmd(['start-server'], timeout=timeout, retries=retries, |
| 160 cpu_affinity=0) | 162 cpu_affinity=0) |
| 161 | 163 |
| 162 @classmethod | 164 @classmethod |
| 163 def GetDevices(cls, filters=None, timeout=_DEFAULT_TIMEOUT, | 165 def GetDevices(cls, timeout=_DEFAULT_TIMEOUT, retries=_DEFAULT_RETRIES): |
| 164 retries=_DEFAULT_RETRIES): | |
| 165 """DEPRECATED. Refer to Devices(...) below.""" | 166 """DEPRECATED. Refer to Devices(...) below.""" |
| 166 # TODO(jbudorick): Remove this function once no more clients are using it. | 167 # TODO(jbudorick): Remove this function once no more clients are using it. |
|
perezju
2015/04/24 08:30:31
nit: maybe log a warning?
jbudorick
2015/04/24 15:59:40
I will be converting the few remaining callers of
| |
| 167 return cls.Devices(filters=filters, timeout=timeout, retries=retries) | 168 return cls.Devices(timeout=timeout, retries=retries) |
| 168 | 169 |
| 169 @classmethod | 170 @classmethod |
| 170 def Devices(cls, filters=None, timeout=_DEFAULT_TIMEOUT, | 171 def Devices(cls, timeout=_DEFAULT_TIMEOUT, retries=_DEFAULT_RETRIES): |
| 171 retries=_DEFAULT_RETRIES): | |
| 172 """Get the list of active attached devices. | 172 """Get the list of active attached devices. |
| 173 | 173 |
| 174 Args: | 174 Args: |
| 175 filters: (optional) A list of binary functions that take an AdbWrapper | 175 filters: DEPRECATED (optional) A list of binary functions that take an |
| 176 instance and a string description. Any device for which all provided | 176 AdbWrapper instance and a string description. Any device for which |
| 177 filter functions do not return True will not be included in the | 177 all provided filter functions do not return True will not be included |
| 178 returned list. | 178 in the returned list. |
|
perezju
2015/04/24 08:30:31
nit: if option is gone, also remove docstring
jbudorick
2015/04/24 15:59:40
whoops, done.
| |
| 179 timeout: (optional) Timeout per try in seconds. | 179 timeout: (optional) Timeout per try in seconds. |
| 180 retries: (optional) Number of retries to attempt. | 180 retries: (optional) Number of retries to attempt. |
| 181 | 181 |
| 182 Yields: | 182 Yields: |
| 183 AdbWrapper instances. | 183 AdbWrapper instances. |
| 184 """ | 184 """ |
| 185 output = cls._RunAdbCmd(['devices'], timeout=timeout, retries=retries) | 185 output = cls._RunAdbCmd(['devices'], timeout=timeout, retries=retries) |
| 186 lines = (line.split() for line in output.splitlines()) | 186 lines = (line.split() for line in output.splitlines()) |
| 187 devices = (AdbWrapper(line[0]) for line in lines if len(line) == 2) | 187 return [AdbWrapper(line[0]) for line in lines if len(line) == 2] |
| 188 | |
| 189 def matches_all_filters(device): | |
| 190 for f in filters or (): | |
| 191 if not f(device): | |
| 192 logging.info('Device %s failed filter %s', device.GetDeviceSerial(), | |
| 193 f.__name__) | |
| 194 return False | |
| 195 return True | |
| 196 | |
| 197 return [d for d in devices if matches_all_filters(d)] | |
| 198 | 188 |
| 199 def GetDeviceSerial(self): | 189 def GetDeviceSerial(self): |
| 200 """Gets the device serial number associated with this object. | 190 """Gets the device serial number associated with this object. |
| 201 | 191 |
| 202 Returns: | 192 Returns: |
| 203 Device serial number as a string. | 193 Device serial number as a string. |
| 204 """ | 194 """ |
| 205 return self._device_serial | 195 return self._device_serial |
| 206 | 196 |
| 207 def Push(self, local, remote, timeout=60*5, retries=_DEFAULT_RETRIES): | 197 def Push(self, local, remote, timeout=60*5, retries=_DEFAULT_RETRIES): |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 544 """Restarts the adbd daemon with root permissions, if possible. | 534 """Restarts the adbd daemon with root permissions, if possible. |
| 545 | 535 |
| 546 Args: | 536 Args: |
| 547 timeout: (optional) Timeout per try in seconds. | 537 timeout: (optional) Timeout per try in seconds. |
| 548 retries: (optional) Number of retries to attempt. | 538 retries: (optional) Number of retries to attempt. |
| 549 """ | 539 """ |
| 550 output = self._RunDeviceAdbCmd(['root'], timeout, retries) | 540 output = self._RunDeviceAdbCmd(['root'], timeout, retries) |
| 551 if 'cannot' in output: | 541 if 'cannot' in output: |
| 552 raise device_errors.AdbCommandFailedError( | 542 raise device_errors.AdbCommandFailedError( |
| 553 ['root'], output, device_serial=self._device_serial) | 543 ['root'], output, device_serial=self._device_serial) |
| 544 | |
| 545 @property | |
| 546 def is_emulator(self): | |
| 547 return _EMULATOR_RE.match(self._device_serial) | |
| 548 | |
| 549 @property | |
| 550 def is_ready(self): | |
| 551 try: | |
| 552 return self.GetState() == 'device' | |
| 553 except device_errors.CommandFailedError: | |
| 554 return False | |
| 555 | |
| OLD | NEW |