00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 import subprocess
00018 import os
00019
00020
00021 def cmd_exec(cmds):
00022 """
00023 Execute provided command and return the STDOUT as string.
00024 Raise ExecError if command return code is not zero
00025 """
00026 cmd_popen = subprocess.Popen(
00027 cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
00028 env={"PATH": os.getenv("PATH")})
00029 str_stdout = "".join(list(cmd_popen.stdout)).strip()
00030 str_stderr = "".join(list(cmd_popen.stderr)).strip()
00031 errno = cmd_popen.wait()
00032 if errno != 0:
00033 raise ExecError(" ".join(cmds), errno, str_stdout, str_stderr)
00034 return str_stdout
00035
00036
00037 class ExecError(Exception):
00038 def __init__(self, cmd, errno, stdout, stderr, *args, **kwargs):
00039 Exception.__init__(self, *args, **kwargs)
00040 self.cmd = cmd
00041 self.errno = errno
00042 self.stdout = stdout
00043 self.stderr = stderr
00044
00045 def __str__(self):
00046 return "cmd: '%s', errno: %d, stdout: '%s', stderr: '%s'" % \
00047 (self.cmd, self.errno, self.stdout, self.stderr)