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