/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4597.9.8 by Vincent Ladeuil
Merge bzr.dev into cleanup
1
# Copyright (C) 2010 Canonical Ltd
4969.2.16 by Aaron Bentley
Updates from review.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
4969.2.10 by Aaron Bentley
Cleanup and docs.
18
import webbrowser
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
19
20
from bzrlib import (
21
    errors,
5184.1.1 by Vincent Ladeuil
Random cleanups to catch up with copyright updates in trunk.
22
    hooks,
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
23
    msgeditor,
24
)
4969.2.16 by Aaron Bentley
Updates from review.
25
from bzrlib.plugins.launchpad import (
26
    lp_api,
27
    lp_registration,
28
)
5546.2.1 by Aaron Bentley
Add lp-find-proposal.
29
from bzrlib.plugins.launchpad.lp_api import canonical_url
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
30
from lazr.restfulclient import errors as restful_errors
31
32
5184.1.1 by Vincent Ladeuil
Random cleanups to catch up with copyright updates in trunk.
33
class ProposeMergeHooks(hooks.Hooks):
4969.2.19 by Aaron Bentley
Rename submit to propose everywhere.
34
    """Hooks for proposing a merge on Launchpad."""
4969.2.6 by Aaron Bentley
Add a hook point for getting the prerequisite branch, and make the pipeline
35
36
    def __init__(self):
5184.1.1 by Vincent Ladeuil
Random cleanups to catch up with copyright updates in trunk.
37
        hooks.Hooks.__init__(self)
4969.2.6 by Aaron Bentley
Add a hook point for getting the prerequisite branch, and make the pipeline
38
        self.create_hook(
5184.1.1 by Vincent Ladeuil
Random cleanups to catch up with copyright updates in trunk.
39
            hooks.HookPoint(
4969.2.6 by Aaron Bentley
Add a hook point for getting the prerequisite branch, and make the pipeline
40
                'get_prerequisite',
41
                "Return the prerequisite branch for proposing as merge.",
4969.2.9 by Aaron Bentley
Add a hook for getting an initial merge proposal body.
42
                (2, 1), None),
4969.2.13 by Aaron Bentley
Get working with lpreview_body.
43
        )
44
        self.create_hook(
5184.1.1 by Vincent Ladeuil
Random cleanups to catch up with copyright updates in trunk.
45
            hooks.HookPoint(
4969.2.9 by Aaron Bentley
Add a hook for getting an initial merge proposal body.
46
                'merge_proposal_body',
4969.2.13 by Aaron Bentley
Get working with lpreview_body.
47
                "Return an initial body for the merge proposal message.",
4969.2.9 by Aaron Bentley
Add a hook for getting an initial merge proposal body.
48
                (2, 1), None),
49
        )
4969.2.6 by Aaron Bentley
Add a hook point for getting the prerequisite branch, and make the pipeline
50
51
4969.2.19 by Aaron Bentley
Rename submit to propose everywhere.
52
class Proposer(object):
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
53
4969.2.19 by Aaron Bentley
Rename submit to propose everywhere.
54
    hooks = ProposeMergeHooks()
4969.2.6 by Aaron Bentley
Add a hook point for getting the prerequisite branch, and make the pipeline
55
56
    def __init__(self, tree, source_branch, target_branch, message, reviews,
5244.1.3 by Robert Collins
Allow setting new proposals as approved immediately.
57
                 staging=False, approve=False):
4969.2.10 by Aaron Bentley
Cleanup and docs.
58
        """Constructor.
59
60
        :param tree: The working tree for the source branch.
61
        :param source_branch: The branch to propose for merging.
62
        :param target_branch: The branch to merge into.
63
        :param message: The commit message to use.  (May be None.)
64
        :param reviews: A list of tuples of reviewer, review type.
65
        :param staging: If True, propose the merge against staging instead of
66
            production.
5244.1.3 by Robert Collins
Allow setting new proposals as approved immediately.
67
        :param approve: If True, mark the new proposal as approved immediately.
68
            This is useful when a project permits some things to be approved
69
            by the submitter (e.g. merges between release and deployment
70
            branches).
4969.2.10 by Aaron Bentley
Cleanup and docs.
71
        """
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
72
        self.tree = tree
4969.2.4 by Aaron Bentley
Remove the staging instance variable and the lp() function. Just make a
73
        if staging:
4969.2.7 by Aaron Bentley
Add lp-submit, get working.
74
            lp_instance = 'staging'
4969.2.4 by Aaron Bentley
Remove the staging instance variable and the lp() function. Just make a
75
        else:
5246.1.1 by Robert Collins
* ``bzr lp-propose`` which was switched to use production Launchpad API
76
            lp_instance = 'edge'
4969.2.7 by Aaron Bentley
Add lp-submit, get working.
77
        service = lp_registration.LaunchpadService(lp_instance=lp_instance)
78
        self.launchpad = lp_api.login(service)
4969.2.3 by Aaron Bentley
Move LaunchpadBranch to lp_api. Change the interface so that it uses launchpad
79
        self.source_branch = lp_api.LaunchpadBranch.from_bzr(
4969.2.7 by Aaron Bentley
Add lp-submit, get working.
80
            self.launchpad, source_branch)
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
81
        if target_branch is None:
4969.2.5 by Aaron Bentley
It makes more sense to get the dev focus from an existing Launchpad branch
82
            self.target_branch = self.source_branch.get_dev_focus()
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
83
        else:
4969.2.3 by Aaron Bentley
Move LaunchpadBranch to lp_api. Change the interface so that it uses launchpad
84
            self.target_branch = lp_api.LaunchpadBranch.from_bzr(
4969.2.7 by Aaron Bentley
Add lp-submit, get working.
85
                self.launchpad, target_branch)
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
86
        self.commit_message = message
5244.1.2 by Robert Collins
Refactor to make calling the webservice cleaner.
87
        # XXX: this is where bug lp:583638 could be tackled.
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
88
        if reviews == []:
89
            target_reviewer = self.target_branch.lp.reviewer
90
            if target_reviewer is None:
91
                raise errors.BzrCommandError('No reviewer specified')
92
            self.reviews = [(target_reviewer, '')]
93
        else:
4969.2.4 by Aaron Bentley
Remove the staging instance variable and the lp() function. Just make a
94
            self.reviews = [(self.launchpad.people[reviewer], review_type)
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
95
                            for reviewer, review_type in
96
                            reviews]
5244.1.3 by Robert Collins
Allow setting new proposals as approved immediately.
97
        self.approve = approve
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
98
99
    def get_comment(self, prerequisite_branch):
4969.2.10 by Aaron Bentley
Cleanup and docs.
100
        """Determine the initial comment for the merge proposal."""
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
101
        info = ["Source: %s\n" % self.source_branch.lp.bzr_identity]
102
        info.append("Target: %s\n" % self.target_branch.lp.bzr_identity)
103
        if prerequisite_branch is not None:
104
            info.append("Prereq: %s\n" % prerequisite_branch.lp.bzr_identity)
105
        for rdata in self.reviews:
106
            uniquename = "%s (%s)" % (rdata[0].display_name, rdata[0].name)
107
            info.append('Reviewer: %s, type "%s"\n' % (uniquename, rdata[1]))
108
        self.source_branch.bzr.lock_read()
109
        try:
110
            self.target_branch.bzr.lock_read()
111
            try:
4969.2.13 by Aaron Bentley
Get working with lpreview_body.
112
                body = self.get_initial_body()
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
113
            finally:
114
                self.target_branch.bzr.unlock()
115
        finally:
116
            self.source_branch.bzr.unlock()
117
        initial_comment = msgeditor.edit_commit_message(''.join(info),
118
                                                        start_message=body)
119
        return initial_comment.strip().encode('utf-8')
120
4969.2.13 by Aaron Bentley
Get working with lpreview_body.
121
    def get_initial_body(self):
4969.2.15 by Aaron Bentley
Update docs.
122
        """Get a body for the proposal for the user to modify.
123
124
        :return: a str or None.
125
        """
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
126
        def list_modified_files():
127
            lca_tree = self.source_branch.find_lca_tree(
128
                self.target_branch)
129
            source_tree = self.source_branch.bzr.basis_tree()
130
            files = modified_files(lca_tree, source_tree)
131
            return list(files)
132
        target_loc = ('bzr+ssh://bazaar.launchpad.net/%s' %
133
                       self.target_branch.lp.unique_name)
4969.2.9 by Aaron Bentley
Add a hook for getting an initial merge proposal body.
134
        body = None
135
        for hook in self.hooks['merge_proposal_body']:
136
            body = hook({
137
                'tree': self.tree,
138
                'target_branch': target_loc,
139
                'modified_files_callback': list_modified_files,
140
                'old_body': body,
141
            })
4969.2.13 by Aaron Bentley
Get working with lpreview_body.
142
        return body
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
143
4969.2.19 by Aaron Bentley
Rename submit to propose everywhere.
144
    def check_proposal(self):
4969.2.10 by Aaron Bentley
Cleanup and docs.
145
        """Check that the submission is sensible."""
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
146
        if self.source_branch.lp.self_link == self.target_branch.lp.self_link:
147
            raise errors.BzrCommandError(
148
                'Source and target branches must be different.')
149
        for mp in self.source_branch.lp.landing_targets:
150
            if mp.queue_status in ('Merged', 'Rejected'):
151
                continue
152
            if mp.target_branch.self_link == self.target_branch.lp.self_link:
153
                raise errors.BzrCommandError(
154
                    'There is already a branch merge proposal: %s' %
155
                    canonical_url(mp))
156
4969.2.6 by Aaron Bentley
Add a hook point for getting the prerequisite branch, and make the pipeline
157
    def _get_prerequisite_branch(self):
4969.2.7 by Aaron Bentley
Add lp-submit, get working.
158
        hooks = self.hooks['get_prerequisite']
4969.2.6 by Aaron Bentley
Add a hook point for getting the prerequisite branch, and make the pipeline
159
        prerequisite_branch = None
160
        for hook in hooks:
161
            prerequisite_branch = hook(
162
                {'launchpad': self.launchpad,
4969.2.7 by Aaron Bentley
Add lp-submit, get working.
163
                 'source_branch': self.source_branch,
164
                 'target_branch': self.target_branch,
4969.2.6 by Aaron Bentley
Add a hook point for getting the prerequisite branch, and make the pipeline
165
                 'prerequisite_branch': prerequisite_branch})
166
        return prerequisite_branch
167
5244.1.2 by Robert Collins
Refactor to make calling the webservice cleaner.
168
    def call_webservice(self, call, *args, **kwargs):
169
        """Make a call to the webservice, wrapping failures.
170
        
171
        :param call: The call to make.
172
        :param *args: *args for the call.
173
        :param **kwargs: **kwargs for the call.
174
        :return: The result of calling call(*args, *kwargs).
175
        """
176
        try:
177
            return call(*args, **kwargs)
178
        except restful_errors.HTTPError, e:
179
            error_lines = []
180
            for line in e.content.splitlines():
181
                if line.startswith('Traceback (most recent call last):'):
182
                    break
183
                error_lines.append(line)
184
            raise Exception(''.join(error_lines))
185
4969.2.19 by Aaron Bentley
Rename submit to propose everywhere.
186
    def create_proposal(self):
4969.2.10 by Aaron Bentley
Cleanup and docs.
187
        """Perform the submission."""
4969.2.6 by Aaron Bentley
Add a hook point for getting the prerequisite branch, and make the pipeline
188
        prerequisite_branch = self._get_prerequisite_branch()
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
189
        if prerequisite_branch is None:
190
            prereq = None
191
        else:
192
            prereq = prerequisite_branch.lp
4969.2.14 by Aaron Bentley
Restore update functionality.
193
            prerequisite_branch.update_lp()
194
        self.source_branch.update_lp()
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
195
        reviewers = []
196
        review_types = []
197
        for reviewer, review_type in self.reviews:
198
            review_types.append(review_type)
199
            reviewers.append(reviewer.self_link)
200
        initial_comment = self.get_comment(prerequisite_branch)
5244.1.2 by Robert Collins
Refactor to make calling the webservice cleaner.
201
        mp = self.call_webservice(
202
            self.source_branch.lp.createMergeProposal,
203
            target_branch=self.target_branch.lp,
204
            prerequisite_branch=prereq,
205
            initial_comment=initial_comment,
206
            commit_message=self.commit_message, reviewers=reviewers,
207
            review_types=review_types)
5244.1.3 by Robert Collins
Allow setting new proposals as approved immediately.
208
        if self.approve:
209
            self.call_webservice(mp.setStatus, status='Approved')
5244.1.2 by Robert Collins
Refactor to make calling the webservice cleaner.
210
        webbrowser.open(canonical_url(mp))
4969.2.1 by Aaron Bentley
Initial import of lp_submit command.
211
4969.2.6 by Aaron Bentley
Add a hook point for getting the prerequisite branch, and make the pipeline
212
4969.2.13 by Aaron Bentley
Get working with lpreview_body.
213
def modified_files(old_tree, new_tree):
4969.2.15 by Aaron Bentley
Update docs.
214
    """Return a list of paths in the new tree with modified contents."""
4969.2.13 by Aaron Bentley
Get working with lpreview_body.
215
    for f, (op, path), c, v, p, n, (ok, k), e in new_tree.iter_changes(
216
        old_tree):
217
        if c and k == 'file':
218
            yield str(path)