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

« back to all changes in this revision

Viewing changes to services/diffservice

  • Committer: matt.giuca
  • Date: 2009-01-12 00:36:24 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1074
Removed "prototypes" directory (very old and clearly no longer used).

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# along with this program; if not, write to the Free Software
18
18
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 
20
 
# Author: David Coles, Will Grant
21
 
 
22
 
'''
23
 
A script for generating a Subversion diff. It is intended to be run via
24
 
trampoline by ivle.webapp.filesystem.diff.DiffView.
25
 
'''
26
 
 
27
 
import os
28
 
import sys
29
 
 
30
 
import cjson
 
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 common.svn
 
29
import cgi
 
30
import os.path
31
31
import pysvn
32
 
 
33
 
import ivle.svn
34
 
 
 
32
import re
 
33
 
 
34
def htmlfy_diff(difftext):
 
35
    """Adds HTML markup to a udiff string"""
 
36
    output = cgi.escape(difftext)
 
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
 
 
53
    return output
 
54
 
 
55
# Use a CGIRequest object to make the CGI environment look like the normal
 
56
# IVLE handler environment.
 
57
 
 
58
req = cgirequest.CGIRequest()
 
59
req.install_error_handler()
 
60
req.content_type = "text/html"
 
61
 
 
62
# Beginning of the page
 
63
req.write('<div id="ivle_padding">\n')
 
64
req.write('<h1>Diff</h1>\n')
35
65
 
36
66
# Default revisions
37
67
revs = [pysvn.Revision(x) for x in [pysvn.opt_revision_kind.base,
38
68
                                    pysvn.opt_revision_kind.working]]
39
 
 
40
 
# Override revisions from argv.
41
 
for ri in range(len(sys.argv[2:4])):
42
 
    r = ivle.svn.revision_from_string(sys.argv[2 + ri])
 
69
# Override revisions from query string
 
70
fields = req.get_fieldstorage()
 
71
field_r = fields.getlist("r")
 
72
for ri in range(len(field_r)):
 
73
    r = common.svn.revision_from_string(field_r[ri])
43
74
    if r is not None:
44
75
        revs[ri] = r
45
76
 
46
77
# Attempt to get the diff for these revisons
47
78
try:
48
79
    svnclient = pysvn.Client()
49
 
    svnclient.exception_style = 1
50
80
    diff = svnclient.diff
51
81
    diff_text = diff( '/tmp/svndiff',
52
 
        os.path.join('/home', sys.argv[1]),
 
82
        os.path.join('/home', req.path),
53
83
        revision1=revs[0],
54
84
        revision2=revs[1]
55
85
    )
56
 
    print cjson.encode({'diff': diff_text})
57
 
except pysvn.ClientError, e:
58
 
    error = e[0]
59
 
 
60
 
    try:
61
 
        code = e[1][0][1]
62
 
        # See subversion/include/svn_error_codes.h.
63
 
        # 155007: WC_NOT_DIRECTORY.
64
 
        # 160013: FS_NOT_FOUND
65
 
        # 200005: UNVERSIONED_RESOURCE
66
 
        if code in (155007, 160013, 200005):
67
 
            error = 'notfound'
68
 
        else:
69
 
            error = '%s (code %d)' % (error, code) 
70
 
    except IndexError:
71
 
        pass
72
 
 
73
 
    print cjson.encode({'error': error})
 
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])
 
97
        req.write('<p><b>File:</b> %s</p>\n'
 
98
            % cgi.escape(filename))
 
99
        req.write('<pre class="diff">%s</pre>\n' % diffbody)
 
100
 
 
101
except pysvn._pysvn_2_5.ClientError, e:
 
102
    req.write('<p><b>Error:</b></p>\n')
 
103
    req.write('<pre class="diff">%s</pre>\n' % cgi.escape(str(e)))
 
104
 
 
105
req.write('</div>\n')