- Timestamp:
- Nov 22, 2009, 4:07:11 PM (15 years ago)
- Location:
- trunk/packages/invirt-base
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/packages/invirt-base/debian/changelog
r2304 r2541 1 invirt-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 1 8 invirt-base (0.0.25) unstable; urgency=low 2 9 -
trunk/packages/invirt-base/python/invirt/common.py
r2097 r2541 4 4 from fcntl import flock, LOCK_EX, LOCK_SH, LOCK_UN 5 5 import contextlib as clib 6 import subprocess 6 7 7 8 class InvirtConfigError(AttributeError): … … 58 59 flock(f, LOCK_UN) 59 60 61 def 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 60 84 # 61 85 # Exceptions.
Note: See TracChangeset
for help on using the changeset viewer.