/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5557.1.7 by John Arbash Meinel
Merge in the bzr.dev 5582
1
# Copyright (C) 2006-2011 Canonical Ltd
0.4.1 by Martin Pool
Start lp-register command
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.4.1 by Martin Pool
Start lp-register command
16
5459.3.1 by Neil Martinsen-Burrell
add more detailed help for Launchpad plugin
17
"""Launchpad.net integration plugin for Bazaar.
18
19
This plugin provides facilities for working with Bazaar branches that are
20
hosted on Launchpad (http://launchpad.net).  It provides a directory service 
5459.3.2 by Neil Martinsen-Burrell
address James Westbys review comments
21
for referring to Launchpad branches using the "lp:" prefix.  For example,
22
lp:bzr refers to the Bazaar's main development branch and
23
lp:~username/project/branch-name can be used to refer to a specific branch.
24
25
This plugin provides a bug tracker so that "bzr commit --fixes lp:1234" will
26
record that revision as fixing Launchpad's bug 1234.
27
28
The plugin also provides the following commands:
29
30
    launchpad-login: Show or set the Launchpad user ID
31
    launchpad-open: Open a Launchpad branch page in your web browser
32
    lp-propose-merge: Propose merging a branch on Launchpad
5459.3.1 by Neil Martinsen-Burrell
add more detailed help for Launchpad plugin
33
    register-branch: Register a branch with launchpad.net
34
    launchpad-mirror: Ask Launchpad to mirror a branch now
35
36
"""
0.4.1 by Martin Pool
Start lp-register command
37
6379.6.1 by Jelmer Vernooij
Import absolute_import in a few places.
38
from __future__ import absolute_import
39
0.4.17 by Martin Pool
Allow xmlrpc service url to be overridden by $BZR_LP_XMLRPC_URL
40
# The XMLRPC server address can be overridden by setting the environment
5053.1.1 by Martin Pool
Merge 2.1 back to trunk
41
# variable $BZR_LP_XMLRPC_URL
0.4.17 by Martin Pool
Allow xmlrpc service url to be overridden by $BZR_LP_XMLRPC_URL
42
5560.2.1 by Vincent Ladeuil
Fix the remaining references to http://bazaar-vcs.org (except the explicitly historical ones).
43
# see http://wiki.bazaar.canonical.com/Specs/BranchRegistrationTool
0.4.9 by Martin Pool
Don't transmit non-standard xmlrpc <nil> value.
44
4031.2.11 by John Arbash Meinel
Turn a bunch of imports into lazy imports.
45
from bzrlib.lazy_import import lazy_import
46
lazy_import(globals(), """
47
from bzrlib import (
5546.2.1 by Aaron Bentley
Add lp-find-proposal.
48
    ui,
4031.2.11 by John Arbash Meinel
Turn a bunch of imports into lazy imports.
49
    trace,
50
    )
6150.3.1 by Jonathan Riddell
gettext() in launchpad plugin
51
from bzrlib.i18n import gettext
4031.2.11 by John Arbash Meinel
Turn a bunch of imports into lazy imports.
52
""")
53
5050.79.5 by John Arbash Meinel
Add a Branch.open hook.
54
from bzrlib import (
55
    branch as _mod_branch,
56
    bzrdir,
57
    lazy_regex,
6024.3.8 by John Arbash Meinel
Move 'place' logic onto LatestPublication.
58
    # Since we are a built-in plugin we share the bzrlib version
5050.79.6 by John Arbash Meinel
small bits of cleanup, and add timing information.
59
    version_info,
5050.79.5 by John Arbash Meinel
Add a Branch.open hook.
60
    )
4969.2.7 by Aaron Bentley
Add lp-submit, get working.
61
from bzrlib.commands import (
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
62
    Command,
63
    register_command,
64
    )
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
65
from bzrlib.directory_service import directories
4031.2.5 by Jonathan Lange
Refactor the code so we don't do too many remote requests.
66
from bzrlib.errors import (
67
    BzrCommandError,
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
68
    InvalidRevisionSpec,
4031.2.5 by Jonathan Lange
Refactor the code so we don't do too many remote requests.
69
    InvalidURL,
70
    NoPublicBranch,
71
    NotBranchError,
72
    )
2245.8.6 by Martin Pool
Documentation under 'help launchpad'
73
from bzrlib.help_topics import topic_registry
4969.2.7 by Aaron Bentley
Add lp-submit, get working.
74
from bzrlib.option import (
4969.2.10 by Aaron Bentley
Cleanup and docs.
75
        Option,
4969.2.7 by Aaron Bentley
Add lp-submit, get working.
76
        ListOption,
77
)
0.4.4 by Martin Pool
Start forming xmlrpc requests
78
79
0.4.2 by Martin Pool
Rename command to 'register-branch'
80
class cmd_register_branch(Command):
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
81
    __doc__ = """Register a branch with launchpad.net.
0.4.1 by Martin Pool
Start lp-register command
82
83
    This command lists a bzr branch in the directory of branches on
2400.2.4 by Robert Collins
(robertc) Typo in the help for ``register-branch`` fixed. (Robert Collins, #96770)
84
    launchpad.net.  Registration allows the branch to be associated with
0.4.1 by Martin Pool
Start lp-register command
85
    bugs or specifications.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
86
4416.7.1 by Neil Martinsen-Burrell
Fix 238764 refer to projects rather than products in launchpad plugin
87
    Before using this command you must register the project to which the
0.4.1 by Martin Pool
Start lp-register command
88
    branch belongs, and create an account for yourself on launchpad.net.
0.4.3 by Martin Pool
More command line processing
89
90
    arguments:
3200.2.3 by Robert Collins
Tweak wording.
91
        public_url: The publicly visible url for the branch to register.
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
92
                    This must be an http or https url (which Launchpad can read
93
                    from to access the branch). Local file urls, SFTP urls, and
94
                    bzr+ssh urls will not work.
95
                    If no public_url is provided, bzr will use the configured
3200.2.3 by Robert Collins
Tweak wording.
96
                    public_url if there is one for the current branch, and
97
                    otherwise error.
0.4.3 by Martin Pool
More command line processing
98
99
    example:
4416.7.1 by Neil Martinsen-Burrell
Fix 238764 refer to projects rather than products in launchpad plugin
100
        bzr register-branch http://foo.com/bzr/fooproject.mine \\
101
                --project fooproject
0.4.1 by Martin Pool
Start lp-register command
102
    """
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
103
    takes_args = ['public_url?']
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
104
    takes_options = [
4416.7.1 by Neil Martinsen-Burrell
Fix 238764 refer to projects rather than products in launchpad plugin
105
         Option('project',
106
                'Launchpad project short name to associate with the branch.',
0.4.15 by Martin Pool
(register-branch) Add command-line options
107
                unicode),
4416.7.3 by Neil Martinsen-Burrell
Retain --product options for register-branch, but deprecate it in favor of --project
108
         Option('product',
6015.21.1 by Vincent Ladeuil
Merge 2.3 into 2.4 (including fix for #614713)
109
                'Launchpad product short name to associate with the branch.',
4416.7.3 by Neil Martinsen-Burrell
Retain --product options for register-branch, but deprecate it in favor of --project
110
                unicode,
111
                hidden=True),
0.4.15 by Martin Pool
(register-branch) Add command-line options
112
         Option('branch-name',
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
113
                'Short name for the branch; '
114
                'by default taken from the last component of the url.',
0.4.15 by Martin Pool
(register-branch) Add command-line options
115
                unicode),
116
         Option('branch-title',
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
117
                'One-sentence description of the branch.',
0.4.15 by Martin Pool
(register-branch) Add command-line options
118
                unicode),
119
         Option('branch-description',
2598.1.5 by Martin Pool
Fix one more option message.
120
                'Longer description of the purpose or contents of the branch.',
0.4.15 by Martin Pool
(register-branch) Add command-line options
121
                unicode),
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
122
         Option('author',
123
                "Branch author's email address, if not yourself.",
0.4.16 by Martin Pool
(register-branch) Add --author option and respect --dry-run
124
                unicode),
0.4.19 by test at canonical
add possibility to link to a bug when registering a branch. factor out some common functionality from BranchRegistrationRequest.
125
         Option('link-bug',
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
126
                'The bug this branch fixes.',
0.4.19 by test at canonical
add possibility to link to a bug when registering a branch. factor out some common functionality from BranchRegistrationRequest.
127
                int),
0.4.15 by Martin Pool
(register-branch) Add command-line options
128
         Option('dry-run',
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
129
                'Prepare the request but don\'t actually send it.')
0.4.15 by Martin Pool
(register-branch) Add command-line options
130
        ]
131
132
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
133
    def run(self,
134
            public_url=None,
4416.7.1 by Neil Martinsen-Burrell
Fix 238764 refer to projects rather than products in launchpad plugin
135
            project='',
4416.7.3 by Neil Martinsen-Burrell
Retain --product options for register-branch, but deprecate it in favor of --project
136
            product=None,
0.4.15 by Martin Pool
(register-branch) Add command-line options
137
            branch_name='',
138
            branch_title='',
139
            branch_description='',
0.4.16 by Martin Pool
(register-branch) Add --author option and respect --dry-run
140
            author='',
0.4.19 by test at canonical
add possibility to link to a bug when registering a branch. factor out some common functionality from BranchRegistrationRequest.
141
            link_bug=None,
0.4.15 by Martin Pool
(register-branch) Add command-line options
142
            dry_run=False):
2898.3.8 by James Henstridge
Get rid of relative imports in Launchpad plugin.
143
        from bzrlib.plugins.launchpad.lp_registration import (
4505.6.4 by Jonathan Lange
Remove unnecessary import.
144
            BranchRegistrationRequest, BranchBugLinkRequest,
4505.6.10 by Jonathan Lange
Move lp_registration imports into commands.
145
            DryRunLaunchpadService, LaunchpadService)
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
146
        if public_url is None:
147
            try:
4031.2.11 by John Arbash Meinel
Turn a bunch of imports into lazy imports.
148
                b = _mod_branch.Branch.open_containing('.')[0]
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
149
            except NotBranchError:
6150.3.1 by Jonathan Riddell
gettext() in launchpad plugin
150
                raise BzrCommandError(gettext(
151
                            'register-branch requires a public '
152
                            'branch url - see bzr help register-branch.'))
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
153
            public_url = b.get_public_branch()
154
            if public_url is None:
155
                raise NoPublicBranch(b)
4416.7.3 by Neil Martinsen-Burrell
Retain --product options for register-branch, but deprecate it in favor of --project
156
        if product is not None:
157
            project = product
6150.3.1 by Jonathan Riddell
gettext() in launchpad plugin
158
            trace.note(gettext(
159
                '--product is deprecated; please use --project.'))
4416.7.3 by Neil Martinsen-Burrell
Retain --product options for register-branch, but deprecate it in favor of --project
160
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
161
162
        rego = BranchRegistrationRequest(branch_url=public_url,
0.4.15 by Martin Pool
(register-branch) Add command-line options
163
                                         branch_name=branch_name,
164
                                         branch_title=branch_title,
165
                                         branch_description=branch_description,
4416.7.1 by Neil Martinsen-Burrell
Fix 238764 refer to projects rather than products in launchpad plugin
166
                                         product_name=project,
0.4.16 by Martin Pool
(register-branch) Add --author option and respect --dry-run
167
                                         author_email=author,
0.4.15 by Martin Pool
(register-branch) Add command-line options
168
                                         )
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
169
        linko = BranchBugLinkRequest(branch_url=public_url,
0.4.19 by test at canonical
add possibility to link to a bug when registering a branch. factor out some common functionality from BranchRegistrationRequest.
170
                                     bug_id=link_bug)
1668.1.12 by Martin Pool
(launchpad plugin) Improved --dry-run that uses a dummy xmlrpc service.
171
        if not dry_run:
172
            service = LaunchpadService()
173
            # This gives back the xmlrpc url that can be used for future
174
            # operations on the branch.  It's not so useful to print to the
175
            # user since they can't do anything with it from a web browser; it
176
            # might be nice for the server to tell us about an html url as
177
            # well.
178
        else:
179
            # Run on service entirely in memory
180
            service = DryRunLaunchpadService()
0.4.19 by test at canonical
add possibility to link to a bug when registering a branch. factor out some common functionality from BranchRegistrationRequest.
181
        service.gather_user_credentials()
4505.6.21 by Jonathan Lange
Clean up flakes.
182
        rego.submit(service)
1668.1.12 by Martin Pool
(launchpad plugin) Improved --dry-run that uses a dummy xmlrpc service.
183
        if link_bug:
4505.6.21 by Jonathan Lange
Clean up flakes.
184
            linko.submit(service)
1668.1.12 by Martin Pool
(launchpad plugin) Improved --dry-run that uses a dummy xmlrpc service.
185
        print 'Branch registered.'
0.4.1 by Martin Pool
Start lp-register command
186
0.4.2 by Martin Pool
Rename command to 'register-branch'
187
register_command(cmd_register_branch)
2245.8.4 by Martin Pool
lp:/// indirection works
188
2898.3.3 by James Henstridge
Add launchpad-login command.
189
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
190
class cmd_launchpad_open(Command):
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
191
    __doc__ = """Open a Launchpad branch page in your web browser."""
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
192
193
    aliases = ['lp-open']
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
194
    takes_options = [
195
        Option('dry-run',
196
               'Do not actually open the browser. Just say the URL we would '
197
               'use.'),
198
        ]
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
199
    takes_args = ['location?']
200
4031.2.5 by Jonathan Lange
Refactor the code so we don't do too many remote requests.
201
    def _possible_locations(self, location):
202
        """Yield possible external locations for the branch at 'location'."""
203
        yield location
204
        try:
4867.2.1 by Neil Martinsen-Burrell
launchpad-open works from a subdirectory of a branch
205
            branch = _mod_branch.Branch.open_containing(location)[0]
4031.2.5 by Jonathan Lange
Refactor the code so we don't do too many remote requests.
206
        except NotBranchError:
207
            return
208
        branch_url = branch.get_public_branch()
209
        if branch_url is not None:
210
            yield branch_url
211
        branch_url = branch.get_push_location()
212
        if branch_url is not None:
213
            yield branch_url
214
215
    def _get_web_url(self, service, location):
4505.6.10 by Jonathan Lange
Move lp_registration imports into commands.
216
        from bzrlib.plugins.launchpad.lp_registration import (
217
            NotLaunchpadBranch)
4031.2.5 by Jonathan Lange
Refactor the code so we don't do too many remote requests.
218
        for branch_url in self._possible_locations(location):
219
            try:
220
                return service.get_web_url_from_branch_url(branch_url)
221
            except (NotLaunchpadBranch, InvalidURL):
222
                pass
223
        raise NotLaunchpadBranch(branch_url)
224
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
225
    def run(self, location=None, dry_run=False):
4505.6.10 by Jonathan Lange
Move lp_registration imports into commands.
226
        from bzrlib.plugins.launchpad.lp_registration import (
227
            LaunchpadService)
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
228
        if location is None:
229
            location = u'.'
4505.6.21 by Jonathan Lange
Clean up flakes.
230
        web_url = self._get_web_url(LaunchpadService(), location)
6150.3.1 by Jonathan Riddell
gettext() in launchpad plugin
231
        trace.note(gettext('Opening %s in web browser') % web_url)
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
232
        if not dry_run:
4511.1.1 by Alexander Belchenko
launchpad plugin: import webbrowse should be explicit and never lazy, otherwise bzr.exe lacks this module.
233
            import webbrowser   # this import should not be lazy
234
                                # otherwise bzr.exe lacks this module
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
235
            webbrowser.open(web_url)
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
236
237
register_command(cmd_launchpad_open)
238
239
2898.3.3 by James Henstridge
Add launchpad-login command.
240
class cmd_launchpad_login(Command):
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
241
    __doc__ = """Show or set the Launchpad user ID.
2898.3.3 by James Henstridge
Add launchpad-login command.
242
243
    When communicating with Launchpad, some commands need to know your
244
    Launchpad user ID.  This command can be used to set or show the
245
    user ID that Bazaar will use for such communication.
246
247
    :Examples:
248
      Show the Launchpad ID of the current user::
249
250
          bzr launchpad-login
251
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
252
      Set the Launchpad ID of the current user to 'bob'::
2898.3.3 by James Henstridge
Add launchpad-login command.
253
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
254
          bzr launchpad-login bob
2898.3.3 by James Henstridge
Add launchpad-login command.
255
    """
256
    aliases = ['lp-login']
257
    takes_args = ['name?']
258
    takes_options = [
4505.1.2 by Jonathan Lange
Add many more tests, fix the actual bug.
259
        'verbose',
2898.3.4 by James Henstridge
Cleanups from mini-review by Tim.
260
        Option('no-check',
261
               "Don't check that the user name is valid."),
2898.3.3 by James Henstridge
Add launchpad-login command.
262
        ]
263
4505.1.2 by Jonathan Lange
Add many more tests, fix the actual bug.
264
    def run(self, name=None, no_check=False, verbose=False):
4505.6.13 by Jonathan Lange
Clarify the comment.
265
        # This is totally separate from any launchpadlib login system.
2898.3.8 by James Henstridge
Get rid of relative imports in Launchpad plugin.
266
        from bzrlib.plugins.launchpad import account
2898.3.3 by James Henstridge
Add launchpad-login command.
267
        check_account = not no_check
268
269
        if name is None:
270
            username = account.get_lp_login()
271
            if username:
2898.3.4 by James Henstridge
Cleanups from mini-review by Tim.
272
                if check_account:
273
                    account.check_lp_login(username)
4505.1.3 by Jonathan Lange
Add some extra untested verbosity.
274
                    if verbose:
6150.3.1 by Jonathan Riddell
gettext() in launchpad plugin
275
                        self.outf.write(gettext(
276
                            "Launchpad user ID exists and has SSH keys.\n"))
2898.3.3 by James Henstridge
Add launchpad-login command.
277
                self.outf.write(username + '\n')
278
            else:
6150.3.1 by Jonathan Riddell
gettext() in launchpad plugin
279
                self.outf.write(gettext('No Launchpad user ID configured.\n'))
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
280
                return 1
2898.3.3 by James Henstridge
Add launchpad-login command.
281
        else:
4216.2.1 by Michael Hudson
well this is it
282
            name = name.lower()
2898.3.3 by James Henstridge
Add launchpad-login command.
283
            if check_account:
284
                account.check_lp_login(name)
4505.1.3 by Jonathan Lange
Add some extra untested verbosity.
285
                if verbose:
6150.3.1 by Jonathan Riddell
gettext() in launchpad plugin
286
                    self.outf.write(gettext(
287
                        "Launchpad user ID exists and has SSH keys.\n"))
2898.3.3 by James Henstridge
Add launchpad-login command.
288
            account.set_lp_login(name)
4505.1.2 by Jonathan Lange
Add many more tests, fix the actual bug.
289
            if verbose:
6150.3.1 by Jonathan Riddell
gettext() in launchpad plugin
290
                self.outf.write(gettext("Launchpad user ID set to '%s'.\n") %
291
                                                                        (name,))
2898.3.3 by James Henstridge
Add launchpad-login command.
292
293
register_command(cmd_launchpad_login)
294
295
4505.6.9 by Jonathan Lange
Add some XXX comments based on the review.
296
# XXX: cmd_launchpad_mirror is untested
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
297
class cmd_launchpad_mirror(Command):
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
298
    __doc__ = """Ask Launchpad to mirror a branch now."""
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
299
300
    aliases = ['lp-mirror']
301
    takes_args = ['location?']
302
303
    def run(self, location='.'):
4505.6.25 by Jonathan Lange
Add a test to check what happens if launchpadlib not available.
304
        from bzrlib.plugins.launchpad import lp_api
4505.6.10 by Jonathan Lange
Move lp_registration imports into commands.
305
        from bzrlib.plugins.launchpad.lp_registration import LaunchpadService
5657.1.2 by Max Bowsher
Also make lp-mirror use open_containing, so it still works when in a subdirectory of a branch.
306
        branch, _ = _mod_branch.Branch.open_containing(location)
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
307
        service = LaunchpadService()
308
        launchpad = lp_api.login(service)
5657.1.1 by Max Bowsher
Fix bzr lp-mirror to work on command line branch URLs and branches
309
        lp_branch = lp_api.LaunchpadBranch.from_bzr(launchpad, branch,
310
                create_missing=False)
311
        lp_branch.lp.requestMirror()
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
312
313
4505.6.8 by Jonathan Lange
Always register the lp-mirror command. Only try to load launchpadlib if
314
register_command(cmd_launchpad_mirror)
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
315
316
4969.2.16 by Aaron Bentley
Updates from review.
317
class cmd_lp_propose_merge(Command):
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
318
    __doc__ = """Propose merging a branch on Launchpad.
4969.2.16 by Aaron Bentley
Updates from review.
319
320
    This will open your usual editor to provide the initial comment.  When it
4969.2.19 by Aaron Bentley
Rename submit to propose everywhere.
321
    has created the proposal, it will open it in your default web browser.
4969.2.16 by Aaron Bentley
Updates from review.
322
323
    The branch will be proposed to merge into SUBMIT_BRANCH.  If SUBMIT_BRANCH
324
    is not supplied, the remembered submit branch will be used.  If no submit
325
    branch is remembered, the development focus will be used.
326
327
    By default, the SUBMIT_BRANCH's review team will be requested to review
328
    the merge proposal.  This can be overriden by specifying --review (-R).
329
    The parameter the launchpad account name of the desired reviewer.  This
330
    may optionally be followed by '=' and the review type.  For example:
331
332
      bzr lp-propose-merge --review jrandom --review review-team=qa
333
334
    This will propose a merge,  request "jrandom" to perform a review of
335
    unspecified type, and request "review-team" to perform a "qa" review.
336
    """
4969.2.7 by Aaron Bentley
Add lp-submit, get working.
337
338
    takes_options = [Option('staging',
339
                            help='Propose the merge on staging.'),
340
                     Option('message', short_name='m', type=unicode,
341
                            help='Commit message.'),
5244.1.3 by Robert Collins
Allow setting new proposals as approved immediately.
342
                     Option('approve',
343
                            help='Mark the proposal as approved immediately.'),
4969.2.7 by Aaron Bentley
Add lp-submit, get working.
344
                     ListOption('review', short_name='R', type=unicode,
345
                            help='Requested reviewer and optional type.')]
346
347
    takes_args = ['submit_branch?']
348
4969.2.16 by Aaron Bentley
Updates from review.
349
    aliases = ['lp-submit', 'lp-propose']
350
4969.2.7 by Aaron Bentley
Add lp-submit, get working.
351
    def run(self, submit_branch=None, review=None, staging=False,
5244.1.3 by Robert Collins
Allow setting new proposals as approved immediately.
352
            message=None, approve=False):
4969.2.19 by Aaron Bentley
Rename submit to propose everywhere.
353
        from bzrlib.plugins.launchpad import lp_propose
4969.2.7 by Aaron Bentley
Add lp-submit, get working.
354
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
355
            '.')
356
        if review is None:
357
            reviews = None
358
        else:
359
            reviews = []
360
            for review in review:
361
                if '=' in review:
362
                    reviews.append(review.split('=', 2))
363
                else:
364
                    reviews.append((review, ''))
365
            if submit_branch is None:
366
                submit_branch = branch.get_submit_branch()
367
        if submit_branch is None:
368
            target = None
369
        else:
370
            target = _mod_branch.Branch.open(submit_branch)
4969.2.19 by Aaron Bentley
Rename submit to propose everywhere.
371
        proposer = lp_propose.Proposer(tree, branch, target, message,
5244.1.3 by Robert Collins
Allow setting new proposals as approved immediately.
372
                                       reviews, staging, approve=approve)
4969.2.19 by Aaron Bentley
Rename submit to propose everywhere.
373
        proposer.check_proposal()
374
        proposer.create_proposal()
4969.2.7 by Aaron Bentley
Add lp-submit, get working.
375
376
4969.2.16 by Aaron Bentley
Updates from review.
377
register_command(cmd_lp_propose_merge)
4969.2.7 by Aaron Bentley
Add lp-submit, get working.
378
379
5546.2.1 by Aaron Bentley
Add lp-find-proposal.
380
class cmd_lp_find_proposal(Command):
381
382
    __doc__ = """Find the proposal to merge this revision.
383
384
    Finds the merge proposal(s) that discussed landing the specified revision.
385
    This works only if the selected branch was the merge proposal target, and
386
    if the merged_revno is recorded for the merge proposal.  The proposal(s)
387
    are opened in a web browser.
388
389
    Any revision involved in the merge may be specified-- the revision in
390
    which the merge was performed, or one of the revisions that was merged.
391
392
    So, to find the merge proposal that reviewed line 1 of README::
393
394
      bzr lp-find-proposal -r annotate:README:1
395
    """
396
397
    takes_options = ['revision']
398
399
    def run(self, revision=None):
400
        from bzrlib.plugins.launchpad import lp_api
401
        import webbrowser
402
        b = _mod_branch.Branch.open_containing('.')[0]
403
        pb = ui.ui_factory.nested_progress_bar()
404
        b.lock_read()
405
        try:
406
            revno = self._find_merged_revno(revision, b, pb)
5546.2.4 by Aaron Bentley
Cleanup
407
            merged = self._find_proposals(revno, b, pb)
5546.2.1 by Aaron Bentley
Add lp-find-proposal.
408
            if len(merged) == 0:
6150.3.1 by Jonathan Riddell
gettext() in launchpad plugin
409
                raise BzrCommandError(gettext('No review found.'))
410
            trace.note(gettext('%d proposals(s) found.') % len(merged))
5546.2.1 by Aaron Bentley
Add lp-find-proposal.
411
            for mp in merged:
412
                webbrowser.open(lp_api.canonical_url(mp))
413
        finally:
414
            b.unlock()
415
            pb.finished()
416
417
    def _find_merged_revno(self, revision, b, pb):
418
        if revision is None:
419
            return b.revno()
6150.3.1 by Jonathan Riddell
gettext() in launchpad plugin
420
        pb.update(gettext('Finding revision-id'))
5546.2.1 by Aaron Bentley
Add lp-find-proposal.
421
        revision_id = revision[0].as_revision_id(b)
422
        # a revno spec is necessarily on the mainline.
423
        if self._is_revno_spec(revision[0]):
424
            merging_revision = revision_id
425
        else:
426
            graph = b.repository.get_graph()
6150.3.1 by Jonathan Riddell
gettext() in launchpad plugin
427
            pb.update(gettext('Finding merge'))
5546.2.1 by Aaron Bentley
Add lp-find-proposal.
428
            merging_revision = graph.find_lefthand_merger(
429
                revision_id, b.last_revision())
430
            if merging_revision is None:
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
431
                raise InvalidRevisionSpec(revision[0].user_spec, b)
6150.3.1 by Jonathan Riddell
gettext() in launchpad plugin
432
        pb.update(gettext('Finding revno'))
5546.2.1 by Aaron Bentley
Add lp-find-proposal.
433
        return b.revision_id_to_revno(merging_revision)
434
5546.2.4 by Aaron Bentley
Cleanup
435
    def _find_proposals(self, revno, b, pb):
436
        launchpad = lp_api.login(lp_registration.LaunchpadService())
6150.3.1 by Jonathan Riddell
gettext() in launchpad plugin
437
        pb.update(gettext('Finding Launchpad branch'))
5546.2.4 by Aaron Bentley
Cleanup
438
        lpb = lp_api.LaunchpadBranch.from_bzr(launchpad, b,
439
                                              create_missing=False)
6150.3.1 by Jonathan Riddell
gettext() in launchpad plugin
440
        pb.update(gettext('Finding proposals'))
5546.2.4 by Aaron Bentley
Cleanup
441
        return list(lpb.lp.getMergeProposals(status=['Merged'],
442
                                             merged_revnos=[revno]))
443
444
5546.2.1 by Aaron Bentley
Add lp-find-proposal.
445
    @staticmethod
446
    def _is_revno_spec(spec):
447
        try:
5546.2.3 by Aaron Bentley
Tighten revno check, avoid creating branches on lp.
448
            int(spec.user_spec)
5546.2.1 by Aaron Bentley
Add lp-find-proposal.
449
        except ValueError:
450
            return False
451
        else:
452
            return True
453
454
455
register_command(cmd_lp_find_proposal)
456
457
3251.4.2 by Aaron Bentley
Clean up Launchpad directory service code
458
def _register_directory():
3251.4.3 by Aaron Bentley
More renames and cleanups
459
    directories.register_lazy('lp:', 'bzrlib.plugins.launchpad.lp_directory',
3251.4.2 by Aaron Bentley
Clean up Launchpad directory service code
460
                              'LaunchpadDirectory',
461
                              'Launchpad-based directory service',)
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
462
    directories.register_lazy(
463
        'debianlp:', 'bzrlib.plugins.launchpad.lp_directory',
464
        'LaunchpadDirectory',
465
        'debianlp: shortcut')
466
    directories.register_lazy(
467
        'ubuntu:', 'bzrlib.plugins.launchpad.lp_directory',
468
        'LaunchpadDirectory',
469
        'ubuntu: shortcut')
470
3251.4.2 by Aaron Bentley
Clean up Launchpad directory service code
471
_register_directory()
2245.8.5 by Martin Pool
Add short-form lp:PRODUCT url form
472
6024.3.5 by John Arbash Meinel
Pull out code into helper functions, which allows us to test it.
473
# This is kept in __init__ so that we don't load lp_api_lite unless the branch
474
# actually matches. That way we can avoid importing extra dependencies like
475
# json.
476
_package_branch = lazy_regex.lazy_compile(
6024.3.4 by John Arbash Meinel
More tests, and a small fix for the regex.
477
    r'bazaar.launchpad.net.*?/'
6024.3.3 by John Arbash Meinel
Start at least testing the package_branch regex.
478
    r'(?P<user>~[^/]+/)?(?P<archive>ubuntu|debian)/(?P<series>[^/]+/)?'
5050.79.5 by John Arbash Meinel
Add a Branch.open hook.
479
    r'(?P<project>[^/]+)(?P<branch>/[^/]+)?'
480
    )
6024.3.5 by John Arbash Meinel
Pull out code into helper functions, which allows us to test it.
481
482
def _get_package_branch_info(url):
483
    """Determine the packaging information for this URL.
484
485
    :return: If this isn't a packaging branch, return None. If it is, return
486
        (archive, series, project)
487
    """
6015.10.2 by John Arbash Meinel
Fix bug #816417, it seems older foreign branch implementations did not set Branch.base.
488
    if url is None:
489
        return None
6024.3.5 by John Arbash Meinel
Pull out code into helper functions, which allows us to test it.
490
    m = _package_branch.search(url)
5050.79.5 by John Arbash Meinel
Add a Branch.open hook.
491
    if m is None:
6015.10.2 by John Arbash Meinel
Fix bug #816417, it seems older foreign branch implementations did not set Branch.base.
492
        return None
6024.3.3 by John Arbash Meinel
Start at least testing the package_branch regex.
493
    archive, series, project, user = m.group('archive', 'series',
494
                                             'project', 'user')
5050.79.5 by John Arbash Meinel
Add a Branch.open hook.
495
    if series is not None:
5050.79.7 by John Arbash Meinel
clarify a comment.
496
        # series is optional, so the regex includes the extra '/', we don't
497
        # want to send that on (it causes Internal Server Errors.)
5050.79.5 by John Arbash Meinel
Add a Branch.open hook.
498
        series = series.strip('/')
6024.3.5 by John Arbash Meinel
Pull out code into helper functions, which allows us to test it.
499
    if user is not None:
500
        user = user.strip('~/')
501
        if user != 'ubuntu-branches':
502
            return None
503
    return archive, series, project
504
505
506
def _check_is_up_to_date(the_branch):
507
    info = _get_package_branch_info(the_branch.base)
508
    if info is None:
509
        return
6024.3.8 by John Arbash Meinel
Move 'place' logic onto LatestPublication.
510
    c = the_branch.get_config()
6024.3.10 by John Arbash Meinel
Try refactoring the code a bit per vila's suggestions.
511
    verbosity = c.get_user_option('launchpad.packaging_verbosity')
6024.3.8 by John Arbash Meinel
Move 'place' logic onto LatestPublication.
512
    if verbosity is not None:
513
        verbosity = verbosity.lower()
514
    if verbosity == 'off':
515
        trace.mutter('not checking %s because verbosity is turned off'
516
                     % (the_branch.base,))
517
        return
6024.3.5 by John Arbash Meinel
Pull out code into helper functions, which allows us to test it.
518
    archive, series, project = info
519
    from bzrlib.plugins.launchpad import lp_api_lite
5050.79.5 by John Arbash Meinel
Add a Branch.open hook.
520
    latest_pub = lp_api_lite.LatestPublication(archive, series, project)
6024.3.8 by John Arbash Meinel
Move 'place' logic onto LatestPublication.
521
    lp_api_lite.report_freshness(the_branch, verbosity, latest_pub)
522
5050.79.5 by John Arbash Meinel
Add a Branch.open hook.
523
524
def _register_hooks():
525
    _mod_branch.Branch.hooks.install_named_hook('open',
526
        _check_is_up_to_date, 'package-branch-up-to-date')
527
528
529
_register_hooks()
530
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
531
def load_tests(basic_tests, module, loader):
532
    testmod_names = [
533
        'test_account',
534
        'test_register',
4505.6.25 by Jonathan Lange
Add a test to check what happens if launchpadlib not available.
535
        'test_lp_api',
5050.79.2 by John Arbash Meinel
Start refactoring lp_api_lite and making it testable.
536
        'test_lp_api_lite',
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
537
        'test_lp_directory',
538
        'test_lp_login',
539
        'test_lp_open',
540
        'test_lp_service',
541
        ]
542
    basic_tests.addTest(loader.loadTestsFromModuleNames(
543
            ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
544
    return basic_tests
2245.8.1 by Martin Pool
Start adding tests for launchpad indirection
545
2245.8.6 by Martin Pool
Documentation under 'help launchpad'
546
547
_launchpad_help = """Integration with Launchpad.net
548
549
Launchpad.net provides free Bazaar branch hosting with integrated bug and
550
specification tracking.
551
3031.1.1 by jml at canonical
Expand the documentation on lp:// URLs and mention the launchpad-login command.
552
The bzr client (through the plugin called 'launchpad') has special
2245.8.6 by Martin Pool
Documentation under 'help launchpad'
553
features to communicate with Launchpad:
554
3031.1.1 by jml at canonical
Expand the documentation on lp:// URLs and mention the launchpad-login command.
555
    * The launchpad-login command tells Bazaar your Launchpad user name. This
556
      is then used by the 'lp:' transport to download your branches using
557
      bzr+ssh://.
558
4258.1.1 by James Westby
Add "--fixes lp:" to the launchpad plugin documentation.
559
    * The 'lp:' transport uses Launchpad as a directory service: for example
560
      'lp:bzr' and 'lp:python' refer to the main branches of the relevant
561
      projects and may be branched, logged, etc. You can also use the 'lp:'
562
      transport to refer to specific branches, e.g. lp:~bzr/bzr/trunk.
563
564
    * The 'lp:' bug tracker alias can expand launchpad bug numbers to their
565
      URLs for use with 'bzr commit --fixes', e.g. 'bzr commit --fixes lp:12345'
566
      will record a revision property that marks that revision as fixing
4258.1.2 by Matt Nordhoff
Fix a typo in the launchpad plugin's help
567
      Launchpad bug 12345. When you push that branch to Launchpad it will
568
      automatically be linked to the bug report.
4258.1.1 by James Westby
Add "--fixes lp:" to the launchpad plugin documentation.
569
3031.1.1 by jml at canonical
Expand the documentation on lp:// URLs and mention the launchpad-login command.
570
    * The register-branch command tells Launchpad about the url of a
2245.8.6 by Martin Pool
Documentation under 'help launchpad'
571
      public branch.  Launchpad will then mirror the branch, display
3031.1.1 by jml at canonical
Expand the documentation on lp:// URLs and mention the launchpad-login command.
572
      its contents and allow it to be attached to bugs and other
2245.8.6 by Martin Pool
Documentation under 'help launchpad'
573
      objects.
574
575
For more information see http://help.launchpad.net/
576
"""
577
topic_registry.register('launchpad',
578
    _launchpad_help,
579
    'Using Bazaar with Launchpad.net')