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

« back to all changes in this revision

Viewing changes to scripts/diffservice

  • Committer: William Grant
  • Date: 2009-03-17 07:12:11 UTC
  • Revision ID: grantw@unimelb.edu.au-20090317071211-641qkzx7nofnkpgy
Stream files from serveservice, rather than reading the whole file in at once.

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: Matt Giuca
22
 
# Date:   25/2/2008
23
 
 
24
 
# A CGI script for handling file requests. This is a wrapper around the
25
 
# FileService library module. It is intended to be run from the trampoline by
26
 
# the FileService application.
27
 
# (The purpose of all of this is to let FileService read and write the user's
28
 
# file system as them).
29
 
 
30
 
from common import cgirequest
31
 
from cgi import parse_qs
32
 
from os import path
33
 
import pysvn
34
 
import re
35
 
import urlparse
36
 
 
37
 
def htmlfy_diff(difftext):
38
 
    """Adds HTML markup to a udiff string"""
39
 
    output = difftext
40
 
    match_file = re.compile(r'^([\+\-]{3})\s(\S+)\s\((.+)\)$',re.MULTILINE)
41
 
    match_range = re.compile(r'^\@\@ (.*) \@\@$',re.MULTILINE)
42
 
    match_add = re.compile(r'^\+(.*)$',re.MULTILINE)
43
 
    match_sub = re.compile(r'^\-(.*)$',re.MULTILINE)
44
 
    match_special = re.compile(r'^\\(.*)$',re.MULTILINE)
45
 
    output = match_file.sub(
46
 
        r'<span class="diff_files">\1 \2 <em>(\3)</em></span>', output)
47
 
    output = match_range.sub(
48
 
        r'<span class="diff_range">@@ \1 @@</span>', output)
49
 
    output = match_add.sub(
50
 
        r'<span class="diff_add">+\1</span>', output)
51
 
    output = match_sub.sub(
52
 
        r'<span class="diff_sub">-\1</span>', output)
53
 
    output = match_special.sub(
54
 
        r'<span class="diff_special">\\\1</span>', output)
55
 
    return output
56
 
 
57
 
def get_revision(r_str):
58
 
    """ Returns a Revison object for pysvn from a r query string """
59
 
    if r_str == "HEAD":
60
 
        return pysvn.Revision( pysvn.opt_revision_kind.head )
61
 
    elif r_str == "WORKING":
62
 
        return pysvn.Revision( pysvn.opt_revision_kind.working )
63
 
    elif r_str == "BASE":
64
 
        return pysvn.Revision( pysvn.opt_revision_kind.base )
65
 
    else:
66
 
        try:
67
 
            r = int(r_str)
68
 
            return pysvn.Revision( pysvn.opt_revision_kind.number, r))
69
 
        except:
70
 
            return None
71
 
        
72
 
# Use a CGIRequest object to make the CGI environment look like the normal
73
 
# IVLE handler environment.
74
 
 
75
 
req = cgirequest.CGIRequest()
76
 
 
77
 
# CGI Headers
78
 
print("Content-Type: text/html\n")
79
 
print("\n")
80
 
 
81
 
# Beginning of the page
82
 
print('<div id="ivle_padding">')
83
 
print('<h1>Diff</h1>')
84
 
 
85
 
# Work out the revisions from query
86
 
url = urlparse.urlparse(req.path)
87
 
querystr = url[4]
88
 
urlpath = url[2]
89
 
query = parse_qs(querystr)
90
 
# Default revisions
91
 
rev1=pysvn.Revision( pysvn.opt_revision_kind.base )
92
 
rev2=pysvn.Revision( pysvn.opt_revision_kind.working )
93
 
if 'r' in query:
94
 
    if len(query['r']) == 2:
95
 
        r2 = get_revision(query['r'][1])
96
 
        if r2 != None:
97
 
            rev2 = r2
98
 
    r1 = get_revision(query['r'][0])
99
 
    if r1 != None:
100
 
        rev1=r1
101
 
 
102
 
# Attempt to get the diff for these revisons
103
 
try:
104
 
    svnclient = pysvn.Client()
105
 
    diff = svnclient.diff
106
 
    diff_text = diff( '/tmp/svndiff',
107
 
        path.join('/home/', urlpath),
108
 
        revision1=rev1,
109
 
        #revision1=pysvn.Revision( pysvn.opt_revision_kind.base ),
110
 
        #url_or_path2=url_or_path,
111
 
        revision2=rev2,
112
 
        #revision2=pysvn.Revision( opt_revision_kind.working ),
113
 
        #recurse=True,
114
 
        #ignore_ancestry=False,
115
 
        #diff_deleted=True,
116
 
        #ignore_content_type=False,
117
 
        #header_encoding="",
118
 
        #diff_options=[]
119
 
    )
120
 
 
121
 
    # Split up the udiff into individual files
122
 
    diff_matcher = re.compile(
123
 
        r'^Index: (.*)\n\=+\n((?:[^I].*\n)*)',re.MULTILINE
124
 
    )
125
 
    split_diff = diff_matcher.findall(diff_text)
126
 
 
127
 
    # Prints output
128
 
    print("<dl>")
129
 
    for filediff in split_diff:
130
 
        filename = filediff[0]
131
 
        diffbody = htmlfy_diff(filediff[1])
132
 
        print("<dt><strong>File:</strong> " + filename + "</dt>")
133
 
        print("<dd><code><pre>" + diffbody + "</pre></code></dd>")
134
 
    print("</dl>")
135
 
 
136
 
except pysvn._pysvn_2_5.ClientError, e:
137
 
    print("<dl><dt><strong>Error:</strong></dt><dd>")
138
 
    print(e)
139
 
    print("</dd></dl>")
140
 
 
141
 
print('</div>')