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

« back to all changes in this revision

Viewing changes to ivle/console.py

  • Committer: Matt Giuca
  • Date: 2010-07-21 04:21:50 UTC
  • Revision ID: matt.giuca@gmail.com-20100721042150-qovg2sth81sgbdq7
Project page: Replaced the confusing title 'Assigned submitters' with 'Expected submitters', and the heading 'assigned' with 'name'.

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. """
108
108
class Console(object):
109
109
    """ Provides a nice python interface to the console
110
110
    """
111
 
    def __init__(self, config, uid, jail_path, working_dir):
 
111
    def __init__(self, config, user, jail_path, working_dir):
112
112
        """Starts up a console service for user uid, inside chroot jail 
113
113
        jail_path with work directory of working_dir
114
114
        """
115
115
        super(Console, self).__init__()
116
116
 
117
117
        self.config = config
118
 
        self.uid = uid
 
118
        self.user = user
119
119
        self.jail_path = jail_path
120
120
        self.working_dir = working_dir
121
121
 
152
152
        # is (k / N) ** t (e.g. 3.2*10e-9).
153
153
 
154
154
        tries = 0
 
155
        error = None
155
156
        while tries < 5:
156
157
            self.port = int(random.uniform(3000, 8000))
157
158
 
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:
 
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)
181
166
                # 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.
 
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.
188
175
        if tries == 5:
189
 
            raise ConsoleError("Unable to start console service!")
 
176
            raise ConsoleError('Unable to start console service: %s'%error)
190
177
 
191
178
    def __chat(self, cmd, args):
192
179
        """ A wrapper around chat.chat to comunicate directly with the