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

« back to all changes in this revision

Viewing changes to ivle/fileservice_lib/action.py

  • Committer: David Coles
  • Date: 2010-08-30 03:26:13 UTC
  • Revision ID: coles.david@gmail.com-20100830032613-d14vng0jkelniu3l
python-console: Fix globals broken with new JSON library.

simplejson always returns unicode strings. cJSON would return ordinary strings 
if possible. cPickle.loads() only accepts strings. At present we use pickle 
version 0 so they should all works as ASCII strings. Higher versions of pickle 
are not plain ASCII and are likely to break this and so this should be fixed 
at some point.

Also replaced unconditional exception with one that catches Pickle errors. Not 
sure the best way to report failures of these functions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
94
94
#       revision: The revision number to update to. If not provided this
95
95
#               defaults to HEAD.
96
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
97
# action=svncommit: Commit a file(s) or directory(s) to the repository.
106
98
#       path:   The path to the file or directory to be committed. Can be
107
99
#               specified multiple times. Directories are committed
144
136
from ivle import (util, studpath, zip)
145
137
from ivle.fileservice_lib.exceptions import WillNotOverwrite
146
138
import ivle.conf
147
 
 
148
 
 
149
 
def get_login(_realm, existing_login, _may_save):
150
 
    """Callback function used by pysvn for authentication.
151
 
    realm, existing_login, _may_save: The 3 arguments passed by pysvn to
152
 
        callback_get_login.
153
 
        The following has been determined empirically, not from docs:
154
 
        existing_login will be the name of the user who owns the process on
155
 
        the first attempt, "" on subsequent attempts. We use this fact.
156
 
    """
157
 
    # Only provide credentials on the _first_ attempt.
158
 
    # If we're being asked again, then it means the credentials failed for
159
 
    # some reason and we should just fail. (This is not desirable, but it's
160
 
    # better than being asked an infinite number of times).
161
 
    return (existing_login != "", str(ivle.conf.login),
162
 
                                  str(ivle.conf.svn_pass), True)
163
 
 
164
 
# Make a Subversion client object
165
 
svnclient = pysvn.Client()
166
 
svnclient.callback_get_login = get_login
 
139
import ivle.svn
 
140
 
 
141
# Make a Subversion client object (which will log in with this user's
 
142
# credentials, upon request)
 
143
svnclient = ivle.svn.create_auth_svn_client(username=ivle.conf.login,
 
144
                                            password=ivle.conf.svn_pass)
167
145
svnclient.exception_style = 0               # Simple (string) exceptions
168
146
 
169
147
DEFAULT_LOGMESSAGE = "No log message supplied."
339
317
    """
340
318
    frompath = fields.getfirst('from')
341
319
    topath = fields.getfirst('to')
342
 
    movefile(req, frompath, topath)
 
320
    svn = fields.getfirst('svn')
 
321
    if svn:
 
322
        svn_movefile(req, frompath, topath)
 
323
    else:
 
324
        movefile(req, frompath, topath)
343
325
 
344
326
def action_mkdir(req, fields):
345
327
    """Creates a directory with the given path.
594
576
    Reads fields: 'path' (multiple)
595
577
    """
596
578
    paths = fields.getlist('path')
597
 
    paths = map(lambda path: actionpath_to_local(req, path), paths)
 
579
    paths = map(lambda path: actionpath_to_local(req, path).decode('utf-8'),
 
580
                paths)
598
581
 
599
582
    try:
600
583
        svnclient.add(paths, recurse=True, force=True)
607
590
    Reads fields: 'path' (multiple)
608
591
    """
609
592
    paths = fields.getlist('path')
610
 
    paths = map(lambda path: actionpath_to_local(req, path), paths)
 
593
    paths = map(lambda path: actionpath_to_local(req, path).decode('utf-8'),
 
594
                paths)
611
595
 
612
596
    try:
613
597
        svnclient.remove(paths, force=True)
631
615
                    int(revision))
632
616
        except ValueError, e:
633
617
            raise ActionError("Bad revision number: '%s'"%revision,)
634
 
    path = actionpath_to_local(req, path)
 
618
    path = actionpath_to_local(req, path).decode('utf-8')
635
619
 
636
620
    try:
637
621
        svnclient.update(path, recurse=True, revision=revision)
646
630
    path = fields.getfirst('path')
647
631
    if path is None:
648
632
        raise ActionError("Required field missing")
649
 
    path = actionpath_to_local(req, path)
 
633
    path = actionpath_to_local(req, path).decode('utf-8')
650
634
 
651
635
    try:
652
636
        svnclient.resolved(path, recurse=True)
659
643
    Reads fields: 'path' (multiple)
660
644
    """
661
645
    paths = fields.getlist('path')
662
 
    paths = map(lambda path: actionpath_to_local(req, path), paths)
 
646
    paths = map(lambda path: actionpath_to_local(req, path).decode('utf-8'),
 
647
                paths)
663
648
 
664
649
    try:
665
650
        svnclient.revert(paths, recurse=True)
666
651
    except pysvn.ClientError, e:
667
652
        raise ActionError(str(e))
668
653
 
669
 
def action_svnpublish(req, fields):
670
 
    """Sets svn property "ivle:published" on each file specified.
671
 
    Should only be called on directories (only effective on directories
672
 
    anyway).
673
 
 
674
 
    Reads fields: 'path'
675
 
 
676
 
    XXX Currently unused by the client (calls action_publish instead, which
677
 
    has a completely different publishing model).
678
 
    """
679
 
    paths = fields.getlist('path')
680
 
    if len(paths):
681
 
        paths = map(lambda path: actionpath_to_local(req, path), paths)
682
 
    else:
683
 
        paths = [studpath.to_home_path(req.path)]
684
 
 
685
 
    try:
686
 
        for path in paths:
687
 
            # Note: Property value doesn't matter
688
 
            svnclient.propset("ivle:published", "", path, recurse=False)
689
 
    except pysvn.ClientError, e:
690
 
        raise ActionError("Directory could not be published")
691
 
 
692
 
def action_svnunpublish(req, fields):
693
 
    """Deletes svn property "ivle:published" on each file specified.
694
 
 
695
 
    Reads fields: 'path'
696
 
 
697
 
    XXX Currently unused by the client (calls action_unpublish instead, which
698
 
    has a completely different publishing model).
699
 
    """
700
 
    paths = fields.getlist('path')
701
 
    paths = map(lambda path: actionpath_to_local(req, path), paths)
702
 
 
703
 
    try:
704
 
        for path in paths:
705
 
            svnclient.propdel("ivle:published", path, recurse=False)
706
 
    except pysvn.ClientError, e:
707
 
        raise ActionError("Directory could not be unpublished")
708
 
 
709
654
def action_svncommit(req, fields):
710
655
    """Performs a "svn commit" to each file specified.
711
656
 
712
657
    Reads fields: 'path' (multiple), 'logmsg' (optional)
713
658
    """
714
659
    paths = fields.getlist('path')
715
 
    paths = map(lambda path: actionpath_to_local(req, str(path)), paths)
716
 
    logmsg = str(fields.getfirst('logmsg', DEFAULT_LOGMESSAGE))
 
660
    if len(paths):
 
661
        paths = map(lambda path:actionpath_to_local(req,path).decode('utf-8'),
 
662
                    paths)
 
663
    else:
 
664
        paths = [studpath.to_home_path(req.path).decode('utf-8')]
 
665
    logmsg = str(fields.getfirst('logmsg',
 
666
                 DEFAULT_LOGMESSAGE)).decode('utf-8')
717
667
    if logmsg == '': logmsg = DEFAULT_LOGMESSAGE
718
668
 
719
669
    try:
731
681
        raise ActionError("usage: svncheckout url local-path")
732
682
    url = ivle.conf.svn_addr + "/" + urllib.quote(paths[0])
733
683
    local_path = actionpath_to_local(req, str(paths[1]))
 
684
    url = url.decode('utf-8')
 
685
    local_path = local_path.decode('utf-8')
734
686
    try:
735
 
        svnclient.callback_get_login = get_login
736
687
        svnclient.checkout(url, local_path, recurse=True)
737
688
    except pysvn.ClientError, e:
738
689
        raise ActionError(str(e))
744
695
    """
745
696
    path = fields.getfirst('path')
746
697
    logmsg = fields.getfirst('logmsg')
747
 
    url = ivle.conf.svn_addr + "/" + path
 
698
    url = (ivle.conf.svn_addr + "/" + urllib.quote(path)).decode('utf-8')
748
699
    try:
749
 
        svnclient.callback_get_login = get_login
750
700
        svnclient.mkdir(url, log_message=logmsg)
751
701
    except pysvn.ClientError, e:
752
702
        raise ActionError(str(e))
759
709
    Reads fields: 'path'
760
710
    """
761
711
    path = fields.getfirst('path')
762
 
    url = ivle.conf.svn_addr + "/" + path
 
712
    url = (ivle.conf.svn_addr + "/" + urllib.quote(path)).decode('utf-8')
763
713
    svnclient.exception_style = 1
764
714
 
765
715
    try:
766
 
        svnclient.callback_get_login = get_login
767
716
        info = svnclient.info2(url,
768
717
            revision=pysvn.Revision(pysvn.opt_revision_kind.head))[0][1]
769
718
        return {'svnrevision': info['rev'].number
773
722
    except pysvn.ClientError, e:
774
723
        # Error code 170000 means ENOENT in this revision.
775
724
        if e[1][0][1] == 170000:
776
 
            raise util.IVLEError(404, 'The specified repository path does not exist')
 
725
            req.status = 404
 
726
            raise ActionError('The specified repository path does not exist')
777
727
        else:
778
728
            raise ActionError(str(e[0]))
779
729
 
786
736
    path = fields.getfirst('path')
787
737
    if path is None:
788
738
        raise ActionError("Required field missing")
789
 
    path = actionpath_to_local(req, path)
 
739
    path = actionpath_to_local(req, path).decode('utf-8')
790
740
 
791
741
    try:
792
742
        svnclient.cleanup(path)
812
762
    "svnupdate" : action_svnupdate,
813
763
    "svnresolved" : action_svnresolved,
814
764
    "svnrevert" : action_svnrevert,
815
 
    "svnpublish" : action_svnpublish,
816
 
    "svnunpublish" : action_svnunpublish,
817
765
    "svncommit" : action_svncommit,
818
766
    "svncheckout" : action_svncheckout,
819
767
    "svnrepomkdir" : action_svnrepomkdir,