def doStart(self): self.running = True self.startTime = util.now(self._reactor) d = defer.maybeDeferred(self.start) def commandComplete(res): self.sendStatus({"elapsed": util.now(self._reactor) - self.startTime}) self.running = False return res d.addBoth(commandComplete) return d
def finished(self, sig, rc): self.elapsedTime = util.now(self._reactor) - self.startTime log.msg("command finished with signal %s, exit code %s, elapsedTime: %0.6f" % (sig, rc, self.elapsedTime)) for w in self.logFileWatchers: # this will send the final updates w.stop() self._sendBuffers() if sig is not None: rc = -1 if self.sendRC: if sig is not None: self.sendStatus( {'header': "process killed by signal %d\n" % sig}) self.sendStatus({'rc': rc}) self.sendStatus({'header': "elapsedTime=%0.6f\n" % self.elapsedTime}) self._cancelTimers() d = self.deferred self.deferred = None if d: d.callback(rc) else: log.msg("Hey, command %s finished twice" % self)
def commandComplete(res): self.sendStatus({"elapsed": util.now(self._reactor) - self.startTime}) self.running = False return res
def _startCommand(self): # ensure workdir exists if not os.path.isdir(self.workdir): os.makedirs(self.workdir) log.msg("RunProcess._startCommand") if self.notreally: self._addToBuffers('header', "command '%s' in dir %s" % (self.fake_command, self.workdir)) self._addToBuffers('header', "(not really)\n") self.finished(None, 0) return self.pp = RunProcessPP(self) self.using_comspec = False if isinstance(self.command, basestring): if runtime.platformType == 'win32': argv = os.environ['COMSPEC'].split() # allow %COMSPEC% to have args if '/c' not in argv: argv += ['/c'] argv += [self.command] self.using_comspec = True else: # for posix, use /bin/sh. for other non-posix, well, doesn't # hurt to try argv = ['/bin/sh', '-c', self.command] display = self.fake_command else: # On windows, CreateProcess requires an absolute path to the executable. # When we call spawnProcess below, we pass argv[0] as the executable. # So, for .exe's that we have absolute paths to, we can call directly # Otherwise, we should run under COMSPEC (usually cmd.exe) to # handle path searching, etc. if runtime.platformType == 'win32' and not \ (self.command[0].lower().endswith(".exe") and os.path.isabs(self.command[0])): argv = os.environ['COMSPEC'].split() # allow %COMSPEC% to have args if '/c' not in argv: argv += ['/c'] argv += list(self.command) self.using_comspec = True else: argv = self.command # Attempt to format this for use by a shell, although the process isn't perfect display = shell_quote(self.fake_command) # $PWD usually indicates the current directory; spawnProcess may not # update this value, though, so we set it explicitly here. This causes # weird problems (bug #456) on msys, though.. if not self.environ.get('MACHTYPE', None) == 'i686-pc-msys': self.environ['PWD'] = os.path.abspath(self.workdir) # self.stdin is handled in RunProcessPP.connectionMade log.msg(" " + display) self._addToBuffers('header', display + "\n") # then comes the secondary information msg = " in dir %s" % (self.workdir,) if self.timeout: if self.timeout == 1: unit = "sec" else: unit = "secs" msg += " (timeout %d %s)" % (self.timeout, unit) if self.maxTime: if self.maxTime == 1: unit = "sec" else: unit = "secs" msg += " (maxTime %d %s)" % (self.maxTime, unit) log.msg(" " + msg) self._addToBuffers('header', msg + "\n") msg = " watching logfiles %s" % (self.logfiles,) log.msg(" " + msg) self._addToBuffers('header', msg + "\n") # then the obfuscated command array for resolving unambiguity msg = " argv: %s" % (self.fake_command,) log.msg(" " + msg) self._addToBuffers('header', msg + "\n") # then the environment, since it sometimes causes problems if self.logEnviron: msg = " environment:\n" env_names = sorted(self.environ.keys()) for name in env_names: msg += " %s=%s\n" % (name, self.environ[name]) log.msg(" environment: %s" % (self.environ,)) self._addToBuffers('header', msg) if self.initialStdin: msg = " writing %d bytes to stdin" % len(self.initialStdin) log.msg(" " + msg) self._addToBuffers('header', msg + "\n") msg = " using PTY: %s" % bool(self.usePTY) log.msg(" " + msg) self._addToBuffers('header', msg + "\n") # put data into stdin and close it, if necessary. This will be # buffered until connectionMade is called if self.initialStdin: self.pp.setStdin(self.initialStdin) self.startTime = util.now(self._reactor) # start the process self.process = self._spawnProcess( self.pp, argv[0], argv, self.environ, self.workdir, usePTY=self.usePTY) # set up timeouts if self.timeout: self.ioTimeoutTimer = self._reactor.callLater(self.timeout, self.doTimeout) if self.maxTime: self.maxTimeoutTimer = self._reactor.callLater(self.maxTime, self.doMaxTimeout) for w in self.logFileWatchers: w.start()