14
15
from threading import Thread
15
from functools import partial
19
class Interrupt(Exception):
21
Exception.__init__(self, "Interrupted!")
23
class ExpiryTimer(object):
24
def __init__(self, idle):
26
signal.signal(signal.SIGALRM, partial(self.timeout))
29
signal.alarm(self.idle)
31
def start(self, time):
37
def timeout(self, signum, frame):
40
19
class StdinFromWeb(object):
41
20
def __init__(self, cmdQ, lineQ):
45
24
def readline(self):
46
27
self.cmdQ.put({"input":None})
48
28
ln = self.lineQ.get()
31
# Some of our 5 seconds may have elapsed, but never mind.
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
35
class PythonRunner(Thread):
100
36
def __init__(self, cmdQ, lineQ):
102
38
self.lineQ = lineQ
103
self.stdout = StdoutToWeb(self.cmdQ, self.lineQ)
39
self.out = cStringIO.StringIO()
104
40
Thread.__init__(self)
106
42
def execCmd(self, cmd):
108
44
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})
48
res = eval(cmd, self.globs, self.locls)
50
self.cmdQ.put({"okay":(self.out.getvalue(),res)})
114
51
self.curr_cmd = ''
52
self.out = cStringIO.StringIO()
115
53
except Exception, exc:
117
self.cmdQ.put({"exc":str(exc)})
55
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
118
56
self.curr_cmd = ''
57
self.out = cStringIO.StringIO()
122
self.globs['__builtins__'] = globals()['__builtins__']
124
61
compiler = codeop.CommandCompiler()
142
79
except Exception, exc:
144
self.cmdQ.put({"exc":str(exc)})
81
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
145
82
self.curr_cmd = ''
83
self.out = cStringIO.StringIO()
147
85
# throw away a partial command.
149
87
cmd = compile(ln['block'], "<web session>", 'exec');
151
89
except Exception, exc:
153
self.cmdQ.put({"exc":str(exc)})
91
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
154
92
self.curr_cmd = ''
93
self.out = cStringIO.StringIO()
97
self.globs['__builtins__'] = globals()['__builtins__']
157
102
if os.fork(): # launch child and...
170
115
lineQ = Queue.Queue()
171
116
interpThread = PythonRunner(cmdQ, lineQ)
173
# Default expiry time of 15 minutes
174
expiry = ExpiryTimer(15 * 60)
176
118
def initializer():
177
119
interpThread.setDaemon(True)
178
120
interpThread.start()
181
122
def dispatch_msg(msg):
183
123
lineQ.put({msg['cmd']:msg['text']})
184
124
return cmdQ.get()
186
126
if __name__ == "__main__":
187
127
port = int(sys.argv[1])
188
128
magic = sys.argv[2]
189
if len(sys.argv) >= 4:
191
os.chdir(sys.argv[3])
192
os.environ['HOME'] = sys.argv[3]
194
130
common.chat.start_server(port, magic, True, dispatch_msg, initializer)