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