~abentley/bundlebuggy/trunk

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# Copyright (C) 2006, 2007 Aaron Bentley
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


import logging
from StringIO import StringIO

from bzrlib.errors import (
    NotABundle,
    BundleNotSupported,
    BadBundle,
    NotAMergeDirective,
    )
from bzrlib.bundle import serializer
from bzrlib.merge_directive import MergeDirective

import cherrypy

from turbogears import controllers, expose, redirect, url
from turbogears import identity
from turbogears.database import session

from bundlebuggy.configfile import Config
from bundlebuggy import jobs
from bundlebuggy.model import (
    Dupe,
    Group,
    Project,
    Merge,
    MergeRequest,
    Submitter,
    Supersede,
    User,
    )
from bundlebuggy.mail import (
    send_vote,
    )
from bundlebuggy import mail_queue
from bundlebuggy import routing, util

log = logging.getLogger("bundlebuggy.controllers")


class UserError(Exception):
    pass


def profiled(func):
    """Decorator to profile function under lsprof"""
    import os.path
    from bzrlib.lsprof import profile
    def apply_profiled(*args, **kwargs):
        ret, stats = profile(func, *args, **kwargs)
        for n in range(1000):
            path = 'callgrind.out.%s.%d' % (func.__name__, n)
            if not os.path.exists(path):
                break
        stats.save(path)
        return ret
    apply_profiled.exposed = True
    return apply_profiled


def user_voted(request):
    user_id = identity.current.user.user_id
    for vote in request.votes:
        if vote.user_id == user_id and vote.value != 'comment':
            return True
    else:
        return False


def is_mine(request, status, user):
    if request.submitter in user.submitters:
        return status in ('Approved', 'Conditionally approved', 'Resubmit')
    elif user in [v.user for v in request.votes if
                  v.value in ('approve', 'tweak', '+1')]:
        return status in ('Waiting', 'Semi-approved', 'Approved',
                          'Conditionally approved')
    else:
        return False


class MailController(controllers.Controller):

    def get_queue(self):
        return mail_queue.MailQueue(Config().get_mail_queue_location())

    @expose(template="bundlebuggy.templates.mail_root")
    def list(self):
        queue = self.get_queue()
        return {
            'queued': queue.list(),
            'frozen': queue.list_frozen(),
        }

    @expose(template="bundlebuggy.templates.mail_message")
    def queued(self, filename):
        text = self.get_queue().open(filename).read()
        return {'text': text}

    @expose(template="bundlebuggy.templates.mail_message")
    def frozen(self, filename):
        text = self.get_queue().open_frozen(filename).read()
        return {'text': text}


class RequestController(controllers.Controller):

    @expose(template="bundlebuggy.templates.welcome")
    def list(self, project_id=None, selected="pending", start='0', count='24',
              unreviewed='y'):
        if project_id is None:
            project_id = Config().get_default_project()
        curproject = Project.get(project_id)
        unreviewed = (unreviewed == 'y' and selected == 'pending')
        if identity.current.user is None:
            unreviewed = False
            if selected in ('mine', 'todo'):
                raise identity.IdentityFailure([])
        skip_approved = unreviewed
        start = int(start)
        count = int(count)
        if selected == "merged":
            requests = ((m.request, m.request.get_status())
                        for m in reversed(Merge.query.filter(
                        """merge.id in (
                         SELECT merge.id
                             FROM merge
                             LEFT OUTER JOIN mergerequest ON merge.request_id =
                             mergerequest.id WHERE
                             mergerequest.project_id = '%s')""" %
                             project_id).all())
                        if m.request is not None)
        elif selected in ("pending", 'mine', 'resubmit'):
            requests = ((r, r.get_status()) for r in
                        MergeRequest.iter_maybe_pending(project_id))
        elif selected == "todo":
            all_reqs = ((r, r.get_status()) for r in
                        MergeRequest.user_nonterminal(
                        identity.current.user).filter_by(
                        project_id=project_id).order_by(
                        MergeRequest.c.date.desc()))
        else:
            requests = ((r, r.get_status()) for r in
                        MergeRequest.query.filter_by(project_id=project_id
                        ).order_by(MergeRequest.c.date.desc()))
        if selected == 'pending':
            all_reqs = ((r, s) for r, s in requests if 
                        s not in ('Vetoed', 'Merged', 'Superseded', 'Dupe',
                                  'Resubmit'))
        if selected == 'mine':
            all_reqs = ((r, s) for r, s in requests if is_mine(r, s,
                        identity.current.user))
        elif selected == "resubmit":
            all_reqs = ((r, s) for r, s in requests if s == 'Resubmit')
        elif selected == "merged":
            all_reqs = ((r, s) for r, s in requests if r.merge is not None)
        elif selected == "rejected":
            all_reqs = ((r, s) for r, s in requests if s in ('Vetoed',
                        'Superseded', 'Dupe'))
        if unreviewed:
            all_reqs = ((r, s) for r, s in all_reqs if not user_voted(r))
        if skip_approved:
            all_reqs = ((r, s) for r, s in all_reqs if s not in
                ('Approved', 'Conditionally approved'))

        reqs, prev, next = get_matching(all_reqs, start, count)
        return dict(merge_requests=reqs, start=start, selected=selected,
                    next=next, prev=prev, merge_date=(selected == 'merged'),
                    unreviewed=unreviewed, project_id=project_id,
                    curproject=curproject)

    @expose(template='bundlebuggy.templates.help')
    def help(self):
        return dict(config=Config())

    @expose(template='bundlebuggy.templates.request')
    def view(self, request_id, project_id=None, action=None):
        request = MergeRequest.get(request_id)
        if request is None:
            raise cherrypy.NotFound
        project_id = request.project_id
        slushpile = Config().open_slushpile()
        slushpile.lock_read()
        try:
            data = self.merge_directive_data(request, slushpile)
            messages, merge_message, nick, based_on = data
            return dict(request=request, messages=messages, nick=nick,
                        merge_message=merge_message, based_on=based_on,
                        project_id=project_id,
                        curproject=Project.get(project_id))
        finally:
            slushpile.unlock()

    @staticmethod
    def merge_directive_data(req, slushpile):
        nick = None
        commit_messages = []
        if req.patch_text is None:
            return [], None, None, []
        try:
            patch_lines = list(StringIO(req.patch_text))
            directive = MergeDirective.from_lines(patch_lines)
        except NotAMergeDirective:
            merge_directive_message = None
            bundle_text = req.patch_text
            directive = None
            base_revision_id = None
        else:
            merge_directive_message = directive.message
            bundle_text = directive.get_raw_bundle()
            base_revision_id = directive.base_revision_id
        if base_revision_id is None:
            based_on = []
            if bundle_text is None:
                commits = []
            else:
                try:
                    bundle_info = serializer.read_bundle(StringIO(bundle_text))
                except (NotABundle, BundleNotSupported, BadBundle):
                    commits = []
                else:
                    commits = bundle_info.real_revisions
        else:
            based_on = MergeRequest.query.filter_by(
                head_revision=base_revision_id).all()
            graph = slushpile.get_graph()
            if graph.get_parent_map([req.head_revision]) == {}:
                if bundle_text != None:
                    slushpile.unlock()
                    slushpile.lock_write()
                    directive.install_revisions(slushpile)
                    graph = slushpile.get_graph()
            commit_ids = mainline_commits(graph, req.head_revision,
                                          base_revision_id)
            commits = slushpile.get_revisions(commit_ids)
        for revision in commits:
            commit_messages.append(revision.message)
            if revision.revision_id == req.head_revision:
                nick = revision.properties.get('branch-nick')
        return commit_messages, merge_directive_message, nick, based_on

    @expose(content_type='text/x-patch')
    def download_patch(self, request_id):
        cherrypy.response.headers['content-type'] = \
            'application/binary-octet-string'
        request = MergeRequest.get(request_id)
        if request is None or request.patch_text is None:
            raise cherrypy.NotFound
        filename = request.filename
        if filename is None:
            filename = 'patch.txt'
        cherrypy.response.headers['content-disposition'] = \
            'attachment; filename=%s' % filename
        return str(request.patch_text)

    @identity.require(identity.has_permission("vote"))
    def vote(self, request_id, votevalue=None, comment=None):
        if votevalue is None:
            raise UserError('Please choose a value to vote.')
        if comment == '':
            comment = None
        vote = identity.current.user.set_vote(MergeRequest.get(request_id),
                                              votevalue, comment)
        send_vote(vote)
        redirect(url(['/request', request_id]))

    @identity.require(identity.has_permission("vote"))
    def merge(self, request_id, project_id=None):
        user_id = identity.current.user.user_id
        merge = Merge(user_id=user_id, request_id=request_id)
        merge.flush()
        self.go_back(merge.request)

    def go_back(self, request):
        redirect(routing.request_url(request))

    @identity.require(identity.has_permission("vote"))
    def unmerge(self, request_id):
        req = MergeRequest.get(request_id)
        req.merge = None
        req.flush()
        self.go_back(req)

    @identity.require(identity.has_permission("vote"))
    def supersede(self, request_id):
        user_id = identity.current.user.user_id
        supersede = Supersede(user_id=user_id, old_request_id=request_id)
        supersede.flush()
        mr = MergeRequest.get(request_id)
        assert mr.supersede == supersede
        self.go_back(mr)

    @identity.require(identity.has_permission("vote"))
    def unsupersede(self, request_id):
        user_id = identity.current.user.user_id
        mr = MergeRequest.get(request_id)
        mr.supersede = None
        self.go_back(mr)

    @identity.require(identity.has_permission("vote"))
    def dupe(self, request_id):
        user_id = identity.current.user.user_id
        dupe = Dupe(user_id=user_id, request_id=request_id)
        dupe.flush()
        mr = MergeRequest.get(request_id)
        assert mr.dupe == dupe
        self.go_back(mr)

    @identity.require(identity.has_permission("vote"))
    def undupe(self, request_id):
        mr = MergeRequest.get(request_id)
        mr.dupe = None
        self.go_back(mr)

    @identity.require(identity.has_permission("vote"))
    @expose(template="bundlebuggy.templates.change_project")
    def change_project(self, project_id, request_id):
        projects = [p for p in Project.query.order_by(Project.c.display_name)
                    if p.id != project_id]
        return {'request': MergeRequest.get(request_id),
                'project_id': project_id,
                'projects': projects
                }

    @identity.require(identity.has_permission("vote"))
    def new_project(self, project_id, request_id, new_project_id):
        request = MergeRequest.get(request_id)
        project = Project.get(new_project_id)
        request.project = project
        request.flush()
        self.go_back(request)

    def newmail(self, message):
        try:
            mail_file = StringIO(message.encode('utf-8'))
            return jobs.handle_mail(mail_file, Config().open_slushpile())
        except Exception, err:
            return 'Error: %s\n' % err


class ProjectController(controllers.Controller):

    @expose(template='bundlebuggy.templates.project_list')
    def list(self):
        return {'projects': Project.query.all()}

    @expose(template='bundlebuggy.templates.project_view')
    def view(self, project_id):
        return {'curproject': Project.get(project_id)}


def is_admin(project, user=None):
    if user is None:
        user = identity.current.user
    if user is not None and user in project.admins:
        return True
    else:
        return False


def require_project_admin(func):
    def decorator(self, project_id, **kwargs):
        project = Project.get(project_id)
        if not is_admin(project):
            raise UserError('You do not have permission to add voters to %s.' %
                            project.display_name)
        return func(self, project_id=project_id, **kwargs)
    return decorator


class VoterController(controllers.Controller):

    @expose(template='bundlebuggy.templates.new_voter')
    @require_project_admin
    def new_voter(self, project_id):
        return {'curproject': Project.get(project_id)}

    @expose(template='bundlebuggy.templates.show_submitter')
    @require_project_admin
    def find_for_add(self, project_id, email_address):
        return {'submitter': Submitter.acquire(email_address),
                'curproject': Project.get(project_id)}

    @require_project_admin
    def create_voter(self, project_id, email_address, display_name=None,
                     user_name=None, password=None, password_confirm=None):
        submitter = Submitter.acquire(email_address)
        user = submitter.user
        if user is None:
            if password != password_confirm:
                raise UserError("Passwords do not match.")
            user = User(user_name=user_name, display_name=display_name,
                        password=password)
            user.submitters.append(submitter)
            group = Group.get_by(group_name='coredev')
            user.groups.append(group)
        project = Project.get(project_id)
        user.projects.append(project)
        redirect(routing.url_for(controller='project', action='list'))


class UserController(controllers.Controller):

    @expose(template="bundlebuggy.templates.login")
    def login(self, forward_url=None, previous_url=None, *args, **kw):

        if not identity.current.anonymous \
            and identity.was_login_attempted() \
            and not identity.get_identity_errors():
            raise redirect(forward_url)

        forward_url = None
        previous_url = cherrypy.request.path

        if identity.was_login_attempted():
            msg=_("The credentials you supplied were not correct or "
                   "did not grant access to this resource.")
        elif identity.get_identity_errors():
            msg=_("You must provide your credentials before accessing "
                   "this resource.")
        else:
            msg=_("Please log in.")
            forward_url= cherrypy.request.headers.get("Referer", "/")
        cherrypy.response.status=403
        return dict(message=msg, previous_url=previous_url, logging_in=True,
                    original_parameters=cherrypy.request.params,
                    forward_url=forward_url)

    def logout(self):
        identity.current.logout()
        raise redirect("/")

class Root(controllers.RootController):

    def __init__(self, *args, **kwargs):
        controllers.RootController.__init__(self, *args, **kwargs)
        self.controllers = {
            'mail': MailController(),
            'project': ProjectController(),
            'request': RequestController(),
            'user': UserController(),
            'voter': VoterController(),
            }
        self.mapper = routing.make_mapper()


    @expose()
    def default(self, *path, **kwargs):
        result = self.mapper.match('/'+'/'.join(path))
        if result is None:
            raise cherrypy.NotFound
        result_copy = dict(result)
        controller_name = result_copy.pop('controller')
        action_name = result_copy.pop('action')
        result_copy.update(kwargs)
        controller = self.controllers[controller_name]
        return getattr(controller, action_name)(**result_copy)

    def _cp_on_error(self):
        traceback = util.traceback_string()
        try:
            raise
        except UserError, e:
            message = e.args[0]
            cherrypy.response.headerMap['Status'] = '200 OK'
            heading = 'Error'
            traceback = None
        except Exception, e:
            message = None
            cherrypy.response.headerMap['Status'] = '500 Internal Server Error'
            heading = '500 Internal Server Error'
        cherrypy.response.body = self._errorpage(heading, message, traceback,
                                                 show_params=True)

    def _cp_on_http_error(self, status, message):
        cherrypy.response.headerMap['Status'] = status
        cherrypy.response.body = self._errorpage(status, message, None)

    @expose(template='bundlebuggy.templates.error')
    def _errorpage(self, heading, message, traceback, show_params=False):
        if show_params:
            params = cherrypy.request.params
        else:
            params = None
        return dict(message=message, heading=heading, traceback=traceback,
                    params=params)


def get_matching(all_reqs, start, count):
    reqs = []
    for n, blob in enumerate(all_reqs):
        if n >= start:
            reqs.append(blob)
            if len(reqs) == count+1:
                break
    if len(reqs) > count:
        next = start+count
    else:
        next = None
    if start > 0:
        prev = start - count
        if prev < 0:
            prev = 0
    else:
        prev = None
    return reqs[:count], prev, next


def matching_requests(q, reqs):
    for req in reqs:
        if q in req.submitter_id:
            yield req, req.get_status()
        elif q in req.summary:
            yield req, req.get_status()
        elif req.text is not None and q in req.text:
            yield req, req.get_status()
        elif q in req.get_unicode_patch():
            yield req, req.get_status()


def lefthand_history(graph, head):
    current_revision = head
    while True:
        yield current_revision
        map = graph.get_parent_map([current_revision])
        parents = map.get(current_revision)
        if parents is None:
            break
        if len(parents) == 0:
            break
        current_revision = parents[0]


def mainline_commits(graph, head, base):
    """Return a list of mainline commits between head and base."""
    allowed_revisions, _unused = graph.find_difference(head, base)
    result = []
    for revision in lefthand_history(graph, head):
        if revision not in allowed_revisions:
            break
        result.append(revision)
    return result