Ignore:
Timestamp:
Nov 22, 2009, 4:07:11 PM (15 years ago)
Author:
broder
Message:

In invirt-base:

  • Add a captureOutput function to invirt.common as a convenient wrapper around subprocess.Popen.
Location:
trunk/packages/invirt-base
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/invirt-base/debian/changelog

    r2304 r2541  
     1invirt-base (0.0.26) unstable; urgency=low
     2
     3  * Add a captureOutput function to invirt.common as a convenient wrapper
     4    around subprocess.Popen.
     5
     6 -- Evan Broder <broder@mit.edu>  Sun, 22 Nov 2009 04:35:43 -0500
     7
    18invirt-base (0.0.25) unstable; urgency=low
    29
  • trunk/packages/invirt-base/python/invirt/common.py

    r2097 r2541  
    44from fcntl import flock, LOCK_EX, LOCK_SH, LOCK_UN
    55import contextlib as clib
     6import subprocess
    67
    78class InvirtConfigError(AttributeError):
     
    5859            flock(f, LOCK_UN)
    5960
     61def captureOutput(popen_args, stdin_str=None, *args, **kwargs):
     62    """Capture stdout from a command.
     63
     64    This method will proxy the arguments to subprocess.Popen. It
     65    returns the output from the command if the call succeeded and
     66    raises an exception if the process returns a non-0 value.
     67
     68    This is intended to be a variant on the subprocess.check_call
     69    function that also allows you access to the output from the
     70    command.
     71    """
     72    if 'stdin' not in kwargs:
     73        kwargs['stdin'] = subprocess.PIPE
     74    if 'stdout' not in kwargs:
     75        kwargs['stdout'] = subprocess.PIPE
     76    if 'stderr' not in kwargs:
     77        kwargs['stderr'] = subprocess.STDOUT
     78    p = subprocess.Popen(popen_args, *args, **kwargs)
     79    out, _ = p.communicate(stdin_str)
     80    if p.returncode:
     81        raise subprocess.CalledProcessError(p.returncode, popen_args, out)
     82    return out
     83
    6084#
    6185# Exceptions.
Note: See TracChangeset for help on using the changeset viewer.