| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 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 subprocess | 5 import subprocess |
| 6 import threading | 6 import threading |
| 7 import tempfile | 7 import tempfile |
| 8 | 8 |
| 9 from devtoolslib import http_server | 9 from devtoolslib import http_server |
| 10 from devtoolslib.shell import Shell | 10 from devtoolslib.shell import Shell |
| 11 from devtoolslib.utils import overrides | 11 from devtoolslib.utils import overrides |
| 12 | 12 |
| 13 | 13 |
| 14 class LinuxShell(Shell): | 14 class LinuxShell(Shell): |
| 15 """Wrapper around Mojo shell running on Linux. | 15 """Wrapper around Mojo shell running on Linux. |
| 16 | 16 |
| 17 Args: | 17 Args: |
| 18 executable_path: path to the shell binary | 18 executable_path: path to the shell binary |
| 19 command_prefix: optional list of arguments to prepend to the shell command, | 19 command_prefix: optional list of arguments to prepend to the shell command, |
| 20 allowing e.g. to run the shell under debugger. | 20 allowing e.g. to run the shell under debugger. |
| 21 """ | 21 """ |
| 22 | 22 |
| 23 def __init__(self, executable_path, command_prefix=None): | 23 def __init__(self, executable_path, command_prefix=None): |
| 24 self.executable_path = executable_path | 24 self.executable_path = executable_path |
| 25 self.command_prefix = command_prefix if command_prefix else [] | 25 self.command_prefix = command_prefix if command_prefix else [] |
| 26 | 26 |
| 27 @overrides(Shell) | 27 @overrides(Shell) |
| 28 def serve_local_directory(self, local_dir_path, port=0): | 28 def serve_local_directory(self, local_dir_path, port=0, free_host_port=False): |
| 29 mappings = [('', [local_dir_path])] | 29 mappings = [('', [local_dir_path])] |
| 30 return 'http://%s:%d/' % http_server.start_http_server(mappings, port) | 30 return 'http://%s:%d/' % http_server.start_http_server(mappings, port) |
| 31 | 31 |
| 32 @overrides(Shell) | 32 @overrides(Shell) |
| 33 def serve_local_directories(self, mappings, port=0): | 33 def serve_local_directories(self, mappings, port=0, free_host_port=False): |
| 34 return 'http://%s:%d/' % http_server.start_http_server(mappings, port) | 34 return 'http://%s:%d/' % http_server.start_http_server(mappings, port) |
| 35 | 35 |
| 36 @overrides(Shell) | 36 @overrides(Shell) |
| 37 def forward_host_port_to_shell(self, host_port): | 37 def forward_host_port_to_shell(self, host_port): |
| 38 pass | 38 pass |
| 39 | 39 |
| 40 @overrides(Shell) | 40 @overrides(Shell) |
| 41 def run(self, arguments): | 41 def run(self, arguments): |
| 42 command = self.command_prefix + [self.executable_path] + arguments | 42 command = self.command_prefix + [self.executable_path] + arguments |
| 43 return subprocess.call(command, stderr=subprocess.STDOUT) | 43 return subprocess.call(command, stderr=subprocess.STDOUT) |
| 44 | 44 |
| 45 @overrides(Shell) | 45 @overrides(Shell) |
| 46 def run_and_get_output(self, arguments, timeout=None): | 46 def run_and_get_output(self, arguments, timeout=None): |
| 47 command = self.command_prefix + [self.executable_path] + arguments | 47 command = self.command_prefix + [self.executable_path] + arguments |
| 48 output_file = tempfile.TemporaryFile() | 48 output_file = tempfile.TemporaryFile() |
| 49 p = subprocess.Popen(command, stdout=output_file, stderr=output_file) | 49 p = subprocess.Popen(command, stdout=output_file, stderr=output_file) |
| 50 | 50 |
| 51 wait_thread = threading.Thread(target=p.wait) | 51 wait_thread = threading.Thread(target=p.wait) |
| 52 wait_thread.start() | 52 wait_thread.start() |
| 53 wait_thread.join(timeout) | 53 wait_thread.join(timeout) |
| 54 | 54 |
| 55 did_time_out = False | 55 did_time_out = False |
| 56 if p.poll() is None: | 56 if p.poll() is None: |
| 57 did_time_out = True | 57 did_time_out = True |
| 58 p.terminate() | 58 p.terminate() |
| 59 p.poll() | 59 p.poll() |
| 60 output_file.seek(0) | 60 output_file.seek(0) |
| 61 output = output_file.read() | 61 output = output_file.read() |
| 62 return p.returncode, output, did_time_out | 62 return p.returncode, output, did_time_out |
| OLD | NEW |