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 # pylint: disable=W0613 |
11 | 11 |
12 import time | |
13 | |
12 import pylib.android_commands | 14 import pylib.android_commands |
13 from pylib.device import adb_wrapper | 15 from pylib.device import adb_wrapper |
14 from pylib.device import decorators | 16 from pylib.device import decorators |
15 from pylib.device import device_errors | 17 from pylib.device import device_errors |
16 from pylib.utils import parallelizer | 18 from pylib.utils import parallelizer |
17 | 19 |
18 _DEFAULT_TIMEOUT = 30 | 20 _DEFAULT_TIMEOUT = 30 |
19 _DEFAULT_RETRIES = 3 | 21 _DEFAULT_RETRIES = 3 |
20 | 22 |
21 | 23 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 elif isinstance(device, adb_wrapper.AdbWrapper): | 65 elif isinstance(device, adb_wrapper.AdbWrapper): |
64 self.old_interface = pylib.android_commands.AndroidCommands(str(device)) | 66 self.old_interface = pylib.android_commands.AndroidCommands(str(device)) |
65 elif isinstance(device, pylib.android_commands.AndroidCommands): | 67 elif isinstance(device, pylib.android_commands.AndroidCommands): |
66 self.old_interface = device | 68 self.old_interface = device |
67 elif not device: | 69 elif not device: |
68 self.old_interface = pylib.android_commands.AndroidCommands() | 70 self.old_interface = pylib.android_commands.AndroidCommands() |
69 else: | 71 else: |
70 raise ValueError('Unsupported type passed for argument "device"') | 72 raise ValueError('Unsupported type passed for argument "device"') |
71 self._default_timeout = default_timeout | 73 self._default_timeout = default_timeout |
72 self._default_retries = default_retries | 74 self._default_retries = default_retries |
75 assert(hasattr(self, decorators.DEFAULT_TIMEOUT_ATTR)) | |
76 assert(hasattr(self, decorators.DEFAULT_RETRIES_ATTR)) | |
73 | 77 |
74 @decorators.WithTimeoutAndRetriesFromInstance( | 78 @decorators.WithTimeoutAndRetriesFromInstance() |
75 '_default_timeout', '_default_retries') | |
76 def IsOnline(self, timeout=None, retries=None): | 79 def IsOnline(self, timeout=None, retries=None): |
77 """ Checks whether the device is online. | 80 """ Checks whether the device is online. |
78 | 81 |
79 Args: | 82 Args: |
80 timeout: An integer containing the number of seconds to wait for the | 83 timeout: An integer containing the number of seconds to wait for the |
81 operation to complete. | 84 operation to complete. |
82 retries: An integer containing the number of times the operation should | 85 retries: An integer containing the number of times the operation should |
83 be retried if it fails. | 86 be retried if it fails. |
84 Returns: | 87 Returns: |
85 True if the device is online, False otherwise. | 88 True if the device is online, False otherwise. |
86 """ | 89 """ |
87 return self.old_interface.IsOnline() | 90 return self.old_interface.IsOnline() |
88 | 91 |
89 @decorators.WithTimeoutAndRetriesFromInstance( | 92 @decorators.WithTimeoutAndRetriesFromInstance() |
90 '_default_timeout', '_default_retries') | |
91 def HasRoot(self, timeout=None, retries=None): | 93 def HasRoot(self, timeout=None, retries=None): |
92 """ Checks whether or not adbd has root privileges. | 94 """ Checks whether or not adbd has root privileges. |
93 | 95 |
94 Args: | 96 Args: |
95 timeout: Same as for |IsOnline|. | 97 timeout: Same as for |IsOnline|. |
96 retries: Same as for |IsOnline|. | 98 retries: Same as for |IsOnline|. |
97 Returns: | 99 Returns: |
98 True if adbd has root privileges, False otherwise. | 100 True if adbd has root privileges, False otherwise. |
99 """ | 101 """ |
100 return self.old_interface.IsRootEnabled() | 102 return self.old_interface.IsRootEnabled() |
101 | 103 |
102 @decorators.WithTimeoutAndRetriesFromInstance( | 104 @decorators.WithTimeoutAndRetriesFromInstance() |
103 '_default_timeout', '_default_retries') | |
104 def EnableRoot(self, timeout=None, retries=None): | 105 def EnableRoot(self, timeout=None, retries=None): |
105 """ Restarts adbd with root privileges. | 106 """ Restarts adbd with root privileges. |
106 | 107 |
107 Args: | 108 Args: |
108 timeout: Same as for |IsOnline|. | 109 timeout: Same as for |IsOnline|. |
109 retries: Same as for |IsOnline|. | 110 retries: Same as for |IsOnline|. |
110 Raises: | 111 Raises: |
111 CommandFailedError if root could not be enabled. | 112 CommandFailedError if root could not be enabled. |
112 """ | 113 """ |
113 if not self.old_interface.EnableAdbRoot(): | 114 if not self.old_interface.EnableAdbRoot(): |
114 raise device_errors.CommandFailedError( | 115 raise device_errors.CommandFailedError( |
115 'adb root', 'Could not enable root.') | 116 'adb root', 'Could not enable root.') |
116 | 117 |
118 @decorators.WithTimeoutAndRetriesFromInstance() | |
119 def GetExternalStoragePath(self, timeout=None, retries=None): | |
120 """ Get the device's path to its SD card. | |
121 | |
122 Args: | |
123 timeout: Same as for |IsOnline|. | |
124 retries: Same as for |IsOnline|. | |
125 Returns: | |
126 The device's path to its SD card. | |
127 """ | |
128 try: | |
129 return self.old_interface.GetExternalStorage() | |
130 except AssertionError as e: | |
131 raise device_errors.CommandFailedError(str(e)) | |
132 | |
133 @decorators.WithTimeoutAndRetriesFromInstance() | |
134 def WaitUntilFullyBooted(self, wifi=False, timeout=None, retries=None): | |
135 """ Wait for the device to fully boot. | |
136 | |
137 This means waiting for the device to boot, the package manager to be | |
138 available, and the SD card to be ready. It can optionally mean waiting | |
139 for wifi to come up, too. | |
140 | |
141 Args: | |
142 wifi: A boolean indicating if we should wait for wifi to come up or not. | |
143 timeout: Same as for |IsOnline|. | |
144 retries: Same as for |IsOnline|. | |
145 Raises: | |
146 CommandTimeoutError if one of the component waits times out. | |
147 DeviceUnreachableError if the device becomes unresponsive. | |
148 """ | |
149 self.old_interface.WaitForSystemBootCompleted(timeout) | |
150 self.old_interface.WaitForDevicePm() | |
151 self.old_interface.WaitForSdCardReady(timeout) | |
152 if wifi: | |
153 while not 'Wi-Fi is enabled' in ( | |
154 self.old_interface.RunShellCommand('dumpsys wifi')): | |
155 time.sleep(0.1) | |
156 | |
117 def __str__(self): | 157 def __str__(self): |
118 """Returns the device serial.""" | 158 """Returns the device serial.""" |
119 return self.old_interface.GetDevice() | 159 return self.old_interface.GetDevice() |
120 | 160 |
121 @staticmethod | 161 @staticmethod |
122 def parallel(devices): | 162 def parallel(devices, async=False): |
123 """ Creates a Parallelizer to operate over the provided list of devices. | 163 """ Creates a Parallelizer to operate over the provided list of devices. |
124 | 164 |
125 If |devices| is either |None| or an empty list, the Parallelizer will | 165 If |devices| is either |None| or an empty list, the Parallelizer will |
126 operate over all attached devices. | 166 operate over all attached devices. |
127 | 167 |
128 Args: | 168 Args: |
129 devices: A list of either DeviceUtils instances or objects from | 169 devices: A list of either DeviceUtils instances or objects from |
130 from which DeviceUtils instances can be constructed. | 170 from which DeviceUtils instances can be constructed. |
171 async: If true, returns a Parallelizer that runs operations | |
172 asynchronously. | |
131 Returns: | 173 Returns: |
132 A Parallelizer operating over |devices|. | 174 A Parallelizer operating over |devices|. |
133 """ | 175 """ |
134 if not devices or len(devices) == 0: | 176 if not devices or len(devices) == 0: |
135 devices = pylib.android_commands.AndroidCommands.GetAttachedDevices() | 177 devices = pylib.android_commands.GetAttachedDevices() |
136 return parallelizer.Parallelizer([ | 178 if async: |
craigdh
2014/05/22 17:22:05
parallelizer_type = parallelizer.Parallelizer if a
jbudorick
2014/05/22 19:26:40
Done.
| |
179 parallelizer_type = parallelizer.Parallelizer | |
180 else: | |
181 parallelizer_type = parallelizer.SyncParallelizer | |
182 return parallelizer_type([ | |
137 d if isinstance(d, DeviceUtils) else DeviceUtils(d) | 183 d if isinstance(d, DeviceUtils) else DeviceUtils(d) |
138 for d in devices]) | 184 for d in devices]) |
139 | 185 |
OLD | NEW |