~arky/ubuntu/karmic/qemulator/fix-257233

« back to all changes in this revision

Viewing changes to usr/local/lib/qemulator/qml_ptsmanager.py

  • Committer: Bazaar Package Importer
  • Author(s): Francesco Namuri
  • Date: 2007-06-20 16:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20070620161122-zzcdsje7in6qsrwm
Tags: upstream-0.5
ImportĀ upstreamĀ versionĀ 0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# coding=utf-8
 
3
 
 
4
##    Qemulator - a qemu gui written in python and GTK/Glade.
 
5
##    Copyright (C) 2006  rainer haage
 
6
##
 
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.
 
11
##    
 
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.
 
16
##    
 
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.
 
20
 
 
21
import threading
 
22
from threading import Thread
 
23
#import signal
 
24
import string
 
25
import os
 
26
import re
 
27
import time
 
28
import locale
 
29
 
 
30
encoding = locale.getpreferredencoding()
 
31
utf8conv = lambda x : unicode(x, encoding).encode('utf8')
 
32
 
 
33
class PtsMan(Thread):
 
34
    
 
35
    def __init__ (self, pts, command, return_func=None):
 
36
        Thread.__init__(self)
 
37
        self.stopall = False
 
38
        self.pts = pts
 
39
        self.command = command
 
40
        self.return_func = return_func
 
41
        self.outlist = []
 
42
        self.call = "echo " + self.command + " > " + self.pts
 
43
 
 
44
    def run(self): 
 
45
        #time.sleep(0.2)
 
46
        error = False
 
47
        runcom = self.run_command()
 
48
        if runcom != True:
 
49
            error = True
 
50
        if error != True:
 
51
            try:
 
52
                fo = open(self.pts,"r")
 
53
                #termencoding = fo.encoding
 
54
                #print "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
 
55
                #print "The qemu console uses the following encoding: " + str(termencoding)
 
56
            except:
 
57
                #raise IOError, "Couldn't open pts device!"
 
58
                error = True
 
59
        if error != True:
 
60
            commandline = None
 
61
            self.outlist = []
 
62
            c = 0
 
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)
 
68
                    fo.close() 
 
69
                    break
 
70
                try:
 
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)
 
75
                except:
 
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())
 
80
                        break
 
81
                c = c+1
 
82
                time.sleep(0.1)
 
83
            #print "waiting done -- result: \n"
 
84
            if commandline == None:
 
85
                print "Error: command: " + self.command +  " could not launched"
 
86
                fo.close()
 
87
            
 
88
            else:
 
89
                #print "Start reading result..."
 
90
                stopper = None
 
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)
 
94
                        fo.close() 
 
95
                        break
 
96
                    outres =  fo.readline()
 
97
                    stopper = re.match("\(qemu\)", outres)  
 
98
                    if stopper != None:
 
99
                        fo.close()
 
100
                        break
 
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)
 
106
                    c = c+1
 
107
                    #print "reading turn: " + str(c)
 
108
                    time.sleep(0.01)
 
109
 
 
110
            if self.stopall != True:
 
111
                if self.return_func != None:
 
112
                    dofunc = self.return_func(self.outlist) 
 
113
                else:
 
114
                    try:
 
115
                        dofunc = self.return_func(['no response'])
 
116
                    except:
 
117
                        pass
 
118
                    return self.outlist            
 
119
        else:
 
120
            print "Error, aborted!"
 
121
            if self.stopall == True:
 
122
                fo.close()  
 
123
    def stop(self):
 
124
        self.stopall = True
 
125
        #print "------------------------------------------------"
 
126
        #print "------------------------------------------------"        
 
127
        #print "All processes stopped!"
 
128
        #time.sleep(0.3)
 
129
        #print "Status: " + str(self.isAlive())
 
130
        #print "------------------------------------------------"
 
131
        #print "------------------------------------------------"     
 
132
 
 
133
    def run_command(self): 
 
134
        returnval = False
 
135
        runit = os.popen(self.call, 'w')
 
136
        if runit != None:
 
137
            runit.flush()
 
138
            runit.close()
 
139
            returnval = True
 
140
        else:
 
141
            returnval = False
 
142
            
 
143
        if self.stopall == True:
 
144
            if runit != None:
 
145
                try:
 
146
                    runit.flush()
 
147
                    runit.close()
 
148
                except:
 
149
                    pass
 
150
            #print "*** runit state: " + str(runit)
 
151
        return returnval
 
152
    
 
153
#class PtsWrite_delete_this_class ( Thread ):
 
154
#
 
155
#    def __init__ (self, pts, command):
 
156
#        Thread.__init__(self)
 
157
#        self.pts = pts
 
158
#        self.command = command
 
159
#        self.call = "echo " + self.command + " > " + self.pts
 
160
#
 
161
#    def run(self):
 
162
#        error = False
 
163
#        runcom = self.run_command()
 
164
#        if runcom != True:
 
165
#            error = True        
 
166
#            try:
 
167
#                fo = open(self.pts,"r")
 
168
#            except:
 
169
#                #raise IOError, "Couldn't open pts device!"
 
170
#                error = True
 
171
#            if error != True:
 
172
#                commandline = None
 
173
#                self.outlist = []
 
174
#                c = 0
 
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())
 
180
#                            break
 
181
#                    else:
 
182
#                        print "waiting for command -- " + str(c)                       
 
183
#                    c = c+1
 
184
#                if commandline == None:
 
185
#                    print "Error: command: " + self.command +  " could not launched"               
 
186
#        else: 
 
187
#            print self.pts + " not found"
 
188
#
 
189
#    def run_command(self):
 
190
#        returnval = False
 
191
#        if os.path.exists(self.pts):
 
192
#            runit = os.popen(self.call, 'w')
 
193
#            print 'runit: ' + str(runit)
 
194
#            if runit != None:
 
195
#                returnval = True
 
196
#                runit.flush()
 
197
#                runit.close()
 
198
#            else:
 
199
#                returnval = False
 
200
#            print "PtsWrite done!"
 
201
#        return returnval            
 
202
            
 
203
#class PtsRead_delete_this_class ( Thread ):
 
204
#    def __init__ (self, pts, ptsout):
 
205
#        Thread.__init__(self)
 
206
#        self.pts = pts
 
207
#        self.ptsout = ptsout
 
208
#        self.outlist = []
 
209
#
 
210
#    def run(self):
 
211
#        #print "PtsRead aufgerufen"
 
212
#        if os.path.exists(self.pts):
 
213
#            ptsout = self.ptsout
 
214
#            lct = 0
 
215
#            m = None
 
216
#            q = None
 
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))
 
222
#                    
 
223
#                m = re.search("\w+", outres)
 
224
#                q = re.search("(qemu)", outres)
 
225
#                lct = lct+1
 
226
#                if q != None: 
 
227
#                    #print q
 
228
#                    lct = 0
 
229
#                    ptsout.close()
 
230
#                    break
 
231
#                time.sleep(0.3)     
 
232
#            print "READ DONE!"
 
233
#        else: 
 
234
#            print self.pts + " not found -  STOP"