import datetime, os, subprocess
-from stgit.out import *
+from stgit.exception import *
+from stgit.out import *
-class RunException(Exception):
+class RunException(StgException):
"""Thrown when something bad happened when we tried to run the
subprocess."""
pass
self.__cmd = list(cmd)
for c in cmd:
if type(c) != str:
- raise Exception, 'Bad command: %r' % cmd
+ raise Exception, 'Bad command: %r' % (cmd,)
self.__good_retvals = [0]
self.__env = None
self.__indata = None
+ self.__discard_stderr = False
def __log_start(self):
if _log_mode == 'debug':
out.start('Running subprocess %s' % self.__cmd)
duration = datetime.datetime.now() - self.__starttime
out.done('%1.3f s' % (duration.microseconds/1e6 + duration.seconds))
def __check_exitcode(self):
+ if self.__good_retvals == None:
+ return
if self.exitcode not in self.__good_retvals:
raise self.exc('%s failed with code %d'
% (self.__cmd[0], self.exitcode))
try:
p = subprocess.Popen(self.__cmd, env = self.__env,
stdin = subprocess.PIPE,
- stdout = subprocess.PIPE)
+ stdout = subprocess.PIPE,
+ stderr = subprocess.PIPE)
outdata, errdata = p.communicate(self.__indata)
self.exitcode = p.returncode
except OSError, e:
raise self.exc('%s failed: %s' % (self.__cmd[0], e))
+ if errdata and not self.__discard_stderr:
+ out.err_raw(errdata)
self.__log_end(self.exitcode)
self.__check_exitcode()
return outdata
def returns(self, retvals):
self.__good_retvals = retvals
return self
+ def discard_exitcode(self):
+ self.__good_retvals = None
+ return self
+ def discard_stderr(self, discard = True):
+ self.__discard_stderr = discard
+ return self
def env(self, env):
self.__env = dict(os.environ)
self.__env.update(env)