author | lbessard |
Tue, 22 Jan 2008 11:05:46 +0100 | |
changeset 86 | f0a9d74e3b26 |
parent 79 | ae06c2da83f7 |
child 89 | 0ab2868c6aa6 |
permissions | -rw-r--r-- |
79 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of Beremiz, a Integrated Development Environment for |
|
5 |
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
# |
|
26 |
# based on wxPopen.py from boa-constructor |
|
27 |
# |
|
28 |
||
29 |
import time |
|
30 |
from StringIO import StringIO |
|
31 |
||
32 |
import wx |
|
33 |
||
34 |
class ProcessRunnerMix: |
|
35 |
||
36 |
if wx.VERSION < (2, 6, 0): |
|
37 |
def Bind(self, event, function, id = None): |
|
38 |
if id is not None: |
|
39 |
event(self, id, function) |
|
40 |
else: |
|
41 |
event(self, function) |
|
42 |
||
43 |
def __init__(self, input, handler=None): |
|
44 |
if handler is None: |
|
45 |
handler = self |
|
46 |
self.handler = handler |
|
47 |
handler.Bind(wx.EVT_MENU, self.OnIdle) |
|
48 |
handler.Bind(wx.EVT_END_PROCESS, self.OnProcessEnded, id=-1) |
|
49 |
||
50 |
input.reverse() # so we can pop |
|
51 |
self.input = input |
|
52 |
||
53 |
self.reset() |
|
54 |
||
55 |
def reset(self): |
|
56 |
self.process = None |
|
57 |
self.pid = -1 |
|
58 |
self.output = [] |
|
59 |
self.errors = [] |
|
60 |
self.inputStream = None |
|
61 |
self.errorStream = None |
|
62 |
self.outputStream = None |
|
63 |
self.outputFunc = None |
|
64 |
self.errorsFunc = None |
|
65 |
self.finishedFunc = None |
|
66 |
self.finished = False |
|
67 |
self.responded = False |
|
68 |
||
69 |
def execute(self, cmd): |
|
70 |
self.process = wx.Process(self.handler) |
|
71 |
self.process.Redirect() |
|
72 |
||
73 |
self.pid = wx.Execute(cmd, wx.EXEC_ASYNC, self.process) |
|
74 |
||
75 |
self.inputStream = self.process.GetOutputStream() |
|
76 |
self.errorStream = self.process.GetErrorStream() |
|
77 |
self.outputStream = self.process.GetInputStream() |
|
78 |
||
79 |
#self.OnIdle() |
|
80 |
wx.WakeUpIdle() |
|
81 |
||
82 |
def setCallbacks(self, output, errors, finished): |
|
83 |
self.outputFunc = output |
|
84 |
self.errorsFunc = errors |
|
85 |
self.finishedFunc = finished |
|
86 |
||
87 |
def detach(self): |
|
88 |
if self.process is not None: |
|
89 |
self.process.CloseOutput() |
|
90 |
self.process.Detach() |
|
91 |
self.process = None |
|
92 |
||
93 |
def kill(self): |
|
94 |
if self.process is not None: |
|
95 |
self.process.CloseOutput() |
|
96 |
if wx.Process_Kill(self.pid, wx.SIGTERM) != wx.KILL_OK: |
|
97 |
wx.Process_Kill(self.pid, wx.SIGKILL) |
|
98 |
self.process = None |
|
99 |
||
100 |
def updateStream(self, stream, data): |
|
101 |
if stream and stream.CanRead(): |
|
102 |
if not self.responded: |
|
103 |
self.responded = True |
|
104 |
text = stream.read() |
|
105 |
data.append(text) |
|
106 |
return text |
|
107 |
else: |
|
108 |
return None |
|
109 |
||
110 |
def updateInpStream(self, stream, input): |
|
111 |
if stream and input: |
|
112 |
line = input.pop() |
|
113 |
stream.write(line) |
|
114 |
||
115 |
def updateErrStream(self, stream, data): |
|
116 |
return self.updateStream(stream, data) |
|
117 |
||
118 |
def updateOutStream(self, stream, data): |
|
119 |
return self.updateStream(stream, data) |
|
120 |
||
121 |
def OnIdle(self, event=None): |
|
122 |
if self.process is not None: |
|
123 |
self.updateInpStream(self.inputStream, self.input) |
|
124 |
e = self.updateErrStream(self.errorStream, self.errors) |
|
125 |
if e is not None and self.errorsFunc is not None: |
|
126 |
wx.CallAfter(self.errorsFunc, e) |
|
127 |
o = self.updateOutStream(self.outputStream, self.output) |
|
128 |
if o is not None and self.outputFunc is not None: |
|
129 |
wx.CallAfter(self.outputFunc, o) |
|
130 |
||
131 |
#wx.WakeUpIdle() |
|
132 |
#time.sleep(0.001) |
|
133 |
||
134 |
def OnProcessEnded(self, event): |
|
135 |
self.OnIdle() |
|
136 |
pid,exitcode = event.GetPid(), event.GetExitCode() |
|
137 |
if self.process: |
|
138 |
self.process.Destroy() |
|
139 |
self.process = None |
|
140 |
||
141 |
self.finished = True |
|
142 |
||
143 |
# XXX doesn't work ??? |
|
144 |
#self.handler.Disconnect(-1, wx.EVT_IDLE) |
|
145 |
||
146 |
if self.finishedFunc: |
|
147 |
wx.CallAfter(self.finishedFunc, pid, exitcode) |
|
148 |
||
149 |
class ProcessRunner(wx.EvtHandler, ProcessRunnerMix): |
|
150 |
def __init__(self, input): |
|
151 |
wx.EvtHandler.__init__(self) |
|
152 |
ProcessRunnerMix.__init__(self, input) |
|
153 |
||
154 |
def wxPopen3(cmd, input, output, errors, finish, handler=None): |
|
155 |
p = ProcessRunnerMix(input, handler) |
|
156 |
p.setCallbacks(output, errors, finish) |
|
157 |
p.execute(cmd) |
|
158 |
return p |
|
159 |
||
48
6b30cfee163e
Enhanced and really multi-platform process logging. Use wxProcess and wxExecute instead of python popen3 (unix only).
etisserant
parents:
diff
changeset
|
160 |