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

159 by mattgiuca
fileservice: Split growing module into three modules: top-level,
1
# IVLE
2
# Copyright (C) 2007-2008 The University of Melbourne
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18
# Module: File Service / Listing
19
# Author: Matt Giuca
20
# Date: 10/1/2008
21
22
# Handles the return part of the 2-stage process of fileservice. This
23
# is both the directory listing, and the raw serving of non-directory files.
24
25
# File Service Format.
26
# If a non-directory file is requested, then the HTTP response body will be
27
# the verbatim bytes of that file (if the file is valid). The HTTP response
28
# headers will include the guessed content type of the file, and the header
29
# "X-IVLE-Return: File".
30
31
# Directory Listing Format.
32
# If the path requested is a directory, then the HTTP response body will be
33
# a valid JSON string describing the directory. The HTTP response headers
34
# will include the header "X-IVLE-Return: Dir".
35
#
36
# The JSON structure is as follows:
162 by mattgiuca
fileservice: Modified the listing output. Now there is an extra object
37
# * The top-level value is an object. It always contains the key "listing",
38
# whose value is the primary listing object. It may also contain a key
39
# "clipboard" which contains the clipboard object.
40
# * The value for "listing" is an object, with one member for each file in the
159 by mattgiuca
fileservice: Split growing module into three modules: top-level,
41
#   directory, plus an additional member (key ".") for the directory itself.
42
# * Each member's key is the filename. Its value is an object, which has
43
#   various members describing the file.
44
# The members of this object are as follows:
45
#   * svnstatus: String. The svn status of the file. Either all files in a
46
#   directory or no files have an svnstatus. String may take the values:
47
#   - none - does not exist
48
#   - unversioned - is not a versioned thing in this wc
49
#   - normal - exists, but uninteresting.
50
#   - added - is scheduled for addition
51
#   - missing - under v.c., but is missing
52
#   - deleted - scheduled for deletion
53
#   - replaced - was deleted and then re-added
54
#   - modified - text or props have been modified
55
#   - merged - local mods received repos mods
56
#   - conflicted - local mods received conflicting repos mods
57
#   - ignored - a resource marked as ignored
58
#   - obstructed - an unversioned resource is in the way of the versioned resource
59
#   - external - an unversioned path populated by an svn:external property
60
#   - incomplete - a directory doesn't contain a complete entries list
61
#   (From pysvn)
62
#   If svnstatus is "Missing" then the file has no other attributes.
63
#   * isdir: Boolean. True if the file is a directory. Always present unless
64
#   svnstatus is "missing".
65
#   * size: Number. Size of the file in bytes. Present for non-directory
66
#   files.
67
#   * type: String. Guessed mime type of the file. Present for non-directory
68
#   files.
179 by mattgiuca
fileservice: Added an extra field to the directory listing: mtime_nice - a
69
#   * mtime: Number. Number of seconds elapsed since the epoch.
70
#   The epoch is not defined (this is an arbitrary number used for sorting
71
#   dates).
72
#   * mtime_nice: String. Modification time of the file or directory. Always
159 by mattgiuca
fileservice: Split growing module into three modules: top-level,
73
#   present unless svnstatus is "Missing". Human-friendly.
74
#
75
# Members are not guaranteed to be present - client code should always check
76
# for each member that it is present, and handle gracefully if a member is not
77
# present.
78
#
79
# The listing object is guaranteed to have a "." key. Use this key to
80
# determine whether the directory is under version control or not. If this
81
# member does NOT have a "svnstatus" key, or "svnstatus" is "unversioned",
82
# then the directory is not under revision control (and no other files will
83
# have "svnstatus" either).
162 by mattgiuca
fileservice: Modified the listing output. Now there is an extra object
84
#
85
# The top-level object MAY contain a "clipboard" key, which specifies the
86
# files copied to the clipboard. This can be used by the client to show the
87
# user what files will be pasted. At the very least, the client should take
88
# the presence or absence of a "clipboard" key as whether to grey out the
89
# "paste" button.
90
#
91
# The "clipboard" object has three members:
92
#   * mode: String. Either "copy" or "cut".
93
#   * base: String. Path relative to the user's root. The common path between
94
#   the files.
95
#   * files: Array of Strings. Each element is a filename relative to base.
96
#   Base and files exactly correspond to the listing path and argument paths
97
#   which were supplied during the last copy or cut request.
159 by mattgiuca
fileservice: Split growing module into three modules: top-level,
98
99
import os
100
import stat
101
import time
102
import mimetypes
103
104
import cjson
105
import pysvn
106
107
from common import (util, studpath)
108
import conf.mimetypes
109
166 by mattgiuca
fileservice:
110
# Make a Subversion client object
111
svnclient = pysvn.Client()
112
159 by mattgiuca
fileservice: Split growing module into three modules: top-level,
113
# Mime types
114
# application/json is the "best" content type but is not good for
115
# debugging because Firefox just tries to download it
116
mime_dirlisting = "text/html"
117
#mime_dirlisting = "application/json"
118
166 by mattgiuca
fileservice:
119
def handle_return(req):
159 by mattgiuca
fileservice: Split growing module into three modules: top-level,
120
    """Perform the "return" part of the response.
121
    This function returns the file or directory listing contained in
122
    req.path. Sets the HTTP response code in req, writes additional headers,
123
    and writes the HTTP response, if any."""
124
125
    (user, path) = studpath.url_to_local(req.path)
126
127
    # FIXME: What to do about req.path == ""?
128
    # Currently goes to 403 Forbidden.
129
    if path is None:
130
        req.status = req.HTTP_FORBIDDEN
131
        req.headers_out['X-IVLE-Return-Error'] = 'Forbidden'
132
        req.write("Forbidden")
133
    elif not os.access(path, os.R_OK):
134
        req.status = req.HTTP_NOT_FOUND
135
        req.headers_out['X-IVLE-Return-Error'] = 'File not found'
136
        req.write("File not found")
137
    elif os.path.isdir(path):
138
        # It's a directory. Return the directory listing.
139
        req.content_type = mime_dirlisting
140
        req.headers_out['X-IVLE-Return'] = 'Dir'
162 by mattgiuca
fileservice: Modified the listing output. Now there is an extra object
141
        req.write(cjson.encode(get_dirlisting(req, svnclient, path)))
159 by mattgiuca
fileservice: Split growing module into three modules: top-level,
142
    else:
143
        # It's a file. Return the file contents.
144
        # First get the mime type of this file
145
        # (Note that importing common.util has already initialised mime types)
146
        (type, _) = mimetypes.guess_type(path)
147
        if type is None:
148
            type = conf.mimetypes.default_mimetype
149
        req.content_type = type
150
        req.headers_out['X-IVLE-Return'] = 'File'
151
152
        req.sendfile(path)
153
162 by mattgiuca
fileservice: Modified the listing output. Now there is an extra object
154
def get_dirlisting(req, svnclient, path):
160 by mattgiuca
fileservice: Abstracted get_dirlisting into a separate function.
155
    """Given a local absolute path, creates a directory listing object
162 by mattgiuca
fileservice: Modified the listing output. Now there is an extra object
156
    ready to be JSONized and sent to the client.
157
158
    req: Request object. Will not be mutated; just reads the session.
159
    svnclient: Svn client object.
160
    path: String. Absolute path on the local file system. Not checked,
161
        must already be guaranteed safe.
162
    """
160 by mattgiuca
fileservice: Abstracted get_dirlisting into a separate function.
163
    # Start by trying to do an SVN status, so we can report file version
164
    # status
165
    listing = {}
166
    try:
167
        status_list = svnclient.status(path, recurse=False, get_all=True,
168
                        update=False)
169
        for status in status_list:
170
            filename, attrs = PysvnStatus_to_fileinfo(path, status)
171
            listing[filename] = attrs
172
    except pysvn.ClientError:
173
        # Presumably the directory is not under version control.
174
        # Fallback to just an OS file listing.
175
        for filename in os.listdir(path):
176
            listing[filename] = file_to_fileinfo(path, filename)
177
        # The subversion one includes "." while the OS one does not.
178
        # Add "." to the output, so the caller can see we are
179
        # unversioned.
179 by mattgiuca
fileservice: Added an extra field to the directory listing: mtime_nice - a
180
        mtime = os.path.getmtime(path)
160 by mattgiuca
fileservice: Abstracted get_dirlisting into a separate function.
181
        listing["."] = {"isdir" : True,
179 by mattgiuca
fileservice: Added an extra field to the directory listing: mtime_nice - a
182
            "mtime" : mtime, "mtime_nice" : time.ctime(mtime)}
162 by mattgiuca
fileservice: Modified the listing output. Now there is an extra object
183
184
    # Listing is a nested object inside the top-level JSON.
185
    listing = {"listing" : listing}
186
187
    # The other object is the clipboard, if present in the browser session.
188
    # This can go straight from the session to JSON.
189
    session = req.get_session()
190
    try:
191
        listing['clipboard'] = session['clipboard']
192
    except KeyError:
193
        pass
194
    
160 by mattgiuca
fileservice: Abstracted get_dirlisting into a separate function.
195
    return listing
196
159 by mattgiuca
fileservice: Split growing module into three modules: top-level,
197
def file_to_fileinfo(path, filename):
198
    """Given a filename (relative to a given path), gets all the info "ls"
199
    needs to display about the filename. Returns a dict containing a number
200
    of fields related to the file (excluding the filename itself)."""
201
    fullpath = os.path.join(path, filename)
202
    d = {}
203
    file_stat = os.stat(fullpath)
204
    if stat.S_ISDIR(file_stat.st_mode):
205
        d["isdir"] = True
206
    else:
207
        d["isdir"] = False
208
        d["size"] = file_stat.st_size
209
        (type, _) = mimetypes.guess_type(filename)
210
        if type is None:
211
            type = conf.mimetypes.default_mimetype
212
        d["type"] = type
179 by mattgiuca
fileservice: Added an extra field to the directory listing: mtime_nice - a
213
    d["mtime"] = file_stat.st_mtime
214
    d["mtime_nice"] = time.ctime(file_stat.st_mtime)
159 by mattgiuca
fileservice: Split growing module into three modules: top-level,
215
    return d
216
217
def PysvnStatus_to_fileinfo(path, status):
218
    """Given a PysvnStatus object, gets all the info "ls"
219
    needs to display about the filename. Returns a pair mapping filename to
220
    a dict containing a number of other fields."""
221
    path = os.path.normcase(path)
222
    fullpath = status.path
223
    # If this is "." (the directory itself)
224
    if path == os.path.normcase(fullpath):
225
        # If this directory is unversioned, then we aren't
226
        # looking at any interesting files, so throw
227
        # an exception and default to normal OS-based listing. 
228
        if status.text_status == pysvn.wc_status_kind.unversioned:
229
            raise pysvn.ClientError
230
        # We actually want to return "." because we want its
231
        # subversion status.
232
        filename = "."
233
    else:
234
        filename = os.path.basename(fullpath)
235
    d = {}
236
    text_status = status.text_status
237
    d["svnstatus"] = str(text_status)
238
    try:
239
        file_stat = os.stat(fullpath)
240
        if stat.S_ISDIR(file_stat.st_mode):
241
            d["isdir"] = True
242
        else:
243
            d["isdir"] = False
244
            d["size"] = file_stat.st_size
245
            (type, _) = mimetypes.guess_type(fullpath)
246
            if type is None:
247
                type = conf.mimetypes.default_mimetype
248
            d["type"] = type
179 by mattgiuca
fileservice: Added an extra field to the directory listing: mtime_nice - a
249
        d["mtime"] = file_stat.st_mtime
250
        d["mtime_nice"] = time.ctime(file_stat.st_mtime)
159 by mattgiuca
fileservice: Split growing module into three modules: top-level,
251
    except OSError:
252
        # Here if, eg, the file is missing.
253
        # Can't get any more information so just return d
254
        pass
255
    return filename, d