~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
28
from cgi import parse_qs
772 by mattgiuca
diffservice: HTML-escape the output.
29
import cgi
562 by dcoles
Added new app: Diff (SVN diff application)
30
from os import path
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)
562 by dcoles
Added new app: Diff (SVN diff application)
37
    match_file = re.compile(r'^([\+\-]{3})\s(\S+)\s\((.+)\)$',re.MULTILINE)
38
    match_range = re.compile(r'^\@\@ (.*) \@\@$',re.MULTILINE)
39
    match_add = re.compile(r'^\+(.*)$',re.MULTILINE)
40
    match_sub = re.compile(r'^\-(.*)$',re.MULTILINE)
41
    match_special = re.compile(r'^\\(.*)$',re.MULTILINE)
42
    output = match_file.sub(
774 by mattgiuca
scripts/diffservice, diff.css: HTML refactor (simpler layout - it's not valid
43
        r'<span class="diff-files">\1 \2 <em>(\3)</em></span>', output)
562 by dcoles
Added new app: Diff (SVN diff application)
44
    output = match_range.sub(
774 by mattgiuca
scripts/diffservice, diff.css: HTML refactor (simpler layout - it's not valid
45
        r'<span class="diff-range">@@ \1 @@</span>', output)
562 by dcoles
Added new app: Diff (SVN diff application)
46
    output = match_add.sub(
774 by mattgiuca
scripts/diffservice, diff.css: HTML refactor (simpler layout - it's not valid
47
        r'<span class="diff-add">+\1</span>', output)
562 by dcoles
Added new app: Diff (SVN diff application)
48
    output = match_sub.sub(
774 by mattgiuca
scripts/diffservice, diff.css: HTML refactor (simpler layout - it's not valid
49
        r'<span class="diff-sub">-\1</span>', output)
562 by dcoles
Added new app: Diff (SVN diff application)
50
    output = match_special.sub(
774 by mattgiuca
scripts/diffservice, diff.css: HTML refactor (simpler layout - it's not valid
51
        r'<span class="diff-special">\\\1</span>', output)
562 by dcoles
Added new app: Diff (SVN diff application)
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 )
576 by dcoles
diffservice: Small code cleanup
62
    else:
63
        try:
64
            r = int(r_str)
586 by dcoles
diffservice: Fixed oops in cleaned up code, also changed the date and author
65
            return pysvn.Revision( pysvn.opt_revision_kind.number, r)
576 by dcoles
diffservice: Small code cleanup
66
        except:
67
            return None
562 by dcoles
Added new app: Diff (SVN diff application)
68
        
69
# Use a CGIRequest object to make the CGI environment look like the normal
566 by dcoles
diff.css: Foreground colours for hilighting of changes
70
# IVLE handler environment.
562 by dcoles
Added new app: Diff (SVN diff application)
71
72
req = cgirequest.CGIRequest()
773 by mattgiuca
cgirequest.py: Fixed req.path attribute, so it now DOES NOT include the
73
req.content_type = "text/html"
562 by dcoles
Added new app: Diff (SVN diff application)
74
566 by dcoles
diff.css: Foreground colours for hilighting of changes
75
# Beginning of the page
773 by mattgiuca
cgirequest.py: Fixed req.path attribute, so it now DOES NOT include the
76
req.write('<div id="ivle_padding">\n')
77
req.write('<h1>Diff</h1>\n')
562 by dcoles
Added new app: Diff (SVN diff application)
78
79
# Work out the revisions from query
773 by mattgiuca
cgirequest.py: Fixed req.path attribute, so it now DOES NOT include the
80
fields = req.get_fieldstorage()
81
field_r = fields.getlist("r")
562 by dcoles
Added new app: Diff (SVN diff application)
82
# Default revisions
83
rev1=pysvn.Revision( pysvn.opt_revision_kind.base )
84
rev2=pysvn.Revision( pysvn.opt_revision_kind.working )
773 by mattgiuca
cgirequest.py: Fixed req.path attribute, so it now DOES NOT include the
85
# Override revisions from query string
86
if len(field_r) >= 1:
87
    r1 = get_revision(field_r[0])
88
    if r1 != None:
89
        rev1 = r1
90
    if len(field_r) >= 2:
91
        r2 = get_revision(field_r[1])
562 by dcoles
Added new app: Diff (SVN diff application)
92
        if r2 != None:
93
            rev2 = r2
94
95
# Attempt to get the diff for these revisons
96
try:
97
    svnclient = pysvn.Client()
98
    diff = svnclient.diff
99
    diff_text = diff( '/tmp/svndiff',
773 by mattgiuca
cgirequest.py: Fixed req.path attribute, so it now DOES NOT include the
100
        path.join('/home/', req.path),
562 by dcoles
Added new app: Diff (SVN diff application)
101
        revision1=rev1,
102
        #revision1=pysvn.Revision( pysvn.opt_revision_kind.base ),
103
        #url_or_path2=url_or_path,
104
        revision2=rev2,
105
        #revision2=pysvn.Revision( opt_revision_kind.working ),
106
        #recurse=True,
107
        #ignore_ancestry=False,
108
        #diff_deleted=True,
109
        #ignore_content_type=False,
110
        #header_encoding="",
111
        #diff_options=[]
112
    )
113
114
    # Split up the udiff into individual files
115
    diff_matcher = re.compile(
116
        r'^Index: (.*)\n\=+\n((?:[^I].*\n)*)',re.MULTILINE
117
    )
118
    split_diff = diff_matcher.findall(diff_text)
119
120
    # Prints output
121
    for filediff in split_diff:
122
        filename = filediff[0]
123
        diffbody = htmlfy_diff(filediff[1])
774 by mattgiuca
scripts/diffservice, diff.css: HTML refactor (simpler layout - it's not valid
124
        req.write('<p><b>File:</b> %s</p>\n'
125
            % cgi.escape(filename))
126
        req.write('<pre class="diff">%s</pre>\n' % diffbody)
562 by dcoles
Added new app: Diff (SVN diff application)
127
128
except pysvn._pysvn_2_5.ClientError, e:
774 by mattgiuca
scripts/diffservice, diff.css: HTML refactor (simpler layout - it's not valid
129
    req.write('<p><b>Error:</b></p>\n')
130
    req.write('<pre class="diff">%s</pre>\n' % cgi.escape(e))
562 by dcoles
Added new app: Diff (SVN diff application)
131
773 by mattgiuca
cgirequest.py: Fixed req.path attribute, so it now DOES NOT include the
132
req.write('</div>\n')