/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3251.4.2 by Aaron Bentley
Clean up Launchpad directory service code
1
# Copyright (C) 2006 - 2008 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
2898.3.8 by James Henstridge
Get rid of relative imports in Launchpad plugin.
17
"""Launchpad.net integration plugin for Bazaar."""
0.4.1 by Martin Pool
Start lp-register command
18
0.4.17 by Martin Pool
Allow xmlrpc service url to be overridden by $BZR_LP_XMLRPC_URL
19
# The XMLRPC server address can be overridden by setting the environment
20
# variable $BZR_LP_XMLRPL_URL
21
0.4.9 by Martin Pool
Don't transmit non-standard xmlrpc <nil> value.
22
# see http://bazaar-vcs.org/Specs/BranchRegistrationTool
23
4283.3.1 by Vincent Ladeuil
Make built-in plugins display the same version than bzrlib.
24
# Since we are a built-in plugin we share the bzrlib version
25
from bzrlib import version_info
26
4031.2.11 by John Arbash Meinel
Turn a bunch of imports into lazy imports.
27
from bzrlib.lazy_import import lazy_import
28
lazy_import(globals(), """
29
from bzrlib import (
30
    branch as _mod_branch,
31
    trace,
32
    )
33
""")
34
0.4.1 by Martin Pool
Start lp-register command
35
from bzrlib.commands import Command, Option, register_command
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
36
from bzrlib.directory_service import directories
4031.2.5 by Jonathan Lange
Refactor the code so we don't do too many remote requests.
37
from bzrlib.errors import (
38
    BzrCommandError,
4505.6.25 by Jonathan Lange
Add a test to check what happens if launchpadlib not available.
39
    DependencyNotPresent,
4031.2.5 by Jonathan Lange
Refactor the code so we don't do too many remote requests.
40
    InvalidURL,
41
    NoPublicBranch,
42
    NotBranchError,
43
    )
2245.8.6 by Martin Pool
Documentation under 'help launchpad'
44
from bzrlib.help_topics import topic_registry
0.4.4 by Martin Pool
Start forming xmlrpc requests
45
46
0.4.2 by Martin Pool
Rename command to 'register-branch'
47
class cmd_register_branch(Command):
48
    """Register a branch with launchpad.net.
0.4.1 by Martin Pool
Start lp-register command
49
50
    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)
51
    launchpad.net.  Registration allows the branch to be associated with
0.4.1 by Martin Pool
Start lp-register command
52
    bugs or specifications.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
53
4416.7.1 by Neil Martinsen-Burrell
Fix 238764 refer to projects rather than products in launchpad plugin
54
    Before using this command you must register the project to which the
0.4.1 by Martin Pool
Start lp-register command
55
    branch belongs, and create an account for yourself on launchpad.net.
0.4.3 by Martin Pool
More command line processing
56
57
    arguments:
3200.2.3 by Robert Collins
Tweak wording.
58
        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
59
                    This must be an http or https url (which Launchpad can read
60
                    from to access the branch). Local file urls, SFTP urls, and
61
                    bzr+ssh urls will not work.
62
                    If no public_url is provided, bzr will use the configured
3200.2.3 by Robert Collins
Tweak wording.
63
                    public_url if there is one for the current branch, and
64
                    otherwise error.
0.4.3 by Martin Pool
More command line processing
65
66
    example:
4416.7.1 by Neil Martinsen-Burrell
Fix 238764 refer to projects rather than products in launchpad plugin
67
        bzr register-branch http://foo.com/bzr/fooproject.mine \\
68
                --project fooproject
0.4.1 by Martin Pool
Start lp-register command
69
    """
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
70
    takes_args = ['public_url?']
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
71
    takes_options = [
4416.7.1 by Neil Martinsen-Burrell
Fix 238764 refer to projects rather than products in launchpad plugin
72
         Option('project',
73
                'Launchpad project short name to associate with the branch.',
0.4.15 by Martin Pool
(register-branch) Add command-line options
74
                unicode),
4416.7.3 by Neil Martinsen-Burrell
Retain --product options for register-branch, but deprecate it in favor of --project
75
         Option('product',
76
                'Launchpad product short name to associate with the branch.', 
77
                unicode,
78
                hidden=True),
0.4.15 by Martin Pool
(register-branch) Add command-line options
79
         Option('branch-name',
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
80
                'Short name for the branch; '
81
                'by default taken from the last component of the url.',
0.4.15 by Martin Pool
(register-branch) Add command-line options
82
                unicode),
83
         Option('branch-title',
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
84
                'One-sentence description of the branch.',
0.4.15 by Martin Pool
(register-branch) Add command-line options
85
                unicode),
86
         Option('branch-description',
2598.1.5 by Martin Pool
Fix one more option message.
87
                'Longer description of the purpose or contents of the branch.',
0.4.15 by Martin Pool
(register-branch) Add command-line options
88
                unicode),
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
89
         Option('author',
90
                "Branch author's email address, if not yourself.",
0.4.16 by Martin Pool
(register-branch) Add --author option and respect --dry-run
91
                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.
92
         Option('link-bug',
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
93
                '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.
94
                int),
0.4.15 by Martin Pool
(register-branch) Add command-line options
95
         Option('dry-run',
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
96
                'Prepare the request but don\'t actually send it.')
0.4.15 by Martin Pool
(register-branch) Add command-line options
97
        ]
98
99
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
100
    def run(self,
101
            public_url=None,
4416.7.1 by Neil Martinsen-Burrell
Fix 238764 refer to projects rather than products in launchpad plugin
102
            project='',
4416.7.3 by Neil Martinsen-Burrell
Retain --product options for register-branch, but deprecate it in favor of --project
103
            product=None,
0.4.15 by Martin Pool
(register-branch) Add command-line options
104
            branch_name='',
105
            branch_title='',
106
            branch_description='',
0.4.16 by Martin Pool
(register-branch) Add --author option and respect --dry-run
107
            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.
108
            link_bug=None,
0.4.15 by Martin Pool
(register-branch) Add command-line options
109
            dry_run=False):
2898.3.8 by James Henstridge
Get rid of relative imports in Launchpad plugin.
110
        from bzrlib.plugins.launchpad.lp_registration import (
4505.6.4 by Jonathan Lange
Remove unnecessary import.
111
            BranchRegistrationRequest, BranchBugLinkRequest,
4505.6.10 by Jonathan Lange
Move lp_registration imports into commands.
112
            DryRunLaunchpadService, LaunchpadService)
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
113
        if public_url is None:
114
            try:
4031.2.11 by John Arbash Meinel
Turn a bunch of imports into lazy imports.
115
                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
116
            except NotBranchError:
117
                raise BzrCommandError('register-branch requires a public '
118
                    'branch url - see bzr help register-branch.')
119
            public_url = b.get_public_branch()
120
            if public_url is None:
121
                raise NoPublicBranch(b)
4416.7.3 by Neil Martinsen-Burrell
Retain --product options for register-branch, but deprecate it in favor of --project
122
        if product is not None:
123
            project = product
124
            trace.note('--product is deprecated; please use --project.')
125
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
126
127
        rego = BranchRegistrationRequest(branch_url=public_url,
0.4.15 by Martin Pool
(register-branch) Add command-line options
128
                                         branch_name=branch_name,
129
                                         branch_title=branch_title,
130
                                         branch_description=branch_description,
4416.7.1 by Neil Martinsen-Burrell
Fix 238764 refer to projects rather than products in launchpad plugin
131
                                         product_name=project,
0.4.16 by Martin Pool
(register-branch) Add --author option and respect --dry-run
132
                                         author_email=author,
0.4.15 by Martin Pool
(register-branch) Add command-line options
133
                                         )
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
134
        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.
135
                                     bug_id=link_bug)
1668.1.12 by Martin Pool
(launchpad plugin) Improved --dry-run that uses a dummy xmlrpc service.
136
        if not dry_run:
137
            service = LaunchpadService()
138
            # This gives back the xmlrpc url that can be used for future
139
            # operations on the branch.  It's not so useful to print to the
140
            # user since they can't do anything with it from a web browser; it
141
            # might be nice for the server to tell us about an html url as
142
            # well.
143
        else:
144
            # Run on service entirely in memory
145
            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.
146
        service.gather_user_credentials()
4505.6.21 by Jonathan Lange
Clean up flakes.
147
        rego.submit(service)
1668.1.12 by Martin Pool
(launchpad plugin) Improved --dry-run that uses a dummy xmlrpc service.
148
        if link_bug:
4505.6.21 by Jonathan Lange
Clean up flakes.
149
            linko.submit(service)
1668.1.12 by Martin Pool
(launchpad plugin) Improved --dry-run that uses a dummy xmlrpc service.
150
        print 'Branch registered.'
0.4.1 by Martin Pool
Start lp-register command
151
0.4.2 by Martin Pool
Rename command to 'register-branch'
152
register_command(cmd_register_branch)
2245.8.4 by Martin Pool
lp:/// indirection works
153
2898.3.3 by James Henstridge
Add launchpad-login command.
154
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
155
class cmd_launchpad_open(Command):
156
    """Open a Launchpad branch page in your web browser."""
157
158
    aliases = ['lp-open']
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
159
    takes_options = [
160
        Option('dry-run',
161
               'Do not actually open the browser. Just say the URL we would '
162
               'use.'),
163
        ]
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
164
    takes_args = ['location?']
165
4031.2.5 by Jonathan Lange
Refactor the code so we don't do too many remote requests.
166
    def _possible_locations(self, location):
167
        """Yield possible external locations for the branch at 'location'."""
168
        yield location
169
        try:
4867.2.1 by Neil Martinsen-Burrell
launchpad-open works from a subdirectory of a branch
170
            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.
171
        except NotBranchError:
172
            return
173
        branch_url = branch.get_public_branch()
174
        if branch_url is not None:
175
            yield branch_url
176
        branch_url = branch.get_push_location()
177
        if branch_url is not None:
178
            yield branch_url
179
180
    def _get_web_url(self, service, location):
4505.6.10 by Jonathan Lange
Move lp_registration imports into commands.
181
        from bzrlib.plugins.launchpad.lp_registration import (
182
            NotLaunchpadBranch)
4031.2.5 by Jonathan Lange
Refactor the code so we don't do too many remote requests.
183
        for branch_url in self._possible_locations(location):
184
            try:
185
                return service.get_web_url_from_branch_url(branch_url)
186
            except (NotLaunchpadBranch, InvalidURL):
187
                pass
188
        raise NotLaunchpadBranch(branch_url)
189
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
190
    def run(self, location=None, dry_run=False):
4505.6.10 by Jonathan Lange
Move lp_registration imports into commands.
191
        from bzrlib.plugins.launchpad.lp_registration import (
192
            LaunchpadService)
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
193
        if location is None:
194
            location = u'.'
4505.6.21 by Jonathan Lange
Clean up flakes.
195
        web_url = self._get_web_url(LaunchpadService(), location)
4031.2.11 by John Arbash Meinel
Turn a bunch of imports into lazy imports.
196
        trace.note('Opening %s in web browser' % web_url)
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
197
        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.
198
            import webbrowser   # this import should not be lazy
199
                                # otherwise bzr.exe lacks this module
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
200
            webbrowser.open(web_url)
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
201
202
register_command(cmd_launchpad_open)
203
204
2898.3.3 by James Henstridge
Add launchpad-login command.
205
class cmd_launchpad_login(Command):
2934.1.1 by Ian Clatworthy
(James Henstridge) add a command for managing the Launchpad user ID
206
    """Show or set the Launchpad user ID.
2898.3.3 by James Henstridge
Add launchpad-login command.
207
208
    When communicating with Launchpad, some commands need to know your
209
    Launchpad user ID.  This command can be used to set or show the
210
    user ID that Bazaar will use for such communication.
211
212
    :Examples:
213
      Show the Launchpad ID of the current user::
214
215
          bzr launchpad-login
216
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
217
      Set the Launchpad ID of the current user to 'bob'::
2898.3.3 by James Henstridge
Add launchpad-login command.
218
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
219
          bzr launchpad-login bob
2898.3.3 by James Henstridge
Add launchpad-login command.
220
    """
221
    aliases = ['lp-login']
222
    takes_args = ['name?']
223
    takes_options = [
4505.1.2 by Jonathan Lange
Add many more tests, fix the actual bug.
224
        'verbose',
2898.3.4 by James Henstridge
Cleanups from mini-review by Tim.
225
        Option('no-check',
226
               "Don't check that the user name is valid."),
2898.3.3 by James Henstridge
Add launchpad-login command.
227
        ]
228
4505.1.2 by Jonathan Lange
Add many more tests, fix the actual bug.
229
    def run(self, name=None, no_check=False, verbose=False):
4505.6.13 by Jonathan Lange
Clarify the comment.
230
        # This is totally separate from any launchpadlib login system.
2898.3.8 by James Henstridge
Get rid of relative imports in Launchpad plugin.
231
        from bzrlib.plugins.launchpad import account
2898.3.3 by James Henstridge
Add launchpad-login command.
232
        check_account = not no_check
233
234
        if name is None:
235
            username = account.get_lp_login()
236
            if username:
2898.3.4 by James Henstridge
Cleanups from mini-review by Tim.
237
                if check_account:
238
                    account.check_lp_login(username)
4505.1.3 by Jonathan Lange
Add some extra untested verbosity.
239
                    if verbose:
240
                        self.outf.write(
241
                            "Launchpad user ID exists and has SSH keys.\n")
2898.3.3 by James Henstridge
Add launchpad-login command.
242
                self.outf.write(username + '\n')
243
            else:
244
                self.outf.write('No Launchpad user ID configured.\n')
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
245
                return 1
2898.3.3 by James Henstridge
Add launchpad-login command.
246
        else:
4216.2.1 by Michael Hudson
well this is it
247
            name = name.lower()
2898.3.3 by James Henstridge
Add launchpad-login command.
248
            if check_account:
249
                account.check_lp_login(name)
4505.1.3 by Jonathan Lange
Add some extra untested verbosity.
250
                if verbose:
251
                    self.outf.write(
252
                        "Launchpad user ID exists and has SSH keys.\n")
2898.3.3 by James Henstridge
Add launchpad-login command.
253
            account.set_lp_login(name)
4505.1.2 by Jonathan Lange
Add many more tests, fix the actual bug.
254
            if verbose:
255
                self.outf.write("Launchpad user ID set to '%s'.\n" % (name,))
2898.3.3 by James Henstridge
Add launchpad-login command.
256
257
register_command(cmd_launchpad_login)
258
259
4505.6.9 by Jonathan Lange
Add some XXX comments based on the review.
260
# XXX: cmd_launchpad_mirror is untested
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
261
class cmd_launchpad_mirror(Command):
262
    """Ask Launchpad to mirror a branch now."""
263
264
    aliases = ['lp-mirror']
265
    takes_args = ['location?']
266
267
    def run(self, location='.'):
4505.6.25 by Jonathan Lange
Add a test to check what happens if launchpadlib not available.
268
        from bzrlib.plugins.launchpad import lp_api
4505.6.10 by Jonathan Lange
Move lp_registration imports into commands.
269
        from bzrlib.plugins.launchpad.lp_registration import LaunchpadService
4505.6.30 by Jonathan Lange
Add basic smoke tests to show that the command exists.
270
        branch = _mod_branch.Branch.open(location)
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
271
        service = LaunchpadService()
272
        launchpad = lp_api.login(service)
273
        lp_branch = lp_api.load_branch(launchpad, branch)
274
        lp_branch.requestMirror()
275
276
4505.6.8 by Jonathan Lange
Always register the lp-mirror command. Only try to load launchpadlib if
277
register_command(cmd_launchpad_mirror)
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
278
279
3251.4.2 by Aaron Bentley
Clean up Launchpad directory service code
280
def _register_directory():
3251.4.3 by Aaron Bentley
More renames and cleanups
281
    directories.register_lazy('lp:', 'bzrlib.plugins.launchpad.lp_directory',
3251.4.2 by Aaron Bentley
Clean up Launchpad directory service code
282
                              'LaunchpadDirectory',
283
                              'Launchpad-based directory service',)
284
_register_directory()
2245.8.5 by Martin Pool
Add short-form lp:PRODUCT url form
285
0.4.1 by Martin Pool
Start lp-register command
286
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
287
def load_tests(basic_tests, module, loader):
288
    testmod_names = [
289
        'test_account',
290
        'test_register',
4505.6.25 by Jonathan Lange
Add a test to check what happens if launchpadlib not available.
291
        'test_lp_api',
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
292
        'test_lp_directory',
293
        'test_lp_login',
294
        'test_lp_open',
295
        'test_lp_service',
296
        ]
297
    basic_tests.addTest(loader.loadTestsFromModuleNames(
298
            ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
299
    return basic_tests
2245.8.1 by Martin Pool
Start adding tests for launchpad indirection
300
2245.8.6 by Martin Pool
Documentation under 'help launchpad'
301
302
_launchpad_help = """Integration with Launchpad.net
303
304
Launchpad.net provides free Bazaar branch hosting with integrated bug and
305
specification tracking.
306
3031.1.1 by jml at canonical
Expand the documentation on lp:// URLs and mention the launchpad-login command.
307
The bzr client (through the plugin called 'launchpad') has special
2245.8.6 by Martin Pool
Documentation under 'help launchpad'
308
features to communicate with Launchpad:
309
3031.1.1 by jml at canonical
Expand the documentation on lp:// URLs and mention the launchpad-login command.
310
    * The launchpad-login command tells Bazaar your Launchpad user name. This
311
      is then used by the 'lp:' transport to download your branches using
312
      bzr+ssh://.
313
4258.1.1 by James Westby
Add "--fixes lp:" to the launchpad plugin documentation.
314
    * The 'lp:' transport uses Launchpad as a directory service: for example
315
      'lp:bzr' and 'lp:python' refer to the main branches of the relevant
316
      projects and may be branched, logged, etc. You can also use the 'lp:'
317
      transport to refer to specific branches, e.g. lp:~bzr/bzr/trunk.
318
319
    * The 'lp:' bug tracker alias can expand launchpad bug numbers to their
320
      URLs for use with 'bzr commit --fixes', e.g. 'bzr commit --fixes lp:12345'
321
      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
322
      Launchpad bug 12345. When you push that branch to Launchpad it will
323
      automatically be linked to the bug report.
4258.1.1 by James Westby
Add "--fixes lp:" to the launchpad plugin documentation.
324
3031.1.1 by jml at canonical
Expand the documentation on lp:// URLs and mention the launchpad-login command.
325
    * The register-branch command tells Launchpad about the url of a
2245.8.6 by Martin Pool
Documentation under 'help launchpad'
326
      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.
327
      its contents and allow it to be attached to bugs and other
2245.8.6 by Martin Pool
Documentation under 'help launchpad'
328
      objects.
329
330
For more information see http://help.launchpad.net/
331
"""
332
topic_registry.register('launchpad',
333
    _launchpad_help,
334
    'Using Bazaar with Launchpad.net')