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 def file_read(file_path):
00038 """
00039 Read file and return string of file content.
00040 """
00041 fd = open(file_path, 'r')
00042 content = fd.read()
00043 fd.close()
00044 return content
00045
00046
00047 class ExecError(Exception):
00048 def __init__(self, cmd, errno, stdout, stderr, *args, **kwargs):
00049 Exception.__init__(self, *args, **kwargs)
00050 self.cmd = cmd
00051 self.errno = errno
00052 self.stdout = stdout
00053 self.stderr = stderr
00054
00055 def __str__(self):
00056 return "cmd: '%s', errno: %d, stdout: '%s', stderr: '%s'" % \
00057 (self.cmd, self.errno, self.stdout, self.stderr)