/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6622.7.1 by Colin Watson
Remove `bzr register-branch`, since it has not worked for a long time.
1
# Copyright (C) 2006-2017 Canonical Ltd
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
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
"""Launchpad plugin commands."""
18
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
19
from ... import (
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
20
    branch as _mod_branch,
21
    controldir,
22
    trace,
23
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
24
from ...commands import (
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
25
    Command,
26
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
27
from ...errors import (
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
28
    BzrCommandError,
29
    NotBranchError,
30
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
31
from ...i18n import gettext
32
from ...option import (
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
33
    Option,
34
    ListOption,
35
    )
36
37
38
class cmd_launchpad_open(Command):
39
    __doc__ = """Open a Launchpad branch page in your web browser."""
40
41
    aliases = ['lp-open']
42
    takes_options = [
43
        Option('dry-run',
44
               'Do not actually open the browser. Just say the URL we would '
45
               'use.'),
46
        ]
47
    takes_args = ['location?']
48
49
    def _possible_locations(self, location):
50
        """Yield possible external locations for the branch at 'location'."""
51
        yield location
52
        try:
53
            branch = _mod_branch.Branch.open_containing(location)[0]
54
        except NotBranchError:
55
            return
56
        branch_url = branch.get_public_branch()
57
        if branch_url is not None:
58
            yield branch_url
59
        branch_url = branch.get_push_location()
60
        if branch_url is not None:
61
            yield branch_url
62
63
    def _get_web_url(self, service, location):
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
64
        from .lp_registration import (
6729.6.1 by Jelmer Vernooij
Move urlutils errors.
65
            InvalidURL,
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
66
            NotLaunchpadBranch)
67
        for branch_url in self._possible_locations(location):
68
            try:
69
                return service.get_web_url_from_branch_url(branch_url)
70
            except (NotLaunchpadBranch, InvalidURL):
71
                pass
72
        raise NotLaunchpadBranch(branch_url)
73
74
    def run(self, location=None, dry_run=False):
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
75
        from .lp_registration import (
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
76
            LaunchpadService)
77
        if location is None:
78
            location = u'.'
79
        web_url = self._get_web_url(LaunchpadService(), location)
80
        trace.note(gettext('Opening %s in web browser') % web_url)
81
        if not dry_run:
82
            import webbrowser   # this import should not be lazy
7143.15.2 by Jelmer Vernooij
Run autopep8.
83
            # otherwise brz.exe lacks this module
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
84
            webbrowser.open(web_url)
85
86
87
class cmd_launchpad_login(Command):
88
    __doc__ = """Show or set the Launchpad user ID.
89
90
    When communicating with Launchpad, some commands need to know your
91
    Launchpad user ID.  This command can be used to set or show the
92
    user ID that Bazaar will use for such communication.
93
94
    :Examples:
95
      Show the Launchpad ID of the current user::
96
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
97
          brz launchpad-login
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
98
99
      Set the Launchpad ID of the current user to 'bob'::
100
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
101
          brz launchpad-login bob
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
102
    """
103
    aliases = ['lp-login']
104
    takes_args = ['name?']
105
    takes_options = [
106
        'verbose',
107
        Option('no-check',
108
               "Don't check that the user name is valid."),
109
        ]
110
111
    def run(self, name=None, no_check=False, verbose=False):
112
        # This is totally separate from any launchpadlib login system.
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
113
        from . import account
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
114
        check_account = not no_check
115
116
        if name is None:
117
            username = account.get_lp_login()
118
            if username:
119
                if check_account:
120
                    account.check_lp_login(username)
121
                    if verbose:
122
                        self.outf.write(gettext(
123
                            "Launchpad user ID exists and has SSH keys.\n"))
124
                self.outf.write(username + '\n')
125
            else:
126
                self.outf.write(gettext('No Launchpad user ID configured.\n'))
127
                return 1
128
        else:
129
            name = name.lower()
130
            if check_account:
131
                account.check_lp_login(name)
132
                if verbose:
133
                    self.outf.write(gettext(
134
                        "Launchpad user ID exists and has SSH keys.\n"))
135
            account.set_lp_login(name)
136
            if verbose:
137
                self.outf.write(gettext("Launchpad user ID set to '%s'.\n") %
7143.15.2 by Jelmer Vernooij
Run autopep8.
138
                                (name,))
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
139
140
6852.1.1 by Jelmer Vernooij
Add lp-logout command.
141
class cmd_launchpad_logout(Command):
142
    __doc__ = """Unset the Launchpad user ID.
143
144
    When communicating with Launchpad, some commands need to know your
145
    Launchpad user ID.  This command will log you out from Launchpad.
146
    This means that communication with Launchpad will happen over
147
    HTTPS, and will not require one of your SSH keys to be available.
148
    """
149
    aliases = ['lp-logout']
150
    takes_options = ['verbose']
151
152
    def run(self, verbose=False):
153
        from . import account
154
        old_username = account.get_lp_login()
155
        if old_username is None:
156
            self.outf.write(gettext('Not logged into Launchpad.\n'))
157
            return 1
158
        account.set_lp_login(None)
159
        if verbose:
160
            self.outf.write(gettext(
161
                "Launchpad user ID %s logged out.\n") %
162
                old_username)
163
164
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
165
class cmd_lp_propose_merge(Command):
166
    __doc__ = """Propose merging a branch on Launchpad.
167
168
    This will open your usual editor to provide the initial comment.  When it
169
    has created the proposal, it will open it in your default web browser.
170
171
    The branch will be proposed to merge into SUBMIT_BRANCH.  If SUBMIT_BRANCH
172
    is not supplied, the remembered submit branch will be used.  If no submit
173
    branch is remembered, the development focus will be used.
174
175
    By default, the SUBMIT_BRANCH's review team will be requested to review
176
    the merge proposal.  This can be overriden by specifying --review (-R).
177
    The parameter the launchpad account name of the desired reviewer.  This
178
    may optionally be followed by '=' and the review type.  For example:
179
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
180
      brz lp-propose-merge --review jrandom --review review-team=qa
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
181
182
    This will propose a merge,  request "jrandom" to perform a review of
183
    unspecified type, and request "review-team" to perform a "qa" review.
184
    """
185
7211.13.3 by Jelmer Vernooij
Hide the lp-propose command.
186
    hidden = True
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
187
    takes_options = [Option('staging',
188
                            help='Propose the merge on staging.'),
7479.2.1 by Jelmer Vernooij
Drop python2 support.
189
                     Option('message', short_name='m', type=str,
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
190
                            help='Commit message.'),
191
                     Option('approve',
6570.1.3 by Jonathan Lange
Document the behaviour
192
                            help=('Mark the proposal as approved immediately, '
193
                                  'setting the approved revision to tip.')),
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
194
                     Option('fixes', 'The bug this proposal fixes.', str),
7479.2.1 by Jelmer Vernooij
Drop python2 support.
195
                     ListOption('review', short_name='R', type=str,
7143.15.2 by Jelmer Vernooij
Run autopep8.
196
                                help='Requested reviewer and optional type.')]
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
197
198
    takes_args = ['submit_branch?']
199
200
    aliases = ['lp-submit', 'lp-propose']
201
202
    def run(self, submit_branch=None, review=None, staging=False,
203
            message=None, approve=False, fixes=None):
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
204
        from . import lp_propose
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
205
        tree, branch, relpath = controldir.ControlDir.open_containing_tree_or_branch(
206
            '.')
207
        if review is None:
208
            reviews = None
209
        else:
210
            reviews = []
211
            for review in review:
212
                if '=' in review:
213
                    reviews.append(review.split('=', 2))
214
                else:
215
                    reviews.append((review, ''))
216
            if submit_branch is None:
217
                submit_branch = branch.get_submit_branch()
218
        if submit_branch is None:
219
            target = None
220
        else:
221
            target = _mod_branch.Branch.open(submit_branch)
222
        proposer = lp_propose.Proposer(tree, branch, target, message,
223
                                       reviews, staging, approve=approve,
224
                                       fixes=fixes)
225
        proposer.check_proposal()
226
        proposer.create_proposal()
227
228
229
class cmd_lp_find_proposal(Command):
230
231
    __doc__ = """Find the proposal to merge this revision.
232
233
    Finds the merge proposal(s) that discussed landing the specified revision.
6538.2.2 by Aaron Bentley
Look up merge proposals by exact revision-id.
234
    This works only if the if the merged_revno was recorded for the merge
235
    proposal.  The proposal(s) are opened in a web browser.
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
236
6538.2.2 by Aaron Bentley
Look up merge proposals by exact revision-id.
237
    Only the revision specified is searched for.  To find the mainline
238
    revision that merged it into mainline, use the "mainline" revision spec.
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
239
240
    So, to find the merge proposal that reviewed line 1 of README::
241
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
242
      brz lp-find-proposal -r mainline:annotate:README:1
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
243
    """
244
245
    takes_options = ['revision']
246
247
    def run(self, revision=None):
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
248
        from ... import ui
249
        from . import lp_api
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
250
        import webbrowser
251
        b = _mod_branch.Branch.open_containing('.')[0]
6861.4.1 by Jelmer Vernooij
Make progress bars context managers.
252
        with ui.ui_factory.nested_progress_bar() as pb, b.lock_read():
6538.2.2 by Aaron Bentley
Look up merge proposals by exact revision-id.
253
            if revision is None:
254
                revision_id = b.last_revision()
255
            else:
256
                revision_id = revision[0].as_revision_id(b)
257
            merged = self._find_proposals(revision_id, pb)
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
258
            if len(merged) == 0:
259
                raise BzrCommandError(gettext('No review found.'))
260
            trace.note(gettext('%d proposals(s) found.') % len(merged))
261
            for mp in merged:
262
                webbrowser.open(lp_api.canonical_url(mp))
263
6538.2.2 by Aaron Bentley
Look up merge proposals by exact revision-id.
264
    def _find_proposals(self, revision_id, pb):
7268.9.1 by Jelmer Vernooij
Fix lp-find-merge-proposal.
265
        from launchpadlib import uris
7240.5.1 by Jelmer Vernooij
Avoid LaunchpadService when connecting to API.
266
        from . import lp_api
6538.2.5 by Aaron Bentley
Updates from review.
267
        # "devel" because branches.getMergeProposals is not part of 1.0 API.
7268.9.1 by Jelmer Vernooij
Fix lp-find-merge-proposal.
268
        lp_base_url = uris.LPNET_SERVICE_ROOT
269
        launchpad = lp_api.connect_launchpad(lp_base_url, version='devel')
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
270
        pb.update(gettext('Finding proposals'))
6538.2.2 by Aaron Bentley
Look up merge proposals by exact revision-id.
271
        return list(launchpad.branches.getMergeProposals(
7268.9.1 by Jelmer Vernooij
Fix lp-find-merge-proposal.
272
                    merged_revision=revision_id.decode('utf-8')))