Source

rsync_backup / exec_cmd.py

Full commit
# -*- coding: ascii
"""
execute (shell or other) commands

author: Thomas Waldmann
license: BSD
"""

import subprocess


def exec_cmd(cmd, input=None, **kw):
    """execute cmd, give it <input> as stdin, return its stdout output,
    stderr output and return code.
    """
    p = subprocess.Popen(cmd, **kw)
    data, errors = p.communicate(input)
    return data, errors, p.returncode


def exec_shell(cmd, input=None,
               shell=True,
               close_fds=not subprocess.mswindows,
               bufsize=1024,
               stdin=subprocess.PIPE,
               stdout=subprocess.PIPE,
               stderr=subprocess.PIPE,
               **kw):
    """execute cmd as a shell command"""
    kw.update(dict(
        shell=shell,
        close_fds=close_fds,
        bufsize=bufsize,
        stdin=stdin,
        stdout=stdout,
        stderr=stderr,
    ))
    return exec_cmd(cmd, input, **kw)