~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: 2010-01-28 00:59:45 UTC
  • mto: This revision was merged to the branch mainline in revision 1453.
  • Revision ID: me@williamgrant.id.au-20100128005945-1iz3hxbuc5q3pjov
Drop offering.subject.url from the result of userservice's get_enrolments, because it's unused and now broken.

Show diffs side-by-side

added added

removed removed

Lines of Context:
91
91
# action=svnupdate: Bring a file up to date with the head revision.
92
92
#       path:   The path to the file to be updated. Only one file may be
93
93
#               specified.
 
94
#       revision: The revision number to update to. If not provided this
 
95
#               defaults to HEAD.
94
96
#
95
97
# action=svnpublish: Set the "published" flag on a file to True.
96
98
#       path:   The path to the file to be published. Can be specified
119
121
#       path:   The path to the directory to be checked (under the IVLE
120
122
#               repository base).
121
123
#
 
124
# action=svncleanup: Recursively clean up the working copy, removing locks,
 
125
#   resuming unfinished operations, etc.
 
126
#       path:   The path to the directory to be cleaned
 
127
#
122
128
# TODO: Implement the following actions:
123
129
#   svnupdate (done?)
124
130
# TODO: Implement ZIP unpacking in putfiles (done?).
138
144
from ivle import (util, studpath, zip)
139
145
from ivle.fileservice_lib.exceptions import WillNotOverwrite
140
146
import ivle.conf
141
 
 
142
 
 
143
 
def get_login(_realm, existing_login, _may_save):
144
 
    """Callback function used by pysvn for authentication.
145
 
    realm, existing_login, _may_save: The 3 arguments passed by pysvn to
146
 
        callback_get_login.
147
 
        The following has been determined empirically, not from docs:
148
 
        existing_login will be the name of the user who owns the process on
149
 
        the first attempt, "" on subsequent attempts. We use this fact.
150
 
    """
151
 
    # Only provide credentials on the _first_ attempt.
152
 
    # If we're being asked again, then it means the credentials failed for
153
 
    # some reason and we should just fail. (This is not desirable, but it's
154
 
    # better than being asked an infinite number of times).
155
 
    return (existing_login != "", str(ivle.conf.login),
156
 
                                  str(ivle.conf.svn_pass), True)
157
 
 
158
 
# Make a Subversion client object
159
 
svnclient = pysvn.Client()
160
 
svnclient.callback_get_login = get_login
 
147
import ivle.svn
 
148
 
 
149
# Make a Subversion client object (which will log in with this user's
 
150
# credentials, upon request)
 
151
svnclient = ivle.svn.create_auth_svn_client(username=ivle.conf.login,
 
152
                                            password=ivle.conf.svn_pass)
161
153
svnclient.exception_style = 0               # Simple (string) exceptions
162
154
 
163
155
DEFAULT_LOGMESSAGE = "No log message supplied."
229
221
 
230
222
    Does not mutate req.
231
223
    """
232
 
    (_, _, r) = studpath.url_to_jailpaths(actionpath_to_urlpath(req, path))
 
224
    r = studpath.to_home_path(actionpath_to_urlpath(req, path))
233
225
    if r is None:
234
226
        raise ActionError("Invalid path")
235
227
    return r
421
413
    for datum in data:
422
414
        # Each of the uploaded files
423
415
        filepath = os.path.join(path, datum.filename)
424
 
        (_, _, filepath_local) = studpath.url_to_jailpaths(filepath)
 
416
        filepath_local = studpath.to_home_path(filepath)
425
417
        if os.path.isdir(filepath_local):
426
418
            raise ActionError("A directory already exists "
427
419
                    + "with that name")
437
429
            # filename)
438
430
            try:
439
431
                # First get the entire path (within jail)
440
 
                _, _, abspath = studpath.url_to_jailpaths(path)
 
432
                abspath = studpath.to_home_path(path)
441
433
                abspath = os.path.join(os.sep, abspath)
442
434
                zip.unzip(abspath, filedata)
443
435
            except (OSError, IOError):
446
438
                raise ActionError("File '" + e.filename + "' already exists.")
447
439
        else:
448
440
            # Not a zip file
449
 
            (_, _, filepath_local) = studpath.url_to_jailpaths(filepath)
 
441
            filepath_local = studpath.to_home_path(filepath)
450
442
            if filepath_local is None:
451
443
                raise ActionError("Invalid path")
452
444
 
533
525
    Reads fields: 'path'
534
526
    """
535
527
    paths = fields.getlist('path')
536
 
    user = studpath.url_to_local(req.path)[0]
 
528
    user = util.split_path(req.path)[0]
537
529
    homedir = "/home/%s" % user
538
530
    if len(paths):
539
531
        paths = map(lambda path: actionpath_to_local(req, path), paths)
540
532
    else:
541
 
        paths = [studpath.url_to_jailpaths(req.path)[2]]
 
533
        paths = [studpath.to_home_path(req.path)]
542
534
 
543
535
    # Set all the dirs in home dir world browsable (o+r,o+x)
544
536
    #FIXME: Should really only do those in the direct path not all of the 
568
560
    if len(paths):
569
561
        paths = map(lambda path: actionpath_to_local(req, path), paths)
570
562
    else:
571
 
        paths = [studpath.url_to_jailpaths(req.path)[2]]
 
563
        paths = [studpath.to_home_path(req.path)]
572
564
 
573
565
    try:
574
566
        for path in paths:
611
603
def action_svnupdate(req, fields):
612
604
    """Performs a "svn update" to each file specified.
613
605
 
614
 
    Reads fields: 'path'
 
606
    Reads fields: 'path' and 'revision'
615
607
    """
616
608
    path = fields.getfirst('path')
 
609
    revision = fields.getfirst('revision')
617
610
    if path is None:
618
611
        raise ActionError("Required field missing")
 
612
    if revision is None:
 
613
        revision = pysvn.Revision( pysvn.opt_revision_kind.head )
 
614
    else:
 
615
        try:
 
616
            revision = pysvn.Revision(pysvn.opt_revision_kind.number,
 
617
                    int(revision))
 
618
        except ValueError, e:
 
619
            raise ActionError("Bad revision number: '%s'"%revision,)
619
620
    path = actionpath_to_local(req, path)
620
621
 
621
622
    try:
622
 
        svnclient.update(path, recurse=True)
 
623
        svnclient.update(path, recurse=True, revision=revision)
623
624
    except pysvn.ClientError, e:
624
625
        raise ActionError(str(e))
625
626
 
665
666
    if len(paths):
666
667
        paths = map(lambda path: actionpath_to_local(req, path), paths)
667
668
    else:
668
 
        paths = [studpath.url_to_jailpaths(req.path)[2]]
 
669
        paths = [studpath.to_home_path(req.path)]
669
670
 
670
671
    try:
671
672
        for path in paths:
717
718
    url = ivle.conf.svn_addr + "/" + urllib.quote(paths[0])
718
719
    local_path = actionpath_to_local(req, str(paths[1]))
719
720
    try:
720
 
        svnclient.callback_get_login = get_login
721
721
        svnclient.checkout(url, local_path, recurse=True)
722
722
    except pysvn.ClientError, e:
723
723
        raise ActionError(str(e))
729
729
    """
730
730
    path = fields.getfirst('path')
731
731
    logmsg = fields.getfirst('logmsg')
732
 
    url = ivle.conf.svn_addr + "/" + path
 
732
    url = ivle.conf.svn_addr + "/" + urllib.quote(path)
733
733
    try:
734
 
        svnclient.callback_get_login = get_login
735
734
        svnclient.mkdir(url, log_message=logmsg)
736
735
    except pysvn.ClientError, e:
737
736
        raise ActionError(str(e))
744
743
    Reads fields: 'path'
745
744
    """
746
745
    path = fields.getfirst('path')
747
 
    url = ivle.conf.svn_addr + "/" + path
 
746
    url = ivle.conf.svn_addr + "/" + urllib.quote(path)
748
747
    svnclient.exception_style = 1
749
748
 
750
749
    try:
751
 
        svnclient.callback_get_login = get_login
752
750
        info = svnclient.info2(url,
753
751
            revision=pysvn.Revision(pysvn.opt_revision_kind.head))[0][1]
754
752
        return {'svnrevision': info['rev'].number
761
759
            raise util.IVLEError(404, 'The specified repository path does not exist')
762
760
        else:
763
761
            raise ActionError(str(e[0]))
764
 
            
 
762
 
 
763
 
 
764
def action_svncleanup(req, fields):
 
765
    """Recursively clean up the working copy, removing locks, resuming 
 
766
    unfinished operations, etc.
 
767
        path:   The path to be cleaned"""
 
768
 
 
769
    path = fields.getfirst('path')
 
770
    if path is None:
 
771
        raise ActionError("Required field missing")
 
772
    path = actionpath_to_local(req, path)
 
773
 
 
774
    try:
 
775
        svnclient.cleanup(path)
 
776
    except pysvn.ClientError, e:
 
777
        raise ActionError(str(e))
 
778
 
765
779
 
766
780
# Table of all action functions #
767
781
# Each function has the interface f(req, fields).
787
801
    "svncheckout" : action_svncheckout,
788
802
    "svnrepomkdir" : action_svnrepomkdir,
789
803
    "svnrepostat" : action_svnrepostat,
 
804
    "svncleanup" : action_svncleanup,
790
805
}