Source

rsync_backup / exec_cmd.py

Full commit
Thomas Waldmann 0694580 










Thomas Waldmann df8543e 
Thomas Waldmann 65bcd55 

Thomas Waldmann df8543e 

Thomas Waldmann 0694580 
Thomas Waldmann 65bcd55 


Thomas Waldmann 0694580 
Thomas Waldmann df8543e 


















# -*- 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 and stderr output.
    """
    p = subprocess.Popen(cmd, **kw)
    data, errors = p.communicate(input)
    if p.returncode != 0:
        raise subprocess.CalledProcessError(p.returncode, cmd, data)
    return data, errors


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)