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

562 by dcoles
Added new app: Diff (SVN diff application)
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
586 by dcoles
diffservice: Fixed oops in cleaned up code, also changed the date and author
21
# Author: David Coles
22
# Date:   26/2/2008
562 by dcoles
Added new app: Diff (SVN diff application)
23
586 by dcoles
diffservice: Fixed oops in cleaned up code, also changed the date and author
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.
562 by dcoles
Added new app: Diff (SVN diff application)
26
27
from common import cgirequest
879 by wagrant
diffservice, fileservice_lib: Factor out conversion of strings to
28
import common.svn
772 by mattgiuca
diffservice: HTML-escape the output.
29
import cgi
823 by wagrant
diffservice: Refactor and polish. URL revision retrieval is less
30
import os.path
562 by dcoles
Added new app: Diff (SVN diff application)
31
import pysvn
32
import re
33
34
def htmlfy_diff(difftext):
35
    """Adds HTML markup to a udiff string"""
772 by mattgiuca
diffservice: HTML-escape the output.
36
    output = cgi.escape(difftext)
826 by wagrant
diffservice: Refactor regexing.
37
    subs = {
38
     r'^([\+\-]{3})\s(\S+)\s\((.+)\)$':
39
         r'<span class="diff-files">\1 \2 <em>(\3)</em></span>',
40
     r'^\@\@ (.*) \@\@$':
41
         r'<span class="diff-range">@@ \1 @@</span>',
42
     r'^\+(.*)$':
43
         r'<span class="diff-add">+\1</span>',
44
     r'^\-(.*)$':
45
         r'<span class="diff-sub">-\1</span>',
46
     r'^\\(.*)$':
47
         r'<span class="diff-special">\\\1</span>'
48
    }
49
50
    for match in subs:
51
        output = re.compile(match, re.MULTILINE).sub(subs[match], output)
52
562 by dcoles
Added new app: Diff (SVN diff application)
53
    return output
54
55
# Use a CGIRequest object to make the CGI environment look like the normal
566 by dcoles
diff.css: Foreground colours for hilighting of changes
56
# IVLE handler environment.
562 by dcoles
Added new app: Diff (SVN diff application)
57
58
req = cgirequest.CGIRequest()
854 by wagrant
Enable the new CGIRequest error handler in the diff, file and svnlog
59
req.install_error_handler()
773 by mattgiuca
cgirequest.py: Fixed req.path attribute, so it now DOES NOT include the
60
req.content_type = "text/html"
562 by dcoles
Added new app: Diff (SVN diff application)
61
566 by dcoles
diff.css: Foreground colours for hilighting of changes
62
# Beginning of the page
773 by mattgiuca
cgirequest.py: Fixed req.path attribute, so it now DOES NOT include the
63
req.write('<div id="ivle_padding">\n')
64
req.write('<h1>Diff</h1>\n')
562 by dcoles
Added new app: Diff (SVN diff application)
65
823 by wagrant
diffservice: Refactor and polish. URL revision retrieval is less
66
# Default revisions
67
revs = [pysvn.Revision(x) for x in [pysvn.opt_revision_kind.base,
68
                                    pysvn.opt_revision_kind.working]]
69
# Override revisions from query string
773 by mattgiuca
cgirequest.py: Fixed req.path attribute, so it now DOES NOT include the
70
fields = req.get_fieldstorage()
71
field_r = fields.getlist("r")
823 by wagrant
diffservice: Refactor and polish. URL revision retrieval is less
72
for ri in range(len(field_r)):
879 by wagrant
diffservice, fileservice_lib: Factor out conversion of strings to
73
    r = common.svn.revision_from_string(field_r[ri])
823 by wagrant
diffservice: Refactor and polish. URL revision retrieval is less
74
    if r is not None:
75
        revs[ri] = r
562 by dcoles
Added new app: Diff (SVN diff application)
76
77
# Attempt to get the diff for these revisons
78
try:
79
    svnclient = pysvn.Client()
80
    diff = svnclient.diff
81
    diff_text = diff( '/tmp/svndiff',
823 by wagrant
diffservice: Refactor and polish. URL revision retrieval is less
82
        os.path.join('/home', req.path),
83
        revision1=revs[0],
84
        revision2=revs[1]
562 by dcoles
Added new app: Diff (SVN diff application)
85
    )
86
87
    # Split up the udiff into individual files
88
    diff_matcher = re.compile(
89
        r'^Index: (.*)\n\=+\n((?:[^I].*\n)*)',re.MULTILINE
90
    )
91
    split_diff = diff_matcher.findall(diff_text)
92
93
    # Prints output
94
    for filediff in split_diff:
95
        filename = filediff[0]
96
        diffbody = htmlfy_diff(filediff[1])
774 by mattgiuca
scripts/diffservice, diff.css: HTML refactor (simpler layout - it's not valid
97
        req.write('<p><b>File:</b> %s</p>\n'
98
            % cgi.escape(filename))
99
        req.write('<pre class="diff">%s</pre>\n' % diffbody)
562 by dcoles
Added new app: Diff (SVN diff application)
100
101
except pysvn._pysvn_2_5.ClientError, e:
774 by mattgiuca
scripts/diffservice, diff.css: HTML refactor (simpler layout - it's not valid
102
    req.write('<p><b>Error:</b></p>\n')
822 by wagrant
diffservice: Exceptions don't print well, so convert them to strings
103
    req.write('<pre class="diff">%s</pre>\n' % cgi.escape(str(e)))
562 by dcoles
Added new app: Diff (SVN diff application)
104
773 by mattgiuca
cgirequest.py: Fixed req.path attribute, so it now DOES NOT include the
105
req.write('</div>\n')