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. | |
frankf
2014/06/13 23:16:10
no space before Run
| |
250 | |
251 Args: | |
252 cmd: A list containing the command to run on the device and any arguments. | |
253 check_return: A boolean indicating whether or not the return code should | |
frankf
2014/06/13 23:16:10
can you add a TODO to flip this to True? (In spiri
| |
254 be checked. | |
255 timeout: Same as for |IsOnline|. | |
256 retries: Same as for |IsOnline|. | |
257 Raises: | |
258 CommandFailedError if check_return is True and the return code is nozero. | |
259 Returns: | |
260 The output of the command. | |
261 """ | |
262 return self._RunShellCommandImpl(cmd, check_return=check_return, root=root, | |
263 timeout=timeout) | |
264 | |
265 def _RunShellCommandImpl(self, cmd, check_return=False, root=False, | |
266 timeout=None): | |
267 """ Implementation of RunShellCommand. | |
268 | |
269 This is split from RunShellCommand to allow other DeviceUtils methods to | |
270 call RunShellCommand without spawning a new timeout thread. | |
271 | |
272 TODO(jbudorick) Remove the timeout parameter once this is no longer | |
273 implemented via AndroidCommands. | |
274 | |
275 Args: | |
276 cmd: Same as for |RunShellCommand|. | |
277 check_return: Same as for |RunShellCommand|. | |
278 timeout: Same as for |IsOnline|. | |
279 Raises: | |
280 Same as for |RunShellCommand|. | |
281 Returns: | |
282 Same as for |RunShellCommand|. | |
283 """ | |
284 if isinstance(cmd, list): | |
285 cmd = ' '.join(cmd) | |
286 if root and not self.HasRoot(): | |
287 cmd = 'su -c %s' % cmd | |
288 if check_return: | |
289 code, output = self.old_interface.GetShellCommandStatusAndOutput( | |
290 cmd, timeout_time=timeout) | |
291 if int(code) != 0: | |
292 raise device_errors.CommandFailedError( | |
293 cmd, 'Nonzero exit code (%d)' % code) | |
294 else: | |
295 output = self.old_interface.RunShellCommand(cmd, timeout_time=timeout) | |
296 return output | |
297 | |
235 def __str__(self): | 298 def __str__(self): |
236 """Returns the device serial.""" | 299 """Returns the device serial.""" |
237 return self.old_interface.GetDevice() | 300 return self.old_interface.GetDevice() |
238 | 301 |
239 @staticmethod | 302 @staticmethod |
240 def parallel(devices=None, async=False): | 303 def parallel(devices=None, async=False): |
241 """Creates a Parallelizer to operate over the provided list of devices. | 304 """Creates a Parallelizer to operate over the provided list of devices. |
242 | 305 |
243 If |devices| is either |None| or an empty list, the Parallelizer will | 306 If |devices| is either |None| or an empty list, the Parallelizer will |
244 operate over all attached devices. | 307 operate over all attached devices. |
245 | 308 |
246 Args: | 309 Args: |
247 devices: A list of either DeviceUtils instances or objects from | 310 devices: A list of either DeviceUtils instances or objects from |
248 from which DeviceUtils instances can be constructed. If None, | 311 from which DeviceUtils instances can be constructed. If None, |
249 all attached devices will be used. | 312 all attached devices will be used. |
250 async: If true, returns a Parallelizer that runs operations | 313 async: If true, returns a Parallelizer that runs operations |
251 asynchronously. | 314 asynchronously. |
252 Returns: | 315 Returns: |
253 A Parallelizer operating over |devices|. | 316 A Parallelizer operating over |devices|. |
254 """ | 317 """ |
255 if not devices or len(devices) == 0: | 318 if not devices or len(devices) == 0: |
256 devices = pylib.android_commands.GetAttachedDevices() | 319 devices = pylib.android_commands.GetAttachedDevices() |
257 parallelizer_type = (parallelizer.Parallelizer if async | 320 parallelizer_type = (parallelizer.Parallelizer if async |
258 else parallelizer.SyncParallelizer) | 321 else parallelizer.SyncParallelizer) |
259 return parallelizer_type([ | 322 return parallelizer_type([ |
260 d if isinstance(d, DeviceUtils) else DeviceUtils(d) | 323 d if isinstance(d, DeviceUtils) else DeviceUtils(d) |
261 for d in devices]) | 324 for d in devices]) |
262 | 325 |
OLD | NEW |