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

« back to all changes in this revision

Viewing changes to ivle/console.py

Merge from object-publishing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
import StringIO
31
31
import uuid
32
32
 
33
 
from ivle import chat, interpret
 
33
import cjson
 
34
 
 
35
from ivle import chat
34
36
 
35
37
class ConsoleError(Exception):
36
38
    """ The console failed in some way. This is bad. """
37
 
    pass
 
39
    def __init__(self, value):
 
40
        self.value = value
 
41
    def __str__(self):
 
42
        return repr(self.value)
38
43
 
39
44
class ConsoleException(Exception):
40
45
    """ The code being exectuted on the console returned an exception. 
41
46
    """
42
 
    pass
 
47
    def __init__(self, value):
 
48
        self.value = value
 
49
    def __str__(self):
 
50
        return repr(self.value)
43
51
 
44
52
class TruncateStringIO(StringIO.StringIO):
45
53
    """ A class that wraps around StringIO and truncates the buffer when the 
106
114
class Console(object):
107
115
    """ Provides a nice python interface to the console
108
116
    """
109
 
    def __init__(self, config, user, jail_path, working_dir):
 
117
    def __init__(self, config, uid, jail_path, working_dir):
110
118
        """Starts up a console service for user uid, inside chroot jail 
111
119
        jail_path with work directory of working_dir
112
120
        """
113
121
        super(Console, self).__init__()
114
122
 
115
123
        self.config = config
116
 
        self.user = user
 
124
        self.uid = uid
117
125
        self.jail_path = jail_path
118
126
        self.working_dir = working_dir
119
127
 
150
158
        # is (k / N) ** t (e.g. 3.2*10e-9).
151
159
 
152
160
        tries = 0
153
 
        error = None
154
161
        while tries < 5:
155
162
            self.port = int(random.uniform(3000, 8000))
156
163
 
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)
 
164
            trampoline_path = os.path.join(self.config['paths']['lib'],
 
165
                                           "trampoline")
 
166
 
 
167
            # Start the console server (port, magic)
 
168
            # trampoline usage: tramp uid jail_dir working_dir script_path args
 
169
            # console usage:    python-console port magic
 
170
            res = os.spawnv(os.P_WAIT, trampoline_path, [
 
171
                trampoline_path,
 
172
                str(self.uid),
 
173
                self.config['paths']['jails']['mounts'],
 
174
                self.config['paths']['jails']['src'],
 
175
                self.config['paths']['jails']['template'],
 
176
                self.jail_path,
 
177
                os.path.join(self.config['paths']['share'], 'services'),
 
178
                "/usr/bin/python",
 
179
                os.path.join(self.config['paths']['share'],
 
180
                             'services/python-console'),
 
181
                str(self.port),
 
182
                str(self.magic),
 
183
                self.working_dir
 
184
                ])
 
185
 
 
186
            if res == 0:
164
187
                # success
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
                break;
 
189
 
 
190
            tries += 1
 
191
 
 
192
        # If we can't start the console after 5 attemps (can't find a free port 
 
193
        # during random probing, syntax errors, segfaults) throw an exception.
173
194
        if tries == 5:
174
 
            raise ConsoleError('Unable to start console service: %s'%error)
 
195
            raise ConsoleError("Unable to start console service!")
175
196
 
176
197
    def __chat(self, cmd, args):
177
198
        """ A wrapper around chat.chat to comunicate directly with the 
188
209
            else:
189
210
                # Some other error - probably serious
190
211
                raise socket.error, (enumber, estring)
191
 
        except ValueError:
 
212
        except cjson.DecodeError:
192
213
            # Couldn't decode the JSON
193
214
            raise ConsoleError(
194
215
                "Could not understand the python console response")
195
 
        except chat.ProtocolError, e:
196
 
            raise ConsoleError(*e.args)
197
216
 
198
217
        return response
199
218
 
247
266
 
248
267
        # Unpickle the globals
249
268
        for g in globals['globals']:
250
 
            globals['globals'][g] = cPickle.loads(str(globals['globals'][g]))
 
269
            globals['globals'][g] = cPickle.loads(globals['globals'][g])
251
270
 
252
271
        return globals['globals']
253
272
        
266
285
        # Unpickle any exceptions
267
286
        if 'exception' in call:
268
287
            call['exception']['except'] = \
269
 
                cPickle.loads(str(call['exception']['except']))
 
288
                cPickle.loads(call['exception']['except'])
270
289
 
271
290
        return call
272
291
 
278
297
              
279
298
        # Unpickle any exceptions
280
299
        if 'exception' in execute:
281
 
            execute['exception'] = cPickle.loads(str(execute['exception']))
282
 
        return execute
 
300
            return cPickle.loads(execute['exception'])
 
301
        else:
 
302
            return execute
283
303
 
284
304
 
285
305
    def set_vars(self, variables):