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

« back to all changes in this revision

Viewing changes to ivle/svn.py

  • Committer: William Grant
  • Date: 2009-12-15 05:14:56 UTC
  • mfrom: (1428.1.3 trunk)
  • Revision ID: me@williamgrant.id.au-20091215051456-jptc3rnbwgub4z68
Polish up the test suite editing UI.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# IVLE - Informatics Virtual Learning Environment
 
2
# Copyright (C) 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: Subversion utilities
 
19
# Author: William Grant
 
20
# Date:   16/07/2008
 
21
 
 
22
import stat
 
23
 
 
24
import pysvn
 
25
 
 
26
def create_auth_svn_client_autopass(username):
 
27
    """Create a new pysvn client which is set up to automatically authenticate
 
28
    with the supplied user. The user's Subversion password is automatically
 
29
    looked up in the database.
 
30
    (Requires database access -- can't be used inside the jail.)
 
31
    @param username: IVLE/Subversion username.
 
32
    """
 
33
    # Note: Must do this inside the function, since this file may be used in
 
34
    # the jail, and importing ivle.database crashes in the jail.
 
35
    from ivle.config import Config
 
36
    import ivle.database
 
37
    from ivle.database import User
 
38
    store = ivle.database.get_store(Config())
 
39
    user = store.find(User, User.login==unicode(username)).one()
 
40
    return create_auth_svn_client(username, user.svn_pass)
 
41
 
 
42
def create_auth_svn_client(username, password):
 
43
    """Create a new pysvn client which is set up to automatically authenticate
 
44
    with the supplied credentials.
 
45
    @param username: IVLE/Subversion username.
 
46
    @param password: Subversion password.
 
47
    """
 
48
    username = str(username)
 
49
    password = str(password)
 
50
    def get_login(_realm, existing_login, _may_save):
 
51
        """Callback function used by pysvn for authentication.
 
52
        realm, existing_login, _may_save: The 3 arguments passed by pysvn to
 
53
            callback_get_login.
 
54
            The following has been determined empirically, not from docs:
 
55
            existing_login will be the name of the user who owns the process on
 
56
            the first attempt, "" on subsequent attempts. We use this fact.
 
57
        """
 
58
        # Only provide credentials on the _first_ attempt.
 
59
        # If we're being asked again, then it means the credentials failed for
 
60
        # some reason and we should just fail. (This is not desirable, but it's
 
61
        # better than being asked an infinite number of times).
 
62
        return (existing_login != "", username, password, True)
 
63
 
 
64
    svnclient = pysvn.Client()
 
65
    svnclient.callback_get_login = get_login
 
66
    return svnclient
 
67
 
 
68
def revision_from_string(r_str):
 
69
    if r_str is None:
 
70
        pass
 
71
    elif r_str == "HEAD":
 
72
        return pysvn.Revision( pysvn.opt_revision_kind.head )
 
73
    elif r_str == "WORKING":
 
74
        return pysvn.Revision( pysvn.opt_revision_kind.working )
 
75
    elif r_str == "BASE":
 
76
        return pysvn.Revision( pysvn.opt_revision_kind.base )
 
77
    else:
 
78
        # Is it a number?
 
79
        try:
 
80
            r = int(r_str)
 
81
            return pysvn.Revision( pysvn.opt_revision_kind.number, r)
 
82
        except:
 
83
            pass
 
84
    return None
 
85
 
 
86
def revision_exists(client, path, revision):
 
87
    try:
 
88
        client.list(path, revision=revision)
 
89
        return True
 
90
    except pysvn.ClientError:
 
91
        return False
 
92
 
 
93
def revision_is_dir(client, path, revision):
 
94
    """Returns True if the given path+revision is a directory.
 
95
    @raises a pysvn.ClientError if it does not exist.
 
96
    """
 
97
    # XXX I *think* the first element of the list is the requested object, and
 
98
    # subsequent items are its possible children (so ignore them).
 
99
    list_object, _ = client.list(path, revision=revision)[0]
 
100
    # list_object is a PySvnList object
 
101
    return list_object.kind == pysvn.node_kind.dir
 
102
 
 
103
class PysvnListStatWrapper:
 
104
    '''Wrap a pysvn listing object to look somewhat like a result of
 
105
       os.stat.
 
106
    '''
 
107
    def __init__(self, pysvn_list):
 
108
        self.pysvn_list = pysvn_list
 
109
 
 
110
    def __getattr__(self, name):
 
111
        try:
 
112
            if name == 'st_mode':
 
113
                # Special magic needed.
 
114
                if self.pysvn_list.kind == pysvn.node_kind.dir:
 
115
                    return stat.S_IFDIR
 
116
                else:
 
117
                    return stat.S_IFREG
 
118
                return value
 
119
            return getattr(self.pysvn_list,
 
120
                           {'st_mtime': 'time',
 
121
                            'st_size': 'size',
 
122
                           }[name])
 
123
        except AttributeError, KeyError:
 
124
            raise AttributeError, name