~bzr-pqm/bzr/bzr.dev

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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
24
from bzrlib.branch import Branch
0.4.1 by Martin Pool
Start lp-register command
25
from bzrlib.commands import Command, Option, register_command
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
26
from bzrlib.directory_service import directories
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
27
from bzrlib.errors import BzrCommandError, NoPublicBranch, NotBranchError
2245.8.6 by Martin Pool
Documentation under 'help launchpad'
28
from bzrlib.help_topics import topic_registry
0.4.4 by Martin Pool
Start forming xmlrpc requests
29
30
0.4.2 by Martin Pool
Rename command to 'register-branch'
31
class cmd_register_branch(Command):
32
    """Register a branch with launchpad.net.
0.4.1 by Martin Pool
Start lp-register command
33
34
    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)
35
    launchpad.net.  Registration allows the branch to be associated with
0.4.1 by Martin Pool
Start lp-register command
36
    bugs or specifications.
37
    
0.4.15 by Martin Pool
(register-branch) Add command-line options
38
    Before using this command you must register the product to which the
0.4.1 by Martin Pool
Start lp-register command
39
    branch belongs, and create an account for yourself on launchpad.net.
0.4.3 by Martin Pool
More command line processing
40
41
    arguments:
3200.2.3 by Robert Collins
Tweak wording.
42
        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
43
                    This must be an http or https url (which Launchpad can read
44
                    from to access the branch). Local file urls, SFTP urls, and
45
                    bzr+ssh urls will not work.
46
                    If no public_url is provided, bzr will use the configured
3200.2.3 by Robert Collins
Tweak wording.
47
                    public_url if there is one for the current branch, and
48
                    otherwise error.
0.4.3 by Martin Pool
More command line processing
49
50
    example:
0.4.15 by Martin Pool
(register-branch) Add command-line options
51
        bzr register-branch http://foo.com/bzr/fooproduct.mine \\
52
                --product fooproduct
0.4.1 by Martin Pool
Start lp-register command
53
    """
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
54
    takes_args = ['public_url?']
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
55
    takes_options = [
56
         Option('product',
57
                'Launchpad product short name to associate with the branch.',
0.4.15 by Martin Pool
(register-branch) Add command-line options
58
                unicode),
59
         Option('branch-name',
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
60
                'Short name for the branch; '
61
                'by default taken from the last component of the url.',
0.4.15 by Martin Pool
(register-branch) Add command-line options
62
                unicode),
63
         Option('branch-title',
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
64
                'One-sentence description of the branch.',
0.4.15 by Martin Pool
(register-branch) Add command-line options
65
                unicode),
66
         Option('branch-description',
2598.1.5 by Martin Pool
Fix one more option message.
67
                'Longer description of the purpose or contents of the branch.',
0.4.15 by Martin Pool
(register-branch) Add command-line options
68
                unicode),
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
69
         Option('author',
70
                "Branch author's email address, if not yourself.",
0.4.16 by Martin Pool
(register-branch) Add --author option and respect --dry-run
71
                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.
72
         Option('link-bug',
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
73
                '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.
74
                int),
0.4.15 by Martin Pool
(register-branch) Add command-line options
75
         Option('dry-run',
2598.1.1 by Martin Pool
Add test for and documentation of option style, fix up existing options to comply
76
                'Prepare the request but don\'t actually send it.')
0.4.15 by Martin Pool
(register-branch) Add command-line options
77
        ]
78
79
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
80
    def run(self,
81
            public_url=None,
1668.1.12 by Martin Pool
(launchpad plugin) Improved --dry-run that uses a dummy xmlrpc service.
82
            product='',
0.4.15 by Martin Pool
(register-branch) Add command-line options
83
            branch_name='',
84
            branch_title='',
85
            branch_description='',
0.4.16 by Martin Pool
(register-branch) Add --author option and respect --dry-run
86
            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.
87
            link_bug=None,
0.4.15 by Martin Pool
(register-branch) Add command-line options
88
            dry_run=False):
2898.3.8 by James Henstridge
Get rid of relative imports in Launchpad plugin.
89
        from bzrlib.plugins.launchpad.lp_registration import (
1668.1.12 by Martin Pool
(launchpad plugin) Improved --dry-run that uses a dummy xmlrpc service.
90
            LaunchpadService, BranchRegistrationRequest, BranchBugLinkRequest,
91
            DryRunLaunchpadService)
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
92
        if public_url is None:
93
            try:
94
                b = Branch.open_containing('.')[0]
95
            except NotBranchError:
96
                raise BzrCommandError('register-branch requires a public '
97
                    'branch url - see bzr help register-branch.')
98
            public_url = b.get_public_branch()
99
            if public_url is None:
100
                raise NoPublicBranch(b)
101
102
        rego = BranchRegistrationRequest(branch_url=public_url,
0.4.15 by Martin Pool
(register-branch) Add command-line options
103
                                         branch_name=branch_name,
104
                                         branch_title=branch_title,
105
                                         branch_description=branch_description,
106
                                         product_name=product,
0.4.16 by Martin Pool
(register-branch) Add --author option and respect --dry-run
107
                                         author_email=author,
0.4.15 by Martin Pool
(register-branch) Add command-line options
108
                                         )
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
109
        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.
110
                                     bug_id=link_bug)
1668.1.12 by Martin Pool
(launchpad plugin) Improved --dry-run that uses a dummy xmlrpc service.
111
        if not dry_run:
112
            service = LaunchpadService()
113
            # This gives back the xmlrpc url that can be used for future
114
            # operations on the branch.  It's not so useful to print to the
115
            # user since they can't do anything with it from a web browser; it
116
            # might be nice for the server to tell us about an html url as
117
            # well.
118
        else:
119
            # Run on service entirely in memory
120
            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.
121
        service.gather_user_credentials()
1668.1.12 by Martin Pool
(launchpad plugin) Improved --dry-run that uses a dummy xmlrpc service.
122
        branch_object_url = rego.submit(service)
123
        if link_bug:
124
            link_bug_url = linko.submit(service)
125
        print 'Branch registered.'
0.4.1 by Martin Pool
Start lp-register command
126
0.4.2 by Martin Pool
Rename command to 'register-branch'
127
register_command(cmd_register_branch)
2245.8.4 by Martin Pool
lp:/// indirection works
128
2898.3.3 by James Henstridge
Add launchpad-login command.
129
3955.3.6 by Jonathan Lange
Add a TODO.
130
# XXX: Make notes to test this.
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
131
class cmd_launchpad_open(Command):
132
    """Open a Launchpad branch page in your web browser."""
133
134
    aliases = ['lp-open']
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
135
    takes_options = [
136
        Option('dry-run',
137
               'Do not actually open the browser. Just say the URL we would '
138
               'use.'),
139
        ]
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
140
    takes_args = ['location?']
141
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
142
    def run(self, location=None, dry_run=False):
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
143
        from bzrlib.plugins.launchpad.lp_registration import LaunchpadService
144
        from bzrlib.trace import note
145
        import webbrowser
146
        if location is None:
147
            location = u'.'
148
        branch = Branch.open(location)
149
        branch_url = branch.get_public_branch()
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
150
        if branch_url is None:
151
            raise NoPublicBranch(branch)
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
152
        service = LaunchpadService()
153
        web_url = service.get_web_url_from_branch_url(branch_url)
154
        note('Opening %s in web browser' % web_url)
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
155
        if not dry_run:
156
            webbrowser.open(web_url)
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
157
158
register_command(cmd_launchpad_open)
159
160
2898.3.3 by James Henstridge
Add launchpad-login command.
161
class cmd_launchpad_login(Command):
2934.1.1 by Ian Clatworthy
(James Henstridge) add a command for managing the Launchpad user ID
162
    """Show or set the Launchpad user ID.
2898.3.3 by James Henstridge
Add launchpad-login command.
163
164
    When communicating with Launchpad, some commands need to know your
165
    Launchpad user ID.  This command can be used to set or show the
166
    user ID that Bazaar will use for such communication.
167
168
    :Examples:
169
      Show the Launchpad ID of the current user::
170
171
          bzr launchpad-login
172
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
173
      Set the Launchpad ID of the current user to 'bob'::
2898.3.3 by James Henstridge
Add launchpad-login command.
174
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
175
          bzr launchpad-login bob
2898.3.3 by James Henstridge
Add launchpad-login command.
176
    """
177
    aliases = ['lp-login']
178
    takes_args = ['name?']
179
    takes_options = [
2898.3.4 by James Henstridge
Cleanups from mini-review by Tim.
180
        Option('no-check',
181
               "Don't check that the user name is valid."),
2898.3.3 by James Henstridge
Add launchpad-login command.
182
        ]
183
2898.3.7 by James Henstridge
* Add tests for account.py exception messages.
184
    def run(self, name=None, no_check=False):
2898.3.8 by James Henstridge
Get rid of relative imports in Launchpad plugin.
185
        from bzrlib.plugins.launchpad import account
2898.3.3 by James Henstridge
Add launchpad-login command.
186
        check_account = not no_check
187
188
        if name is None:
189
            username = account.get_lp_login()
190
            if username:
2898.3.4 by James Henstridge
Cleanups from mini-review by Tim.
191
                if check_account:
192
                    account.check_lp_login(username)
2898.3.3 by James Henstridge
Add launchpad-login command.
193
                self.outf.write(username + '\n')
194
            else:
195
                self.outf.write('No Launchpad user ID configured.\n')
2898.3.9 by James Henstridge
* Add a simple NEWS item for the command.
196
                return 1
2898.3.3 by James Henstridge
Add launchpad-login command.
197
        else:
198
            if check_account:
199
                account.check_lp_login(name)
200
            account.set_lp_login(name)
201
202
register_command(cmd_launchpad_login)
203
204
3251.4.2 by Aaron Bentley
Clean up Launchpad directory service code
205
def _register_directory():
3251.4.3 by Aaron Bentley
More renames and cleanups
206
    directories.register_lazy('lp:', 'bzrlib.plugins.launchpad.lp_directory',
3251.4.2 by Aaron Bentley
Clean up Launchpad directory service code
207
                              'LaunchpadDirectory',
208
                              'Launchpad-based directory service',)
209
_register_directory()
2245.8.5 by Martin Pool
Add short-form lp:PRODUCT url form
210
0.4.1 by Martin Pool
Start lp-register command
211
212
def test_suite():
213
    """Called by bzrlib to fetch tests for this plugin"""
214
    from unittest import TestSuite, TestLoader
2898.3.8 by James Henstridge
Get rid of relative imports in Launchpad plugin.
215
    from bzrlib.plugins.launchpad import (
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
216
        test_account,
217
        test_lp_directory,
218
        test_lp_open,
219
        test_lp_service,
220
        test_register,
221
        )
2245.8.1 by Martin Pool
Start adding tests for launchpad indirection
222
223
    loader = TestLoader()
224
    suite = TestSuite()
3221.4.2 by Martin Pool
Add in thumpers tests for selection of the right Launchpad instance
225
    for module in [
3221.4.5 by Martin Pool
Tweak indents
226
        test_account,
227
        test_register,
3251.4.3 by Aaron Bentley
More renames and cleanups
228
        test_lp_directory,
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
229
        test_lp_open,
3221.4.5 by Martin Pool
Tweak indents
230
        test_lp_service,
3193.5.3 by Tim Penhey
Tweaks following review.
231
        ]:
3221.4.2 by Martin Pool
Add in thumpers tests for selection of the right Launchpad instance
232
        suite.addTests(loader.loadTestsFromModule(module))
2245.8.1 by Martin Pool
Start adding tests for launchpad indirection
233
    return suite
2245.8.6 by Martin Pool
Documentation under 'help launchpad'
234
235
_launchpad_help = """Integration with Launchpad.net
236
237
Launchpad.net provides free Bazaar branch hosting with integrated bug and
238
specification tracking.
239
3031.1.1 by jml at canonical
Expand the documentation on lp:// URLs and mention the launchpad-login command.
240
The bzr client (through the plugin called 'launchpad') has special
2245.8.6 by Martin Pool
Documentation under 'help launchpad'
241
features to communicate with Launchpad:
242
3031.1.1 by jml at canonical
Expand the documentation on lp:// URLs and mention the launchpad-login command.
243
    * The launchpad-login command tells Bazaar your Launchpad user name. This
244
      is then used by the 'lp:' transport to download your branches using
245
      bzr+ssh://.
246
247
    * The register-branch command tells Launchpad about the url of a
2245.8.6 by Martin Pool
Documentation under 'help launchpad'
248
      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.
249
      its contents and allow it to be attached to bugs and other
2245.8.6 by Martin Pool
Documentation under 'help launchpad'
250
      objects.
251
3031.1.1 by jml at canonical
Expand the documentation on lp:// URLs and mention the launchpad-login command.
252
    * The 'lp:' transport uses Launchpad as a directory service: for example
253
      'lp:bzr' and 'lp:python' refer to the main branches of the relevant
254
      projects and may be branched, logged, etc. You can also use the 'lp:'
255
      transport to refer to specific branches, e.g. lp:///~bzr/bzr/trunk.
2245.8.6 by Martin Pool
Documentation under 'help launchpad'
256
257
For more information see http://help.launchpad.net/
258
"""
259
topic_registry.register('launchpad',
260
    _launchpad_help,
261
    'Using Bazaar with Launchpad.net')