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

« back to all changes in this revision

Viewing changes to ivle/webapp/console/service.py

Fix circular import in ivle.zip.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
'''
25
25
 
26
 
import cStringIO
27
 
import md5
28
26
import os
29
 
import random
30
27
import socket
31
 
import sys
32
 
import uuid
33
28
 
34
29
import cjson
35
30
import errno
36
31
 
37
 
from ivle import (util, studpath, chat, console)
 
32
import ivle.console
 
33
import ivle.chat
38
34
import ivle.conf
39
 
from ivle.webapp.base.views import JSONRESTView, named_operation
 
35
from ivle.webapp.base.rest import JSONRESTView, named_operation
 
36
from ivle.webapp.errors import BadRequest
40
37
 
41
38
# XXX: Should be RPC view, with actions in URL?
42
39
class ConsoleServiceRESTView(JSONRESTView):
43
40
    '''An RPC interface to a Python console.'''
44
 
    @named_operation
 
41
    def get_permissions(self, user):
 
42
        if user is not None:
 
43
            return set(['use'])
 
44
        else:
 
45
            return set()
 
46
 
 
47
    @named_operation('use')
45
48
    def start(self, req, cwd=''):
46
49
        working_dir = os.path.join("/home", req.user.login, cwd)
47
50
 
49
52
 
50
53
        # Start the server
51
54
        jail_path = os.path.join(ivle.conf.jail_base, req.user.login)
52
 
        cons = console.Console(uid, jail_path, working_dir)
 
55
        cons = ivle.console.Console(uid, jail_path, working_dir)
53
56
 
54
57
        # Assemble the key and return it. Yes, it is double-encoded.
55
58
        return {'key': cjson.encode({"host": cons.host,
56
59
                                     "port": cons.port,
57
60
                                     "magic": cons.magic}).encode('hex')}
58
61
 
59
 
    @named_operation
 
62
    @named_operation('use')
60
63
    def chat(self, req, key, text='', kind="chat"):
61
64
        # The request *should* have the following four fields:
62
65
        # key: Hex JSON dict of host and port where the console server lives,
79
82
 
80
83
        msg = {'cmd':kind, 'text':text}
81
84
        try:
82
 
            json_response = chat.chat(host, port, msg, magic, decode = False)
 
85
            json_response = ivle.chat.chat(host, port, msg, magic,decode=False)
83
86
 
84
87
            # Snoop the response from python-console to check that it's valid
85
88
            try:
105
108
                raise socket.error, (enumber, estring)
106
109
        return response
107
110
 
108
 
def handle(req):
109
 
    """Handler for the Console Service AJAX backend application."""
110
 
    if len(req.path) > 0 and req.path[-1] == os.sep:
111
 
        path = req.path[:-1]
112
 
    else:
113
 
        path = req.path
114
 
    # The path determines which "command" we are receiving
115
 
    if req.path == "start":
116
 
        handle_start(req)
117
 
    elif req.path == "interrupt":
118
 
        handle_chat(req, kind='interrupt')
119
 
    elif req.path == "restart":
120
 
        handle_chat(req, kind='terminate')
121
 
    elif req.path == "chat":
122
 
        handle_chat(req)
123
 
    elif req.path == "block":
124
 
        handle_chat(req, kind="block")
125
 
    elif req.path == "flush":
126
 
        handle_chat(req, kind="flush")
127
 
    elif req.path == "inspect":
128
 
        handle_chat(req, kind="inspect")
129
 
    else:
130
 
        req.throw_error(req.HTTP_BAD_REQUEST)
131
 
 
132
 
 
133
111
 
134
112
def restart_console(uid, jail_path, working_dir, reason):
135
113
    """Tells the client that it must be issued a new console since the old 
137
115
    Returns the JSON response to be given to the client.
138
116
    """
139
117
    # Start a new console server console
140
 
    cons = console.Console(uid, jail_path, working_dir)
 
118
    cons = ivle.console.Console(uid, jail_path, working_dir)
141
119
 
142
120
    # Make a JSON object to tell the browser to restart its console client
143
121
    new_key = cjson.encode(