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