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 import atexit | 5 import atexit |
| 6 import hashlib | 6 import hashlib |
| 7 import logging | 7 import logging |
| 8 import os | 8 import os |
| 9 import os.path | 9 import os.path |
| 10 import random | 10 import random |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 class AndroidShell(Shell): | 89 class AndroidShell(Shell): |
| 90 """Wrapper around Mojo shell running on an Android device. | 90 """Wrapper around Mojo shell running on an Android device. |
| 91 | 91 |
| 92 Args: | 92 Args: |
| 93 adb_path: Path to adb, optional if adb is in PATH. | 93 adb_path: Path to adb, optional if adb is in PATH. |
| 94 target_device: Device to run on, if multiple devices are connected. | 94 target_device: Device to run on, if multiple devices are connected. |
| 95 logcat_tags: Comma-separated list of additional logcat tags to use. | 95 logcat_tags: Comma-separated list of additional logcat tags to use. |
| 96 """ | 96 """ |
| 97 | 97 |
| 98 def __init__(self, adb_path="adb", target_device=None, logcat_tags=None, | 98 def __init__(self, adb_path="adb", target_device=None, logcat_tags=None, |
| 99 verbose_pipe=None): | 99 verbose=False): |
| 100 self.adb_path = adb_path | 100 self.adb_path = adb_path |
| 101 self.target_device = target_device | 101 self.target_device = target_device |
| 102 self.stop_shell_registered = False | 102 self.stop_shell_registered = False |
| 103 self.adb_running_as_root = None | 103 self.adb_running_as_root = None |
| 104 self.additional_logcat_tags = logcat_tags | 104 self.additional_logcat_tags = logcat_tags |
| 105 self.verbose_pipe = verbose_pipe if verbose_pipe else open(os.devnull, 'w') | 105 self.verbose_stdout = sys.stdout if verbose else open(os.devnull, 'w') |
| 106 self.verbose_stderr = sys.stderr if verbose else open(os.devnull, 'w') | |
|
qsr
2015/11/12 14:47:37
sys.stderr if verbose else self.verbose_stdout
Thi
ppi
2015/11/12 14:50:17
Done.
| |
| 106 | 107 |
| 107 def _adb_command(self, args): | 108 def _adb_command(self, args): |
| 108 """Forms an adb command from the given arguments, prepending the adb path | 109 """Forms an adb command from the given arguments, prepending the adb path |
| 109 and adding a target device specifier, if needed. | 110 and adding a target device specifier, if needed. |
| 110 """ | 111 """ |
| 111 adb_command = [self.adb_path] | 112 adb_command = [self.adb_path] |
| 112 if self.target_device: | 113 if self.target_device: |
| 113 adb_command.extend(['-s', self.target_device]) | 114 adb_command.extend(['-s', self.target_device]) |
| 114 adb_command.extend(args) | 115 adb_command.extend(args) |
| 115 return adb_command | 116 return adb_command |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 | 202 |
| 202 def _run_adb_as_root(self): | 203 def _run_adb_as_root(self): |
| 203 if self.adb_running_as_root is not None: | 204 if self.adb_running_as_root is not None: |
| 204 return self.adb_running_as_root | 205 return self.adb_running_as_root |
| 205 | 206 |
| 206 if ('cannot run as root' not in subprocess.check_output( | 207 if ('cannot run as root' not in subprocess.check_output( |
| 207 self._adb_command(['root']))): | 208 self._adb_command(['root']))): |
| 208 # Wait for adbd to restart. | 209 # Wait for adbd to restart. |
| 209 subprocess.check_call( | 210 subprocess.check_call( |
| 210 self._adb_command(['wait-for-device']), | 211 self._adb_command(['wait-for-device']), |
| 211 stdout=self.verbose_pipe) | 212 stdout=self.verbose_stdout, stderr=self.verbose_stderr) |
| 212 self.adb_running_as_root = True | 213 self.adb_running_as_root = True |
| 213 else: | 214 else: |
| 214 self.adb_running_as_root = False | 215 self.adb_running_as_root = False |
| 215 | 216 |
| 216 return self.adb_running_as_root | 217 return self.adb_running_as_root |
| 217 | 218 |
| 218 def _is_shell_package_installed(self): | 219 def _is_shell_package_installed(self): |
| 219 # Adb should print one line if the package is installed and return empty | 220 # Adb should print one line if the package is installed and return empty |
| 220 # string otherwise. | 221 # string otherwise. |
| 221 return len(subprocess.check_output(self._adb_command([ | 222 return len(subprocess.check_output(self._adb_command([ |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 290 apk_sha1 = hashlib.sha1(open(shell_apk_path, 'rb').read()).hexdigest() | 291 apk_sha1 = hashlib.sha1(open(shell_apk_path, 'rb').read()).hexdigest() |
| 291 device_apk_sha1 = subprocess.check_output(self._adb_command([ | 292 device_apk_sha1 = subprocess.check_output(self._adb_command([ |
| 292 'shell', 'cat', device_sha1_path])) | 293 'shell', 'cat', device_sha1_path])) |
| 293 do_install = (apk_sha1 != device_apk_sha1 or | 294 do_install = (apk_sha1 != device_apk_sha1 or |
| 294 not self._is_shell_package_installed()) | 295 not self._is_shell_package_installed()) |
| 295 | 296 |
| 296 if do_install: | 297 if do_install: |
| 297 subprocess.check_call( | 298 subprocess.check_call( |
| 298 self._adb_command(['install', '-r', shell_apk_path, '-i', | 299 self._adb_command(['install', '-r', shell_apk_path, '-i', |
| 299 _MOJO_SHELL_PACKAGE_NAME]), | 300 _MOJO_SHELL_PACKAGE_NAME]), |
| 300 stdout=self.verbose_pipe) | 301 stdout=self.verbose_stdout, stderr=self.verbose_stderr) |
| 301 | 302 |
| 302 # Update the stamp on the device. | 303 # Update the stamp on the device. |
| 303 with tempfile.NamedTemporaryFile() as fp: | 304 with tempfile.NamedTemporaryFile() as fp: |
| 304 fp.write(apk_sha1) | 305 fp.write(apk_sha1) |
| 305 fp.flush() | 306 fp.flush() |
| 306 subprocess.check_call(self._adb_command(['push', fp.name, | 307 subprocess.check_call(self._adb_command(['push', fp.name, |
| 307 device_sha1_path]), | 308 device_sha1_path]), |
| 308 stdout=self.verbose_pipe) | 309 stdout=self.verbose_stdout, |
| 310 stderr=self.verbose_stderr) | |
| 309 else: | 311 else: |
| 310 # To ensure predictable state after running install_apk(), we need to stop | 312 # To ensure predictable state after running install_apk(), we need to stop |
| 311 # the shell here, as this is what "adb install" implicitly does. | 313 # the shell here, as this is what "adb install" implicitly does. |
| 312 self.stop_shell() | 314 self.stop_shell() |
| 313 | 315 |
| 314 def start_shell(self, | 316 def start_shell(self, |
| 315 arguments, | 317 arguments, |
| 316 stdout=None, | 318 stdout=None, |
| 317 on_application_stop=None): | 319 on_application_stop=None): |
| 318 """Starts the mojo shell, passing it the given arguments. | 320 """Starts the mojo shell, passing it the given arguments. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 352 if parameters: | 354 if parameters: |
| 353 device_filename = ( | 355 device_filename = ( |
| 354 '/sdcard/%s/args_%s' % (_MOJO_SHELL_PACKAGE_NAME, str(uuid.uuid4()))) | 356 '/sdcard/%s/args_%s' % (_MOJO_SHELL_PACKAGE_NAME, str(uuid.uuid4()))) |
| 355 with tempfile.NamedTemporaryFile(delete=False) as temp: | 357 with tempfile.NamedTemporaryFile(delete=False) as temp: |
| 356 try: | 358 try: |
| 357 for parameter in parameters: | 359 for parameter in parameters: |
| 358 temp.write(parameter) | 360 temp.write(parameter) |
| 359 temp.write('\n') | 361 temp.write('\n') |
| 360 temp.close() | 362 temp.close() |
| 361 subprocess.check_call(self._adb_command( | 363 subprocess.check_call(self._adb_command( |
| 362 ['push', temp.name, device_filename])) | 364 ['push', temp.name, device_filename]), |
| 365 stdout=self.verbose_stdout, stderr=self.verbose_stderr) | |
| 363 finally: | 366 finally: |
| 364 os.remove(temp.name) | 367 os.remove(temp.name) |
| 365 | 368 |
| 366 cmd += ['--es', 'argsFile', device_filename] | 369 cmd += ['--es', 'argsFile', device_filename] |
| 367 | 370 |
| 368 subprocess.check_call(cmd, stdout=self.verbose_pipe) | 371 subprocess.check_call(cmd, stdout=self.verbose_stdout, |
| 372 stderr=self.verbose_stderr) | |
| 369 | 373 |
| 370 def stop_shell(self): | 374 def stop_shell(self): |
| 371 """Stops the mojo shell.""" | 375 """Stops the mojo shell.""" |
| 372 subprocess.check_call(self._adb_command(['shell', | 376 subprocess.check_call(self._adb_command(['shell', |
| 373 'am', | 377 'am', |
| 374 'force-stop', | 378 'force-stop', |
| 375 _MOJO_SHELL_PACKAGE_NAME])) | 379 _MOJO_SHELL_PACKAGE_NAME])) |
| 376 | 380 |
| 377 def clean_logs(self): | 381 def clean_logs(self): |
| 378 """Cleans the logs on the device.""" | 382 """Cleans the logs on the device.""" |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 464 Results.output = rf.read() | 468 Results.output = rf.read() |
| 465 | 469 |
| 466 run_thread = threading.Thread(target=do_run) | 470 run_thread = threading.Thread(target=do_run) |
| 467 run_thread.start() | 471 run_thread.start() |
| 468 run_thread.join(timeout) | 472 run_thread.join(timeout) |
| 469 | 473 |
| 470 if run_thread.is_alive(): | 474 if run_thread.is_alive(): |
| 471 self.stop_shell() | 475 self.stop_shell() |
| 472 return None, Results.output, True | 476 return None, Results.output, True |
| 473 return None, Results.output, False | 477 return None, Results.output, False |
| OLD | NEW |