| 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 | 6 |
| 7 from devtoolslib.shell import Shell | 7 from devtoolslib.shell import Shell |
| 8 from devtoolslib import http_server |
| 8 | 9 |
| 9 | 10 |
| 10 class LinuxShell(Shell): | 11 class LinuxShell(Shell): |
| 11 """Wrapper around Mojo shell running on Linux. | 12 """Wrapper around Mojo shell running on Linux. |
| 12 | 13 |
| 13 Args: | 14 Args: |
| 14 executable_path: path to the shell binary | 15 executable_path: path to the shell binary |
| 15 command_prefix: optional list of arguments to prepend to the shell command, | 16 command_prefix: optional list of arguments to prepend to the shell command, |
| 16 allowing e.g. to run the shell under debugger. | 17 allowing e.g. to run the shell under debugger. |
| 17 """ | 18 """ |
| 18 | 19 |
| 19 def __init__(self, executable_path, command_prefix=None): | 20 def __init__(self, executable_path, command_prefix=None): |
| 20 self.executable_path = executable_path | 21 self.executable_path = executable_path |
| 21 self.command_prefix = command_prefix if command_prefix else [] | 22 self.command_prefix = command_prefix if command_prefix else [] |
| 22 | 23 |
| 24 def ServeLocalDirectory(self, local_dir_path, port=0): |
| 25 """Serves the content of the local (host) directory, making it available to |
| 26 the shell under the url returned by the function. |
| 27 |
| 28 The server will run on a separate thread until the program terminates. The |
| 29 call returns immediately. |
| 30 |
| 31 Args: |
| 32 local_dir_path: path to the directory to be served |
| 33 port: port at which the server will be available to the shell |
| 34 |
| 35 Returns: |
| 36 The url that the shell can use to access the content of |local_dir_path|. |
| 37 """ |
| 38 return 'http://%s:%d/' % http_server.StartHttpServer(local_dir_path, port) |
| 39 |
| 23 def Run(self, arguments): | 40 def Run(self, arguments): |
| 24 """Runs the shell with given arguments until shell exits, passing the stdout | 41 """Runs the shell with given arguments until shell exits, passing the stdout |
| 25 mingled with stderr produced by the shell onto the stdout. | 42 mingled with stderr produced by the shell onto the stdout. |
| 26 | 43 |
| 27 Returns: | 44 Returns: |
| 28 Exit code retured by the shell or None if the exit code cannot be | 45 Exit code retured by the shell or None if the exit code cannot be |
| 29 retrieved. | 46 retrieved. |
| 30 """ | 47 """ |
| 31 command = self.command_prefix + [self.executable_path] + arguments | 48 command = self.command_prefix + [self.executable_path] + arguments |
| 32 return subprocess.call(command, stderr=subprocess.STDOUT) | 49 return subprocess.call(command, stderr=subprocess.STDOUT) |
| 33 | 50 |
| 34 def RunAndGetOutput(self, arguments): | 51 def RunAndGetOutput(self, arguments): |
| 35 """Runs the shell with given arguments until shell exits. | 52 """Runs the shell with given arguments until shell exits. |
| 36 | 53 |
| 37 Args: | 54 Args: |
| 38 arguments: list of arguments for the shell | 55 arguments: list of arguments for the shell |
| 39 | 56 |
| 40 Returns: | 57 Returns: |
| 41 A tuple of (return_code, output). |return_code| is the exit code returned | 58 A tuple of (return_code, output). |return_code| is the exit code returned |
| 42 by the shell or None if the exit code cannot be retrieved. |output| is the | 59 by the shell or None if the exit code cannot be retrieved. |output| is the |
| 43 stdout mingled with the stderr produced by the shell. | 60 stdout mingled with the stderr produced by the shell. |
| 44 """ | 61 """ |
| 45 command = self.command_prefix + [self.executable_path] + arguments | 62 command = self.command_prefix + [self.executable_path] + arguments |
| 46 p = subprocess.Popen(command, stdout=subprocess.PIPE, | 63 p = subprocess.Popen(command, stdout=subprocess.PIPE, |
| 47 stderr=subprocess.STDOUT) | 64 stderr=subprocess.STDOUT) |
| 48 (output, _) = p.communicate() | 65 (output, _) = p.communicate() |
| 49 return p.returncode, output | 66 return p.returncode, output |
| OLD | NEW |