/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_find_proposal(Command):
166
167
    __doc__ = """Find the proposal to merge this revision.
168
169
    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.
170
    This works only if the if the merged_revno was recorded for the merge
171
    proposal.  The proposal(s) are opened in a web browser.
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
172
6538.2.2 by Aaron Bentley
Look up merge proposals by exact revision-id.
173
    Only the revision specified is searched for.  To find the mainline
174
    revision that merged it into mainline, use the "mainline" revision spec.
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
175
176
    So, to find the merge proposal that reviewed line 1 of README::
177
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
178
      brz lp-find-proposal -r mainline:annotate:README:1
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
179
    """
180
181
    takes_options = ['revision']
182
183
    def run(self, revision=None):
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
184
        from ... import ui
185
        from . import lp_api
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
186
        import webbrowser
187
        b = _mod_branch.Branch.open_containing('.')[0]
6861.4.1 by Jelmer Vernooij
Make progress bars context managers.
188
        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.
189
            if revision is None:
190
                revision_id = b.last_revision()
191
            else:
192
                revision_id = revision[0].as_revision_id(b)
193
            merged = self._find_proposals(revision_id, pb)
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
194
            if len(merged) == 0:
195
                raise BzrCommandError(gettext('No review found.'))
196
            trace.note(gettext('%d proposals(s) found.') % len(merged))
197
            for mp in merged:
198
                webbrowser.open(lp_api.canonical_url(mp))
199
6538.2.2 by Aaron Bentley
Look up merge proposals by exact revision-id.
200
    def _find_proposals(self, revision_id, pb):
7268.9.1 by Jelmer Vernooij
Fix lp-find-merge-proposal.
201
        from launchpadlib import uris
7240.5.1 by Jelmer Vernooij
Avoid LaunchpadService when connecting to API.
202
        from . import lp_api
6538.2.5 by Aaron Bentley
Updates from review.
203
        # "devel" because branches.getMergeProposals is not part of 1.0 API.
7268.9.1 by Jelmer Vernooij
Fix lp-find-merge-proposal.
204
        lp_base_url = uris.LPNET_SERVICE_ROOT
205
        launchpad = lp_api.connect_launchpad(lp_base_url, version='devel')
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
206
        pb.update(gettext('Finding proposals'))
6538.2.2 by Aaron Bentley
Look up merge proposals by exact revision-id.
207
        return list(launchpad.branches.getMergeProposals(
7268.9.1 by Jelmer Vernooij
Fix lp-find-merge-proposal.
208
                    merged_revision=revision_id.decode('utf-8')))