| 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_directories(self, mappings, port=0, free_host_port=False): | 28 def serve_local_directories(self, mappings, port=0, reuse_servers=False): |
| 29 return 'http://%s:%d/' % http_server.start_http_server(mappings, port) | 29 if reuse_servers: |
| 30 assert port, 'Cannot reuse the server when |port| is 0.' |
| 31 server_address = ('127.0.0.1', port) |
| 32 else: |
| 33 server_address = http_server.start_http_server(mappings, port) |
| 34 |
| 35 return 'http://%s:%d/' % server_address |
| 30 | 36 |
| 31 @overrides(Shell) | 37 @overrides(Shell) |
| 32 def forward_host_port_to_shell(self, host_port): | 38 def forward_host_port_to_shell(self, host_port): |
| 33 pass | 39 pass |
| 34 | 40 |
| 35 @overrides(Shell) | 41 @overrides(Shell) |
| 36 def run(self, arguments): | 42 def run(self, arguments): |
| 37 command = self.command_prefix + [self.executable_path] + arguments | 43 command = self.command_prefix + [self.executable_path] + arguments |
| 38 return subprocess.call(command, stderr=subprocess.STDOUT) | 44 return subprocess.call(command, stderr=subprocess.STDOUT) |
| 39 | 45 |
| 40 @overrides(Shell) | 46 @overrides(Shell) |
| 41 def run_and_get_output(self, arguments, timeout=None): | 47 def run_and_get_output(self, arguments, timeout=None): |
| 42 command = self.command_prefix + [self.executable_path] + arguments | 48 command = self.command_prefix + [self.executable_path] + arguments |
| 43 output_file = tempfile.TemporaryFile() | 49 output_file = tempfile.TemporaryFile() |
| 44 p = subprocess.Popen(command, stdout=output_file, stderr=output_file) | 50 p = subprocess.Popen(command, stdout=output_file, stderr=output_file) |
| 45 | 51 |
| 46 wait_thread = threading.Thread(target=p.wait) | 52 wait_thread = threading.Thread(target=p.wait) |
| 47 wait_thread.start() | 53 wait_thread.start() |
| 48 wait_thread.join(timeout) | 54 wait_thread.join(timeout) |
| 49 | 55 |
| 50 did_time_out = False | 56 did_time_out = False |
| 51 if p.poll() is None: | 57 if p.poll() is None: |
| 52 did_time_out = True | 58 did_time_out = True |
| 53 p.terminate() | 59 p.terminate() |
| 54 p.poll() | 60 p.poll() |
| 55 output_file.seek(0) | 61 output_file.seek(0) |
| 56 output = output_file.read() | 62 output = output_file.read() |
| 57 return p.returncode, output, did_time_out | 63 return p.returncode, output, did_time_out |
| OLD | NEW |