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

« back to all changes in this revision

Viewing changes to ivle/fileservice_lib/action.py

  • Committer: William Grant
  • Date: 2009-05-05 06:46:58 UTC
  • mto: (1165.3.65 submissions-admin)
  • mto: This revision was merged to the branch mainline in revision 1247.
  • Revision ID: grantw@unimelb.edu.au-20090505064658-o913x4ooxxbjfo1q
Avoid clobbering the submission owner's privileges if they have offering privs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
131
131
import os
132
132
import cStringIO
133
133
import shutil
 
134
import urllib
134
135
 
135
136
import pysvn
136
137
 
137
138
from ivle import (util, studpath, zip)
 
139
from ivle.fileservice_lib.exceptions import WillNotOverwrite
138
140
import ivle.conf
139
141
 
 
142
 
140
143
def get_login(_realm, existing_login, _may_save):
141
144
    """Callback function used by pysvn for authentication.
142
145
    realm, existing_login, _may_save: The 3 arguments passed by pysvn to
149
152
    # If we're being asked again, then it means the credentials failed for
150
153
    # some reason and we should just fail. (This is not desirable, but it's
151
154
    # better than being asked an infinite number of times).
152
 
    return (existing_login != "", ivle.conf.login, ivle.conf.svn_pass, True)
 
155
    return (existing_login != "", str(ivle.conf.login),
 
156
                                  str(ivle.conf.svn_pass), True)
153
157
 
154
158
# Make a Subversion client object
155
159
svnclient = pysvn.Client()
194
198
    except KeyError:
195
199
        # Default, just send an error but then continue
196
200
        raise ActionError("Unknown action")
197
 
    action(req, fields)
 
201
    return action(req, fields)
198
202
 
199
203
def actionpath_to_urlpath(req, path):
200
204
    """Determines the URL path (relative to the student home) upon which the
399
403
 
400
404
    Reads fields: 'path', 'data' (file upload, multiple), 'unpack'
401
405
    """
402
 
 
403
406
    # Important: Data is "None" if the file submitted is empty.
404
407
    path = fields.getfirst('path')
405
408
    data = fields['data']
418
421
    for datum in data:
419
422
        # Each of the uploaded files
420
423
        filepath = os.path.join(path, datum.filename)
 
424
        (_, _, filepath_local) = studpath.url_to_jailpaths(filepath)
 
425
        if os.path.isdir(filepath_local):
 
426
            raise ActionError("A directory already exists "
 
427
                    + "with that name")
 
428
        else:
 
429
            if os.path.exists(filepath_local):
 
430
                raise ActionError("A file already exists with that name")
421
431
        filedata = datum.file
422
432
 
423
433
        if unpack and datum.filename.lower().endswith(".zip"):
432
442
                zip.unzip(abspath, filedata)
433
443
            except (OSError, IOError):
434
444
                goterror = True
 
445
            except WillNotOverwrite, e:
 
446
                raise ActionError("File '" + e.filename + "' already exists.")
435
447
        else:
436
448
            # Not a zip file
437
449
            (_, _, filepath_local) = studpath.url_to_jailpaths(filepath)
702
714
    paths = fields.getlist('path')
703
715
    if len(paths) != 2:
704
716
        raise ActionError("usage: svncheckout url local-path")
705
 
    url = ivle.conf.svn_addr + "/" + paths[0]
 
717
    url = ivle.conf.svn_addr + "/" + urllib.quote(paths[0])
706
718
    local_path = actionpath_to_local(req, str(paths[1]))
707
719
    try:
708
720
        svnclient.callback_get_login = get_login
727
739
def action_svnrepostat(req, fields):
728
740
    """Discovers whether a path exists in a repo under the IVLE SVN root.
729
741
 
 
742
    If it does exist, returns a dict containing its metadata.
 
743
 
730
744
    Reads fields: 'path'
731
745
    """
732
746
    path = fields.getfirst('path')
733
747
    url = ivle.conf.svn_addr + "/" + path
734
 
    svnclient.exception_style = 1 
 
748
    svnclient.exception_style = 1
735
749
 
736
750
    try:
737
751
        svnclient.callback_get_login = get_login
738
 
        svnclient.info2(url)
 
752
        info = svnclient.info2(url,
 
753
            revision=pysvn.Revision(pysvn.opt_revision_kind.head))[0][1]
 
754
        return {'svnrevision': info['rev'].number
 
755
                  if info['rev'] and
 
756
                     info['rev'].kind == pysvn.opt_revision_kind.number
 
757
                  else None}
739
758
    except pysvn.ClientError, e:
740
759
        # Error code 170000 means ENOENT in this revision.
741
760
        if e[1][0][1] == 170000: