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

« back to all changes in this revision

Viewing changes to services/python-console

  • Committer: matt.giuca
  • Date: 2009-01-12 01:03:59 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1077
Reverted revisions 1059 and 1069 - renaming the DB user from 'postgres' to
    'ivleuser'.
    Heart was in the right place, but currently doesn't work because postgres
    owns the relations and we've no way to correct that yet (since we manually
    run the .sql script as postgres).

    This will be reverted back again once that's solved.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
import cPickle
9
9
import cStringIO
10
10
import md5
 
11
import os
11
12
import Queue
12
13
import signal
13
14
import socket
15
16
import traceback
16
17
from threading import Thread
17
18
 
18
 
import ivle.chat
19
 
import ivle.util
 
19
import common.chat
 
20
import common.util
20
21
 
21
22
# This version must be supported by both the local and remote code
22
23
PICKLEVERSION = 0
66
67
        '''Trim an incomplete UTF-8 character from the end of a string.
67
68
           Returns (trimmed_string, count_of_trimmed_bytes).
68
69
        '''
69
 
        tokill = ivle.util.incomplete_utf8_sequence(stuff)
 
70
        tokill = common.util.incomplete_utf8_sequence(stuff)
70
71
        if tokill == 0:
71
72
            return (stuff, tokill)
72
73
        else:
170
171
 
171
172
    def run(self):
172
173
        # Set up global space and partial command buffer
173
 
        self.globs = {'__name__': '__main__'}
 
174
        self.globs = {}
174
175
        self.curr_cmd = ''
175
176
 
176
177
        # Set up I/O to use web interface
180
181
 
181
182
        # Handlers for each action
182
183
        actions = {
183
 
            'splash': self.handle_splash,
184
184
            'chat': self.handle_chat,
185
185
            'block': self.handle_block,
186
186
            'globals': self.handle_globals,
198
198
                response = {'error': repr(e)}
199
199
            finally:
200
200
                self.cmdQ.put(response)
201
 
 
202
 
    def handle_splash(self, params):
203
 
        # Initial console splash screen
204
 
        python_version = '.'.join(str(v) for v in sys.version_info[:3])
205
 
        splash_text = ("""IVLE %s Python Console (Python %s)
206
 
Type "help", "copyright", "credits" or "license" for more information.
207
 
""" % (ivle.__version__, python_version))
208
 
        return {'output': splash_text}
209
 
 
 
201
                   
210
202
    def handle_chat(self, params):
211
203
        # Set up the partial cmd buffer
212
204
        if self.curr_cmd == '':
216
208
 
217
209
        # Try to execute the buffer
218
210
        try:
219
 
            # A single trailing newline simply indicates that the line is
220
 
            # finished. Two trailing newlines indicate the end of a block.
221
 
            # Unfortunately, codeop.CommandCompiler causes even one to
222
 
            # terminate a block.
223
 
            # Thus we need to remove a trailing newline from the command,
224
 
            # unless there are *two* trailing newlines, or multi-line indented
225
 
            # blocks are impossible. See Google Code issue 105.
226
 
            cmd_text = self.curr_cmd
227
 
            if cmd_text.endswith('\n') and not cmd_text.endswith('\n\n'):
228
 
                cmd_text = cmd_text[:-1]
229
 
            cmd = self.cc(cmd_text, '<web session>')
 
211
            cmd = self.cc(self.curr_cmd, '<web session>')
230
212
            if cmd is None:
231
213
                # The command was incomplete, so send back a None, so the              
232
214
                # client can print a '...'
262
244
    def handle_globals(self, params):
263
245
        # Unpickle the new space (if provided)
264
246
        if isinstance(params, dict):
265
 
            self.globs = {'__name__': '__main__'}
 
247
            self.globs = {}
266
248
            for g in params:
267
249
                try:
268
250
                    self.globs[g] = cPickle.loads(params[g])
377
359
    """Handles response from signals"""
378
360
    global terminate
379
361
    if signum == signal.SIGXCPU:
380
 
        terminate = "CPU time limit exceeded"
 
362
        terminate = "CPU Time Limit Exceeded"
381
363
 
382
364
def dispatch_msg(msg):
383
365
    global terminate
384
366
    if msg['cmd'] == 'terminate':
385
 
        terminate = "User requested restart"
 
367
        terminate = "User requested console be terminated"
386
368
    if terminate:
387
 
        raise ivle.chat.Terminate({"terminate":terminate})
 
369
        raise common.chat.Terminate({"terminate":terminate})
388
370
    expiry.ping()
389
371
    lineQ.put((msg['cmd'],msg['text']))
390
372
    response = cmdQ.get()
391
373
    if terminate:
392
 
        raise ivle.chat.Terminate({"terminate":terminate})
 
374
        raise common.chat.Terminate({"terminate":terminate})
393
375
    return response
394
376
 
395
377
def format_exc_start(start=0):
407
389
    for o in object:
408
390
        try:
409
391
            flat[o] = cPickle.dumps(object[o], PICKLEVERSION)
410
 
        except (TypeError, cPickle.PicklingError):
 
392
        except TypeError:
411
393
            try:
412
394
                o_type = type(object[o]).__name__
413
395
                o_name = object[o].__name__
414
 
                fake_o = ivle.util.FakeObject(o_type, o_name)
 
396
                fake_o = common.util.FakeObject(o_type, o_name)
415
397
                flat[o] = cPickle.dumps(fake_o, PICKLEVERSION)
416
398
            except AttributeError:
417
399
                pass
420
402
if __name__ == "__main__":
421
403
    port = int(sys.argv[1])
422
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]
423
414
 
424
415
    # Make python's search path follow the cwd
425
416
    sys.path[0] = ''
426
417
 
427
 
    ivle.chat.start_server(port, magic, True, dispatch_msg, initializer)
 
418
    common.chat.start_server(port, magic, True, dispatch_msg, initializer)