~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to services/python-console

  • Committer: William Grant
  • Date: 2009-01-22 04:47:42 UTC
  • mfrom: (1080.1.93 storm)
  • Revision ID: grantw@unimelb.edu.au-20090122044742-sa8gnww0ma2bm2rv
Merge Storm branch. ivle.db is dead. Watch out for the schema change.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
# usage:
4
4
#   python-console <port> <magic> [<working-dir>]
5
5
 
 
6
import cjson
6
7
import codeop
7
8
import cPickle
8
9
import cStringIO
9
10
import md5
 
11
import os
10
12
import Queue
11
13
import signal
12
14
import socket
169
171
 
170
172
    def run(self):
171
173
        # Set up global space and partial command buffer
172
 
        self.globs = {'__name__': '__main__'}
 
174
        self.globs = {}
173
175
        self.curr_cmd = ''
174
176
 
175
177
        # Set up I/O to use web interface
179
181
 
180
182
        # Handlers for each action
181
183
        actions = {
182
 
            'splash': self.handle_splash,
183
184
            'chat': self.handle_chat,
184
185
            'block': self.handle_block,
185
186
            'globals': self.handle_globals,
197
198
                response = {'error': repr(e)}
198
199
            finally:
199
200
                self.cmdQ.put(response)
200
 
 
201
 
    def handle_splash(self, params):
202
 
        # Initial console splash screen
203
 
        python_version = '.'.join(str(v) for v in sys.version_info[:3])
204
 
        splash_text = ("""IVLE %s Python Console (Python %s)
205
 
Type "help", "copyright", "credits" or "license" for more information.
206
 
""" % (ivle.__version__, python_version))
207
 
        return {'output': splash_text}
208
 
 
 
201
                   
209
202
    def handle_chat(self, params):
210
203
        # Set up the partial cmd buffer
211
204
        if self.curr_cmd == '':
215
208
 
216
209
        # Try to execute the buffer
217
210
        try:
218
 
            # A single trailing newline simply indicates that the line is
219
 
            # finished. Two trailing newlines indicate the end of a block.
220
 
            # Unfortunately, codeop.CommandCompiler causes even one to
221
 
            # terminate a block.
222
 
            # Thus we need to remove a trailing newline from the command,
223
 
            # unless there are *two* trailing newlines, or multi-line indented
224
 
            # blocks are impossible. See Google Code issue 105.
225
 
            cmd_text = self.curr_cmd
226
 
            if cmd_text.endswith('\n') and not cmd_text.endswith('\n\n'):
227
 
                cmd_text = cmd_text[:-1]
228
 
            cmd = self.cc(cmd_text, '<web session>')
 
211
            cmd = self.cc(self.curr_cmd, '<web session>')
229
212
            if cmd is None:
230
213
                # The command was incomplete, so send back a None, so the              
231
214
                # client can print a '...'
261
244
    def handle_globals(self, params):
262
245
        # Unpickle the new space (if provided)
263
246
        if isinstance(params, dict):
264
 
            self.globs = {'__name__': '__main__'}
 
247
            self.globs = {}
265
248
            for g in params:
266
249
                try:
267
250
                    self.globs[g] = cPickle.loads(params[g])
376
359
    """Handles response from signals"""
377
360
    global terminate
378
361
    if signum == signal.SIGXCPU:
379
 
        terminate = "CPU time limit exceeded"
 
362
        terminate = "CPU Time Limit Exceeded"
380
363
 
381
364
def dispatch_msg(msg):
382
365
    global terminate
383
366
    if msg['cmd'] == 'terminate':
384
 
        terminate = "User requested restart"
 
367
        terminate = "User requested console be terminated"
385
368
    if terminate:
386
369
        raise ivle.chat.Terminate({"terminate":terminate})
387
370
    expiry.ping()
406
389
    for o in object:
407
390
        try:
408
391
            flat[o] = cPickle.dumps(object[o], PICKLEVERSION)
409
 
        except (TypeError, cPickle.PicklingError):
 
392
        except TypeError:
410
393
            try:
411
394
                o_type = type(object[o]).__name__
412
395
                o_name = object[o].__name__
419
402
if __name__ == "__main__":
420
403
    port = int(sys.argv[1])
421
404
    magic = sys.argv[2]
 
405
    
 
406
    # Sanitise the Enviroment
 
407
    os.environ = {}
 
408
    os.environ['PATH'] = '/usr/local/bin:/usr/bin:/bin'
 
409
 
 
410
    if len(sys.argv) >= 4:
 
411
        # working_dir
 
412
        os.chdir(sys.argv[3])
 
413
        os.environ['HOME'] = sys.argv[3]
422
414
 
423
415
    # Make python's search path follow the cwd
424
416
    sys.path[0] = ''