Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the output_type option when streaming, and allow both stdout/stderr to be captured #209

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion spython/main/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def execute(
sudo_options=None,
quiet=True,
environ=None,
stream_type="stdout"
):
"""execute: send a command to a container

Expand All @@ -51,6 +52,7 @@ def execute(
and message result not (default)
quiet: Do not print verbose output.
environ: extra environment to add.
stream_type: Sets which output stream from the singularity command should be return. Values are 'stdout', 'stderr', 'both'.
"""
from spython.utils import check_install

Expand Down Expand Up @@ -115,7 +117,7 @@ def execute(
quiet=quiet,
environ=environ,
)
return stream_command(cmd, sudo=sudo, sudo_options=sudo_options)
return stream_command(cmd, sudo=sudo, sudo_options=sudo_options, output_type=stream_type)

bot.exit("Please include a command (list) to execute.")

Expand Down
6 changes: 4 additions & 2 deletions spython/utils/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,14 @@ def stream_command(
sudo_options: string or list of strings that will be passed as options to sudo

"""
if output_type not in ["stdout", "stderr"]:
if output_type not in ["stdout", "stderr", "both"]:
bot.exit("Invalid output type %s. Must be stderr or stdout." % output_type)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bot.exit("Invalid output type %s. Must be stderr or stdout." % output_type)
bot.exit("Invalid output type %s. Must be stderr, stdout, or both" % output_type)

cmd = _process_sudo_cmd(cmd, sudo, sudo_options)

stderr_pipe = subprocess.STDOUT if output_type == "both" else subprocess.PIPE

process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True
cmd, stdout=subprocess.PIPE, stderr=stderr_pipe, universal_newlines=True
)

# Allow the runner to choose streaming output or error
Expand Down