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

411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
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 / Action
19
# Author: Matt Giuca
20
# Date: 10/1/2008
21
22
# Handles actions requested by the client as part of the 2-stage process of
23
# fileservice (the second part being the return listing).
24
25
### Actions ###
26
27
# The most important argument is "action". This determines which action is
28
# taken. Note that action, and all other arguments, are ignored unless the
29
# request is a POST request. The other arguments depend upon the action.
30
# Note that paths are often specified as arguments. Paths that begin with a
31
# slash are taken relative to the user's home directory (the top-level
32
# directory seen when fileservice has no arguments or path). Paths without a
33
# slash are taken relative to the specified path.
34
35
# action=remove: Delete a file(s) or directory(s) (recursively).
36
#       path:   The path to the file or directory to delete. Can be specified
37
#               multiple times.
38
#
39
# action=move: Move or rename a file or directory.
40
#       from:   The path to the file or directory to be renamed.
41
#       to:     The path of the target filename. Error if the file already
42
#               exists.
43
#
787 by mattgiuca
fileservice_lib/action.py:
44
# action=putfile: Upload a file to the student workspace.
45
#       path:   The path to the file to be written. Error if the target
46
#               file is a directory.
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
47
#       data:   Bytes to be written to the file verbatim. May either be
48
#               a string variable or a file upload.
787 by mattgiuca
fileservice_lib/action.py:
49
#       overwrite: Optional. If supplied, the file will be overwritten.
50
#               Otherwise, error if path already exists.
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
51
#
52
# action=putfiles: Upload multiple files to the student workspace, and
53
#                 optionally accept zip files which will be unpacked.
54
#       path:   The path to the DIRECTORY to place files in. Must not be a
55
#               file.
56
#       data:   A file upload (may not be a simple string). The filename
57
#               will be used to determine the target filename within
58
#               the given path.
787 by mattgiuca
fileservice_lib/action.py:
59
#       unpack: Optional. If supplied, if any data is a valid ZIP file,
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
60
#               will create a directory instead and unpack the ZIP file
61
#               into it.
62
#
592 by mattgiuca
fileservice, browser/listing: Added new fileservice action, "mkdir".
63
# action=mkdir: Create a directory. The parent dir must exist.
64
#       path:   The path to a file which does not exist, but whose parent
65
#               does. The dir will be made with this name.
66
#
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
67
# The differences between putfile and putfiles are:
787 by mattgiuca
fileservice_lib/action.py:
68
# * putfile can only accept a single file, and can't unpack zipfiles.
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
69
# * putfile can accept string data, doesn't have to be a file upload.
70
# * putfile ignores the upload filename, the entire filename is specified on
71
#       path. putfiles calls files after the name on the user's machine.
72
#
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
73
# action=paste: Copy or move the files to a specified dir.
74
#       src:    The path to the DIRECTORY to get the files from (relative).
75
#       dst:    The path to the DIRECTORY to paste the files to. Must not
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
76
#               be a file.
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
77
#       mode:   'copy' or 'move'
78
#       file:   File to be copied or moved, relative to src, to a destination
79
#               relative to dst. Can be specified multiple times.
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
80
#
81
# Subversion actions.
82
# action=svnadd: Add an existing file(s) to version control.
83
#       path:   The path to the file to be added. Can be specified multiple
84
#               times.
85
#
86
# action=svnrevert: Revert a file(s) to its state as of the current revision
87
#               / undo local edits.
88
#       path:   The path to the file to be reverted. Can be specified multiple
89
#               times.
90
#
91
# action=svnupdate: Bring a file up to date with the head revision.
92
#       path:   The path to the file to be updated. Only one file may be
93
#               specified.
94
#
95
# action=svnpublish: Set the "published" flag on a file to True.
96
#       path:   The path to the file to be published. Can be specified
97
#               multiple times.
98
#
99
# action=svnunpublish: Set the "published" flag on a file to False.
100
#       path:   The path to the file to be unpublished. Can be specified
101
#               multiple times.
102
#
103
# action=svncommit: Commit a file(s) or directory(s) to the repository.
104
#       path:   The path to the file or directory to be committed. Can be
105
#               specified multiple times. Directories are committed
106
#               recursively.
107
#       logmsg: Text of the log message. Optional. There is a default log
108
#               message if unspecified.
522 by drtomc
Add quite a lot of stuff to get usrmgt happening.
109
# action=svncheckout: Checkout a file/directory from the repository.
110
#       path:   The [repository] path to the file or directory to be
111
#               checked out.
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
112
# 
113
# TODO: Implement the following actions:
787 by mattgiuca
fileservice_lib/action.py:
114
#   svnupdate (done?)
115
# TODO: Implement ZIP unpacking in putfiles (done?).
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
116
# TODO: svnupdate needs a digest to tell the user the files that were updated.
117
#   This can be implemented by some message passing between action and
118
#   listing, and having the digest included in the listing. (Problem if
119
#   the listing is not a directory, but we could make it an error to do an
120
#   update if the path is not a directory).
121
122
import os
436 by drtomc
fileservice: Make things less broken than they were. No claims of perfection yet! Unpacking form data with cgi.py is AWFUL.
123
import cStringIO
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
124
import shutil
125
126
import pysvn
127
128
from common import (util, studpath, zip)
522 by drtomc
Add quite a lot of stuff to get usrmgt happening.
129
import conf
130
783 by mattgiuca
usrmgt-server, fileservice_lib/action.py: Fixed SVN get_login callback.
131
def get_login(_realm, existing_login, _may_save):
132
    """Callback function used by pysvn for authentication.
133
    realm, existing_login, _may_save: The 3 arguments passed by pysvn to
134
        callback_get_login.
135
        The following has been determined empirically, not from docs:
136
        existing_login will be the name of the user who owns the process on
137
        the first attempt, "" on subsequent attempts. We use this fact.
138
    """
139
    # Only provide credentials on the _first_ attempt.
140
    # If we're being asked again, then it means the credentials failed for
141
    # some reason and we should just fail. (This is not desirable, but it's
142
    # better than being asked an infinite number of times).
143
    return (existing_login != "", conf.login, conf.svn_pass, True)
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
144
145
# Make a Subversion client object
146
svnclient = pysvn.Client()
522 by drtomc
Add quite a lot of stuff to get usrmgt happening.
147
svnclient.callback_get_login = get_login
786 by mattgiuca
fileservice_lib/action.py: Fixed SVN error messages. No longer just give a
148
svnclient.exception_style = 0               # Simple (string) exceptions
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
149
150
DEFAULT_LOGMESSAGE = "No log message supplied."
151
152
# Mime types
153
# application/json is the "best" content type but is not good for
154
# debugging because Firefox just tries to download it
155
mime_dirlisting = "text/html"
156
#mime_dirlisting = "application/json"
157
158
class ActionError(Exception):
159
    """Represents an error processing an action. This can be
160
    raised by any of the action functions, and will be caught
161
    by the top-level handler, put into the HTTP response field,
162
    and continue.
163
164
    Important Security Consideration: The message passed to this
165
    exception will be relayed to the client.
166
    """
167
    pass
168
169
def handle_action(req, action, fields):
170
    """Perform the "action" part of the response.
171
    This function should only be called if the response is a POST.
172
    This performs the action's side-effect on the server. If unsuccessful,
173
    writes the X-IVLE-Action-Error header to the request object. Otherwise,
174
    does not touch the request object. Does NOT write any bytes in response.
175
176
    May throw an ActionError. The caller should put this string into the
177
    X-IVLE-Action-Error header, and then continue normally.
178
179
    action: String, the action requested. Not sanitised.
180
    fields: FieldStorage object containing all arguments passed.
181
    """
182
    global actions_table        # Table of function objects
183
    try:
184
        action = actions_table[action]
185
    except KeyError:
186
        # Default, just send an error but then continue
187
        raise ActionError("Unknown action")
188
    action(req, fields)
189
190
def actionpath_to_urlpath(req, path):
191
    """Determines the URL path (relative to the student home) upon which the
192
    action is intended to act. See actionpath_to_local.
193
    """
194
    if path is None:
195
        return req.path
196
    elif len(path) > 0 and path[0] == os.sep:
197
        # Relative to student home
198
        return path[1:]
199
    else:
200
        # Relative to req.path
201
        return os.path.join(req.path, path)
202
203
def actionpath_to_local(req, path):
204
    """Determines the local path upon which an action is intended to act.
205
    Note that fileservice actions accept two paths: the request path,
206
    and the "path" argument given to the action.
207
    According to the rules, if the "path" argument begins with a '/' it is
208
    relative to the user's home; if it does not, it is relative to the
209
    supplied path.
210
211
    This resolves the path, given the request and path argument.
212
213
    May raise an ActionError("Invalid path"). The caller is expected to
214
    let this fall through to the top-level handler, where it will be
215
    put into the HTTP response field. Never returns None.
216
217
    Does not mutate req.
218
    """
436 by drtomc
fileservice: Make things less broken than they were. No claims of perfection yet! Unpacking form data with cgi.py is AWFUL.
219
    (_, _, r) = studpath.url_to_jailpaths(actionpath_to_urlpath(req, path))
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
220
    if r is None:
221
        raise ActionError("Invalid path")
222
    return r
223
224
def movefile(req, frompath, topath, copy=False):
225
    """Performs a file move, resolving filenames, checking for any errors,
226
    and throwing ActionErrors if necessary. Can also be used to do a copy
227
    operation instead.
228
229
    frompath and topath are straight paths from the client. Will be checked.
230
    """
231
    # TODO: Do an SVN mv if the file is versioned.
232
    # TODO: Disallow tampering with student's home directory
233
    if frompath is None or topath is None:
234
        raise ActionError("Required field missing")
235
    frompath = actionpath_to_local(req, frompath)
236
    topath = actionpath_to_local(req, topath)
237
    if not os.path.exists(frompath):
238
        raise ActionError("The source file does not exist")
239
    if os.path.exists(topath):
240
        if frompath == topath:
241
            raise ActionError("Source and destination are the same")
788 by mattgiuca
fileservice_lib: Fixed and made consistent the error message if a file already
242
        raise ActionError("A file already exists with that name")
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
243
244
    try:
245
        if copy:
246
            if os.path.isdir(frompath):
247
                shutil.copytree(frompath, topath)
248
            else:
249
                shutil.copy2(frompath, topath)
250
        else:
251
            shutil.move(frompath, topath)
252
    except OSError:
253
        raise ActionError("Could not move the file specified")
254
    except shutil.Error:
255
        raise ActionError("Could not move the file specified")
256
257
### ACTIONS ###
258
259
def action_remove(req, fields):
260
    # TODO: Do an SVN rm if the file is versioned.
261
    # TODO: Disallow removal of student's home directory
262
    """Removes a list of files or directories.
263
264
    Reads fields: 'path' (multiple)
265
    """
266
    paths = fields.getlist('path')
267
    goterror = False
268
    for path in paths:
269
        path = actionpath_to_local(req, path)
270
        try:
271
            if os.path.isdir(path):
272
                shutil.rmtree(path)
273
            else:
274
                os.remove(path)
275
        except OSError:
276
            goterror = True
277
        except shutil.Error:
278
            goterror = True
279
    if goterror:
280
        if len(paths) == 1:
281
            raise ActionError("Could not delete the file specified")
282
        else:
283
            raise ActionError(
284
                "Could not delete one or more of the files specified")
285
286
def action_move(req, fields):
287
    # TODO: Do an SVN mv if the file is versioned.
288
    # TODO: Disallow tampering with student's home directory
289
    """Removes a list of files or directories.
290
291
    Reads fields: 'from', 'to'
292
    """
293
    frompath = fields.getfirst('from')
294
    topath = fields.getfirst('to')
295
    movefile(req, frompath, topath)
296
592 by mattgiuca
fileservice, browser/listing: Added new fileservice action, "mkdir".
297
def action_mkdir(req, fields):
298
    """Creates a directory with the given path.
299
    Reads fields: 'path'
300
    """
301
    path = fields.getfirst('path')
302
    if path is None:
303
        raise ActionError("Required field missing")
304
    path = actionpath_to_local(req, path)
305
788 by mattgiuca
fileservice_lib: Fixed and made consistent the error message if a file already
306
    if os.path.exists(path):
307
        raise ActionError("A file already exists with that name")
308
592 by mattgiuca
fileservice, browser/listing: Added new fileservice action, "mkdir".
309
    # Create the directory
310
    try:
311
        os.mkdir(path)
312
    except OSError:
313
        raise ActionError("Could not create directory")
314
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
315
def action_putfile(req, fields):
316
    """Writes data to a file, overwriting it if it exists and creating it if
317
    it doesn't.
318
787 by mattgiuca
fileservice_lib/action.py:
319
    Reads fields: 'path', 'data' (file upload), 'overwrite'
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
320
    """
321
    # TODO: Read field "unpack".
322
    # Important: Data is "None" if the file submitted is empty.
323
    path = fields.getfirst('path')
324
    data = fields.getfirst('data')
611 by mattgiuca
"New File" now works. (This is a MUCH better replacement for having to go to
325
    if path is None:
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
326
        raise ActionError("Required field missing")
611 by mattgiuca
"New File" now works. (This is a MUCH better replacement for having to go to
327
    if data is None:
328
        # Workaround - field reader treats "" as None, so this is the only
329
        # way to allow blank file uploads
330
        data = ""
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
331
    path = actionpath_to_local(req, path)
436 by drtomc
fileservice: Make things less broken than they were. No claims of perfection yet! Unpacking form data with cgi.py is AWFUL.
332
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
333
    if data is not None:
436 by drtomc
fileservice: Make things less broken than they were. No claims of perfection yet! Unpacking form data with cgi.py is AWFUL.
334
        data = cStringIO.StringIO(data)
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
335
787 by mattgiuca
fileservice_lib/action.py:
336
    overwrite = fields.getfirst('overwrite')
337
    if overwrite is None:
338
        overwrite = False
339
    else:
340
        overwrite = True
341
790 by mattgiuca
fileservice_lib/action.py: Putfile (in overwrite mode) now gives an error if
342
    if overwrite:
343
        # Overwrite files; but can't if it's a directory
344
        if os.path.isdir(path):
345
            raise ActionError("A directory already exists "
346
                    + "with that name")
347
    else:
787 by mattgiuca
fileservice_lib/action.py:
348
        if os.path.exists(path):
788 by mattgiuca
fileservice_lib: Fixed and made consistent the error message if a file already
349
            raise ActionError("A file already exists with that name")
787 by mattgiuca
fileservice_lib/action.py:
350
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
351
    # Copy the contents of file object 'data' to the path 'path'
352
    try:
353
        dest = open(path, 'wb')
354
        if data is not None:
355
            shutil.copyfileobj(data, dest)
841 by wagrant
filservice_lib now catches IOErrors (not just OSErrors) when uploading
356
    except (IOError, OSError), e:
357
        raise ActionError("Could not write to target file: %s" % e.strerror)
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
358
359
def action_putfiles(req, fields):
360
    """Writes data to one or more files in a directory, overwriting them if
361
    it they exist.
362
363
    Reads fields: 'path', 'data' (file upload, multiple), 'unpack'
364
    """
436 by drtomc
fileservice: Make things less broken than they were. No claims of perfection yet! Unpacking form data with cgi.py is AWFUL.
365
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
366
    # Important: Data is "None" if the file submitted is empty.
367
    path = fields.getfirst('path')
436 by drtomc
fileservice: Make things less broken than they were. No claims of perfection yet! Unpacking form data with cgi.py is AWFUL.
368
    data = fields['data']
369
    if type(data) != type([]):
370
        data = [data]
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
371
    unpack = fields.getfirst('unpack')
372
    if unpack is None:
373
        unpack = False
374
    else:
375
        unpack = True
376
    if path is None:
377
        raise ActionError("Required field missing")
378
    path = actionpath_to_urlpath(req, path)
379
    goterror = False
380
381
    for datum in data:
382
        # Each of the uploaded files
383
        filepath = os.path.join(path, datum.filename)
763 by mattgiuca
Zip file uploading now works.
384
        filedata = datum.file
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
385
386
        if unpack and datum.filename.lower().endswith(".zip"):
387
            # A zip file - unpack it instead of just copying
388
            # TODO: Use the magic number instead of file extension
389
            # Note: Just unzip into the current directory (ignore the
390
            # filename)
391
            try:
763 by mattgiuca
Zip file uploading now works.
392
                # First get the entire path (within jail)
393
                _, _, abspath = studpath.url_to_jailpaths(path)
394
                abspath = os.path.join(os.sep, abspath)
395
                zip.unzip(abspath, filedata)
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
396
            except (OSError, IOError):
397
                goterror = True
398
        else:
399
            # Not a zip file
436 by drtomc
fileservice: Make things less broken than they were. No claims of perfection yet! Unpacking form data with cgi.py is AWFUL.
400
            (_, _, filepath_local) = studpath.url_to_jailpaths(filepath)
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
401
            if filepath_local is None:
402
                raise ActionError("Invalid path")
403
404
            # Copy the contents of file object 'data' to the path 'path'
405
            try:
406
                dest = open(filepath_local, 'wb')
407
                if data is not None:
791 by mattgiuca
fileservice_lib/action.py: Fixed regression.
408
                    shutil.copyfileobj(filedata, dest)
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
409
            except OSError:
410
                goterror = True
411
412
    if goterror:
413
        if len(data) == 1:
414
            raise ActionError("Could not write to target file")
415
        else:
416
            raise ActionError(
417
                "Could not write to one or more of the target files")
418
419
def action_paste(req, fields):
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
420
    """Performs the copy or move action with the files specified.
421
    Copies/moves the files to the specified directory.
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
422
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
423
    Reads fields: 'src', 'dst', 'mode', 'file' (multiple).
424
    src: Base path that all the files are relative to (source).
425
    dst: Destination path to paste into.
426
    mode: 'copy' or 'move'.
427
    file: (Multiple) Files relative to base, which will be copied
428
        or moved to new locations relative to path.
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
429
    """
430
    errormsg = None
431
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
432
    dst = fields.getfirst('dst')
433
    src = fields.getfirst('src')
434
    mode = fields.getfirst('mode')
435
    files = fields.getlist('file')
436
    if dst is None or src is None or mode is None:
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
437
        raise ActionError("Required field missing")
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
438
    if mode == "copy":
439
        copy = True
440
    elif mode == "move":
441
        copy = False
442
    else:
443
        raise ActionError("Invalid mode (must be 'copy' or 'move')")
444
    dst_local = actionpath_to_local(req, dst)
445
    if not os.path.isdir(dst_local):
446
        raise ActionError("dst is not a directory")
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
447
448
    errorfiles = []
449
    for file in files:
450
        # The source must not be interpreted as relative to req.path
451
        # Add a slash (relative to top-level)
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
452
        if src[:1] != '/':
453
            src = '/' + src
454
        frompath = os.path.join(src, file)
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
455
        # The destination is found by taking just the basename of the file
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
456
        topath = os.path.join(dst, os.path.basename(file))
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
457
        try:
458
            movefile(req, frompath, topath, copy)
459
        except ActionError, message:
460
            # Store the error for later; we want to copy as many as possible
461
            if errormsg is None:
462
                errormsg = message
463
            else:
464
                # Multiple errors; generic message
465
                errormsg = "One or more files could not be pasted"
466
            # Add this file to errorfiles; it will be put back on the
467
            # clipboard for possible future pasting.
468
            errorfiles.append(file)
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
469
    if errormsg is not None:
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
470
        raise ActionError(errormsg)
471
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
472
    # XXX errorfiles contains a list of files that couldn't be pasted.
473
    # we currently do nothing with this.
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
474
726 by dcoles
Browser: Added code to allow directories to be published through the UI. Also
475
def action_publish(req,fields):
476
    """Marks the folder as published by adding a '.published' file to the 
477
    directory and ensuring that the parent directory permissions are correct
478
479
    Reads fields: 'path'
480
    """
481
    paths = fields.getlist('path')
482
    user = studpath.url_to_local(req.path)[0]
483
    homedir = "/home/%s" % user
484
    if len(paths):
485
        paths = map(lambda path: actionpath_to_local(req, path), paths)
486
    else:
487
        paths = [studpath.url_to_jailpaths(req.path)[2]]
488
489
    # Set all the dirs in home dir world browsable (o+r,o+x)
490
    #FIXME: Should really only do those in the direct path not all of the 
491
    # folders in a students home directory
492
    for root,dirs,files in os.walk(homedir):
493
        os.chmod(root, os.stat(root).st_mode|0005)
494
495
    try:
496
        for path in paths:
497
            if os.path.isdir(path):
498
                pubfile = open(os.path.join(path,'.published'),'w')
499
                pubfile.write("This directory is published\n")
500
                pubfile.close()
501
            else:
502
                raise ActionError("Can only publish directories")
503
    except OSError, e:
504
        raise ActionError("Directory could not be published")
505
506
def action_unpublish(req,fields):
507
    """Marks the folder as unpublished by removing a '.published' file in the 
508
    directory (if it exits). It does not change the permissions of the parent 
509
    directories.
510
511
    Reads fields: 'path'
512
    """
513
    paths = fields.getlist('path')
514
    if len(paths):
515
        paths = map(lambda path: actionpath_to_local(req, path), paths)
516
    else:
517
        paths = [studpath.url_to_jailpaths(req.path)[2]]
518
519
    try:
520
        for path in paths:
521
            if os.path.isdir(path):
522
                pubfile = os.path.join(path,'.published')
523
                if os.path.isfile(pubfile):
524
                    os.remove(pubfile)
525
            else:
526
                raise ActionError("Can only unpublish directories")
527
    except OSError, e:
528
        raise ActionError("Directory could not be unpublished")
529
530
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
531
def action_svnadd(req, fields):
532
    """Performs a "svn add" to each file specified.
533
534
    Reads fields: 'path' (multiple)
535
    """
536
    paths = fields.getlist('path')
537
    paths = map(lambda path: actionpath_to_local(req, path), paths)
538
539
    try:
540
        svnclient.add(paths, recurse=True, force=True)
786 by mattgiuca
fileservice_lib/action.py: Fixed SVN error messages. No longer just give a
541
    except pysvn.ClientError, e:
542
        raise ActionError(str(e))
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
543
544
def action_svnupdate(req, fields):
545
    """Performs a "svn update" to each file specified.
546
547
    Reads fields: 'path'
548
    """
549
    path = fields.getfirst('path')
550
    if path is None:
551
        raise ActionError("Required field missing")
552
    path = actionpath_to_local(req, path)
553
554
    try:
555
        svnclient.update(path, recurse=True)
786 by mattgiuca
fileservice_lib/action.py: Fixed SVN error messages. No longer just give a
556
    except pysvn.ClientError, e:
557
        raise ActionError(str(e))
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
558
559
def action_svnrevert(req, fields):
560
    """Performs a "svn revert" to each file specified.
561
562
    Reads fields: 'path' (multiple)
563
    """
564
    paths = fields.getlist('path')
565
    paths = map(lambda path: actionpath_to_local(req, path), paths)
566
567
    try:
568
        svnclient.revert(paths, recurse=True)
786 by mattgiuca
fileservice_lib/action.py: Fixed SVN error messages. No longer just give a
569
    except pysvn.ClientError, e:
570
        raise ActionError(str(e))
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
571
572
def action_svnpublish(req, fields):
573
    """Sets svn property "ivle:published" on each file specified.
574
    Should only be called on directories (only effective on directories
575
    anyway).
576
577
    Reads fields: 'path'
784 by mattgiuca
fileservice_lib/action.py: Comments that svnpublish and svnunpublish are
578
579
    XXX Currently unused by the client (calls action_publish instead, which
580
    has a completely different publishing model).
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
581
    """
582
    paths = fields.getlist('path')
633 by drtomc
publishing: Make publishing the current directory work.
583
    if len(paths):
584
        paths = map(lambda path: actionpath_to_local(req, path), paths)
585
    else:
586
        paths = [studpath.url_to_jailpaths(req.path)[2]]
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
587
588
    try:
589
        for path in paths:
590
            # Note: Property value doesn't matter
591
            svnclient.propset("ivle:published", "", path, recurse=False)
445 by drtomc
fileservice: Repair a couple of error messages suffering from cutandpastitis.
592
    except pysvn.ClientError, e:
593
        raise ActionError("Directory could not be published")
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
594
595
def action_svnunpublish(req, fields):
596
    """Deletes svn property "ivle:published" on each file specified.
597
598
    Reads fields: 'path'
784 by mattgiuca
fileservice_lib/action.py: Comments that svnpublish and svnunpublish are
599
600
    XXX Currently unused by the client (calls action_unpublish instead, which
601
    has a completely different publishing model).
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
602
    """
603
    paths = fields.getlist('path')
604
    paths = map(lambda path: actionpath_to_local(req, path), paths)
605
606
    try:
607
        for path in paths:
608
            svnclient.propdel("ivle:published", path, recurse=False)
786 by mattgiuca
fileservice_lib/action.py: Fixed SVN error messages. No longer just give a
609
    except pysvn.ClientError, e:
448 by mattgiuca
lib/fileservice_lib: Fixed a syntax error.
610
        raise ActionError("Directory could not be unpublished")
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
611
612
def action_svncommit(req, fields):
613
    """Performs a "svn commit" to each file specified.
614
615
    Reads fields: 'path' (multiple), 'logmsg' (optional)
616
    """
617
    paths = fields.getlist('path')
618
    paths = map(lambda path: actionpath_to_local(req, str(path)), paths)
619
    logmsg = str(fields.getfirst('logmsg', DEFAULT_LOGMESSAGE))
620
    if logmsg == '': logmsg = DEFAULT_LOGMESSAGE
621
622
    try:
623
        svnclient.checkin(paths, logmsg, recurse=True)
786 by mattgiuca
fileservice_lib/action.py: Fixed SVN error messages. No longer just give a
624
    except pysvn.ClientError, e:
625
        raise ActionError(str(e))
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
626
522 by drtomc
Add quite a lot of stuff to get usrmgt happening.
627
def action_svncheckout(req, fields):
628
    """Performs a "svn checkout" of each path specified.
629
630
    Reads fields: 'path'    (multiple)
631
    """
632
    paths = fields.getlist('path')
633
    if len(paths) != 2:
634
        raise ActionError("usage: svncheckout url local-path")
635
    url = conf.svn_addr + "/" + login + "/" + paths[0]
636
    local_path = actionpath_to_local(req, str(paths[1]))
637
    try:
638
        svnclient.callback_get_login = get_login
639
        svnclient.checkout(url, local_path, recurse=True)
786 by mattgiuca
fileservice_lib/action.py: Fixed SVN error messages. No longer just give a
640
    except pysvn.ClientError, e:
641
        raise ActionError(str(e))
522 by drtomc
Add quite a lot of stuff to get usrmgt happening.
642
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
643
# Table of all action functions #
644
# Each function has the interface f(req, fields).
645
646
actions_table = {
647
    "remove" : action_remove,
648
    "move" : action_move,
592 by mattgiuca
fileservice, browser/listing: Added new fileservice action, "mkdir".
649
    "mkdir" : action_mkdir,
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
650
    "putfile" : action_putfile,
651
    "putfiles" : action_putfiles,
652
    "paste" : action_paste,
726 by dcoles
Browser: Added code to allow directories to be published through the UI. Also
653
    "publish" : action_publish,
654
    "unpublish" : action_unpublish,
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
655
656
    "svnadd" : action_svnadd,
657
    "svnupdate" : action_svnupdate,
658
    "svnrevert" : action_svnrevert,
659
    "svnpublish" : action_svnpublish,
660
    "svnunpublish" : action_svnunpublish,
661
    "svncommit" : action_svncommit,
522 by drtomc
Add quite a lot of stuff to get usrmgt happening.
662
    "svncheckout" : action_svncheckout,
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
663
}