Changeset 2541 for trunk/packages/invirt-base/python/invirt
- Timestamp:
- Nov 22, 2009, 4:07:11 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.