1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
#!/usr/bin/python
# usage:
# python-console <port> <magic> [<working-dir>]
import codeop
import cPickle
import cStringIO
import md5
import Queue
import signal
import socket
import sys
import traceback
from threading import Thread
import ivle.chat
import ivle.util
# This version must be supported by both the local and remote code
PICKLEVERSION = 0
class Interrupt(Exception):
def __init__(self):
Exception.__init__(self, "Interrupted!")
class ExpiryTimer(object):
def __init__(self, idle):
self.idle = idle
signal.signal(signal.SIGALRM, self.timeout)
def ping(self):
signal.alarm(self.idle)
def start(self, time):
signal.alarm(time)
def stop(self):
self.ping()
def timeout(self, signum, frame):
sys.exit(1)
class StdinFromWeb(object):
def __init__(self, cmdQ, lineQ):
self.cmdQ = cmdQ
self.lineQ = lineQ
def readline(self):
self.cmdQ.put({"input":None})
expiry.ping()
action, params = self.lineQ.get()
if action == 'chat':
return params
elif action == 'interrupt':
raise Interrupt()
class StdoutToWeb(object):
def __init__(self, cmdQ, lineQ):
self.cmdQ = cmdQ
self.lineQ = lineQ
self.remainder = ''
def _trim_incomplete_final(self, stuff):
'''Trim an incomplete UTF-8 character from the end of a string.
Returns (trimmed_string, count_of_trimmed_bytes).
'''
tokill = ivle.util.incomplete_utf8_sequence(stuff)
if tokill == 0:
return (stuff, tokill)
else:
return (stuff[:-tokill], tokill)
def write(self, stuff):
# print will only give a non-file a unicode or str. There's no way
# to convince it to encode unicodes, so we have to do it ourselves.
# Yay for file special-cases (fileobject.c, PyFile_WriteObject).
# If somebody wants to write some other object to here, they do it
# at their own peril.
if isinstance(stuff, unicode):
stuff = stuff.encode('utf-8')
self.remainder = self.remainder + stuff
# if there's less than 128 bytes, buffer
if len(self.remainder) < 128:
return
# if there's lots, then send it in 1/2K blocks
while len(self.remainder) > 512:
# We send things as Unicode inside JSON, so we must only send
# complete UTF-8 characters.
(blk, count) = self._trim_incomplete_final(self.remainder[:512])
self.cmdQ.put({"output":blk.decode('utf-8', 'replace')})
expiry.ping()
action, params = self.lineQ.get()
self.remainder = self.remainder[512 - count:]
# Finally, split the remainder up into lines, and ship all the
# completed lines off to the server.
lines = self.remainder.split("\n")
self.remainder = lines[-1]
del lines[-1]
if len(lines) > 0:
lines.append('')
text = "\n".join(lines)
self.cmdQ.put({"output":text.decode('utf-8', 'replace')})
expiry.ping()
action, params = self.lineQ.get()
if action == 'interrupt':
raise Interrupt()
def flush(self):
if len(self.remainder) > 0:
(out, count) = self._trim_incomplete_final(self.remainder)
self.cmdQ.put({"output":out.decode('utf-8', 'replace')})
expiry.ping()
action, params = self.lineQ.get()
# Leave incomplete characters in the buffer.
# Yes, this does mean that an incomplete character will be left
# off the end, but we discussed this and it was deemed best.
self.remainder = self.remainder[len(self.remainder)-count:]
if action == 'interrupt':
raise Interrupt()
class WebIO(object):
"""Provides a file like interface to the Web front end of the console.
You may print text to the console using write(), flush any buffered output
using flush(), or request text from the console using readline()"""
# FIXME: Clean up the whole stdin, stdout, stderr mess. We really need to
# be able to deal with the streams individually.
def __init__(self, cmdQ, lineQ):
self.cmdQ = cmdQ
self.lineQ = lineQ
self.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
self.stdout = StdoutToWeb(self.cmdQ, self.lineQ)
def write(self, stuff):
self.stdout.write(stuff)
def flush(self):
self.stdout.flush()
def readline(self):
self.stdout.flush()
return self.stdin.readline()
class PythonRunner(Thread):
def __init__(self, cmdQ, lineQ):
self.cmdQ = cmdQ
self.lineQ = lineQ
self.webio = WebIO(self.cmdQ, self.lineQ)
self.cc = codeop.CommandCompiler()
Thread.__init__(self)
def execCmd(self, cmd):
try:
# We don't expect a return value - 'single' symbol prints it.
self.eval(cmd)
self.curr_cmd = ''
self.webio.flush()
return({"okay": None})
except:
self.curr_cmd = ''
self.webio.flush()
tb = format_exc_start(start=2)
return({"exc": ''.join(tb).decode('utf-8', 'replace')})
def run(self):
# Set up global space and partial command buffer
self.globs = {'__name__': '__main__'}
self.curr_cmd = ''
# Set up I/O to use web interface
sys.stdin = self.webio
sys.stdout = self.webio
sys.stderr = self.webio
# Handlers for each action
actions = {
'splash': self.handle_splash,
'chat': self.handle_chat,
'block': self.handle_block,
'globals': self.handle_globals,
'call': self.handle_call,
'execute': self.handle_execute,
'setvars': self.handle_setvars,
}
# Run the processing loop
while True:
action, params = self.lineQ.get()
try:
response = actions[action](params)
except Exception, e:
response = {'error': repr(e)}
finally:
self.cmdQ.put(response)
def handle_splash(self, params):
# Initial console splash screen
python_version = '.'.join(str(v) for v in sys.version_info[:3])
splash_text = ("""IVLE %s Python Console (Python %s)
Type "help", "copyright", "credits" or "license" for more information.
""" % (ivle.__version__, python_version))
return {'output': splash_text}
def handle_chat(self, params):
# Set up the partial cmd buffer
if self.curr_cmd == '':
self.curr_cmd = params
else:
self.curr_cmd = self.curr_cmd + '\n' + params
# Try to execute the buffer
try:
# A single trailing newline simply indicates that the line is
# finished. Two trailing newlines indicate the end of a block.
# Unfortunately, codeop.CommandCompiler causes even one to
# terminate a block.
# Thus we need to remove a trailing newline from the command,
# unless there are *two* trailing newlines, or multi-line indented
# blocks are impossible. See Google Code issue 105.
cmd_text = self.curr_cmd
if cmd_text.endswith('\n') and not cmd_text.endswith('\n\n'):
cmd_text = cmd_text[:-1]
cmd = self.cc(cmd_text, '<web session>')
if cmd is None:
# The command was incomplete, so send back a None, so the
# client can print a '...'
return({"more":None})
else:
return(self.execCmd(cmd))
except:
# Clear any partial command
self.curr_cmd = ''
# Flush the output buffers
sys.stderr.flush()
sys.stdout.flush()
# Return the exception
tb = format_exc_start(start=3)
return({"exc": ''.join(tb).decode('utf-8', 'replace')})
def handle_block(self, params):
# throw away any partial command.
self.curr_cmd = ''
# Try to execute a complete block of code
try:
cmd = compile(params, "<web session>", 'exec');
return(self.execCmd(cmd))
except:
# Flush the output buffers
sys.stderr.flush()
sys.stdout.flush()
# Return the exception
tb = format_exc_start(start=1)
return({"exc": ''.join(tb).decode('utf-8', 'replace')})
def handle_globals(self, params):
# Unpickle the new space (if provided)
if isinstance(params, dict):
self.globs = {'__name__': '__main__'}
for g in params:
try:
self.globs[g] = cPickle.loads(str(params[g]))
except cPickle.UnpicklingError:
pass
# Return the current globals
return({'globals': flatten(self.globs)})
def handle_call(self, params):
call = {}
# throw away any partial command.
self.curr_cmd = ''
if isinstance(params, dict):
try:
# Expand parameters
if isinstance(params['args'], list):
args = map(self.eval, params['args'])
else:
args = []
if isinstance(params['kwargs'], dict):
kwargs = {}
for kwarg in params['kwargs']:
kwargs[kwarg] = self.eval(
params['kwargs'][kwarg])
else:
kwargs = {}
# Run the fuction
function = self.eval(params['function'])
try:
call['result'] = function(*args, **kwargs)
except Exception, e:
exception = {}
tb = format_exc_start(start=1)
exception['traceback'] = \
''.join(tb).decode('utf-8', 'replace')
exception['except'] = cPickle.dumps(e,
PICKLEVERSION)
call['exception'] = exception
except Exception, e:
tb = format_exc_start(start=1)
call = {"exc": ''.join(tb).decode('utf-8', 'replace')}
# Flush the output buffers
sys.stderr.flush()
sys.stdout.flush()
# Write out the inspection object
return(call)
else:
return({'response': 'failure'})
def handle_execute(self, params):
# throw away any partial command.
self.curr_cmd = ''
# Like block but return a serialization of the state
# throw away partial command
response = {'okay': None}
try:
cmd = compile(params, "<web session>", 'exec');
# We don't expect a return value - 'single' symbol prints it.
self.eval(cmd)
except Exception, e:
response = {'exception': cPickle.dumps(e, PICKLEVERSION)}
# Flush the output
sys.stderr.flush()
sys.stdout.flush()
# Return the inspection object
return(response)
def handle_setvars(self, params):
# Adds some variables to the global dictionary
for var in params['set_vars']:
try:
self.globs[var] = self.eval(params['set_vars'][var])
except Exception, e:
tb = format_exc_start(start=1)
return({"exc": ''.join(tb).decode('utf-8', 'replace')})
return({'okay': None})
def eval(self, source):
""" Evaluates a string in the private global space """
return eval(source, self.globs)
# The global 'magic' is the secret that the client and server share
# which is used to create and md5 digest to authenticate requests.
# It is assigned a real value at startup.
magic = ''
cmdQ = Queue.Queue()
lineQ = Queue.Queue()
interpThread = PythonRunner(cmdQ, lineQ)
terminate = None
# Default expiry time of 15 minutes
expiry = ExpiryTimer(15 * 60)
def initializer():
interpThread.setDaemon(True)
interpThread.start()
signal.signal(signal.SIGXCPU, sig_handler)
expiry.ping()
def sig_handler(signum, frame):
"""Handles response from signals"""
global terminate
if signum == signal.SIGXCPU:
terminate = "CPU time limit exceeded"
def dispatch_msg(msg):
global terminate
if msg['cmd'] == 'terminate':
terminate = "User requested restart"
if terminate:
raise ivle.chat.Terminate({"terminate":terminate})
expiry.ping()
lineQ.put((msg['cmd'],msg['text']))
response = cmdQ.get()
if terminate:
raise ivle.chat.Terminate({"terminate":terminate})
return response
def format_exc_start(start=0):
etype, value, tb = sys.exc_info()
tbbits = traceback.extract_tb(tb)[start:]
list = ['Traceback (most recent call last):\n']
list = list + traceback.format_list(tbbits)
list = list + traceback.format_exception_only(etype, value)
return ''.join(list)
# Takes an object and returns a flattened version suitable for JSON
def flatten(object):
flat = {}
for o in object:
try:
flat[o] = cPickle.dumps(object[o], PICKLEVERSION)
except (TypeError, cPickle.PicklingError):
try:
o_type = type(object[o]).__name__
o_name = object[o].__name__
fake_o = ivle.util.FakeObject(o_type, o_name)
flat[o] = cPickle.dumps(fake_o, PICKLEVERSION)
except AttributeError:
pass
return flat
if __name__ == "__main__":
port = int(sys.argv[1])
magic = sys.argv[2]
# Make python's search path follow the cwd
sys.path[0] = ''
ivle.chat.start_server(port, magic, True, dispatch_msg, initializer)
|