4
## Qemulator - a qemu gui written in python and GTK/Glade.
5
## Copyright (C) 2006 rainer haage
7
## This program is free software; you can redistribute it and/or
8
## modify it under the terms of the GNU General Public License
9
## as published by the Free Software Foundation; either version 2
10
## of the License, or (at your option) any later version.
12
## This program is distributed in the hope that it will be useful,
13
## but WITHOUT ANY WARRANTY; without even the implied warranty of
14
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
## GNU General Public License for more details.
17
## You should have received a copy of the GNU General Public License
18
## along with this program; if not, write to the Free Software
19
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
from threading import Thread
30
encoding = locale.getpreferredencoding()
31
utf8conv = lambda x : unicode(x, encoding).encode('utf8')
35
def __init__ (self, pts, command, return_func=None):
39
self.command = command
40
self.return_func = return_func
42
self.call = "echo " + self.command + " > " + self.pts
47
runcom = self.run_command()
52
fo = open(self.pts,"r")
53
#termencoding = fo.encoding
54
#print "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
55
#print "The qemu console uses the following encoding: " + str(termencoding)
57
#raise IOError, "Couldn't open pts device!"
63
commandline = re.search(self.command, fo.readline())
64
while commandline == None:
65
#print "+++ fo state: " + str(fo)
66
if self.stopall == True:
67
#print "*** fo state: " + str(fo)
71
searchline = fo.readline()
72
uniline = unicode(searchline, "iso8859-1")
73
outline = string.strip(uniline.encode('utf8', 'replace'))
74
commandline = re.search(self.command, outline)
76
print "could not read commandline from " + self.pts
77
if commandline != None:
78
if commandline.group() == self.command:
79
#print "commandline found in output: " + str(commandline.group())
83
#print "waiting done -- result: \n"
84
if commandline == None:
85
print "Error: command: " + self.command + " could not launched"
89
#print "Start reading result..."
91
while os.path.exists(self.pts) and stopper == None and self.stopall != True:
92
if self.stopall == True:
93
#print "*** fo state: " + str(fo)
96
outres = fo.readline()
97
stopper = re.match("\(qemu\)", outres)
101
if string.strip(outres) != "":
102
apstr = unicode(outres, "iso8859-1")
103
apstr = string.strip(apstr.encode('utf8', 'replace'))
104
self.outlist.append(apstr)
105
#print "outres: " + str(outres)
107
#print "reading turn: " + str(c)
110
if self.stopall != True:
111
if self.return_func != None:
112
dofunc = self.return_func(self.outlist)
115
dofunc = self.return_func(['no response'])
120
print "Error, aborted!"
121
if self.stopall == True:
125
#print "------------------------------------------------"
126
#print "------------------------------------------------"
127
#print "All processes stopped!"
129
#print "Status: " + str(self.isAlive())
130
#print "------------------------------------------------"
131
#print "------------------------------------------------"
133
def run_command(self):
135
runit = os.popen(self.call, 'w')
143
if self.stopall == True:
150
#print "*** runit state: " + str(runit)
153
#class PtsWrite_delete_this_class ( Thread ):
155
# def __init__ (self, pts, command):
156
# Thread.__init__(self)
158
# self.command = command
159
# self.call = "echo " + self.command + " > " + self.pts
163
# runcom = self.run_command()
167
# fo = open(self.pts,"r")
169
# #raise IOError, "Couldn't open pts device!"
175
# while commandline == None and c < 200:
176
# commandline = re.search(self.command, fo.readline())
177
# if commandline != None:
178
# if commandline.group() == self.command:
179
# #print "commandline found in output: " + str(commandline.group())
182
# print "waiting for command -- " + str(c)
184
# if commandline == None:
185
# print "Error: command: " + self.command + " could not launched"
187
# print self.pts + " not found"
189
# def run_command(self):
191
# if os.path.exists(self.pts):
192
# runit = os.popen(self.call, 'w')
193
# print 'runit: ' + str(runit)
200
# print "PtsWrite done!"
203
#class PtsRead_delete_this_class ( Thread ):
204
# def __init__ (self, pts, ptsout):
205
# Thread.__init__(self)
207
# self.ptsout = ptsout
211
# #print "PtsRead aufgerufen"
212
# if os.path.exists(self.pts):
213
# ptsout = self.ptsout
217
# while os.path.exists(self.pts):
218
# outres = ptsout.readline()
219
# outres = string.strip(outres)
220
# if lct != 0 and outres != "" and outres != "(qemu)":
221
# self.outlist.append(str(outres))
223
# m = re.search("\w+", outres)
224
# q = re.search("(qemu)", outres)
234
# print self.pts + " not found - STOP"