Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 """ | 5 """ |
| 6 Provides a variety of device interactions based on adb. | 6 Provides a variety of device interactions based on adb. |
| 7 | 7 |
| 8 Eventually, this will be based on adb_wrapper. | 8 Eventually, this will be based on adb_wrapper. |
| 9 """ | 9 """ |
| 10 # pylint: disable=W0613 | |
| 10 | 11 |
| 11 import pylib.android_commands | 12 import pylib.android_commands |
| 12 from pylib.device import adb_wrapper | 13 from pylib.device import adb_wrapper |
| 14 from pylib.utils import reraiser_thread | |
| 15 from pylib.utils import timeout_retry | |
| 13 | 16 |
| 14 | 17 |
| 15 def GetAVDs(): | 18 _DEFAULT_TIMEOUT = 30 |
| 19 _DEFAULT_RETRIES = 3 | |
| 20 | |
| 21 | |
| 22 def WithTimeoutAndRetries(default_timeout, default_retries): | |
| 23 """ An annotation that handles timeouts and retries using the provided | |
| 24 default timeout and retry values if they aren't specified as a kwarg. | |
| 25 """ | |
| 26 def ResultingAnnotation(f): | |
| 27 def wrapper(*args, **kwargs): | |
| 28 if 'timeout' in kwargs: | |
| 29 timeout = kwargs['timeout'] | |
| 30 else: | |
| 31 timeout = default_timeout | |
| 32 kwargs['timeout'] = timeout | |
| 33 if 'retries' in kwargs: | |
| 34 retries = kwargs['retries'] | |
| 35 else: | |
| 36 retries = default_retries | |
| 37 kwargs['retries'] = retries | |
|
bulach
2014/05/01 11:18:57
nit: how about:
timeout = kwargs['timeout'] = kwar
| |
| 38 def _impl(): | |
| 39 return f(*args, **kwargs) | |
| 40 try: | |
| 41 return timeout_retry.Run(_impl, timeout, retries) | |
| 42 except reraiser_thread.TimeoutError as e: | |
| 43 raise adb_wrapper.CommandTimeoutError(str(e)) | |
| 44 return wrapper | |
| 45 return ResultingAnnotation | |
| 46 | |
| 47 | |
| 48 def WithTimeoutAndRetriesFromInstance(f): | |
| 49 """ An annotation that handles timeouts and retries using default timeout | |
| 50 and retry values from the object if they aren't specified as a kwarg. | |
| 51 """ | |
| 52 def wrapper(device, *args, **kwargs): | |
| 53 timeout = device._default_timeout # pylint: disable=W0212 | |
| 54 retries = device._default_retries # pylint: disable=W0212 | |
| 55 return WithTimeoutAndRetries(timeout, retries)(f)(device, *args, **kwargs) | |
| 56 return wrapper | |
| 57 | |
| 58 | |
| 59 @WithTimeoutAndRetries(_DEFAULT_TIMEOUT, _DEFAULT_RETRIES) | |
|
bulach
2014/05/01 11:18:57
to be very honest... :)
this is neat, but I think
| |
| 60 def GetAVDs(timeout=None, retries=None): | |
| 61 """ Returns a list of Android Virtual Devices. | |
| 62 | |
| 63 Args: | |
| 64 timeout: The length of time to wait before giving up on either killing or | |
| 65 restarting the server. | |
| 66 retries: The number of times to retry either killing or restarting the | |
| 67 server. | |
| 68 Returns: | |
| 69 A list containing the configured AVDs. | |
| 70 """ | |
| 16 return pylib.android_commands.GetAVDs() | 71 return pylib.android_commands.GetAVDs() |
| 17 | 72 |
| 18 | 73 |
| 74 @WithTimeoutAndRetries(_DEFAULT_TIMEOUT, _DEFAULT_RETRIES) | |
| 75 def RestartServer(timeout=None, retries=None): | |
| 76 """ Restarts the adb server. | |
| 77 | |
| 78 Args: | |
| 79 timeout: The length of time to wait before giving up on either killing or | |
| 80 restarting the server. | |
| 81 retries: The number of times to retry either killing or restarting the | |
| 82 server. | |
| 83 Raises: | |
| 84 CommandFailedError if we fail to kill or restart the server. | |
| 85 """ | |
| 86 pylib.android_commands.AndroidCommands().RestartAdbServer() | |
| 87 | |
| 88 | |
| 19 class DeviceUtils(object): | 89 class DeviceUtils(object): |
| 20 | 90 |
| 21 def __init__(self, device): | 91 def __init__(self, device): |
| 22 self.old_interface = None | 92 self.old_interface = None |
| 23 if isinstance(device, basestring): | 93 if isinstance(device, basestring): |
| 24 self.old_interface = pylib.android_commands.AndroidCommands(device) | 94 self.old_interface = pylib.android_commands.AndroidCommands(device) |
| 25 elif isinstance(device, adb_wrapper.AdbWrapper): | 95 elif isinstance(device, adb_wrapper.AdbWrapper): |
| 26 self.old_interface = pylib.android_commands.AndroidCommands(str(device)) | 96 self.old_interface = pylib.android_commands.AndroidCommands(str(device)) |
| 27 elif isinstance(device, pylib.android_commands.AndroidCommands): | 97 elif isinstance(device, pylib.android_commands.AndroidCommands): |
| 28 self.old_interface = device | 98 self.old_interface = device |
| 29 elif not device: | 99 elif not device: |
| 30 self.old_interface = pylib.android_commands.AndroidCommands() | 100 self.old_interface = pylib.android_commands.AndroidCommands() |
| 31 | 101 |
| OLD | NEW |