| 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 json | 7 import json |
| 8 import logging | 8 import logging |
| 9 import os | 9 import os |
| 10 import os.path | 10 import os.path |
| (...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 self.CleanLogs() | 414 self.CleanLogs() |
| 415 self.ForwardObservatoryPorts() | 415 self.ForwardObservatoryPorts() |
| 416 | 416 |
| 417 # If we are running as root, don't carry over the native logs from logcat - | 417 # If we are running as root, don't carry over the native logs from logcat - |
| 418 # we will have these in the stdout. | 418 # we will have these in the stdout. |
| 419 p = self.ShowLogs(include_native_logs=(not self._RunAdbAsRoot())) | 419 p = self.ShowLogs(include_native_logs=(not self._RunAdbAsRoot())) |
| 420 self.StartShell(arguments, sys.stdout, p.terminate) | 420 self.StartShell(arguments, sys.stdout, p.terminate) |
| 421 p.wait() | 421 p.wait() |
| 422 return None | 422 return None |
| 423 | 423 |
| 424 def RunAndGetOutput(self, arguments): | 424 def RunAndGetOutput(self, arguments, timeout=None): |
| 425 """Runs the shell with given arguments until shell exits. | 425 """Runs the shell with given arguments until shell exits and returns the |
| 426 output. |
| 426 | 427 |
| 427 Args: | 428 Args: |
| 428 arguments: list of arguments for the shell | 429 arguments: list of arguments for the shell |
| 430 timeout: maximum running time in seconds, after which the shell will be |
| 431 terminated |
| 429 | 432 |
| 430 Returns: | 433 Returns: |
| 431 A tuple of (return_code, output). |return_code| is the exit code returned | 434 A tuple of (return_code, output, did_time_out). |return_code| is the exit |
| 432 by the shell or None if the exit code cannot be retrieved. |output| is the | 435 code returned by the shell or None if the exit code cannot be retrieved. |
| 433 stdout mingled with the stderr produced by the shell. | 436 |output| is the stdout mingled with the stderr produced by the shell. |
| 437 |did_time_out| is True iff the shell was terminated because it exceeded |
| 438 the |timeout| and False otherwise. |
| 434 """ | 439 """ |
| 435 (r, w) = os.pipe() | 440 class Results: |
| 436 with os.fdopen(r, "r") as rf: | 441 """Workaround for Python scoping rules that prevent assigning to variables |
| 437 with os.fdopen(w, "w") as wf: | 442 from the outer scope. |
| 438 self.StartShell(arguments, wf, wf.close) | 443 """ |
| 439 output = rf.read() | 444 output = None |
| 440 return None, output | 445 |
| 446 def do_run(): |
| 447 (r, w) = os.pipe() |
| 448 with os.fdopen(r, "r") as rf: |
| 449 with os.fdopen(w, "w") as wf: |
| 450 self.StartShell(arguments, wf, wf.close) |
| 451 Results.output = rf.read() |
| 452 |
| 453 run_thread = threading.Thread(target=do_run) |
| 454 run_thread.start() |
| 455 run_thread.join(timeout) |
| 456 |
| 457 if run_thread.is_alive(): |
| 458 self.StopShell() |
| 459 return None, Results.output, True |
| 460 return None, Results.output, False |
| OLD | NEW |