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

« back to all changes in this revision

Viewing changes to ivle/console.py

  • Committer: David Coles
  • Date: 2010-07-27 04:52:14 UTC
  • Revision ID: coles.david@gmail.com-20100727045214-p32h1kc0gcv48dpr
Worksheets: Strip off whitespace from the end of exercise attempts.

This solves an issue where accidental whitespace in an attempt will cause 
"IndentationError" syntax error (which don't occur when run in console).

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
 
33
33
import cjson
34
34
 
35
 
from ivle import chat
 
35
from ivle import chat, interpret
36
36
 
37
37
class ConsoleError(Exception):
38
38
    """ The console failed in some way. This is bad. """
39
 
    def __init__(self, value):
40
 
        self.value = value
41
 
    def __str__(self):
42
 
        return repr(self.value)
 
39
    pass
43
40
 
44
41
class ConsoleException(Exception):
45
42
    """ The code being exectuted on the console returned an exception. 
46
43
    """
47
 
    def __init__(self, value):
48
 
        self.value = value
49
 
    def __str__(self):
50
 
        return repr(self.value)
 
44
    pass
51
45
 
52
46
class TruncateStringIO(StringIO.StringIO):
53
47
    """ A class that wraps around StringIO and truncates the buffer when the 
114
108
class Console(object):
115
109
    """ Provides a nice python interface to the console
116
110
    """
117
 
    def __init__(self, config, uid, jail_path, working_dir):
 
111
    def __init__(self, config, user, jail_path, working_dir):
118
112
        """Starts up a console service for user uid, inside chroot jail 
119
113
        jail_path with work directory of working_dir
120
114
        """
121
115
        super(Console, self).__init__()
122
116
 
123
117
        self.config = config
124
 
        self.uid = uid
 
118
        self.user = user
125
119
        self.jail_path = jail_path
126
120
        self.working_dir = working_dir
127
121
 
158
152
        # is (k / N) ** t (e.g. 3.2*10e-9).
159
153
 
160
154
        tries = 0
 
155
        error = None
161
156
        while tries < 5:
162
157
            self.port = int(random.uniform(3000, 8000))
163
158
 
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:
 
159
            python_console = os.path.join(self.config['paths']['share'],
 
160
                        'services/python-console')
 
161
            args = [python_console, str(self.port), str(self.magic)]
 
162
 
 
163
            try:
 
164
                interpret.execute_raw(self.config, self.user, self.jail_path,
 
165
                        self.working_dir, "/usr/bin/python", args)
187
166
                # success
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.
 
167
                break
 
168
            except interpret.ExecutionError, e:
 
169
                tries += 1
 
170
                error = e.message
 
171
 
 
172
        # If we can't start the console after 5 attemps (can't find a free 
 
173
        # port during random probing, syntax errors, segfaults) throw an 
 
174
        # exception.
194
175
        if tries == 5:
195
 
            raise ConsoleError("Unable to start console service!")
 
176
            raise ConsoleError('Unable to start console service: %s'%error)
196
177
 
197
178
    def __chat(self, cmd, args):
198
179
        """ A wrapper around chat.chat to comunicate directly with the 
213
194
            # Couldn't decode the JSON
214
195
            raise ConsoleError(
215
196
                "Could not understand the python console response")
 
197
        except chat.ProtocolError, e:
 
198
            raise ConsoleError(*e.args)
216
199
 
217
200
        return response
218
201
 
297
280
              
298
281
        # Unpickle any exceptions
299
282
        if 'exception' in execute:
300
 
            return cPickle.loads(execute['exception'])
301
 
        else:
302
 
            return execute
 
283
            execute['exception'] = cPickle.loads(execute['exception'])
 
284
        return execute
303
285
 
304
286
 
305
287
    def set_vars(self, variables):