Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- import shlex
- def run_command(command, expected_retcodes = 0, stdin = None):
- """run a command and raise an exception if retcode not in expected_retcode"""
- # we want expected_retcode to be a tuple but will manage integers
- if type(expected_retcodes) == type(0):
- expected_retcodes = (expected_retcodes,)
- # we want the command to be a list, but accomodate when given a string
- cmd = command
- if type(cmd) == type('string'):
- cmd = shlex.split(command)
- print cmd
- proc = subprocess.Popen(cmd,
- stdin = stdin,
- stdout = subprocess.PIPE,
- stderr = subprocess.PIPE)
- out, err = proc.communicate()
- if proc.returncode not in expected_retcodes:
- # when nothing gets to stderr, add stdout to Detail
- if err.strip() == '':
- err = out
- mesg = 'Error [%d]: %s' % (proc.returncode, command)
- mesg += '\nDetail: %s' % err
- raise Exception, mesg
- return proc.returncode, out, err
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement