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

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/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.

from common import cgirequest
from cgi import parse_qs
import cgi
from os import path
import pysvn
import re

def htmlfy_diff(difftext):
    """Adds HTML markup to a udiff string"""
    output = cgi.escape(difftext)
    match_file = re.compile(r'^([\+\-]{3})\s(\S+)\s\((.+)\)$',re.MULTILINE)
    match_range = re.compile(r'^\@\@ (.*) \@\@$',re.MULTILINE)
    match_add = re.compile(r'^\+(.*)$',re.MULTILINE)
    match_sub = re.compile(r'^\-(.*)$',re.MULTILINE)
    match_special = re.compile(r'^\\(.*)$',re.MULTILINE)
    output = match_file.sub(
        r'<span class="diff-files">\1 \2 <em>(\3)</em></span>', output)
    output = match_range.sub(
        r'<span class="diff-range">@@ \1 @@</span>', output)
    output = match_add.sub(
        r'<span class="diff-add">+\1</span>', output)
    output = match_sub.sub(
        r'<span class="diff-sub">-\1</span>', output)
    output = match_special.sub(
        r'<span class="diff-special">\\\1</span>', output)
    return output

def get_revision(r_str):
    """ Returns a Revison object for pysvn from a r query string """
    if r_str == "HEAD":
        return pysvn.Revision( pysvn.opt_revision_kind.head )
    elif r_str == "WORKING":
        return pysvn.Revision( pysvn.opt_revision_kind.working )
    elif r_str == "BASE":
        return pysvn.Revision( pysvn.opt_revision_kind.base )
    else:
        try:
            r = int(r_str)
            return pysvn.Revision( pysvn.opt_revision_kind.number, r)
        except:
            return None
        
# Use a CGIRequest object to make the CGI environment look like the normal
# IVLE handler environment.

req = cgirequest.CGIRequest()
req.content_type = "text/html"

# Beginning of the page
req.write('<div id="ivle_padding">\n')
req.write('<h1>Diff</h1>\n')

# Work out the revisions from query
fields = req.get_fieldstorage()
field_r = fields.getlist("r")
# Default revisions
rev1=pysvn.Revision( pysvn.opt_revision_kind.base )
rev2=pysvn.Revision( pysvn.opt_revision_kind.working )
# Override revisions from query string
if len(field_r) >= 1:
    r1 = get_revision(field_r[0])
    if r1 != None:
        rev1 = r1
    if len(field_r) >= 2:
        r2 = get_revision(field_r[1])
        if r2 != None:
            rev2 = r2

# Attempt to get the diff for these revisons
try:
    svnclient = pysvn.Client()
    diff = svnclient.diff
    diff_text = diff( '/tmp/svndiff',
        path.join('/home/', req.path),
        revision1=rev1,
        #revision1=pysvn.Revision( pysvn.opt_revision_kind.base ),
        #url_or_path2=url_or_path,
        revision2=rev2,
        #revision2=pysvn.Revision( opt_revision_kind.working ),
        #recurse=True,
        #ignore_ancestry=False,
        #diff_deleted=True,
        #ignore_content_type=False,
        #header_encoding="",
        #diff_options=[]
    )

    # 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(e))

req.write('</div>\n')