etisserant@48: #!/usr/bin/env python etisserant@48: # -*- coding: utf-8 -*- etisserant@48: etisserant@48: #This file is part of Beremiz, a Integrated Development Environment for etisserant@48: #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. etisserant@48: # etisserant@48: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD etisserant@48: # etisserant@48: #See COPYING file for copyrights details. etisserant@48: # etisserant@48: #This library is free software; you can redistribute it and/or etisserant@48: #modify it under the terms of the GNU General Public etisserant@48: #License as published by the Free Software Foundation; either etisserant@48: #version 2.1 of the License, or (at your option) any later version. etisserant@48: # etisserant@48: #This library is distributed in the hope that it will be useful, etisserant@48: #but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@48: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU etisserant@48: #General Public License for more details. etisserant@48: # etisserant@48: #You should have received a copy of the GNU General Public etisserant@48: #License along with this library; if not, write to the Free Software etisserant@48: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@48: etisserant@48: # etisserant@48: # based on wxPopen.py from boa-constructor etisserant@48: # etisserant@48: etisserant@48: import time etisserant@48: from StringIO import StringIO etisserant@48: etisserant@48: from wxPython.wx import * etisserant@48: etisserant@48: class ProcessRunnerMix: etisserant@48: def __init__(self, input, handler=None): etisserant@48: if handler is None: etisserant@48: handler = self etisserant@48: self.handler = handler etisserant@48: EVT_IDLE(handler, self.OnIdle) etisserant@48: EVT_END_PROCESS(handler, -1, self.OnProcessEnded) etisserant@48: etisserant@48: input.reverse() # so we can pop etisserant@48: self.input = input etisserant@48: etisserant@48: self.reset() etisserant@48: etisserant@48: def reset(self): etisserant@48: self.process = None etisserant@48: self.pid = -1 etisserant@48: self.output = [] etisserant@48: self.errors = [] etisserant@48: self.inputStream = None etisserant@48: self.errorStream = None etisserant@48: self.outputStream = None etisserant@48: self.outputFunc = None etisserant@48: self.errorsFunc = None etisserant@48: self.finishedFunc = None etisserant@48: self.finished = false etisserant@48: self.responded = false etisserant@48: etisserant@48: def execute(self, cmd): etisserant@48: self.process = wxProcess(self.handler) etisserant@48: self.process.Redirect() etisserant@48: etisserant@48: self.pid = wxExecute(cmd, wxEXEC_NOHIDE, self.process) etisserant@48: etisserant@48: self.inputStream = self.process.GetOutputStream() etisserant@48: self.errorStream = self.process.GetErrorStream() etisserant@48: self.outputStream = self.process.GetInputStream() etisserant@48: etisserant@48: #self.OnIdle() etisserant@48: wxWakeUpIdle() etisserant@48: etisserant@48: def setCallbacks(self, output, errors, finished): etisserant@48: self.outputFunc = output etisserant@48: self.errorsFunc = errors etisserant@48: self.finishedFunc = finished etisserant@48: etisserant@48: def detach(self): etisserant@48: if self.process is not None: etisserant@48: self.process.CloseOutput() etisserant@48: self.process.Detach() etisserant@48: self.process = None etisserant@48: etisserant@48: def kill(self): etisserant@48: if self.process is not None: etisserant@48: self.process.CloseOutput() etisserant@48: if wxProcess_Kill(self.pid, wxSIGTERM) != wxKILL_OK: etisserant@48: wxProcess_Kill(self.pid, wxSIGKILL) etisserant@48: self.process = None etisserant@48: etisserant@48: def updateStream(self, stream, data): etisserant@48: if stream and stream.CanRead(): etisserant@48: if not self.responded: etisserant@48: self.responded = true etisserant@48: text = stream.read() etisserant@48: data.append(text) etisserant@48: return text etisserant@48: else: etisserant@48: return None etisserant@48: etisserant@48: def updateInpStream(self, stream, input): etisserant@48: if stream and input: etisserant@48: line = input.pop() etisserant@48: stream.write(line) etisserant@48: etisserant@48: def updateErrStream(self, stream, data): etisserant@48: return self.updateStream(stream, data) etisserant@48: etisserant@48: def updateOutStream(self, stream, data): etisserant@48: return self.updateStream(stream, data) etisserant@48: etisserant@48: def OnIdle(self, event=None): etisserant@48: if self.process is not None: etisserant@48: self.updateInpStream(self.inputStream, self.input) etisserant@48: e = self.updateErrStream(self.errorStream, self.errors) etisserant@48: if e is not None and self.errorsFunc is not None: etisserant@48: wxCallAfter(self.errorsFunc, e) etisserant@48: o = self.updateOutStream(self.outputStream, self.output) etisserant@48: if o is not None and self.outputFunc is not None: etisserant@48: wxCallAfter(self.outputFunc, o) etisserant@48: etisserant@48: #wxWakeUpIdle() etisserant@48: #time.sleep(0.001) etisserant@48: etisserant@48: def OnProcessEnded(self, event): etisserant@48: self.OnIdle() etisserant@48: pid,exitcode = event.GetPid(), event.GetExitCode() etisserant@48: if self.process: etisserant@48: self.process.Destroy() etisserant@48: self.process = None etisserant@48: etisserant@48: self.finished = true etisserant@48: etisserant@48: # XXX doesn't work ??? etisserant@48: #self.handler.Disconnect(-1, wxEVT_IDLE) etisserant@48: etisserant@48: if self.finishedFunc: etisserant@48: wxCallAfter(self.finishedFunc, pid, exitcode) etisserant@48: etisserant@48: class ProcessRunner(wxEvtHandler, ProcessRunnerMix): etisserant@48: def __init__(self, input): etisserant@48: wxEvtHandler.__init__(self) etisserant@48: ProcessRunnerMix.__init__(self, input) etisserant@48: etisserant@48: def wxPopen3(cmd, input, output, errors, finish, handler=None): etisserant@48: p = ProcessRunnerMix(input, handler) etisserant@48: p.setCallbacks(output, errors, finish) etisserant@48: p.execute(cmd) etisserant@48: return p etisserant@48: etisserant@48: