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

« back to all changes in this revision

Viewing changes to ivle/studpath.py

  • Committer: William Grant
  • Date: 2010-02-24 01:21:07 UTC
  • Revision ID: grantw@unimelb.edu.au-20100224012107-e02qr9ce1ftg05y5
Remove unused UserRESTView and associated infrastructure.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
import os
28
28
import stat
29
 
import pysvn
30
29
 
31
 
import ivle.conf
32
30
from ivle import util
33
31
 
34
 
# Make a Subversion client object (for published)
35
 
svnclient = pysvn.Client()
36
 
 
37
32
def url_to_local(config, urlpath):
38
33
    """Given a URL path (part of a URL query string, see below), returns a
39
34
    tuple of
52
47
    Returns (None, None) if the path is empty.
53
48
 
54
49
    >>> stubconfig = {'paths': {'jails': {'mounts': '/jails'}}}
55
 
    >>> url_to_local(stubconfig, '')
56
 
    (None, None)
 
50
 
57
51
    >>> url_to_local(stubconfig, 'joe/foo/bar/baz')
58
52
    ('joe', '/jails/joe/home/joe/foo/bar/baz')
 
53
    >>> url_to_local(stubconfig, 'joe')
 
54
    ('joe', '/jails/joe/home/joe')
 
55
    >>> url_to_local(stubconfig, 'joe/')
 
56
    ('joe', '/jails/joe/home/joe')
 
57
 
 
58
    We have some protection from various potential attacks. An empty,
 
59
    absolute, or ..-prefixed path yields a special result.
 
60
 
 
61
    >>> url_to_local(stubconfig, '')
 
62
    (None, None)
 
63
    >>> url_to_local(stubconfig, '/foo')
 
64
    (None, None)
 
65
    >>> url_to_local(stubconfig, '../bar')
 
66
    (None, None)
59
67
    """
60
68
 
61
69
    # First normalise the path
78
86
 
79
87
    return (user, path)
80
88
 
81
 
def url_to_jailpaths(urlpath):
 
89
def url_to_jailpaths(config, urlpath):
82
90
    """Given a URL path (part of a URL query string), returns a tuple of
83
91
        * the username of the student whose directory is being browsed
84
92
        * the absolute path where the jail will be located.
86
94
 
87
95
    urlpath: See urlpath in url_to_local.
88
96
 
89
 
    >>> url_to_jailpaths("joe/mydir/myfile")
90
 
    ('joe', '/var/lib/ivle/jailmounts/joe', '/home/joe/mydir/myfile')
 
97
    >>> stubconfig = {'paths': {'jails': {'mounts': '/jails'}}}
91
98
 
92
 
    >>> url_to_jailpaths("")
 
99
    >>> url_to_jailpaths(stubconfig, "joe/mydir//myfile/.././myfile")
 
100
    ('joe', '/jails/joe', '/home/joe/mydir/myfile')
 
101
    >>> url_to_jailpaths(stubconfig, "")
 
102
    (None, None, None)
 
103
    >>> url_to_jailpaths(stubconfig, "../foo")
 
104
    (None, None, None)
 
105
    >>> url_to_jailpaths(stubconfig, "/foo")
93
106
    (None, None, None)
94
107
    """
95
108
    # First normalise the path
96
109
    urlpath = os.path.normpath(urlpath)
97
 
    # Now if it begins with ".." then it's illegal
98
 
    if urlpath.startswith(".."):
 
110
    # Now if it begins with "..", or is absolute, then it's illegal
 
111
    if urlpath.startswith("..") or os.path.isabs(urlpath):
99
112
        return (None, None, None)
100
113
    # Note: User can be a group name. There is absolutely no difference in our
101
114
    # current directory scheme.
102
115
    (user, subpath) = util.split_path(urlpath)
103
116
    if user is None: return (None, None, None)
104
117
 
105
 
    jail = os.path.join(ivle.conf.jail_base, user)
 
118
    jail = os.path.join(config['paths']['jails']['mounts'], user)
106
119
    path = to_home_path(urlpath)
107
120
 
108
121
    return (user, jail, path)
112
125
 
113
126
    >>> to_home_path('joe/foo/bar/baz')
114
127
    '/home/joe/foo/bar/baz'
 
128
    >>> to_home_path('joe/foo//bar/baz/../../')
 
129
    '/home/joe/foo'
 
130
    >>> to_home_path('joe/foo//bar/baz/../../../../../') is None
 
131
    True
115
132
    """
 
133
 
 
134
    urlpath = os.path.normpath(urlpath)
 
135
    # If it begins with '..', it's illegal.
 
136
    if urlpath.startswith(".."):
 
137
        return None
 
138
 
116
139
    return os.path.join('/home', urlpath)
117
140
 
118
 
def svnpublished(path):
119
 
    """Given a path on the LOCAL file system, determines whether the path has
120
 
    its "ivle:published" property active (in subversion). Returns True
121
 
    or False."""
122
 
    # Read SVN properties for this path
123
 
    try:
124
 
        props = svnclient.propget("ivle:published", path, recurse=False)
125
 
    except pysvn.ClientError:
126
 
        # Not under version control? Then it isn't published.
127
 
        return False
128
 
    return len(props) > 0
129
 
 
130
141
def published(path):
131
142
    """Given a path on the LOCAL file system, determines whether the path has a 
132
143
    '.published' file.  Returns True or False."""