~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/revision_ui.py

add a '-C' option for ensuring loggerhead is running (from a cron job, for
example), and clean up the main() code a bit in the process.  also added
log rolling (daily, weekly, or none).

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
#
19
19
 
20
 
try:
21
 
    import simplejson
22
 
except ImportError:
23
 
    import json as simplejson
24
 
import urllib
 
20
import datetime
 
21
import logging
 
22
import os
 
23
import textwrap
 
24
import time
25
25
 
26
 
from paste.httpexceptions import HTTPServerError
 
26
import turbogears
 
27
from cherrypy import InternalError, session
27
28
 
28
29
from loggerhead import util
29
 
from loggerhead.controllers import TemplatedBranchView
30
 
from loggerhead.controllers.filediff_ui import diff_chunks_for_file
31
 
 
32
 
 
33
 
DEFAULT_LINE_COUNT_LIMIT = 3000
34
 
 
35
 
def dq(p):
36
 
    return urllib.quote(urllib.quote(p, safe=''))
37
 
 
38
 
 
39
 
class RevisionUI(TemplatedBranchView):
40
 
 
41
 
    template_path = 'loggerhead.templates.revision'
42
 
 
43
 
    def get_values(self, path, kwargs, headers):
44
 
        h = self._history
45
 
        revid = self.get_revid()
46
 
 
47
 
        filter_file_id = kwargs.get('filter_file_id', None)
48
 
        start_revid = h.fix_revid(kwargs.get('start_revid', None))
49
 
        query = kwargs.get('q', None)
50
 
        remember = h.fix_revid(kwargs.get('remember', None))
51
 
        compare_revid = h.fix_revid(kwargs.get('compare_revid', None))
52
 
 
 
30
 
 
31
 
 
32
class RevisionUI (object):
 
33
 
 
34
    def __init__(self, branch):
 
35
        # BranchView object
 
36
        self._branch = branch
 
37
        self.log = branch.log
 
38
    
 
39
#    @util.lsprof
 
40
    @util.strip_whitespace
 
41
    @turbogears.expose(html='loggerhead.templates.revision')
 
42
    def default(self, *args, **kw):
 
43
        z = time.time()
 
44
        h = self._branch.get_history()
 
45
        util.set_context(kw)
 
46
        
 
47
        if len(args) > 0:
 
48
            revid = h.fix_revid(args[0])
 
49
        else:
 
50
            revid = None
 
51
        
 
52
        file_id = kw.get('file_id', None)
 
53
        start_revid = h.fix_revid(kw.get('start_revid', None))
 
54
        query = kw.get('q', None)
 
55
        remember = kw.get('remember', None)
 
56
        compare_revid = kw.get('compare_revid', None)
 
57
        
53
58
        try:
54
 
            revid, start_revid, revid_list = h.get_view(revid,
55
 
                                                        start_revid,
56
 
                                                        filter_file_id,
57
 
                                                        query)
 
59
            revid, start_revid, revid_list = h.get_view(revid, start_revid, file_id, query)
58
60
        except:
59
61
            self.log.exception('Exception fetching changes')
60
 
            raise HTTPServerError('Could not fetch changes')
61
 
 
62
 
        navigation = util.Container(
63
 
            revid_list=revid_list, revid=revid, start_revid=start_revid,
64
 
            filter_file_id=filter_file_id, pagesize=1,
65
 
            scan_url='/revision', branch=self._branch, feed=True, history=h)
 
62
            raise InternalError('Could not fetch changes')
 
63
        
 
64
        navigation = util.Container(revid_list=revid_list, revid=revid, start_revid=start_revid, file_id=file_id,
 
65
                                    pagesize=1, scan_url='/revision', branch=self._branch, feed=True)
66
66
        if query is not None:
67
67
            navigation.query = query
68
 
        util.fill_in_navigation(navigation)
69
 
 
70
 
        change = h.get_changes([revid])[0]
71
 
 
72
 
        if compare_revid is None:
73
 
            file_changes = h.get_file_changes(change)
74
 
        else:
75
 
            file_changes = h.file_changes_for_revision_ids(
76
 
                compare_revid, change.revid)
77
 
 
78
 
        if path in ('', '/'):
79
 
            path = None
80
 
 
81
 
        link_data = {}
82
 
        path_to_id = {}
83
 
        if path:
84
 
            item = [x for x in file_changes.text_changes if x.filename == path][0]
85
 
            diff_chunks = diff_chunks_for_file(
86
 
                self._history._branch.repository, item.file_id,
87
 
                item.old_revision, item.new_revision)
88
 
        else:
89
 
            diff_chunks = None
90
 
            for i, item in enumerate(file_changes.text_changes):
91
 
                item.index = i
92
 
                link_data['diff-' + str(i)] = '%s/%s/%s' % (
93
 
                    dq(item.new_revision), dq(item.old_revision), dq(item.file_id))
94
 
                path_to_id[item.filename] = 'diff-' + str(i)
95
 
 
96
 
        h.add_branch_nicks(change)
97
 
 
98
 
        if '.' in change.revno:
99
 
            # Walk "up" though the merge-sorted graph until we find a
100
 
            # revision with merge depth 0: this is the revision that merged
101
 
            # this one to mainline.
102
 
            ri = self._history._rev_info
103
 
            i = self._history._rev_indices[change.revid]
104
 
            while ri[i][0][2] > 0:
105
 
                i -= 1
106
 
            merged_in = ri[i][0][3]
107
 
        else:
108
 
            merged_in = None
109
 
 
110
 
        # Directory Breadcrumbs
111
 
        directory_breadcrumbs = (
112
 
            util.directory_breadcrumbs(
113
 
                self._branch.friendly_name,
114
 
                self._branch.is_root,
115
 
                'changes'))
116
 
 
117
 
        return {
 
68
        util.fill_in_navigation(h, navigation)
 
69
 
 
70
        if compare_revid is not None:
 
71
            change = h.get_diff(compare_revid, revid)
 
72
        else:
 
73
            change = h.get_changes([ revid ], get_diffs=True)[0]
 
74
        # add parent & merge-point branch-nick info, in case it's useful
 
75
        h.get_branch_nicks([ change ])
 
76
 
 
77
        # let's make side-by-side diff be the default
 
78
        side_by_side = not kw.get('unified', False)
 
79
        if side_by_side:
 
80
            h.add_side_by_side([ change ])
 
81
        
 
82
        vals = {
118
83
            'branch': self._branch,
119
84
            'revid': revid,
120
85
            'change': change,
121
 
            'file_changes': file_changes,
122
 
            'diff_chunks': diff_chunks,
123
 
            'link_data': simplejson.dumps(link_data),
124
 
            'specific_path': path,
125
 
            'json_specific_path': simplejson.dumps(path),
126
 
            'path_to_id': simplejson.dumps(path_to_id),
127
86
            'start_revid': start_revid,
128
 
            'filter_file_id': filter_file_id,
 
87
            'file_id': file_id,
129
88
            'util': util,
130
89
            'history': h,
131
 
            'merged_in': merged_in,
132
90
            'navigation': navigation,
133
91
            'query': query,
134
92
            'remember': remember,
135
93
            'compare_revid': compare_revid,
136
 
            'url': self._branch.context_url,
137
 
            'directory_breadcrumbs': directory_breadcrumbs,
 
94
            'side_by_side': side_by_side,
138
95
        }
 
96
        h.flush_cache()
 
97
        self.log.info('/revision: %r seconds' % (time.time() - z,))
 
98
        return vals