~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugins/launchpad/lp_registration.py

(jelmer) Use the absolute_import feature everywhere in bzrlib,
 and add a source test to make sure it's used everywhere. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
from __future__ import absolute_import
 
18
 
17
19
 
18
20
import os
19
21
import socket
21
23
import urllib
22
24
import xmlrpclib
23
25
 
24
 
from bzrlib.lazy_import import lazy_import
25
 
lazy_import(globals(), """
26
 
from bzrlib import urlutils
27
 
""")
28
 
 
29
26
from bzrlib import (
30
27
    config,
31
28
    errors,
 
29
    urlutils,
32
30
    __version__ as _bzrlib_version,
33
31
    )
34
32
from bzrlib.transport.http import _urllib2_wrappers
35
33
 
 
34
 
36
35
# for testing, do
37
36
'''
38
37
export BZR_LP_XMLRPC_URL=http://xmlrpc.staging.launchpad.net/bazaar/
84
83
class LaunchpadService(object):
85
84
    """A service to talk to Launchpad via XMLRPC.
86
85
 
87
 
    See http://bazaar-vcs.org/Specs/LaunchpadRpc for the methods we can call.
 
86
    See http://wiki.bazaar.canonical.com/Specs/LaunchpadRpc for the methods we can call.
88
87
    """
89
88
 
90
89
    LAUNCHPAD_DOMAINS = {
91
90
        'production': 'launchpad.net',
92
 
        'edge': 'edge.launchpad.net',
93
91
        'staging': 'staging.launchpad.net',
 
92
        'qastaging': 'qastaging.launchpad.net',
94
93
        'demo': 'demo.launchpad.net',
95
94
        'dev': 'launchpad.dev',
96
95
        }
101
100
    for instance, domain in LAUNCHPAD_DOMAINS.iteritems():
102
101
        LAUNCHPAD_INSTANCE[instance] = 'https://xmlrpc.%s/bazaar/' % domain
103
102
 
104
 
    # We use edge as the default because:
105
 
    # Beta users get redirected to it
106
 
    # All users can use it
107
 
    # There is a bug in the launchpad side where redirection causes an OOPS.
108
 
    DEFAULT_INSTANCE = 'edge'
 
103
    # We use production as the default because edge has been deprecated circa
 
104
    # 2010-11 (see bug https://bugs.launchpad.net/bzr/+bug/583667)
 
105
    DEFAULT_INSTANCE = 'production'
109
106
    DEFAULT_SERVICE_URL = LAUNCHPAD_INSTANCE[DEFAULT_INSTANCE]
110
107
 
111
108
    transport = None
123
120
                    % (_bzrlib_version, xmlrpclib.__version__)
124
121
        self.transport = transport
125
122
 
126
 
 
127
123
    @property
128
124
    def service_url(self):
129
125
        """Return the http or https url for the xmlrpc server.
141
137
        else:
142
138
            return self.DEFAULT_SERVICE_URL
143
139
 
 
140
    @classmethod
 
141
    def for_url(cls, url, **kwargs):
 
142
        """Return the Launchpad service corresponding to the given URL."""
 
143
        result = urlsplit(url)
 
144
        lp_instance = result[1]
 
145
        if lp_instance == '':
 
146
            lp_instance = None
 
147
        elif lp_instance not in cls.LAUNCHPAD_INSTANCE:
 
148
            raise errors.InvalidURL(path=url)
 
149
        return cls(lp_instance=lp_instance, **kwargs)
 
150
 
144
151
    def get_proxy(self, authenticated):
145
152
        """Return the proxy for XMLRPC requests."""
146
153
        if authenticated:
161
168
            # the url?  perhaps a bit more secure against accidentally
162
169
            # revealing it.  std66 s3.2.1 discourages putting the
163
170
            # password in the url.
164
 
            hostinfo = '%s:%s@%s' % (urllib.quote(self.registrant_email),
165
 
                                     urllib.quote(self.registrant_password),
 
171
            hostinfo = '%s:%s@%s' % (urlutils.quote(self.registrant_email),
 
172
                                     urlutils.quote(self.registrant_password),
166
173
                                     hostinfo)
167
174
            url = urlunsplit((scheme, hostinfo, path, '', ''))
168
175
        else:
216
223
            instance = self._lp_instance
217
224
        return self.LAUNCHPAD_DOMAINS[instance]
218
225
 
219
 
    def get_web_url_from_branch_url(self, branch_url, _request_factory=None):
220
 
        """Get the Launchpad web URL for the given branch URL.
221
 
 
222
 
        :raise errors.InvalidURL: if 'branch_url' cannot be identified as a
223
 
            Launchpad branch URL.
224
 
        :return: The URL of the branch on Launchpad.
225
 
        """
 
226
    def _guess_branch_path(self, branch_url, _request_factory=None):
226
227
        scheme, hostinfo, path = urlsplit(branch_url)[:3]
227
228
        if _request_factory is None:
228
229
            _request_factory = ResolveLaunchpadPathRequest
240
241
                for domain in self.LAUNCHPAD_DOMAINS.itervalues())
241
242
            if hostinfo not in domains:
242
243
                raise NotLaunchpadBranch(branch_url)
 
244
        return path.lstrip('/')
 
245
 
 
246
    def get_web_url_from_branch_url(self, branch_url, _request_factory=None):
 
247
        """Get the Launchpad web URL for the given branch URL.
 
248
 
 
249
        :raise errors.InvalidURL: if 'branch_url' cannot be identified as a
 
250
            Launchpad branch URL.
 
251
        :return: The URL of the branch on Launchpad.
 
252
        """
 
253
        path = self._guess_branch_path(branch_url, _request_factory)
243
254
        return urlutils.join('https://code.%s' % self.domain, path)
244
255
 
245
256