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

« back to all changes in this revision

Viewing changes to scripts/diffservice

  • Committer: wagrant
  • Date: 2008-07-08 07:00:54 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:830
Add an svnlog app. It nicely displays all log entries for a path,
though it won't scale well.

Show diffs side-by-side

added added

removed removed

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