diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b253e9..31e3c70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ The client here will eventually be released as "spython" (and eventually to singularity on pypi), and the versions here will coincide with these releases. ## [master](https://github.com/singularityhub/singularity-cli/tree/master) + - choose output for stream_command (0.0.14) - adding support to pull from a url (0.1.13) - add more verbosity to instance start/stop (0.1.12) - adding more verbosity to running commands (0.1.11) diff --git a/spython/main/pull.py b/spython/main/pull.py index 2018260..65860e5 100644 --- a/spython/main/pull.py +++ b/spython/main/pull.py @@ -97,7 +97,9 @@ def pull( # Option 3: A custom name we can predict (not commit/hash) and can also show else: - return final_image, stream_command(cmd, sudo=False) + + # As of Singularity 3.x (at least 3.8) output goes to stderr + return final_image, stream_command(cmd, sudo=False, output_type="stderr") if os.path.exists(final_image) and not quiet: bot.info(final_image) diff --git a/spython/utils/terminal.py b/spython/utils/terminal.py index 503de64..361c2c7 100644 --- a/spython/utils/terminal.py +++ b/spython/utils/terminal.py @@ -110,7 +110,13 @@ def get_installdir(): return os.path.abspath(os.path.dirname(os.path.dirname(__file__))) -def stream_command(cmd, no_newline_regexp="Progess", sudo=False, sudo_options=None): +def stream_command( + cmd, + no_newline_regexp="Progess", + sudo=False, + sudo_options=None, + output_type="stdout", +): """stream a command (yield) back to the user, as each line is available. # Example usage: @@ -127,14 +133,25 @@ def stream_command(cmd, no_newline_regexp="Progess", sudo=False, sudo_options=No sudo_options: string or list of strings that will be passed as options to sudo """ + if output_type not in ["stdout", "stderr"]: + bot.exit("Invalid output type %s. Must be stderr or stdout." % output_type) cmd = _process_sudo_cmd(cmd, sudo, sudo_options) process = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True ) - for line in iter(process.stdout.readline, ""): + + # Allow the runner to choose streaming output or error + stream = process.stdout.readline + if output_type == "stderr": + stream = process.stderr.readline + + # Stream lines back to the caller + for line in iter(stream, ""): if not re.search(no_newline_regexp, line): yield line + + # If there is an error, raise. process.stdout.close() return_code = process.wait() if return_code: diff --git a/spython/version.py b/spython/version.py index 674a420..52d4906 100644 --- a/spython/version.py +++ b/spython/version.py @@ -5,7 +5,7 @@ # with this file, You can obtain one at http://mozilla.org/MPL/2.0/. -__version__ = "0.1.13" +__version__ = "0.1.14" AUTHOR = "Vanessa Sochat" AUTHOR_EMAIL = "vsochat@stanford.edu" NAME = "spython"