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

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