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

« back to all changes in this revision

Viewing changes to ivle/console.py

  • Committer: William Grant
  • Date: 2010-07-27 12:09:13 UTC
  • mto: This revision was merged to the branch mainline in revision 1826.
  • Revision ID: grantw@unimelb.edu.au-20100727120913-v0kfnwxzbiwrjnue
(simple)json always returns a unicode when decoding, while cjson returned a str where possible. This makes cPickle unhappy, so convert back to a str.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
import StringIO
31
31
import uuid
32
32
 
33
 
import cjson
34
 
 
35
 
from ivle import chat
 
33
from ivle import chat, interpret
36
34
 
37
35
class ConsoleError(Exception):
38
36
    """ The console failed in some way. This is bad. """
108
106
class Console(object):
109
107
    """ Provides a nice python interface to the console
110
108
    """
111
 
    def __init__(self, config, uid, jail_path, working_dir):
 
109
    def __init__(self, config, user, jail_path, working_dir):
112
110
        """Starts up a console service for user uid, inside chroot jail 
113
111
        jail_path with work directory of working_dir
114
112
        """
115
113
        super(Console, self).__init__()
116
114
 
117
115
        self.config = config
118
 
        self.uid = uid
 
116
        self.user = user
119
117
        self.jail_path = jail_path
120
118
        self.working_dir = working_dir
121
119
 
152
150
        # is (k / N) ** t (e.g. 3.2*10e-9).
153
151
 
154
152
        tries = 0
 
153
        error = None
155
154
        while tries < 5:
156
155
            self.port = int(random.uniform(3000, 8000))
157
156
 
158
 
            trampoline_path = os.path.join(self.config['paths']['lib'],
159
 
                                           "trampoline")
160
 
 
161
 
            # Start the console server (port, magic)
162
 
            # trampoline usage: tramp uid jail_dir working_dir script_path args
163
 
            # console usage:    python-console port magic
164
 
            res = os.spawnv(os.P_WAIT, trampoline_path, [
165
 
                trampoline_path,
166
 
                str(self.uid),
167
 
                self.config['paths']['jails']['mounts'],
168
 
                self.config['paths']['jails']['src'],
169
 
                self.config['paths']['jails']['template'],
170
 
                self.jail_path,
171
 
                os.path.join(self.config['paths']['share'], 'services'),
172
 
                "/usr/bin/python",
173
 
                os.path.join(self.config['paths']['share'],
174
 
                             'services/python-console'),
175
 
                str(self.port),
176
 
                str(self.magic),
177
 
                self.working_dir
178
 
                ])
179
 
 
180
 
            if res == 0:
 
157
            python_console = os.path.join(self.config['paths']['share'],
 
158
                        'services/python-console')
 
159
            args = [python_console, str(self.port), str(self.magic)]
 
160
 
 
161
            try:
 
162
                interpret.execute_raw(self.config, self.user, self.jail_path,
 
163
                        self.working_dir, "/usr/bin/python", args)
181
164
                # success
182
 
                break;
183
 
 
184
 
            tries += 1
185
 
 
186
 
        # If we can't start the console after 5 attemps (can't find a free port 
187
 
        # during random probing, syntax errors, segfaults) throw an exception.
 
165
                break
 
166
            except interpret.ExecutionError, e:
 
167
                tries += 1
 
168
                error = e.message
 
169
 
 
170
        # If we can't start the console after 5 attemps (can't find a free 
 
171
        # port during random probing, syntax errors, segfaults) throw an 
 
172
        # exception.
188
173
        if tries == 5:
189
 
            raise ConsoleError("Unable to start console service!")
 
174
            raise ConsoleError('Unable to start console service: %s'%error)
190
175
 
191
176
    def __chat(self, cmd, args):
192
177
        """ A wrapper around chat.chat to comunicate directly with the 
203
188
            else:
204
189
                # Some other error - probably serious
205
190
                raise socket.error, (enumber, estring)
206
 
        except cjson.DecodeError:
 
191
        except ValueError:
207
192
            # Couldn't decode the JSON
208
193
            raise ConsoleError(
209
194
                "Could not understand the python console response")
262
247
 
263
248
        # Unpickle the globals
264
249
        for g in globals['globals']:
265
 
            globals['globals'][g] = cPickle.loads(globals['globals'][g])
 
250
            globals['globals'][g] = cPickle.loads(str(globals['globals'][g]))
266
251
 
267
252
        return globals['globals']
268
253
        
281
266
        # Unpickle any exceptions
282
267
        if 'exception' in call:
283
268
            call['exception']['except'] = \
284
 
                cPickle.loads(call['exception']['except'])
 
269
                cPickle.loads(str(call['exception']['except']))
285
270
 
286
271
        return call
287
272
 
293
278
              
294
279
        # Unpickle any exceptions
295
280
        if 'exception' in execute:
296
 
            execute['exception'] = cPickle.loads(execute['exception'])
 
281
            execute['exception'] = cPickle.loads(str(execute['exception']))
297
282
        return execute
298
283
 
299
284