1099.1.54
by William Grant
ivle.webapp.filesystem.svnlog: Port www/apps/svnlog to new framework. As with |
1 |
# IVLE - Informatics Virtual Learning Environment
|
2 |
# Copyright (C) 2007-2009 The University of Melbourne
|
|
562
by dcoles
Added new app: Diff (SVN diff application) |
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 |
||
1099.1.48
by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves |
18 |
# Author: David Coles, Will Grant
|
19 |
||
1099.1.57
by William Grant
ivle.webapp.filesystem.diff: Add some docstrings, and remove a constant which was only used in one place. |
20 |
'''Components of the webapp for diffing user files.'''
|
21 |
||
1099.1.48
by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves |
22 |
import os |
23 |
import re |
|
24 |
import cgi |
|
25 |
||
1801.1.1
by William Grant
Replace cjson with json, or simplejson if json is not available (Python <2.6) |
26 |
try: |
27 |
import json |
|
28 |
except ImportError: |
|
29 |
import simplejson as json |
|
30 |
||
1099.1.48
by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves |
31 |
import genshi |
562
by dcoles
Added new app: Diff (SVN diff application) |
32 |
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
33 |
import ivle.interpret |
1099.1.48
by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves |
34 |
from ivle.webapp.base.xhtml import XHTMLView |
1099.1.99
by William Grant
Require that plugins providing media subclass MediaPlugin. |
35 |
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin |
1099.1.50
by William Grant
ivle.webapp.filesystem.diff: Import BadRequest; it was used. |
36 |
from ivle.webapp.errors import NotFound, BadRequest |
1294.2.102
by William Grant
Replace the breadcrumb-h1 with proper breadcrumbs throughout the filesystem views. |
37 |
from ivle.webapp.filesystem import make_path_breadcrumbs |
1294.2.56
by William Grant
Port the diff views. |
38 |
from ivle.webapp import ApplicationRoot |
39 |
||
1099.1.48
by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves |
40 |
class DiffView(XHTMLView): |
1099.1.57
by William Grant
ivle.webapp.filesystem.diff: Add some docstrings, and remove a constant which was only used in one place. |
41 |
'''A view to present a nice XHTML Subversion diff from a user's jail.'''
|
1099.1.48
by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves |
42 |
template = 'template.html' |
1251
by William Grant
Put Subversion diffs and logs in the Files tab. |
43 |
tab = 'files' |
1373
by William Grant
Improve the diff/log breadcrumbs. |
44 |
breadcrumb_text = 'Files' |
1099.1.48
by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves |
45 |
|
1294.2.60
by William Grant
Move the already-ported filesystem views to use subpaths. |
46 |
subpath_allowed = True |
47 |
||
1099.1.110
by William Grant
Implement an authorization system in the new framework. This breaks the REST |
48 |
def authorize(self, req): |
49 |
return req.user is not None |
|
50 |
||
1099.1.48
by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves |
51 |
def populate(self, req, ctx): |
1099.1.67
by William Grant
Port ivle.webapp.filesystem.{diff,svnlog}'s media to the new framework. |
52 |
self.plugin_styles[Plugin] = ['diff.css'] |
1099.1.48
by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves |
53 |
|
54 |
revfields = req.get_fieldstorage().getlist("r") |
|
55 |
if len(revfields) > 2: |
|
56 |
raise BadRequest('A maximum of two revisions can be given.') |
|
57 |
||
58 |
revs = [revfield.value for revfield in revfields] |
|
59 |
||
1220
by William Grant
Remove ivle.conf dependency from ivle.webapp.filesystem.diff. |
60 |
jail_dir = os.path.join(req.config['paths']['jails']['mounts'], |
61 |
req.user.login) |
|
1276
by William Grant
Drop ivle.conf usage from ivle.interpret. |
62 |
(out, err) = ivle.interpret.execute_raw(req.config, req.user, jail_dir, |
63 |
'/home', os.path.join(req.config['paths']['share'], |
|
64 |
'services/diffservice'), |
|
1294.2.60
by William Grant
Move the already-ported filesystem views to use subpaths. |
65 |
[self.path] + revs |
1276
by William Grant
Drop ivle.conf usage from ivle.interpret. |
66 |
)
|
1099.1.48
by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves |
67 |
assert not err |
68 |
||
1801.1.1
by William Grant
Replace cjson with json, or simplejson if json is not available (Python <2.6) |
69 |
response = json.loads(out) |
1099.1.48
by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves |
70 |
if 'error' in response: |
71 |
if response['error'] == 'notfound': |
|
72 |
raise NotFound() |
|
73 |
else: |
|
74 |
raise AssertionError('Unknown error from diffservice: %s' % |
|
75 |
response['error']) |
|
76 |
||
77 |
# No error. We must be safe.
|
|
78 |
diff = response['diff'] |
|
79 |
||
80 |
# Split up the udiff into individual files
|
|
81 |
diff_matcher = re.compile( |
|
82 |
r'^Index: (.*)\n\=+\n((?:[^I].*\n)*)',re.MULTILINE |
|
83 |
)
|
|
84 |
||
1294.2.60
by William Grant
Move the already-ported filesystem views to use subpaths. |
85 |
ctx['title'] = os.path.normpath(self.path).rsplit('/', 1)[-1] |
1373
by William Grant
Improve the diff/log breadcrumbs. |
86 |
self.extra_breadcrumbs = make_path_breadcrumbs(req, self.subpath) |
87 |
self.extra_breadcrumbs.append(SubversionDiffBreadcrumb()) |
|
1173
by William Grant
Show the filename in a diff view. |
88 |
|
1099.1.48
by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves |
89 |
# Create a dict with (name, HTMLdiff) pairs for each non-empty diff.
|
90 |
ctx['files'] = dict([(fd[0], genshi.XML(htmlfy_diff(fd[1]))) |
|
91 |
for fd in diff_matcher.findall(diff) |
|
92 |
if fd[1]]) |
|
93 |
||
1294.2.60
by William Grant
Move the already-ported filesystem views to use subpaths. |
94 |
@property
|
95 |
def path(self): |
|
96 |
return os.path.join(*self.subpath) if self.subpath else '' |
|
97 |
||
1099.1.48
by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves |
98 |
|
1373
by William Grant
Improve the diff/log breadcrumbs. |
99 |
class SubversionDiffBreadcrumb(object): |
100 |
text = 'Subversion Diff' |
|
101 |
||
102 |
||
1099.1.48
by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves |
103 |
def htmlfy_diff(difftext): |
104 |
"""Adds HTML markup to a udiff string"""
|
|
105 |
output = cgi.escape(difftext) |
|
106 |
subs = { |
|
107 |
r'^([\+\-]{3})\s(\S+)\s\((.+)\)$': |
|
108 |
r'<span class="diff-files">\1 \2 <em>(\3)</em></span>', |
|
109 |
r'^\@\@ (.*) \@\@$': |
|
110 |
r'<span class="diff-range">@@ \1 @@</span>', |
|
111 |
r'^\+(.*)$': |
|
112 |
r'<span class="diff-add">+\1</span>', |
|
113 |
r'^\-(.*)$': |
|
114 |
r'<span class="diff-sub">-\1</span>', |
|
115 |
r'^\\(.*)$': |
|
116 |
r'<span class="diff-special">\\\1</span>' |
|
117 |
}
|
|
118 |
||
119 |
for match in subs: |
|
120 |
output = re.compile(match, re.MULTILINE).sub(subs[match], output) |
|
121 |
||
122 |
return '<pre class="diff">%s</pre>' % output |
|
123 |
||
1099.1.99
by William Grant
Require that plugins providing media subclass MediaPlugin. |
124 |
class Plugin(ViewPlugin, MediaPlugin): |
1294.2.60
by William Grant
Move the already-ported filesystem views to use subpaths. |
125 |
views = [(ApplicationRoot, 'diff', DiffView)] |
1099.1.67
by William Grant
Port ivle.webapp.filesystem.{diff,svnlog}'s media to the new framework. |
126 |
|
127 |
media = 'media' |