Example #1
0
def check_output(*popenargs, **kwargs):
    r"""Run command with arguments and return its output as a byte string.

    If the exit code was non-zero it raises a CalledProcessError.  The
    CalledProcessError object will have the return code in the returncode
    attribute and output in the output attribute.

    The arguments are the same as for the Popen constructor.  Example:

    >>> check_output(["ls", "-l", "/dev/null"])
    'crw-rw-rw- 1 root root 1, 3 Oct 18  2007 /dev/null\n'

    The stdout argument is not allowed as it is used internally.
    To capture standard error in the result, use stderr=STDOUT.

    >>> check_output(["/bin/sh", "-c",
    ...               "ls -l non_existent_file ; exit 0"],
    ...              stderr=STDOUT)
    'ls: non_existent_file: No such file or directory\n'
    """
    from subprocess import PIPE, CalledProcessError, Popen
    if 'stdout' in kwargs:
        raise ValueError('stdout argument not allowed, it will be overridden.')
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
    output, unused_err = process.communicate()
    retcode = process.poll()
    if retcode:
        cmd = kwargs.get("args")
        if cmd is None:
            cmd = popenargs[0]
        err = CalledProcessError(retcode, cmd)
        err.output = output
        raise err
    return output
Example #2
0
def out_and_err(command, input=None, shell=False, env=None):
    """Run a shell command, and return stderr and stdout as string.

    If the command returns nonzero, raise CalledProcessError.

    :arg command: A list of commandline args
    :arg input: Data to pipe to stdin. Omit for none.

    Remaining args have the same meaning as for Popen.

    """
    process = Popen(command,
                    stdout=PIPE,
                    stdin=PIPE,
                    stderr=PIPE,
                    shell=shell,
                    env=env)
    out, err = process.communicate(input=input)
    status = process.poll(
    )  # same as in check_output(), though wait() sounds better
    if status:
        error = CalledProcessError(status, command)
        error.output = out
        print('stdout output was:')
        print(out)
        print('stderr output was:')
        print(err)
        raise error
    return out, err
Example #3
0
 def check_media_file(self, filename):
     valid_media_msg = '%s => OK' % filename
     invalid_media_msg = '%s => INVALID' % filename
     try:
         # cmd = self.validate_cmd.format(filename)
         cmd = self.validate_cmd
         log.debug('cmd: %s %s', cmd, filename)
         log.info('verifying {0}'.format(filename))
         # capturing stderr to stdout because ffprobe prints to stderr in all cases
         # Python 2.7+
         #subprocess.check_output(cmd.split() + [filename], stderr=subprocess.STDOUT)
         proc = subprocess.Popen(cmd.split() + [filename], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
         (stdout, _) = proc.communicate()
         returncode = proc.wait()
         if returncode != 0 or (stdout is not None and 'Error' in stdout):
             _ = CalledProcessError(returncode, cmd)
             _.output = stdout
             raise _
         print(valid_media_msg)
     except CalledProcessError as _:
         if self.verbose > 2:
             print(_.output)
         if self.skip_errors:
             print(invalid_media_msg)
             self.failed = True
             return False
         die(invalid_media_msg)
Example #4
0
    def __run(self, *args, **kwargs):
        _args = [i for i in args if i is not None]
        argsLog = [self.__hidePassw(i) for i in args if i is not None ]
        logger.debug("Shell _run CMD: " + " ". join(argsLog))
        process = Popen(
            _args, stdout = PIPE, stderr = PIPE, close_fds = True, **kwargs)
        _stdout, _stderr = process.communicate()
        retCode = process.returncode
        logger.debug("Shell _run retCode: %d",  retCode)
        stdout = str(_stdout).replace("\\n",  "\n").strip('\n\' ')
        stdout = _stdout.decode("utf-8")
        if stdout.startswith("b'\n") or stdout.startswith('b"\n'):
            stdout = stdout[4:-1]
        logger.debug("            stdout:'%s'",  stdout)
        stderr = _stderr.decode("utf-8").replace("\\n",  "\n").lstrip('\n').lstrip("'")
        logger.debug("            stderr:'%s'",  stderr)

        if retCode or ((len(stderr) > 0) and ('Error' in stderr)) or ((retCode == 4) and ('<Exception>' in stdout)):
            exception = CalledProcessError(process.returncode, repr(args))
            err_msg = "retCode: "+str(retCode)+"\n'"+str(''.join([str(_f) for _f in [stderr] if _f]))
            if (retCode == 4) and ('<Exception>' in stdout):
                err_msg += str(''.join([_f for _f in [stdout] if _f]))
            exception.output = err_msg +"'"
            logger.debug("exception.output:'%s'",  err_msg)
            #raise Error('1001', err=str(err_msg))  # Unnecessary noise. The problem will be reported later in the test case result.
        return stdout, stderr
Example #5
0
def check_output(*popenargs, **kwargs):
    r"""Run command with arguments and return its output as a byte string.

    If the exit code was non-zero it raises a CalledProcessError.  The
    CalledProcessError object will have the return code in the returncode
    attribute and output in the output attribute.

    The arguments are the same as for the Popen constructor.  Example:

    >>> check_output(["ls", "-l", "/dev/null"])
    'crw-rw-rw- 1 root root 1, 3 Oct 18  2007 /dev/null\n'

    The stdout argument is not allowed as it is used internally.
    To capture standard error in the result, use stderr=STDOUT.

    >>> check_output(["/bin/sh", "-c",
    ...               "ls -l non_existent_file ; exit 0"],
    ...              stderr=STDOUT)
    'ls: non_existent_file: No such file or directory\n'
    """
    from subprocess import PIPE, CalledProcessError, Popen
    if 'stdout' in kwargs:
        raise ValueError('stdout argument not allowed, it will be overridden.')
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
    output, unused_err = process.communicate()
    retcode = process.poll()
    if retcode:
        cmd = kwargs.get("args")
        if cmd is None:
            cmd = popenargs[0]
        err = CalledProcessError(retcode, cmd)
        err.output = output
        raise err
    return output
Example #6
0
    def __run(self, *args, **kwargs):
        _args = [i for i in args if i is not None]
        argsLog = [self.__hidePassw(i) for i in args if i is not None]
        logging.debug("Shell _run CMD: " + " ".join(argsLog))
        process = Popen(_args,
                        stdout=PIPE,
                        stderr=PIPE,
                        close_fds=True,
                        **kwargs)
        stdout, stderr = process.communicate()
        retCode = process.returncode
        logging.debug("Shell _run retCode: %d", retCode)
        logging.debug("            stdout:'%s'", stdout)
        logging.debug("            stderr:'%s'", stderr)

        if retCode or ((len(stderr) > 0) and
                       ('Error' in stderr)) or ((retCode == 4) and
                                                ('<Exception>' in stdout)):
            exception = CalledProcessError(process.returncode, repr(args))
            err_msg = "retCode: " + str(retCode) + "\n'" + str(''.join(
                filter(None, [stderr])))
            if (retCode == 4) and ('<Exception>' in stdout):
                err_msg += str(''.join(filter(None, [stdout])))
            exception.output = err_msg + "'"
            logging.debug("exception.output:'%s'", err_msg)
            raise Error('1001', err=str(err_msg))
        return stdout, stderr
Example #7
0
 def check_media_file(self, filename):
     valid_media_msg = '%s => OK' % filename
     invalid_media_msg = '%s => INVALID' % filename
     cmd = self.validate_cmd
     log.debug('cmd: %s %s', cmd, filename)
     log.info('verifying {0}'.format(filename))
     # cmd = self.validate_cmd.format(filename)
     try:
         # capturing stderr to stdout because ffprobe prints to stderr in all cases
         # Python 2.7+
         #subprocess.check_output(cmd.split() + [filename], stderr=subprocess.STDOUT)
         proc = subprocess.Popen(cmd.split() + [filename],
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.STDOUT)
         (stdout, _) = proc.communicate()
         returncode = proc.wait()
         if returncode != 0 or (stdout is not None and 'Error' in stdout):
             _ = CalledProcessError(returncode, cmd)
             _.output = stdout
             raise _
         print(valid_media_msg)
     except CalledProcessError as _:
         if self.verbose > 2:
             print(_.output)
         if self.skip_errors:
             print(invalid_media_msg)
             self.failed = True
             return False
         die(invalid_media_msg)
     except OSError as _:
         die("OSError: '{0}' when running '{1} {2}'", _, cmd, filename)
Example #8
0
    def test_execute_error_quiet(self, check_output, mocker):
        error = CalledProcessError(1, "cmd")
        error.output = "some output"
        check_output.side_effect = error

        with pytest.raises(BumprError):
            mocker.patch("builtins.print")
            execute("some failed command")
Example #9
0
    def test_execute_error_quiet(self, check_call, check_output):
        error = CalledProcessError(1, 'cmd')
        error.output = 'some output'
        check_output.side_effect = error

        with pytest.raises(BumprError):
            to_patch = '{0}.print'.format('builtins' if IS_PY3 else '__builtin__')
            with patch(to_patch):
                execute('some failed command')
Example #10
0
    def test_execute_error_quiet(self, check_output, mocker):
        error = CalledProcessError(1, 'cmd')
        error.output = 'some output'
        check_output.side_effect = error

        with pytest.raises(BumprError):
            to_patch = '{0}.print'.format(
                'builtins' if IS_PY3 else '__builtin__')
            mocker.patch(to_patch)
            execute('some failed command')
Example #11
0
def log_check_call(*args, **kwargs):
    kwargs['record_output'] = True
    retcode, output = log_call(*args, **kwargs)
    if retcode != 0:
        cmd = kwargs.get('args')
        if cmd is None:
            cmd = args[0]
        e = CalledProcessError(retcode, cmd)
        e.output = output
        raise e
    return 0
 def check_output(*args, **kwds):
     process = Popen(stdout=PIPE, *args, **kwds)
     output, _ = process.communicate()
     retcode = process.poll()
     if retcode:
         cmd = kwds.get("args")
         if cmd is None:
             cmd = args[0]
         error = CalledProcessError(retcode, cmd)
         error.output = output
         raise error
     return output
Example #13
0
def check_installed(arguments, stdin=None, stderr=None, shell=False):

    ps = Popen(arguments, shell=shell, stdin=None, stdout=PIPE, stderr=STDOUT)
    cmd_output = ps.communicate()[0]
    returncode = ps.returncode

    if returncode != 0:
        error_cmd = CalledProcessError(returncode, arguments[0])
        error_cmd.output = cmd_output
        raise error_cmd
    else:
        return cmd_output
Example #14
0
 def check_output(*args, **kwds):
     process = Popen(stdout=PIPE, *args, **kwds)
     output, _ = process.communicate()
     retcode = process.poll()
     if retcode:
         cmd = kwds.get("args")
         if cmd is None:
             cmd = args[0]
         error = CalledProcessError(retcode, cmd)
         error.output = output
         raise error
     return output
 def mockreturn(*args, **kwargs):
     if args == (['task', 'stats'],):
         output = b"""
         Category Data
         -------- ----
         Pending 0
         Waiting 0"""
         return output
     elif args == (['task', 'overdue'],):
         output = b'No matches.'
         e = CalledProcessError(1, 'task')
         e.output = output
         raise e
def check_output(*popenargs, **kwargs):
    """Run command with arguments and return its output as a byte string."""
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
    output, unused_err = process.communicate()
    retcode = process.poll()
    if retcode:
        cmd = kwargs.get("args")
        if cmd is None:
            cmd = popenargs[0]
        error = CalledProcessError(retcode, cmd)
        error.output = output
        raise error
    return output
Example #17
0
def call_for_stderr(command, *args, **kwargs):
    kwargs["stderr"] = _subprocess.PIPE

    proc = start_process(command, *args, **kwargs)
    output = proc.communicate()[1].decode("utf-8")
    exit_code = proc.poll()

    if exit_code != 0:
        error = CalledProcessError(exit_code, proc.command_string)
        error.output = output

        raise error

    return output
Example #18
0
def call_for_stderr(command, *args, **kwargs):
    kwargs["stderr"] = _subprocess.PIPE

    proc = start_process(command, *args, **kwargs)
    output = proc.communicate()[1].decode("utf-8")
    exit_code = proc.poll()

    if exit_code != 0:
        error = CalledProcessError(exit_code, proc.command_string)
        error.output = output

        raise error

    return output
def check_output(command, **kargs):
    kargs['stdout'] = out = io.BytesIO()
    kargs['stderr'] = err = io.BytesIO()

    ps = SubProcess(command, **kargs)
    ps.wait()

    if ps.returncode:
        ex = CalledProcessError(ps.returncode, str.join(' ', ps.command))
        ex.output = out.getvalue()
        ex.err = err.getvalue()
        raise ex

    return out.getvalue()
Example #20
0
def spawn(*args, **kwargs):
    """Runs a program, waits for its termination and returns its stdout

    This function is similar to python 2.7's subprocess.check_output,
    but is favored due to python 2.6 compatibility.

    The arguments may be:

        spawn(string)
        spawn(command, arg1, arg2...)
        spawn([command, arg1, arg2])

    The string will be run through a shell, otherwise the command is executed
    directly.

    The keyword argument "decode" determines if the output shall be decoded
    with the encoding '%s'.

    Further keyword arguments are passed to Popen.
    """ % (ENCODING, )

    if len(args) == 1:
        popen_arguments = args[0]
        shell = isinstance(popen_arguments, str)
    else:
        popen_arguments = args
        shell = False

    if 'decode' in kwargs:
        do_decode = kwargs['decode']
        del kwargs['decode']
    else:
        do_decode = True
    if 'stdout' not in kwargs:
        kwargs['stdout'] = PIPE
    if 'shell' not in kwargs:
        kwargs['shell'] = shell

    process = Popen(popen_arguments, **kwargs)
    stdout, stderr = process.communicate()
    return_value = process.poll()
    if return_value:
        error = CalledProcessError(return_value, popen_arguments[0])
        error.output = stdout
        raise error

    if do_decode:
        return stdout.decode(ENCODING)
    else:
        return stdout
Example #21
0
 def __run(self, *args, **kwargs):
     args = [i for i in args if i is not None]
     logging.debug("CMD: " + " ".join(args))
     process = Popen(args,
                     stdout=kwargs.pop('stdout', PIPE),
                     stderr=kwargs.pop('stderr', PIPE),
                     close_fds=kwargs.pop('close_fds', True),
                     **kwargs)
     stdout, stderr = process.communicate()
     if process.returncode:
         exception = CalledProcessError(process.returncode, repr(args))
         exception.output = ''.join(filter(None, [stdout, stderr]))
         raise Error('1001', err=exception.output)
     return stdout
Example #22
0
 def __run(self, *args, **kwargs):
     _args = [i for i in args if i is not None]
     argsLog = [self.__hidePassw(i) for i in args if i is not None ]
     logging.debug("CMD: " + " ". join(argsLog))
     process = Popen(
         _args, stdout=kwargs.pop('stdout', PIPE),
         stderr=kwargs.pop('stderr', PIPE),
         close_fds=kwargs.pop('close_fds', True), **kwargs)
     stdout, stderr = process.communicate()
     if process.returncode:
         exception = CalledProcessError(
             process.returncode, repr(args))
         exception.output = ''.join(filter(None, [stdout, stderr]))
         raise Error('1001', err=exception.output)
     return stdout
Example #23
0
    def check_output(arguments, stdin=None, stderr=None, shell=False):
        temp_f = mkstemp()
        returncode = call(arguments, stdin=stdin, stdout=temp_f[0], stderr=stderr, shell=shell)
        close(temp_f[0])
        file_o = open(temp_f[1], 'r')
        cmd_output = file_o.read()
        file_o.close()
        remove(temp_f[1])

        if returncode != 0:
            error_cmd = CalledProcessError(returncode, arguments[0])
            error_cmd.output = cmd_output
            raise error_cmd
        else:
            return cmd_output
Example #24
0
    def check_output(arguments, stdin=None, stderr=None, shell=False):
        temp_f = mkstemp()
        returncode = call(arguments, stdin=stdin, stdout=temp_f[0], stderr=stderr, shell=shell)
        close(temp_f[0])
        file_o = open(temp_f[1], 'r')
        cmd_output = file_o.read()
        file_o.close()
        remove(temp_f[1])

        if returncode != 0:
            error_cmd = CalledProcessError(returncode, arguments[0])
            error_cmd.output = cmd_output
            raise error_cmd
        else:
            return cmd_output
Example #25
0
 def __run(self, *args, **kwargs):
     _args = [i for i in args if i is not None]
     argsLog = [self.__hidePassw(i) for i in args if i is not None]
     logging.debug("Shell _run CMD: " + " ".join(argsLog))
     process = Popen(_args, stdout=PIPE, stderr=PIPE, close_fds=True, **kwargs)
     stdout, stderr = process.communicate()
     retCode = process.returncode
     logging.debug("Shell _run retCode: %d", retCode)
     logging.debug("            stdout:'%s'", stdout)
     logging.debug("            stderr:'%s'", stderr)
     if retCode or ((len(stderr) > 0) and ("Error" in stderr)):
         exception = CalledProcessError(process.returncode, repr(args))
         exception.output = "".join(filter(None, [stdout, stderr]))
         logging.debug("exception.output:'%s'", exception.output)
         raise Error("1001", err=repr(exception.output))
     return stdout
Example #26
0
def check_output(*args, **kwargs):

    if 'stdout' in kwargs:
        raise ValueError('stdout argument not allowed, it will be overridden.')
    process = subprocess.Popen(stdout=subprocess.PIPE, *args, **kwargs)
    output, unused_err = process.communicate()
    retcode = process.poll()
    if retcode:
        cmd = kwargs.get("args")
        if cmd is None:
            cmd = args[0]

        error = CalledProcessError(retcode, cmd)
        error.output = output
        raise error

    return output
Example #27
0
def call_for_output(command, *args, **kwargs):
    kwargs["stdout"] = _subprocess.PIPE

    proc = start_process(command, *args, **kwargs)
    output = proc.communicate()[0]
    exit_code = proc.poll()

    if exit_code not in (None, 0):
        command_string = _command_string(command)
        command_string = command_string.format(*args)

        error = CalledProcessError(exit_code, command_string)
        error.output = output

        raise error

    return output
Example #28
0
def call_for_output(command, *args, **kwargs):
    kwargs["stdout"] = _subprocess.PIPE

    proc = start_process(command, *args, **kwargs)
    output = proc.communicate()[0]
    exit_code = proc.poll()

    if exit_code not in (None, 0):
        command_string = _command_string(command)
        command_string = command_string.format(*args)

        error = CalledProcessError(exit_code, command_string)
        error.output = output

        raise error

    return output
Example #29
0
def call_for_output(command, *args, **kwargs):
    kwargs["stdout"] = _subprocess.PIPE

    proc = start_process(command, *args, **kwargs)
    output = proc.communicate()[0]
    exit_code = proc.poll()

    # XXX I don't know if None is possible here
    assert exit_code is not None

    if exit_code not in (None, 0):
        error = CalledProcessError(exit_code, proc.command_string)
        error.output = output

        raise error

    return output
Example #30
0
 def __run(self, *args, **kwargs):
     _args = [i for i in args if i is not None]
     argsLog = [self.__hidePassw(i) for i in args if i is not None]
     logging.debug("Shell _run CMD: " + " ".join(argsLog))
     process = Popen(_args,
                     stdout=kwargs.pop('stdout', PIPE),
                     stderr=kwargs.pop('stderr', PIPE),
                     close_fds=kwargs.pop('close_fds', True),
                     **kwargs)
     stdout, stderr = process.communicate()
     retCode = process.returncode
     if retCode:
         logging.debug("Shell _run retCode:: %d, stdout:'%s', stderr:'%s'",
                       retCode, stdout, stderr)
         exception = CalledProcessError(process.returncode, repr(args))
         exception.output = ''.join(filter(None, [stdout, stderr]))
         raise Error('1001', err=repr(exception.output))
     return stdout
Example #31
0
def _check_output(*popenargs, **kwargs):
    # Copyright (c) 2003-2005 by Peter Astrand <*****@*****.**>
    #
    # Licensed to PSF under a Contributor Agreement.
    # See http://www.python.org/2.4/license for licensing details.
    if 'stdout' in kwargs:
        raise ValueError('stdout argument not allowed, it will be overridden.')
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
    output, _ = process.communicate()
    retcode = process.poll()
    if retcode:
        cmd = kwargs.get("args")
        if cmd is None:
            cmd = popenargs[0]
        e = CalledProcessError(retcode, cmd)
        e.output = output
        raise e
    return output
Example #32
0
def execute_php_export(command, articleid):
    print "PHP Exporting article {0}:\n\t`$ {1}\n`".format(articleid, command)
    args = command.split()
    process = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
    pout, perr = process.communicate()
    code = process.poll()
    if code or pout or perr:
        output = pout + perr
        try:
            raise CalledProcessError(
                code, command, output=output
            )
        except Exception:
            error = CalledProcessError(code, command)
            error.output = output
            raise error
    print "PHP export of article {0} complete\n".format(articleid)
    return code
Example #33
0
def _check_output(*popenargs, **kwargs):
    # Copyright (c) 2003-2005 by Peter Astrand <*****@*****.**>
    #
    # Licensed to PSF under a Contributor Agreement.
    # See http://www.python.org/2.4/license for licensing details.
    if 'stdout' in kwargs:
        raise ValueError('stdout argument not allowed, it will be overridden.')
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
    output, _ = process.communicate()
    retcode = process.poll()
    if retcode:
        cmd = kwargs.get("args")
        if cmd is None:
            cmd = popenargs[0]
        e = CalledProcessError(retcode, cmd)
        e.output = output
        raise e
    return output
Example #34
0
def log_check_call(*args, **kwargs):
    log = kwargs.pop('log', logging.debug)
    kwargs['stdout'] = PIPE
    kwargs['stderr'] = STDOUT
    kwargs['env'] = env = kwargs.get('env', os.environ.copy())
    env['PYTHONUNBUFFERED'] = '1'
    p = Popen(*args, **kwargs)
    output = []
    for line in iter(p.stdout.readline, ''):
        log(line.rstrip())
        output.append(line)
    retcode = p.wait()
    if retcode != 0:
        cmd = kwargs.get('args') or args[0]
        e = CalledProcessError(retcode, cmd)
        e.output = ''.join(output)
        raise e
    return 0
Example #35
0
    def check_output(*popenargs, **kwargs):
        r"""Run command with arguments and return its output as a byte string.

        Backported from Python 2.7 as it's implemented as pure python on stdlib.

        >>> check_output(['/usr/bin/python', '--version'])
        Python 2.6.2
        """
        process = Popen(stdout=PIPE, *popenargs, **kwargs)
        output, _ = process.communicate()
        retcode = process.poll()
        if retcode:
            cmd = kwargs.get("args")
            if cmd is None:
                cmd = popenargs[0]
            error = CalledProcessError(retcode, cmd)
            error.output = output
            raise error
        return output
Example #36
0
def check_output(*popenargs, **kwargs):
    """
    Run command with arguments and return its output as a byte string.

    Backported from Python 2.7 as it's implemented as pure python on stdlib.

    >>> check_output(['/usr/bin/python', '--version'])
    Python 2.6.2
    """
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
    output, unused_err = process.communicate()
    retcode = process.poll()
    if retcode:
        cmd = kwargs.get("args")
        if cmd is None:
            cmd = popenargs[0]
        error = CalledProcessError(retcode, cmd)
        error.output = output
        raise error
    return output
Example #37
0
def check_output(command,
                 cwd=None,
                 shell=False,
                 env=None,
                 stdin=__sentinel__,
                 stderr=__sentinel__,
                 preexec_fn=None,
                 use_texpath=True,
                 show_window=False):
    '''
    Takes a command to be passed to subprocess.Popen.

    Returns the output if the command was successful.

    By default stderr is redirected to stdout, so this will return any output
    to either stream. This can be changed by calling execute_command with
    stderr set to subprocess.PIPE or any other valid value.

    Raises CalledProcessError if the command returned a non-zero value
    Raises OSError if the executable is not found

    This is pretty much identical to subprocess.check_output(), but
    implemented here since it is unavailable in Python 2.6's library.
    '''
    returncode, stdout, stderr = execute_command(command,
                                                 cwd=cwd,
                                                 shell=shell,
                                                 env=env,
                                                 stdin=stdin,
                                                 stderr=stderr,
                                                 preexec_fn=preexec_fn,
                                                 use_texpath=use_texpath,
                                                 show_window=show_window)

    if returncode:
        e = CalledProcessError(returncode, command)
        e.output = stdout
        e.stderr = stderr
        raise e

    return stdout
Example #38
0
def check_output(command, cwd=None, shell=False, env=None,
                 stdin=__sentinel__, stderr=__sentinel__,
                 preexec_fn=None, use_texpath=True,
                 show_window=False):
    '''
    Takes a command to be passed to subprocess.Popen.

    Returns the output if the command was successful.

    By default stderr is redirected to stdout, so this will return any output
    to either stream. This can be changed by calling execute_command with
    stderr set to subprocess.PIPE or any other valid value.

    Raises CalledProcessError if the command returned a non-zero value
    Raises OSError if the executable is not found

    This is pretty much identical to subprocess.check_output(), but
    implemented here since it is unavailable in Python 2.6's library.
    '''
    returncode, stdout, stderr = execute_command(
        command,
        cwd=cwd,
        shell=shell,
        env=env,
        stdin=stdin,
        stderr=stderr,
        preexec_fn=preexec_fn,
        use_texpath=use_texpath,
        show_window=show_window
    )

    if returncode:
        e = CalledProcessError(
            returncode,
            command
        )
        e.output = stdout
        e.stderr = stderr
        raise e

    return stdout
Example #39
0
    def __run(self, *args, **kwargs):
        _args = [i for i in args if i is not None]
        argsLog = [self.__hidePassw(i) for i in args if i is not None ]
        logging.debug("Shell _run CMD: " + " ". join(argsLog))
        process = Popen(
            _args, stdout = PIPE, stderr = PIPE, close_fds = True, **kwargs)
        stdout, stderr = process.communicate()
        retCode = process.returncode
        logging.debug("Shell _run retCode: %d",  retCode)
        logging.debug("            stdout:'%s'",  stdout)
        logging.debug("            stderr:'%s'",  stderr)

        if retCode or ((len(stderr) > 0) and ('Error' in stderr)) or ((retCode == 4) and ('<Exception>' in stdout)):
            exception = CalledProcessError(process.returncode, repr(args))
            err_msg = "retCode: "+str(retCode)+"\n'"+str(''.join(filter(None, [stderr])))
            if (retCode == 4) and ('<Exception>' in stdout):
                err_msg += str(''.join(filter(None, [stdout])))
            exception.output = err_msg +"'"
            logging.debug("exception.output:'%s'",  err_msg)
            raise Error('1001', err=str(err_msg))
        return stdout, stderr
Example #40
0
def call_for_stderr(command, *args, **kwargs):
    if "quiet" in kwargs or "output" in kwargs or "stdout" in kwargs:
        raise PlanoException("Illegal output options")

    kwargs["stderr"] = _subprocess.PIPE

    proc = start_process(command, *args, **kwargs)
    output = proc.communicate()[1]

    if output is None:
        output = b""

    output = output.decode("utf-8")
    exit_code = proc.poll()

    if exit_code != 0:
        error = CalledProcessError(exit_code, proc.command_string)
        error.output = output

        raise error

    return output
Example #41
0
def out_and_err(command, input=None, shell=False, env=None):
    """Run a shell command, and return stderr and stdout as string.

    If the command returns nonzero, raise CalledProcessError.

    :arg command: A list of commandline args
    :arg input: Data to pipe to stdin. Omit for none.

    Remaining args have the same meaning as for Popen.

    """
    process = Popen(command,
                    stdout=PIPE,
                    stdin=PIPE,
                    stderr=PIPE,
                    shell=shell,
                    env=env)
    out, err = process.communicate(input=input)
    status = process.poll()  # same as in check_output(), though wait() sounds better
    if status:
        error = CalledProcessError(status, command)
        error.output = out
        raise error
    return out, err
Example #42
0
def check_output(popenargs, **kwargs):
    """Runs a program, waits for its termination and returns its output

    This function is functionally identical to python 2.7's subprocess.check_output,
    but is favored due to python 2.6 compatibility.

    Will be run through a shell if `popenargs` is a string, otherwise the command
    is executed directly.

    The keyword argument `decode` determines if the output shall be decoded
    with the encoding UTF-8.

    Further keyword arguments are passed to Popen.
    """

    do_decode = kwargs.pop('decode', True)
    kwargs.setdefault('stdout', PIPE)
    kwargs.setdefault('shell', isinstance(popenargs, str))

    if 'stderr' in kwargs:
        with Popen23(popenargs, **kwargs) as process:
            stdout, _ = process.communicate()
    else:
        with open(devnull, mode='w', encoding="utf-8") as fd_devnull:
            with Popen23(popenargs, stderr=fd_devnull, **kwargs) as process:
                stdout, _ = process.communicate()

    if process.returncode != 0:
        error = CalledProcessError(process.returncode, popenargs)
        error.output = stdout
        raise error

    if do_decode and stdout is not None:
        stdout = stdout.decode(ENCODING)

    return stdout
Example #43
0
def check_output(popenargs, **kwargs):
    """Runs a program, waits for its termination and returns its output

    This function is functionally identical to python 2.7's subprocess.check_output,
    but is favored due to python 2.6 compatibility.

    Will be run through a shell if `popenargs` is a string, otherwise the command
    is executed directly.

    The keyword argument `decode` determines if the output shall be decoded
    with the encoding UTF-8.

    Further keyword arguments are passed to Popen.
    """

    do_decode = kwargs.pop('decode', True)
    kwargs.setdefault('stdout', PIPE)
    kwargs.setdefault('shell', isinstance(popenargs, str))

    if 'stderr' in kwargs:
        process = Popen(popenargs, **kwargs)
        stdout, _ = process.communicate()
    else:
        with open(devnull, mode='w') as fd_devnull:
            process = Popen(popenargs, stderr=fd_devnull, **kwargs)
            stdout, _ = process.communicate()

    if process.returncode != 0:
        error = CalledProcessError(process.returncode, popenargs)
        error.output = stdout
        raise error

    if do_decode and stdout is not None:
        stdout = stdout.decode(ENCODING)

    return stdout
Example #44
0
def oscap(profile=None):

    # If FIFO exists, delete it
    try:
        unlink(FIFO_PATH)
    except OSError:
        pass

    # Create an unique FIFO file
    mkfifo(FIFO_PATH, 0666)

    try:
        cmd = [OSCAP_BIN, arg_module, 'eval', '--results', FIFO_PATH]

        if profile:
            cmd.extend(["--profile", profile])
        if arg_xccdfid:
            for arg_id in arg_xccdfid:
                cmd.extend(["--xccdf-id", arg_id])
        if arg_ovalid:
            for arg_id in arg_ovalid:
                cmd.extend(["--oval-id", arg_id])
        if arg_dsid:
            for arg_id in arg_dsid:
                cmd.extend(["--datastream-id", arg_id])
        if arg_cpe:
            cmd.extend(["--cpe", arg_cpe])

        cmd.append(arg_file)

        if debug:
            print("\nCMD: '{0}'".format(' '.join(cmd)))

        DEVNULL = open(devnull, 'wb')
        ps = Popen(cmd, shell=False, stdout=DEVNULL, stderr=None)

    except CalledProcessError as error:

        # return code 2 means that some checks failed
        if error.returncode != 2:
            # output = error.output
            print(
                "{0} Executing profile \"{1}\" of file \"{2}\": Return Code: \"{3}\" Error: \"{4}\"."
                .format(OSCAP_LOG_ERROR, profile, arg_file, error.returncode,
                        error.output.replace('\r', '').split("\n")[0]))
            unlink(FIFO_PATH)
            return

    try:
        content_filename = arg_file.split('/')[-1]

        # Generate scan ID: agent_id + epoch
        try:
            if isdir('ruleset'):
                agent_id = '000'
            else:
                with open('etc/client.keys', 'r') as f:
                    first_line = f.readline()
                agent_id = first_line.split(' ')[0]
        except:
            agent_id = randrange(1, 9999)

        scan_id = "{0}{1}".format(agent_id, int(time()))

        if arg_module == 'xccdf':

            ps_xsltproc = Popen([XSLT_BIN, TEMPLATE_XCCDF, FIFO_PATH],
                                stdin=None,
                                stdout=PIPE,
                                stderr=STDOUT)
            ps.wait()
            output = ps_xsltproc.communicate()[0]
            ps_xsltproc.wait()
            returncode = ps_xsltproc.returncode

            if returncode != 0:
                error_cmd = CalledProcessError(
                    returncode, [XSLT_BIN, TEMPLATE_XCCDF, FIFO_PATH])
                error_cmd.output = output
                raise error_cmd
            else:

                if version_info[0] >= 3:
                    output = output.decode('utf-8', 'backslashreplace')

                for line in output.split("\n"):
                    if not line:
                        continue

                    # Adding file
                    if 'msg: "xccdf-overview"' in line:
                        new_line = line.replace(
                            'oscap: msg: "xccdf-overview",',
                            'oscap: msg: "xccdf-overview", scan-id: "{0}", content: "{1}",'
                            .format(scan_id, content_filename))
                    else:
                        new_line = line.replace(
                            'oscap: msg: "xccdf-result",',
                            'oscap: msg: "xccdf-result", scan-id: "{0}", content: "{1}",'
                            .format(scan_id, content_filename))

                    print(new_line)

        else:

            ps_xsltproc = Popen([XSLT_BIN, TEMPLATE_OVAL, FIFO_PATH],
                                stdin=None,
                                stdout=PIPE,
                                stderr=STDOUT)
            ps.wait()
            output = ps_xsltproc.communicate()[0]
            ps_xsltproc.wait()
            returncode = ps_xsltproc.returncode

            if version_info[0] >= 3:
                output = output.decode('utf-8', 'backslashreplace')

            total = 0
            total_KO = 0
            for line in output.split("\n"):
                if not line:
                    continue

                total += 1

                # Adding file
                new_line = line.replace(
                    'oscap: msg: "oval-result"',
                    'oscap: msg: "oval-result", scan-id: "{0}", content: "{1}"'
                    .format(scan_id, content_filename))

                class1 = ['class: "compliance"', 'class: "inventory"']
                class2 = ['class: "vulnerability"', 'class: "patch"']

                if any(x in line for x in class1):
                    if 'result: "false"' in line:
                        total_KO += 1
                        new_line = new_line.replace('result: "false"',
                                                    'result: "fail"')
                    elif 'result: "true"' in line:
                        new_line = new_line.replace('result: "true"',
                                                    'result: "pass"')
                elif any(x in line for x in class2):
                    if 'result: "true"' in line:
                        total_KO += 1
                        new_line = new_line.replace('result: "true"',
                                                    'result: "fail"')
                    elif 'result: "false"' in line:
                        new_line = new_line.replace('result: "false"',
                                                    'result: "pass"')

                new_line = new_line.replace('", class: "',
                                            '", profile-title: "')

                print(new_line)

            score = (float((total - total_KO)) / float(total)) * 100

            # summary
            print(
                'oscap: msg: "oval-overview", scan-id: "{0}", content: "{1}", score: "{2:.2f}".'
                .format(scan_id, content_filename, score))

    except CalledProcessError as error:
        print(
            "{0} Formatting data for profile \"{1}\" of file \"{2}\": Return Code: \"{3}\" Error: \"{4}\"."
            .format(OSCAP_LOG_ERROR, profile, arg_file, error.returncode,
                    error.output.replace('\r', '').split("\n")[0]))

    unlink(FIFO_PATH)
def check_output(*popenargs, **kwargs):
    r"""Run command with arguments and return its output as a byte string.

    If the exit code was non-zero it raises a CalledProcessError.  The
    CalledProcessError object will have the return code in the returncode
    attribute and output in the output attribute.

    The arguments are the same as for the Popen constructor.  Example:

    >>> check_output(["ls", "-l", "/dev/null"])  #doctest: +ELLIPSIS
    'crw-rw-rw-  1 root  wheel    3...

    The stdout argument is not allowed as it is used internally.
    To capture standard error in the result, use stderr=STDOUT.

    >>> check_output(["/bin/sh", "-c",
    ...               "ls -l non_existent_file ; exit 0"],
    ...              stderr=STDOUT)
    'ls: non_existent_file: No such file or directory\n'

    Calling check_output without the ignore_404 keyword argument
    will raise a CalledProcessError if wkhtmltopdf exits with a non-zero code

    >>> check_output(['wkhtmltopdf',               #doctest: +ELLIPSIS
    ...               '--quiet',
    ...               '--encoding', 'utf8',
    ...               os.getenv('WKHTML_IN'), os.getenv('WKHTML_OUT')])
    Traceback (most recent call last):
    ...
    CalledProcessError...

    Calling check_output WITH the ignore_404 keyword will not raise
    the CalledProcessError, but only if the error == ContentNotFoundError

    >>> check_output(['/usr/local/bin/wkhtmltopdf',
    ...               '--quiet',
    ...               '--encoding', 'utf8',
    ...               os.getenv('WKHTML_IN'), os.getenv('WKHTML_OUT')],
    ...               env={'ignore_404': True})
    ''

    Calling check_output WITH the ignore_404 keyword should still
    raise a CalledProcessError if the error != ContentNotFoundError

    >>> check_output(['/usr/local/bin/wkhtmltopdf', #doctest: +ELLIPSIS
    ...               '--blaa',
    ...               '--encoding', 'utf8',
    ...               os.getenv('WKHTML_IN'), os.getenv('WKHTML_OUT')],
    ...               env={'ignore_404': True})
    Traceback (most recent call last):
    ...
    CalledProcessError...
    """
    if 'stdout' in kwargs:
        raise ValueError('stdout argument not allowed, it will be overridden.')

    if kwargs.get('env'):
        ignore_404 = kwargs['env'].pop('ignore_404', False)
    else:
        ignore_404 = False

    kwargs.setdefault('stderr', PIPE)
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
    output, error_message = process.communicate()
    retcode = process.poll()

    if retcode:
        if error_message and ignore_404:
            if 'ContentNotFoundError' in str(error_message):
                return output
        cmd = kwargs.get("args")
        if cmd is None:
            cmd = popenargs[0]
        error = CalledProcessError(retcode, cmd)
        # Add the output attribute to CalledProcessError, which
        # doesn't exist until Python 2.7.
        error.output = output
        raise error

    return output