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

« back to all changes in this revision

Viewing changes to services/python-console

  • Committer: David Coles
  • Date: 2009-07-21 02:19:56 UTC
  • mto: (1281.1.8 aufsless)
  • mto: This revision was merged to the branch mainline in revision 1300.
  • Revision ID: coles.david@gmail.com-20090721021956-c1jiwu7fhi2dna1g
Updated to work on bind mounts

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 == '':
261
254
    def handle_globals(self, params):
262
255
        # Unpickle the new space (if provided)
263
256
        if isinstance(params, dict):
264
 
            self.globs = {'__name__': '__main__'}
 
257
            self.globs = {}
265
258
            for g in params:
266
259
                try:
267
 
                    self.globs[g] = cPickle.loads(str(params[g]))
268
 
                except cPickle.UnpicklingError:
 
260
                    self.globs[g] = cPickle.loads(params[g])
 
261
                except:
269
262
                    pass
270
263
 
271
264
        # Return the current globals
376
369
    """Handles response from signals"""
377
370
    global terminate
378
371
    if signum == signal.SIGXCPU:
379
 
        terminate = "CPU time limit exceeded"
 
372
        terminate = "CPU Time Limit Exceeded"
380
373
 
381
374
def dispatch_msg(msg):
382
375
    global terminate
383
376
    if msg['cmd'] == 'terminate':
384
 
        terminate = "User requested restart"
 
377
        terminate = "User requested console be terminated"
385
378
    if terminate:
386
379
        raise ivle.chat.Terminate({"terminate":terminate})
387
380
    expiry.ping()
406
399
    for o in object:
407
400
        try:
408
401
            flat[o] = cPickle.dumps(object[o], PICKLEVERSION)
409
 
        except (TypeError, cPickle.PicklingError):
 
402
        except TypeError:
410
403
            try:
411
404
                o_type = type(object[o]).__name__
412
405
                o_name = object[o].__name__
419
412
if __name__ == "__main__":
420
413
    port = int(sys.argv[1])
421
414
    magic = sys.argv[2]
 
415
    
 
416
    # Sanitise the Enviroment
 
417
    os.environ = {}
 
418
    os.environ['PATH'] = '/usr/local/bin:/usr/bin:/bin'
 
419
 
 
420
    if len(sys.argv) >= 4:
 
421
        # working_dir
 
422
        os.chdir(sys.argv[3])
 
423
        os.environ['HOME'] = sys.argv[3]
422
424
 
423
425
    # Make python's search path follow the cwd
424
426
    sys.path[0] = ''