19
class Interrupt(Exception):
21
Exception.__init__(self, "Interrupted!")
23
20
class ExpiryTimer(object):
24
21
def __init__(self, idle):
26
signal.signal(signal.SIGALRM, partial(self.timeout))
23
signal.signal(signal.SIGALRM, partial(self.timeout,self))
29
26
signal.alarm(self.idle)
45
42
def readline(self):
46
44
self.cmdQ.put({"input":None})
48
46
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})
99
50
class PythonRunner(Thread):
100
51
def __init__(self, cmdQ, lineQ):
102
53
self.lineQ = lineQ
103
self.stdout = StdoutToWeb(self.cmdQ, self.lineQ)
54
self.out = cStringIO.StringIO()
104
55
Thread.__init__(self)
106
57
def execCmd(self, cmd):
108
59
sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
109
sys.stdout = self.stdout
110
sys.stderr = self.stdout
111
res = eval(cmd, self.globs)
113
self.cmdQ.put({"okay":res})
62
res = eval(cmd, self.globs, self.locls)
63
self.cmdQ.put({"okay":(self.out.getvalue(),res)})
114
64
self.curr_cmd = ''
65
self.out = cStringIO.StringIO()
115
66
except Exception, exc:
117
self.cmdQ.put({"exc":str(exc)})
67
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
118
68
self.curr_cmd = ''
69
self.out = cStringIO.StringIO()
122
self.globs['__builtins__'] = globals()['__builtins__']
124
73
compiler = codeop.CommandCompiler()
142
91
except Exception, exc:
144
self.cmdQ.put({"exc":str(exc)})
92
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
145
93
self.curr_cmd = ''
94
self.out = cStringIO.StringIO()
147
96
# throw away a partial command.
149
98
cmd = compile(ln['block'], "<web session>", 'exec');
151
100
except Exception, exc:
153
self.cmdQ.put({"exc":str(exc)})
101
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
154
102
self.curr_cmd = ''
103
self.out = cStringIO.StringIO()
105
def init_state(self):
107
self.globs['__builtins__'] = globals()['__builtins__']
157
112
if os.fork(): # launch child and...
186
141
if __name__ == "__main__":
187
142
port = int(sys.argv[1])
188
143
magic = sys.argv[2]
189
if len(sys.argv) >= 4:
191
os.chdir(sys.argv[3])
192
os.environ['HOME'] = sys.argv[3]
194
145
common.chat.start_server(port, magic, True, dispatch_msg, initializer)