Example #1
0
def get_command_out(cmd, stdin=None, allow_fail=False, env_add={}):
    new_env = os.environ.copy()
    new_env.update(env_add)

    logging.info(cmd, extra={"context": "[CMD] "})

    r, w = os.pipe()

    if stdin is None:
        p = Popen(cmd, shell=True, stdout=PIPE, stderr=w, env=new_env)
    else:
        p = Popen(cmd,
                  shell=True,
                  stdin=PIPE,
                  stdout=w,
                  stderr=STDOUT,
                  env=new_env)

    async_logging(r, w, soap, log)
    stdout, stderr = p.communicate(input=stdin)

    if p.returncode and not allow_fail:
        raise CommandError(cmd, p.returncode)

    return stdout
Example #2
0
def get_command_out(cmd, stdin=None, allow_fail=False, env_add=None):
    """get_command_out() - Like do() but returns stdout.

    --

    Let's quiet the loggers

    >>> import os
    >>> from elbepack.log import open_logging
    >>> open_logging({"files":os.devnull})

    >>> get_command_out("echo ELBE")
    b'ELBE\\n'

    >>> get_command_out("false") # doctest: +ELLIPSIS
    Traceback (most recent call last):
    ...
    elbepack.shellhelper.CommandError: ...

    >>> get_command_out("false", allow_fail=True)
    b''

    >>> get_command_out("cat -", stdin=b"ELBE", env_add={"TRUE":"true"})
    b'ELBE'

    >>> get_command_out("cat -", stdin="ELBE", env_add={"TRUE":"true"})
    b'ELBE'
    """

    new_env = os.environ.copy()

    if env_add:
        new_env.update(env_add)

    if type(stdin) == str:
        stdin = stdin.encode()

    logging.info(cmd, extra={"context": "[CMD] "})

    r, w = os.pipe()

    if stdin is None:
        p = Popen(cmd, shell=True, stdout=PIPE, stderr=w, env=new_env)
    else:
        p = Popen(cmd,
                  shell=True,
                  stdin=PIPE,
                  stdout=PIPE,
                  stderr=w,
                  env=new_env)

    async_logging(r, w, soap, log)
    stdout, _ = p.communicate(input=stdin)

    if p.returncode and not allow_fail:
        raise CommandError(cmd, p.returncode)

    return stdout
Example #3
0
def do(cmd, allow_fail=False, stdin=None, env_add=None):
    """do() - Execute cmd in a shell and redirect outputs to logging.

    Throws a CommandError if cmd failed with allow_Fail=False.

    --

    Let's redirect the loggers to current stdout
    >>> import sys
    >>> from elbepack.log import open_logging
    >>> open_logging({"streams":sys.stdout})

    >>> do("true")
    [CMD] true

    >>> do("false", allow_fail=True)
    [CMD] false

    >>> do("cat -", stdin=b"ELBE")
    [CMD] cat -

    >>> do("cat - && false", stdin=b"ELBE") # doctest: +ELLIPSIS
    Traceback (most recent call last):
    ...
    elbepack.shellhelper.CommandError: ...

    >>> do("false") # doctest: +ELLIPSIS
    Traceback (most recent call last):
    ...
    elbepack.shellhelper.CommandError: ...
    """

    new_env = os.environ.copy()
    if env_add:
        new_env.update(env_add)

    if type(stdin) == str:
        stdin = stdin.encode()

    logging.info(cmd, extra={"context": "[CMD] "})

    r, w = os.pipe()

    if stdin is None:
        p = Popen(cmd, shell=True, stdout=w, stderr=STDOUT, env=new_env)
    else:
        p = Popen(cmd,
                  shell=True,
                  stdin=PIPE,
                  stdout=w,
                  stderr=STDOUT,
                  env=new_env)

    async_logging(r, w, soap, log)
    p.communicate(input=stdin)

    if p.returncode and not allow_fail:
        raise CommandError(cmd, p.returncode)
Example #4
0
 def start(self):
     """Redirect outputs of the process to an async logging thread"""
     r, w = os.pipe()
     super(MyMan, self).start(MyMan.redirect_outputs, [r, w])
     async_logging(r, w, soap, log)