OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 """This module wraps Android's adb tool. | 5 """This module wraps Android's adb tool. |
6 | 6 |
7 This is a thin wrapper around the adb interface. Any additional complexity | 7 This is a thin wrapper around the adb interface. Any additional complexity |
8 should be delegated to a higher level (ex. DeviceUtils). | 8 should be delegated to a higher level (ex. DeviceUtils). |
9 """ | 9 """ |
10 | 10 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 """ | 44 """ |
45 self._device_serial = str(device_serial) | 45 self._device_serial = str(device_serial) |
46 | 46 |
47 # pylint: disable=W0613 | 47 # pylint: disable=W0613 |
48 @classmethod | 48 @classmethod |
49 @decorators.WithTimeoutAndRetries | 49 @decorators.WithTimeoutAndRetries |
50 def _RunAdbCmd(cls, arg_list, timeout=None, retries=None, check_error=True): | 50 def _RunAdbCmd(cls, arg_list, timeout=None, retries=None, check_error=True): |
51 cmd = ['adb'] + arg_list | 51 cmd = ['adb'] + arg_list |
52 exit_code, output = cmd_helper.GetCmdStatusAndOutput(cmd) | 52 exit_code, output = cmd_helper.GetCmdStatusAndOutput(cmd) |
53 if exit_code != 0: | 53 if exit_code != 0: |
54 raise device_errors.CommandFailedError( | 54 raise device_errors.AdbCommandFailedError( |
55 cmd, 'returned non-zero exit code %s, output: %s' % | 55 cmd, 'returned non-zero exit code %s, output: %s' % |
56 (exit_code, output)) | 56 (exit_code, output)) |
57 # This catches some errors, including when the device drops offline; | 57 # This catches some errors, including when the device drops offline; |
58 # unfortunately adb is very inconsistent with error reporting so many | 58 # unfortunately adb is very inconsistent with error reporting so many |
59 # command failures present differently. | 59 # command failures present differently. |
60 if check_error and output[:len('error:')] == 'error:': | 60 if check_error and output[:len('error:')] == 'error:': |
61 raise device_errors.CommandFailedError(arg_list, output) | 61 raise device_errors.AdbCommandFailedError(arg_list, output) |
62 return output | 62 return output |
63 # pylint: enable=W0613 | 63 # pylint: enable=W0613 |
64 | 64 |
65 def _DeviceAdbCmd(self, arg_list, timeout, retries, check_error=True): | 65 def _DeviceAdbCmd(self, arg_list, timeout, retries, check_error=True): |
66 """Runs an adb command on the device associated with this object. | 66 """Runs an adb command on the device associated with this object. |
67 | 67 |
68 Args: | 68 Args: |
69 arg_list: A list of arguments to adb. | 69 arg_list: A list of arguments to adb. |
70 timeout: Timeout in seconds. | 70 timeout: Timeout in seconds. |
71 retries: Number of retries. | 71 retries: Number of retries. |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 command: The shell command to run. | 158 command: The shell command to run. |
159 expect_rc: (optional) If set checks that the command's return code matches | 159 expect_rc: (optional) If set checks that the command's return code matches |
160 this value. | 160 this value. |
161 timeout: (optional) Timeout per try in seconds. | 161 timeout: (optional) Timeout per try in seconds. |
162 retries: (optional) Number of retries to attempt. | 162 retries: (optional) Number of retries to attempt. |
163 | 163 |
164 Returns: | 164 Returns: |
165 The output of the shell command as a string. | 165 The output of the shell command as a string. |
166 | 166 |
167 Raises: | 167 Raises: |
168 device_errors.CommandFailedError: If the return code doesn't match | 168 device_errors.AdbCommandFailedError: If the return code doesn't match |
169 |expect_rc|. | 169 |expect_rc|. |
170 """ | 170 """ |
171 if expect_rc is None: | 171 if expect_rc is None: |
172 actual_command = command | 172 actual_command = command |
173 else: | 173 else: |
174 actual_command = '%s; echo $?;' % command | 174 actual_command = '%s; echo $?;' % command |
175 output = self._DeviceAdbCmd( | 175 output = self._DeviceAdbCmd( |
176 ['shell', actual_command], timeout, retries, check_error=False) | 176 ['shell', actual_command], timeout, retries, check_error=False) |
177 if expect_rc is not None: | 177 if expect_rc is not None: |
178 output_end = output.rstrip().rfind('\n') + 1 | 178 output_end = output.rstrip().rfind('\n') + 1 |
179 rc = output[output_end:].strip() | 179 rc = output[output_end:].strip() |
180 output = output[:output_end] | 180 output = output[:output_end] |
181 if int(rc) != expect_rc: | 181 if int(rc) != expect_rc: |
182 raise device_errors.CommandFailedError( | 182 raise device_errors.AdbCommandFailedError( |
183 ['shell', command], | 183 ['shell', command], |
184 'shell command exited with code: %s' % rc, | 184 'shell command exited with code: %s' % rc, |
185 self._device_serial) | 185 self._device_serial) |
186 return output | 186 return output |
187 | 187 |
188 def Logcat(self, filter_spec=None, timeout=_DEFAULT_TIMEOUT, | 188 def Logcat(self, filter_spec=None, timeout=_DEFAULT_TIMEOUT, |
189 retries=_DEFAULT_RETRIES): | 189 retries=_DEFAULT_RETRIES): |
190 """Get the logcat output. | 190 """Get the logcat output. |
191 | 191 |
192 Args: | 192 Args: |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 cmd = ['install'] | 251 cmd = ['install'] |
252 if forward_lock: | 252 if forward_lock: |
253 cmd.append('-l') | 253 cmd.append('-l') |
254 if reinstall: | 254 if reinstall: |
255 cmd.append('-r') | 255 cmd.append('-r') |
256 if sd_card: | 256 if sd_card: |
257 cmd.append('-s') | 257 cmd.append('-s') |
258 cmd.append(apk_path) | 258 cmd.append(apk_path) |
259 output = self._DeviceAdbCmd(cmd, timeout, retries) | 259 output = self._DeviceAdbCmd(cmd, timeout, retries) |
260 if 'Success' not in output: | 260 if 'Success' not in output: |
261 raise device_errors.CommandFailedError(cmd, output) | 261 raise device_errors.AdbCommandFailedError(cmd, output) |
262 | 262 |
263 def Uninstall(self, package, keep_data=False, timeout=_DEFAULT_TIMEOUT, | 263 def Uninstall(self, package, keep_data=False, timeout=_DEFAULT_TIMEOUT, |
264 retries=_DEFAULT_RETRIES): | 264 retries=_DEFAULT_RETRIES): |
265 """Remove the app |package| from the device. | 265 """Remove the app |package| from the device. |
266 | 266 |
267 Args: | 267 Args: |
268 package: The package to uninstall. | 268 package: The package to uninstall. |
269 keep_data: (optional) If set keep the data and cache directories. | 269 keep_data: (optional) If set keep the data and cache directories. |
270 timeout: (optional) Timeout per try in seconds. | 270 timeout: (optional) Timeout per try in seconds. |
271 retries: (optional) Number of retries to attempt. | 271 retries: (optional) Number of retries to attempt. |
272 """ | 272 """ |
273 cmd = ['uninstall'] | 273 cmd = ['uninstall'] |
274 if keep_data: | 274 if keep_data: |
275 cmd.append('-k') | 275 cmd.append('-k') |
276 cmd.append(package) | 276 cmd.append(package) |
277 output = self._DeviceAdbCmd(cmd, timeout, retries) | 277 output = self._DeviceAdbCmd(cmd, timeout, retries) |
278 if 'Failure' in output: | 278 if 'Failure' in output: |
279 raise device_errors.CommandFailedError(cmd, output) | 279 raise device_errors.AdbCommandFailedError(cmd, output) |
280 | 280 |
281 def Backup(self, path, packages=None, apk=False, shared=False, | 281 def Backup(self, path, packages=None, apk=False, shared=False, |
282 nosystem=True, include_all=False, timeout=_DEFAULT_TIMEOUT, | 282 nosystem=True, include_all=False, timeout=_DEFAULT_TIMEOUT, |
283 retries=_DEFAULT_RETRIES): | 283 retries=_DEFAULT_RETRIES): |
284 """Write an archive of the device's data to |path|. | 284 """Write an archive of the device's data to |path|. |
285 | 285 |
286 Args: | 286 Args: |
287 path: Local path to store the backup file. | 287 path: Local path to store the backup file. |
288 packages: List of to packages to be backed up. | 288 packages: List of to packages to be backed up. |
289 apk: (optional) If set include the .apk files in the archive. | 289 apk: (optional) If set include the .apk files in the archive. |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 | 376 |
377 def Root(self, timeout=_DEFAULT_TIMEOUT, retries=_DEFAULT_RETRIES): | 377 def Root(self, timeout=_DEFAULT_TIMEOUT, retries=_DEFAULT_RETRIES): |
378 """Restarts the adbd daemon with root permissions, if possible. | 378 """Restarts the adbd daemon with root permissions, if possible. |
379 | 379 |
380 Args: | 380 Args: |
381 timeout: (optional) Timeout per try in seconds. | 381 timeout: (optional) Timeout per try in seconds. |
382 retries: (optional) Number of retries to attempt. | 382 retries: (optional) Number of retries to attempt. |
383 """ | 383 """ |
384 output = self._DeviceAdbCmd(['root'], timeout, retries) | 384 output = self._DeviceAdbCmd(['root'], timeout, retries) |
385 if 'cannot' in output: | 385 if 'cannot' in output: |
386 raise device_errors.CommandFailedError(['root'], output) | 386 raise device_errors.AdbCommandFailedError(['root'], output) |
387 | 387 |
OLD | NEW |