How to use the spython.utils.run_command function in spython

To help you get started, we’ve selected a few spython examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github singularityhub / singularity-cli / spython / main / instances.py View on Github external
sudo: if the command should be done with sudo (exposes different set of
             instances)

    """
    from spython.utils import check_install

    check_install()

    subgroup = "instance.stop"

    if "version 3" in self.version():
        subgroup = ["instance", "stop"]

    cmd = self._init_command(subgroup, singularity_options)
    cmd = cmd + ["--all"]
    output = run_command(cmd, sudo=sudo, quiet=quiet)

    if output["return_code"] != 0:
        message = "%s : return code %s" % (output["message"], output["return_code"])
        bot.error(message)
        return output["return_code"]

    return output["return_code"]
github singularityhub / singularity-cli / spython / main / instances.py View on Github external
from spython.utils import check_install

    check_install()

    subgroup = "instance.list"

    if "version 3" in self.version():
        subgroup = ["instance", "list"]

    cmd = self._init_command(subgroup, singularity_options)

    # If the user has provided a name, we want to see a particular instance
    if name is not None:
        cmd.append(name)

    output = run_command(cmd, quiet=True, sudo=sudo)
    instances = []

    # Success, we have instances

    if output["return_code"] == 0:

        # Only print the table if we are returning json
        if not quiet:
            print("".join(output["message"]))

        # Prepare json result from table
        # Singularity after 3.5.2 has an added ipaddress
        try:
            header = ["daemon_name", "pid", "container_image"]
            instances = parse_table(output["message"][0], header)
        except:
github singularityhub / singularity-cli / spython / instance / cmd / start.py View on Github external
# Assemble the command!
    cmd = cmd + options + [image, self.name]

    # If arguments are provided
    if args is not None:
        if not isinstance(args, list):
            args = [args]
        cmd = cmd + args

    # Save the options and cmd, if the user wants to see them later
    self.options = options
    self.args = args
    self.cmd = cmd

    output = run_command(cmd, sudo=sudo, quiet=True, capture=capture)

    if output["return_code"] == 0:
        self._update_metadata()

    else:
        message = "%s : return code %s" % (output["message"], output["return_code"])
        bot.error(message)

    return self
github singularityhub / singularity-cli / spython / main / base / command.py View on Github external
Parameters
       ==========
       cmd: the command to run
       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, sudo_options=sudo_options
    )

    # If one line is returned, squash dimension
    if len(result["message"]) == 1:
        result["message"] = result["message"][0]

    # If the user wants to return the result, just return it
    if return_result:
        return result

    # On success, return result
    if result["return_code"] == 0:
        return result["message"]

    return result
github singularityhub / singularity-cli / spython / main / inspect.py View on Github external
options = ["e", "d", "l", "r", "hf", "t"]

    # After Singularity 3.0, helpfile was changed to H from

    if "version 3" in self.version():
        options = ["e", "d", "l", "r", "H", "t"]

    for x in options:
        cmd.append("-%s" % x)

    if json:
        cmd.append("--json")

    cmd.append(image)
    result = run_command(cmd, quiet=quiet)

    if result["return_code"] == 0:
        result = jsonp.loads(result["message"][0])

        # Unify output to singularity 3 format
        if "data" in result:
            result = result["data"]

        # Fix up labels
        result = parse_labels(result)

        if not quiet:
            print(jsonp.dumps(result, indent=4))

    return result
github singularityhub / singularity-cli / spython / instance / cmd / stop.py View on Github external
subgroup = "instance.stop"

    if "version 3" in self.version():
        subgroup = ["instance", "stop"]
        if timeout:
            subgroup += ["-t", str(timeout)]

    cmd = self._init_command(subgroup, singularity_options)

    # If name is provided assume referencing an instance
    instance_name = self.name
    if name is not None:
        instance_name = name
    cmd = cmd + [instance_name]

    output = run_command(cmd, sudo=sudo, quiet=True)

    if output["return_code"] != 0:
        message = "%s : return code %s" % (output["message"], output["return_code"])
        bot.error(message)
        return output["return_code"]

    return output["return_code"]
github singularityhub / singularity-cli / spython / instance / cmd / __init__.py View on Github external
'''
    from spython.instance import Instance

    from spython.main.base.logger import println
    from spython.main.instances import list_instances
    from spython.utils import run_command as run_cmd

    # run_command uses run_cmd, but wraps to catch error
    from spython.main.base.command import (init_command, run_command)
    from spython.main.base.generate import RobotNamer
    from .start import start
    from .stop import stop

    Instance.RobotNamer = RobotNamer()
    Instance._init_command = init_command
    Instance.run_command = run_cmd
    Instance._run_command = run_command
    Instance._list = list_instances  # list command is used to get metadata
    Instance._println = println
    Instance.start = start     # intended to be called on init, not by user
    Instance.stop = stop

    # Give an instance the ability to breed :)
    Instance.instance = Instance
 
    return Instance