Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion spython/main/pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 19 additions & 2 deletions spython/utils/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion spython/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down