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 RunWithTimeoutAndRetries(f, *args, **kwargs): | |
|
frankf
2014/05/01 18:31:01
There's also something similar in AdbWrapper. Can
| |
| 23 """ Handles the timeout and retry logic. | |
| 24 Args: | |
| 25 f: The callable object to run with timeout and retry support. | |
| 26 """ | |
| 27 assert('timeout' in kwargs and 'retries' in kwargs) | |
| 28 timeout = kwargs['timeout'] | |
| 29 retries = kwargs['retries'] | |
| 30 | |
| 31 def bound_f(): | |
| 32 return f(*args, **kwargs) | |
| 33 | |
| 34 try: | |
| 35 return timeout_retry.Run(bound_f, timeout, retries) | |
| 36 except reraiser_thread.TimeoutError as e: | |
| 37 raise adb_wrapper.CommandTimeoutError(str(e)) | |
| 38 | |
| 39 | |
| 40 def GetAVDs(timeout=_DEFAULT_TIMEOUT, retries=_DEFAULT_RETRIES): | |
| 41 """ Returns a list of Android Virtual Devices. | |
| 42 | |
| 43 Args: | |
| 44 timeout: The length of time to wait before giving up on either killing or | |
| 45 restarting the server. | |
| 46 retries: The number of times to retry either killing or restarting the | |
| 47 server. | |
| 48 Returns: | |
| 49 A list containing the configured AVDs. | |
| 50 """ | |
| 51 return RunWithTimeoutAndRetries(_GetAVDsImpl, timeout=timeout, | |
| 52 retries=retries) | |
| 53 | |
| 54 | |
| 55 def _GetAVDsImpl(): | |
| 56 """ Implementation of GetAVDs. | |
| 57 """ | |
| 16 return pylib.android_commands.GetAVDs() | 58 return pylib.android_commands.GetAVDs() |
| 17 | 59 |
| 18 | 60 |
| 61 def RestartServer(timeout=_DEFAULT_TIMEOUT, retries=_DEFAULT_RETRIES): | |
|
frankf
2014/05/01 18:31:01
RestartServer -> RestartAdbServer
| |
| 62 """ Restarts the adb server. | |
| 63 | |
| 64 Args: | |
| 65 timeout: The length of time to wait before giving up on either killing or | |
| 66 restarting the server. | |
| 67 retries: The number of times to retry either killing or restarting the | |
| 68 server. | |
| 69 Raises: | |
| 70 CommandFailedError if we fail to kill or restart the server. | |
| 71 """ | |
| 72 RunWithTimeoutAndRetries(_RestartServerImpl, timeout=timeout, | |
| 73 retries=retries) | |
| 74 | |
| 75 | |
| 76 def _RestartServerImpl(): | |
| 77 """ Implementation of RestartServer. | |
| 78 """ | |
| 79 pylib.android_commands.AndroidCommands().RestartAdbServer() | |
|
frankf
2014/05/01 18:31:01
You should think of thread-safety for any method y
| |
| 80 | |
| 81 | |
| 19 class DeviceUtils(object): | 82 class DeviceUtils(object): |
| 20 | 83 |
| 21 def __init__(self, device): | 84 def __init__(self, device): |
| 22 self.old_interface = None | 85 self.old_interface = None |
| 23 if isinstance(device, basestring): | 86 if isinstance(device, basestring): |
| 24 self.old_interface = pylib.android_commands.AndroidCommands(device) | 87 self.old_interface = pylib.android_commands.AndroidCommands(device) |
| 25 elif isinstance(device, adb_wrapper.AdbWrapper): | 88 elif isinstance(device, adb_wrapper.AdbWrapper): |
| 26 self.old_interface = pylib.android_commands.AndroidCommands(str(device)) | 89 self.old_interface = pylib.android_commands.AndroidCommands(str(device)) |
| 27 elif isinstance(device, pylib.android_commands.AndroidCommands): | 90 elif isinstance(device, pylib.android_commands.AndroidCommands): |
| 28 self.old_interface = device | 91 self.old_interface = device |
| 29 elif not device: | 92 elif not device: |
| 30 self.old_interface = pylib.android_commands.AndroidCommands() | 93 self.old_interface = pylib.android_commands.AndroidCommands() |
| 31 | 94 |
| OLD | NEW |