1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#!/usr/bin/python
# IVLE - Informatics Virtual Learning Environment
# Copyright (C) 2007-2008 The University of Melbourne
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# Script: diffservice
# Author: David Coles
# Date: 26/2/2008
# A CGI script for generating a diff report page in HTML. It is intended to be
# run from the trampoline by the Diff application.
import cgi
import os.path
import pysvn
import re
from ivle import cgirequest
import ivle.svn
def htmlfy_diff(difftext):
"""Adds HTML markup to a udiff string"""
output = cgi.escape(difftext)
subs = {
r'^([\+\-]{3})\s(\S+)\s\((.+)\)$':
r'<span class="diff-files">\1 \2 <em>(\3)</em></span>',
r'^\@\@ (.*) \@\@$':
r'<span class="diff-range">@@ \1 @@</span>',
r'^\+(.*)$':
r'<span class="diff-add">+\1</span>',
r'^\-(.*)$':
r'<span class="diff-sub">-\1</span>',
r'^\\(.*)$':
r'<span class="diff-special">\\\1</span>'
}
for match in subs:
output = re.compile(match, re.MULTILINE).sub(subs[match], output)
return output
# Use a CGIRequest object to make the CGI environment look like the normal
# IVLE handler environment.
req = cgirequest.CGIRequest()
req.install_error_handler()
req.content_type = "text/html"
# Beginning of the page
req.write('<div id="ivle_padding">\n')
req.write('<h1>Diff</h1>\n')
# Default revisions
revs = [pysvn.Revision(x) for x in [pysvn.opt_revision_kind.base,
pysvn.opt_revision_kind.working]]
# Override revisions from query string
fields = req.get_fieldstorage()
field_r = fields.getlist("r")
for ri in range(len(field_r)):
r = ivle.svn.revision_from_string(field_r[ri])
if r is not None:
revs[ri] = r
# Attempt to get the diff for these revisons
try:
svnclient = pysvn.Client()
diff = svnclient.diff
diff_text = diff( '/tmp/svndiff',
os.path.join('/home', req.path),
revision1=revs[0],
revision2=revs[1]
)
# Split up the udiff into individual files
diff_matcher = re.compile(
r'^Index: (.*)\n\=+\n((?:[^I].*\n)*)',re.MULTILINE
)
split_diff = diff_matcher.findall(diff_text)
# Prints output
for filediff in split_diff:
filename = filediff[0]
diffbody = htmlfy_diff(filediff[1])
req.write('<p><b>File:</b> %s</p>\n'
% cgi.escape(filename))
req.write('<pre class="diff">%s</pre>\n' % diffbody)
except pysvn._pysvn_2_5.ClientError, e:
req.write('<p><b>Error:</b></p>\n')
req.write('<pre class="diff">%s</pre>\n' % cgi.escape(str(e)))
req.write('</div>\n')
|