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)
- add sudo_options option to spython.main.Client.build (0.0.73)
- list of options and writable added to shell, execute, and run (0.0.72)
- client is not honoring quiet for pull (0.0.71)
- removing debugging line in pull (0.0.70)
Expand Down
6 changes: 4 additions & 2 deletions spython/main/base/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ def run_command(self, cmd,
sudo=False,
capture=True,
quiet=None,
return_result=False):
return_result=False,
sudo_options=None):

'''run_command is a wrapper for the global run_command, checking first
for sudo and exiting on error if needed. The message is returned as
Expand All @@ -119,14 +120,15 @@ def run_command(self, cmd,
sudo: does the command require sudo?
quiet: if quiet set by function, overrides client setting.
return_result: return the result, if not successful (default False).
sudo_options: string or list of strings that will be passed as options to sudo
On success, returns result.

'''
# First preference to function, then to client setting
if quiet is None:
quiet = self.quiet

result = run_cmd(cmd, sudo=sudo, capture=capture, quiet=quiet)
result = run_cmd(cmd, sudo=sudo, capture=capture, quiet=quiet, sudo_options=sudo_options)

# If one line is returned, squash dimension
if len(result['message']) == 1:
Expand Down
7 changes: 5 additions & 2 deletions spython/main/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def build(self, recipe=None,
force=False,
options=None,
quiet=False,
return_result=False):
return_result=False,
sudo_options=None):

'''build a singularity image, optionally for an isolated build
(requires sudo). If you specify to stream, expect the image name
Expand All @@ -47,6 +48,7 @@ def build(self, recipe=None,
name (with "image") then a fun robot name will be generated
instead. Highly recommended :)
sudo: give sudo to the command (or not) default is True for build
sudo_options: options to pass to sudo (e.g. --preserve-env=SINGULARITY_CACHEDIR,SINGULARITY_TMPDIR)
options: for all other options, specify them in this list.
quiet: quiet verbose printing from the client.
return_result: if True, return complete error code / message dictionary
Expand Down Expand Up @@ -105,14 +107,15 @@ def build(self, recipe=None,
if not stream:
self._run_command(cmd,
sudo=sudo,
sudo_options=sudo_options,
quiet=quiet,
return_result=return_result,
capture=False)

else:
# Here we return the expected image, and an iterator!
# The caller must iterate over
return image, stream_command(cmd, sudo=sudo)
return image, stream_command(cmd, sudo=sudo, sudo_options=sudo_options)

if os.path.exists(image):
return image
24 changes: 17 additions & 7 deletions spython/utils/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@
from spython.logger import decodeUtf8String
import subprocess
import sys
import shlex

################################################################################
# Local commands and requests
################################################################################

def _process_sudo_cmd(cmd, sudo, sudo_options):
if sudo and sudo_options is not None:
if isinstance(sudo_options, str):
sudo_options = shlex.split(sudo_options)
cmd = ['sudo'] + sudo_options + cmd
elif sudo:
cmd = ['sudo'] + cmd
return cmd


def check_install(software='singularity', quiet=True):
'''check_install will attempt to run the singularity command, and
Expand Down Expand Up @@ -89,7 +99,7 @@ def get_installdir():
return os.path.abspath(os.path.dirname(os.path.dirname(__file__)))


def stream_command(cmd, no_newline_regexp="Progess", sudo=False):
def stream_command(cmd, no_newline_regexp="Progess", sudo=False, sudo_options=None):
'''stream a command (yield) back to the user, as each line is available.

# Example usage:
Expand All @@ -103,10 +113,10 @@ def stream_command(cmd, no_newline_regexp="Progess", sudo=False):
cmd: the command to send, should be a list for subprocess
no_newline_regexp: the regular expression to determine skipping a
newline. Defaults to finding Progress
sudo_options: string or list of strings that will be passed as options to sudo

'''
if sudo:
cmd = ['sudo'] + cmd
cmd = _process_sudo_cmd(cmd, sudo, sudo_options)

process = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
Expand All @@ -124,7 +134,8 @@ def run_command(cmd,
sudo=False,
capture=True,
no_newline_regexp="Progess",
quiet=False):
quiet=False,
sudo_options=None):

'''run_command uses subprocess to send a command to the terminal. If
capture is True, we use the parent stdout, so the progress bar (and
Expand All @@ -140,10 +151,9 @@ def run_command(cmd,
capture: if True, don't set stdout and have it go to console. This
option can print a progress bar, but won't return the lines
as output.
sudo_options: string or list of strings that will be passed as options to sudo
'''

if sudo:
cmd = ['sudo'] + cmd
cmd = _process_sudo_cmd(cmd, sudo, sudo_options)

stdout = None
if capture:
Expand Down
2 changes: 1 addition & 1 deletion spython/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# with this file, You can obtain one at http://mozilla.org/MPL/2.0/.


__version__ = "0.0.72"
__version__ = "0.0.73"
AUTHOR = 'Vanessa Sochat'
AUTHOR_EMAIL = 'vsochat@stanford.edu'
NAME = 'spython'
Expand Down