Chromium Code Reviews| Index: build/android/pylib/device/device_utils.py |
| diff --git a/build/android/pylib/device/device_utils.py b/build/android/pylib/device/device_utils.py |
| index c8a5c3a48124107fe9142489c6cf999e4d143eb4..8c89eb28771012c8b3d3ff5ccff89f13b2215716 100644 |
| --- a/build/android/pylib/device/device_utils.py |
| +++ b/build/android/pylib/device/device_utils.py |
| @@ -7,15 +7,85 @@ Provides a variety of device interactions based on adb. |
| Eventually, this will be based on adb_wrapper. |
| """ |
| +# pylint: disable=W0613 |
| import pylib.android_commands |
| from pylib.device import adb_wrapper |
| +from pylib.utils import reraiser_thread |
| +from pylib.utils import timeout_retry |
| -def GetAVDs(): |
| +_DEFAULT_TIMEOUT = 30 |
| +_DEFAULT_RETRIES = 3 |
| + |
| + |
| +def WithTimeoutAndRetries(default_timeout, default_retries): |
| + """ An annotation that handles timeouts and retries using the provided |
| + default timeout and retry values if they aren't specified as a kwarg. |
| + """ |
| + def ResultingAnnotation(f): |
| + def wrapper(*args, **kwargs): |
| + if 'timeout' in kwargs: |
| + timeout = kwargs['timeout'] |
| + else: |
| + timeout = default_timeout |
| + kwargs['timeout'] = timeout |
| + if 'retries' in kwargs: |
| + retries = kwargs['retries'] |
| + else: |
| + retries = default_retries |
| + kwargs['retries'] = retries |
|
bulach
2014/05/01 11:18:57
nit: how about:
timeout = kwargs['timeout'] = kwar
|
| + def _impl(): |
| + return f(*args, **kwargs) |
| + try: |
| + return timeout_retry.Run(_impl, timeout, retries) |
| + except reraiser_thread.TimeoutError as e: |
| + raise adb_wrapper.CommandTimeoutError(str(e)) |
| + return wrapper |
| + return ResultingAnnotation |
| + |
| + |
| +def WithTimeoutAndRetriesFromInstance(f): |
| + """ An annotation that handles timeouts and retries using default timeout |
| + and retry values from the object if they aren't specified as a kwarg. |
| + """ |
| + def wrapper(device, *args, **kwargs): |
| + timeout = device._default_timeout # pylint: disable=W0212 |
| + retries = device._default_retries # pylint: disable=W0212 |
| + return WithTimeoutAndRetries(timeout, retries)(f)(device, *args, **kwargs) |
| + return wrapper |
| + |
| + |
| +@WithTimeoutAndRetries(_DEFAULT_TIMEOUT, _DEFAULT_RETRIES) |
|
bulach
2014/05/01 11:18:57
to be very honest... :)
this is neat, but I think
|
| +def GetAVDs(timeout=None, retries=None): |
| + """ Returns a list of Android Virtual Devices. |
| + |
| + Args: |
| + timeout: The length of time to wait before giving up on either killing or |
| + restarting the server. |
| + retries: The number of times to retry either killing or restarting the |
| + server. |
| + Returns: |
| + A list containing the configured AVDs. |
| + """ |
| return pylib.android_commands.GetAVDs() |
| +@WithTimeoutAndRetries(_DEFAULT_TIMEOUT, _DEFAULT_RETRIES) |
| +def RestartServer(timeout=None, retries=None): |
| + """ Restarts the adb server. |
| + |
| + Args: |
| + timeout: The length of time to wait before giving up on either killing or |
| + restarting the server. |
| + retries: The number of times to retry either killing or restarting the |
| + server. |
| + Raises: |
| + CommandFailedError if we fail to kill or restart the server. |
| + """ |
| + pylib.android_commands.AndroidCommands().RestartAdbServer() |
| + |
| + |
| class DeviceUtils(object): |
| def __init__(self, device): |