- def __run_io(self, cmd):
- """Run with captured IO. Note: arguments are parsed by the
- shell. We single-quote them, so don't use anything with single
- quotes in it."""
- if self.__env == None:
- ecmd = cmd
- else:
- ecmd = (['env'] + ['%s=%s' % (key, val)
- for key, val in self.__env.iteritems()]
- + cmd)
- p = popen2.Popen3(' '.join(["'%s'" % c for c in ecmd]), True)
- if self.__indata != None:
- p.tochild.write(self.__indata)
- p.tochild.close()
- outdata = p.fromchild.read()
- errdata = p.childerr.read()
- self.exitcode = p.wait() >> 8
- if errdata or self.exitcode not in self.__good_retvals:
- raise self.exc('%s failed with code %d:\n%s'
- % (cmd[0], self.exitcode, errdata))
+ self.__discard_stderr = False
+ def __log_start(self):
+ if _log_mode == 'debug':
+ out.start('Running subprocess %s' % self.__cmd)
+ if self.__cwd != None:
+ out.info('cwd: %s' % self.__cwd)
+ if self.__env != None:
+ for k in sorted(self.__env.iterkeys()):
+ if k not in os.environ or os.environ[k] != self.__env[k]:
+ out.info('%s: %s' % (k, self.__env[k]))
+ elif _log_mode == 'profile':
+ out.start('Running subprocess %s' % self.__cmd[0])
+ self.__starttime = datetime.datetime.now()
+ def __log_end(self, retcode):
+ if _log_mode == 'debug':
+ out.done('return code: %d' % retcode)
+ elif _log_mode == 'profile':
+ 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))
+ def __run_io(self):
+ """Run with captured IO."""
+ self.__log_start()
+ try:
+ p = subprocess.Popen(self.__cmd, env = self.__env, cwd = self.__cwd,
+ stdin = 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()