19
class Interrupt(Exception):
21
Exception.__init__(self, "Interrupted!")
20
23
class ExpiryTimer(object):
21
24
def __init__(self, idle):
23
signal.signal(signal.SIGALRM, partial(self.timeout,self))
26
signal.signal(signal.SIGALRM, partial(self.timeout))
26
29
signal.alarm(self.idle)
42
45
def readline(self):
44
46
self.cmdQ.put({"input":None})
46
48
ln = self.lineQ.get()
54
class StdoutToWeb(object):
55
def __init__(self, cmdQ, lineQ):
60
def write(self, stuff):
61
self.remainder = self.remainder + stuff
63
# if there's less than 128 bytes, buffer
64
if len(self.remainder) < 128:
67
# if there's lots, then send it in 1/2K blocks
68
while len(self.remainder) > 512:
69
blk = self.remainder[0:512]
70
self.cmdQ.put({"output":blk})
73
self.remainder = self.remainder[512:]
75
# Finally, split the remainder up into lines, and ship all the
76
# completed lines off to the server.
77
lines = self.remainder.split("\n")
78
self.remainder = lines[-1]
83
text = "\n".join(lines)
84
self.cmdQ.put({"output":text})
91
if len(self.remainder) > 0:
92
self.cmdQ.put({"output":self.remainder})
100
"""Provides a file like interface to the Web front end of the console.
101
You may print text to the console using write(), flush any buffered output
102
using flush(), or request text from the console using readline()"""
104
def __init__(self, cmdQ, lineQ):
107
self.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
108
self.stdout = StdoutToWeb(self.cmdQ, self.lineQ)
110
def write(self, stuff):
111
self.stdout.write(stuff)
118
return self.stdin.readline()
50
120
class PythonRunner(Thread):
51
121
def __init__(self, cmdQ, lineQ):
53
123
self.lineQ = lineQ
54
self.out = cStringIO.StringIO()
124
self.webio = WebIO(self.cmdQ, self.lineQ)
55
125
Thread.__init__(self)
57
127
def execCmd(self, cmd):
59
sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
62
res = eval(cmd, self.globs, self.locls)
63
self.cmdQ.put({"okay":(self.out.getvalue(),res)})
129
sys.stdin = self.webio
130
sys.stdout = self.webio
131
sys.stderr = self.webio
132
res = eval(cmd, self.globs)
134
self.cmdQ.put({"okay":res})
64
135
self.curr_cmd = ''
65
self.out = cStringIO.StringIO()
66
136
except Exception, exc:
67
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
138
exc_classname = exc.__class__.__name__
139
self.cmdQ.put({"exc": exc_classname + ": " + str(exc)})
68
140
self.curr_cmd = ''
69
self.out = cStringIO.StringIO()
144
self.globs['__builtins__'] = globals()['__builtins__']
73
146
compiler = codeop.CommandCompiler()
91
164
except Exception, exc:
92
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
166
self.cmdQ.put({"exc":str(exc)})
93
167
self.curr_cmd = ''
94
self.out = cStringIO.StringIO()
96
169
# throw away a partial command.
98
171
cmd = compile(ln['block'], "<web session>", 'exec');
100
173
except Exception, exc:
101
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
175
self.cmdQ.put({"exc":str(exc)})
102
176
self.curr_cmd = ''
103
self.out = cStringIO.StringIO()
105
def init_state(self):
107
self.globs['__builtins__'] = globals()['__builtins__']
112
179
if os.fork(): # launch child and...