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 """Provides a variety of device interactions based on adb. | 5 """Provides a variety of device interactions based on adb. |
6 | 6 |
7 Eventually, this will be based on adb_wrapper. | 7 Eventually, this will be based on adb_wrapper. |
8 """ | 8 """ |
9 # pylint: disable=W0613 | 9 # pylint: disable=W0613 |
10 | 10 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 @decorators.WithTimeoutAndRetriesFromInstance() | 92 @decorators.WithTimeoutAndRetriesFromInstance() |
93 def HasRoot(self, timeout=None, retries=None): | 93 def HasRoot(self, timeout=None, retries=None): |
94 """Checks whether or not adbd has root privileges. | 94 """Checks whether or not adbd has root privileges. |
95 | 95 |
96 Args: | 96 Args: |
97 timeout: Same as for |IsOnline|. | 97 timeout: Same as for |IsOnline|. |
98 retries: Same as for |IsOnline|. | 98 retries: Same as for |IsOnline|. |
99 Returns: | 99 Returns: |
100 True if adbd has root privileges, False otherwise. | 100 True if adbd has root privileges, False otherwise. |
101 """ | 101 """ |
| 102 return self._HasRootImpl() |
| 103 |
| 104 def _HasRootImpl(self): |
| 105 """ Implementation of HasRoot. |
| 106 |
| 107 This is split from HasRoot to allow other DeviceUtils methods to call |
| 108 HasRoot without spawning a new timeout thread. |
| 109 |
| 110 Returns: |
| 111 Same as for |HasRoot|. |
| 112 """ |
102 return self.old_interface.IsRootEnabled() | 113 return self.old_interface.IsRootEnabled() |
103 | 114 |
104 @decorators.WithTimeoutAndRetriesFromInstance() | 115 @decorators.WithTimeoutAndRetriesFromInstance() |
105 def EnableRoot(self, timeout=None, retries=None): | 116 def EnableRoot(self, timeout=None, retries=None): |
106 """Restarts adbd with root privileges. | 117 """Restarts adbd with root privileges. |
107 | 118 |
108 Args: | 119 Args: |
109 timeout: Same as for |IsOnline|. | 120 timeout: Same as for |IsOnline|. |
110 retries: Same as for |IsOnline|. | 121 retries: Same as for |IsOnline|. |
111 Raises: | 122 Raises: |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 Raises: | 175 Raises: |
165 Same as for |WaitUntilFullyBooted|. | 176 Same as for |WaitUntilFullyBooted|. |
166 """ | 177 """ |
167 if timeout is None: | 178 if timeout is None: |
168 timeout = self._default_timeout | 179 timeout = self._default_timeout |
169 self.old_interface.WaitForSystemBootCompleted(timeout) | 180 self.old_interface.WaitForSystemBootCompleted(timeout) |
170 self.old_interface.WaitForDevicePm() | 181 self.old_interface.WaitForDevicePm() |
171 self.old_interface.WaitForSdCardReady(timeout) | 182 self.old_interface.WaitForSdCardReady(timeout) |
172 if wifi: | 183 if wifi: |
173 while not 'Wi-Fi is enabled' in ( | 184 while not 'Wi-Fi is enabled' in ( |
174 self.old_interface.RunShellCommand('dumpsys wifi')): | 185 self._RunShellCommandImpl('dumpsys wifi')): |
175 time.sleep(0.1) | 186 time.sleep(0.1) |
176 | 187 |
177 @decorators.WithTimeoutAndRetriesDefaults( | 188 @decorators.WithTimeoutAndRetriesDefaults( |
178 10 * _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) | 189 10 * _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) |
179 def Reboot(self, block=True, timeout=None, retries=None): | 190 def Reboot(self, block=True, timeout=None, retries=None): |
180 """Reboot the device. | 191 """Reboot the device. |
181 | 192 |
182 Args: | 193 Args: |
183 block: A boolean indicating if we should wait for the reboot to complete. | 194 block: A boolean indicating if we should wait for the reboot to complete. |
184 timeout: Same as for |IsOnline|. | 195 timeout: Same as for |IsOnline|. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 try: | 236 try: |
226 out = self.old_interface.Install(apk_path, reinstall=reinstall) | 237 out = self.old_interface.Install(apk_path, reinstall=reinstall) |
227 for line in out.splitlines(): | 238 for line in out.splitlines(): |
228 if 'Failure' in line: | 239 if 'Failure' in line: |
229 raise device_errors.CommandFailedError( | 240 raise device_errors.CommandFailedError( |
230 ['adb', 'install', apk_path], line.strip()) | 241 ['adb', 'install', apk_path], line.strip()) |
231 except AssertionError as e: | 242 except AssertionError as e: |
232 raise device_errors.CommandFailedError( | 243 raise device_errors.CommandFailedError( |
233 ['adb', 'install', apk_path], str(e)) | 244 ['adb', 'install', apk_path], str(e)) |
234 | 245 |
| 246 @decorators.WithTimeoutAndRetriesFromInstance() |
| 247 def RunShellCommand(self, cmd, check_return=False, root=False, timeout=None, |
| 248 retries=None): |
| 249 """Run an ADB shell command. |
| 250 |
| 251 TODO(jbudorick) Switch the default value of check_return to True after |
| 252 AndroidCommands is gone. |
| 253 |
| 254 Args: |
| 255 cmd: A list containing the command to run on the device and any arguments. |
| 256 check_return: A boolean indicating whether or not the return code should |
| 257 be checked. |
| 258 timeout: Same as for |IsOnline|. |
| 259 retries: Same as for |IsOnline|. |
| 260 Raises: |
| 261 CommandFailedError if check_return is True and the return code is nozero. |
| 262 Returns: |
| 263 The output of the command. |
| 264 """ |
| 265 return self._RunShellCommandImpl(cmd, check_return=check_return, root=root, |
| 266 timeout=timeout) |
| 267 |
| 268 def _RunShellCommandImpl(self, cmd, check_return=False, root=False, |
| 269 timeout=None): |
| 270 """Implementation of RunShellCommand. |
| 271 |
| 272 This is split from RunShellCommand to allow other DeviceUtils methods to |
| 273 call RunShellCommand without spawning a new timeout thread. |
| 274 |
| 275 TODO(jbudorick) Remove the timeout parameter once this is no longer |
| 276 implemented via AndroidCommands. |
| 277 |
| 278 Args: |
| 279 cmd: Same as for |RunShellCommand|. |
| 280 check_return: Same as for |RunShellCommand|. |
| 281 timeout: Same as for |IsOnline|. |
| 282 Raises: |
| 283 Same as for |RunShellCommand|. |
| 284 Returns: |
| 285 Same as for |RunShellCommand|. |
| 286 """ |
| 287 if isinstance(cmd, list): |
| 288 cmd = ' '.join(cmd) |
| 289 if root and not self.HasRoot(): |
| 290 cmd = 'su -c %s' % cmd |
| 291 if check_return: |
| 292 code, output = self.old_interface.GetShellCommandStatusAndOutput( |
| 293 cmd, timeout_time=timeout) |
| 294 if int(code) != 0: |
| 295 raise device_errors.CommandFailedError( |
| 296 cmd, 'Nonzero exit code (%d)' % code) |
| 297 else: |
| 298 output = self.old_interface.RunShellCommand(cmd, timeout_time=timeout) |
| 299 return output |
| 300 |
235 def __str__(self): | 301 def __str__(self): |
236 """Returns the device serial.""" | 302 """Returns the device serial.""" |
237 return self.old_interface.GetDevice() | 303 return self.old_interface.GetDevice() |
238 | 304 |
239 @staticmethod | 305 @staticmethod |
240 def parallel(devices=None, async=False): | 306 def parallel(devices=None, async=False): |
241 """Creates a Parallelizer to operate over the provided list of devices. | 307 """Creates a Parallelizer to operate over the provided list of devices. |
242 | 308 |
243 If |devices| is either |None| or an empty list, the Parallelizer will | 309 If |devices| is either |None| or an empty list, the Parallelizer will |
244 operate over all attached devices. | 310 operate over all attached devices. |
245 | 311 |
246 Args: | 312 Args: |
247 devices: A list of either DeviceUtils instances or objects from | 313 devices: A list of either DeviceUtils instances or objects from |
248 from which DeviceUtils instances can be constructed. If None, | 314 from which DeviceUtils instances can be constructed. If None, |
249 all attached devices will be used. | 315 all attached devices will be used. |
250 async: If true, returns a Parallelizer that runs operations | 316 async: If true, returns a Parallelizer that runs operations |
251 asynchronously. | 317 asynchronously. |
252 Returns: | 318 Returns: |
253 A Parallelizer operating over |devices|. | 319 A Parallelizer operating over |devices|. |
254 """ | 320 """ |
255 if not devices or len(devices) == 0: | 321 if not devices or len(devices) == 0: |
256 devices = pylib.android_commands.GetAttachedDevices() | 322 devices = pylib.android_commands.GetAttachedDevices() |
257 parallelizer_type = (parallelizer.Parallelizer if async | 323 parallelizer_type = (parallelizer.Parallelizer if async |
258 else parallelizer.SyncParallelizer) | 324 else parallelizer.SyncParallelizer) |
259 return parallelizer_type([ | 325 return parallelizer_type([ |
260 d if isinstance(d, DeviceUtils) else DeviceUtils(d) | 326 d if isinstance(d, DeviceUtils) else DeviceUtils(d) |
261 for d in devices]) | 327 for d in devices]) |
262 | 328 |
OLD | NEW |