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

« back to all changes in this revision

Viewing changes to ivle/console.py

  • Committer: William Grant
  • Date: 2010-02-23 08:55:42 UTC
  • mto: This revision was merged to the branch mainline in revision 1674.
  • Revision ID: grantw@unimelb.edu.au-20100223085542-r8xw14bxxoraza51
Permit underscores in all names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
import errno
25
25
import cPickle
26
 
import md5
 
26
import hashlib
27
27
import os
28
28
import random
29
29
import socket
32
32
 
33
33
import cjson
34
34
 
35
 
import ivle.conf
36
 
from ivle import (chat, util)
37
 
 
38
 
# Outside Jail
39
 
trampoline_path = os.path.join(ivle.conf.lib_path, "trampoline")
40
 
# Inside Jail
41
 
python_path = "/usr/bin/python"
42
 
console_dir = os.path.join(ivle.conf.share_path, 'services')
43
 
console_path = os.path.join(console_dir, 'python-console')
 
35
from ivle import chat
44
36
 
45
37
class ConsoleError(Exception):
46
38
    """ The console failed in some way. This is bad. """
47
 
    def __init__(self, value):
48
 
        self.value = value
49
 
    def __str__(self):
50
 
        return repr(self.value)
 
39
    pass
51
40
 
52
41
class ConsoleException(Exception):
53
42
    """ The code being exectuted on the console returned an exception. 
54
43
    """
55
 
    def __init__(self, value):
56
 
        self.value = value
57
 
    def __str__(self):
58
 
        return repr(self.value)
 
44
    pass
59
45
 
60
46
class TruncateStringIO(StringIO.StringIO):
61
47
    """ A class that wraps around StringIO and truncates the buffer when the 
122
108
class Console(object):
123
109
    """ Provides a nice python interface to the console
124
110
    """
125
 
    def __init__(self, uid, jail_path, working_dir):
 
111
    def __init__(self, config, uid, jail_path, working_dir):
126
112
        """Starts up a console service for user uid, inside chroot jail 
127
113
        jail_path with work directory of working_dir
128
114
        """
129
115
        super(Console, self).__init__()
130
116
 
 
117
        self.config = config
131
118
        self.uid = uid
132
119
        self.jail_path = jail_path
133
120
        self.working_dir = working_dir
154
141
 
155
142
        # Create magic
156
143
        # TODO
157
 
        self.magic = md5.new(uuid.uuid4().bytes).digest().encode('hex')
 
144
        self.magic = hashlib.md5(uuid.uuid4().bytes).hexdigest()
158
145
 
159
146
        # Try to find a free port on the server.
160
147
        # Just try some random ports in the range [3000,8000)
168
155
        while tries < 5:
169
156
            self.port = int(random.uniform(3000, 8000))
170
157
 
 
158
            trampoline_path = os.path.join(self.config['paths']['lib'],
 
159
                                           "trampoline")
 
160
 
171
161
            # Start the console server (port, magic)
172
162
            # trampoline usage: tramp uid jail_dir working_dir script_path args
173
163
            # console usage:    python-console port magic
174
164
            res = os.spawnv(os.P_WAIT, trampoline_path, [
175
165
                trampoline_path,
176
166
                str(self.uid),
177
 
                ivle.conf.jail_base,
178
 
                ivle.conf.jail_src_base,
179
 
                ivle.conf.jail_system,
 
167
                self.config['paths']['jails']['mounts'],
 
168
                self.config['paths']['jails']['src'],
 
169
                self.config['paths']['jails']['template'],
180
170
                self.jail_path,
181
 
                console_dir,
182
 
                python_path,
183
 
                console_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'),
184
175
                str(self.port),
185
176
                str(self.magic),
186
177
                self.working_dir
216
207
            # Couldn't decode the JSON
217
208
            raise ConsoleError(
218
209
                "Could not understand the python console response")
 
210
        except chat.ProtocolError, e:
 
211
            raise ConsoleError(*e.args)
219
212
 
220
213
        return response
221
214
 
300
293
              
301
294
        # Unpickle any exceptions
302
295
        if 'exception' in execute:
303
 
            return cPickle.loads(execute['exception'])
304
 
        else:
305
 
            return execute
 
296
            execute['exception'] = cPickle.loads(execute['exception'])
 
297
        return execute
306
298
 
307
299
 
308
300
    def set_vars(self, variables):