/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

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

Merge test-run support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
from __future__ import absolute_import
18
18
 
19
 
from io import BytesIO
 
19
 
20
20
import os
21
21
import socket
22
22
try:
23
23
    from urllib.parse import urlsplit, urlunsplit
24
24
except ImportError:
25
 
    from urlparse import urlsplit, urlunsplit  # noqa: F401
 
25
    from urlparse import urlsplit, urlunsplit
26
26
import urllib
27
 
try:
28
 
    from xmlrpc.client import (
29
 
        __version__ as xmlrpc_version,
30
 
        Fault,
31
 
        ProtocolError,
32
 
        ServerProxy,
33
 
        Transport,
34
 
        )
35
 
except ImportError:  # python < 3
36
 
    from xmlrpclib import (
37
 
        __version__ as xmlrpc_version,
38
 
        Fault,
39
 
        ProtocolError,
40
 
        Transport,
41
 
        ServerProxy,
42
 
        )
 
27
import xmlrpclib
43
28
 
44
29
from ... import (
 
30
    config,
45
31
    errors,
46
32
    urlutils,
47
33
    __version__ as _breezy_version,
48
34
    )
49
 
from ...transport import http, get_transport
50
 
 
51
 
from .uris import (
52
 
    DEFAULT_INSTANCE,
53
 
    LAUNCHPAD_DOMAINS,
54
 
    LAUNCHPAD_BAZAAR_DOMAINS,
55
 
    )
 
35
from ...transport.http import _urllib2_wrappers
56
36
 
57
37
 
58
38
# for testing, do
82
62
        errors.BzrError.__init__(self, url=url)
83
63
 
84
64
 
85
 
class XMLRPCTransport(Transport):
 
65
class XMLRPCTransport(xmlrpclib.Transport):
86
66
 
87
67
    def __init__(self, scheme):
88
 
        Transport.__init__(self)
 
68
        xmlrpclib.Transport.__init__(self)
89
69
        self._scheme = scheme
 
70
        self._opener = _urllib2_wrappers.Opener()
90
71
        self.verbose = 0
91
 
        self._possible_bzr_transports = []
92
72
 
93
73
    def request(self, host, handler, request_body, verbose=0):
94
74
        self.verbose = verbose
95
75
        url = self._scheme + "://" + host + handler
96
 
        transport = get_transport(
97
 
            url, possible_transports=self._possible_bzr_transports)
98
 
        response = transport.request("POST", url, body=request_body, headers={
99
 
            "Content-Type": "text/xml"})
 
76
        request = _urllib2_wrappers.Request("POST", url, request_body)
 
77
        request.add_header("User-Agent", self.user_agent)
 
78
        request.add_header("Content-Type", "text/xml")
100
79
 
101
 
        if response.status != 200:
102
 
            raise ProtocolError(url, response.status,
103
 
                                response.text, response.headers)
104
 
        return self.parse_response(BytesIO(response.data))
 
80
        response = self._opener.open(request)
 
81
        if response.code != 200:
 
82
            raise xmlrpclib.ProtocolError(host + handler, response.code,
 
83
                                          response.msg, response.info())
 
84
        return self.parse_response(response)
105
85
 
106
86
 
107
87
class LaunchpadService(object):
111
91
    can call.
112
92
    """
113
93
 
 
94
    LAUNCHPAD_DOMAINS = {
 
95
        'production': 'launchpad.net',
 
96
        'staging': 'staging.launchpad.net',
 
97
        'qastaging': 'qastaging.launchpad.net',
 
98
        'demo': 'demo.launchpad.net',
 
99
        'dev': 'launchpad.dev',
 
100
        }
 
101
 
114
102
    # NB: these should always end in a slash to avoid xmlrpclib appending
115
103
    # '/RPC2'
116
104
    LAUNCHPAD_INSTANCE = {}
117
105
    for instance, domain in LAUNCHPAD_DOMAINS.items():
118
106
        LAUNCHPAD_INSTANCE[instance] = 'https://xmlrpc.%s/bazaar/' % domain
119
107
 
 
108
    # We use production as the default because edge has been deprecated circa
 
109
    # 2010-11 (see bug https://bugs.launchpad.net/bzr/+bug/583667)
 
110
    DEFAULT_INSTANCE = 'production'
120
111
    DEFAULT_SERVICE_URL = LAUNCHPAD_INSTANCE[DEFAULT_INSTANCE]
121
112
 
122
113
    transport = None
123
114
    registrant_email = None
124
115
    registrant_password = None
125
116
 
 
117
 
126
118
    def __init__(self, transport=None, lp_instance=None):
127
119
        """Construct a new service talking to the launchpad rpc server"""
128
120
        self._lp_instance = lp_instance
129
121
        if transport is None:
130
 
            uri_type = urlutils.parse_url(self.service_url)[0]
 
122
            uri_type = urllib.splittype(self.service_url)[0]
131
123
            transport = XMLRPCTransport(uri_type)
 
124
            transport.user_agent = 'Breezy/%s (xmlrpclib/%s)' \
 
125
                    % (_breezy_version, xmlrpclib.__version__)
132
126
        self.transport = transport
133
127
 
134
128
    @property
162
156
    def get_proxy(self):
163
157
        """Return the proxy for XMLRPC requests."""
164
158
        url = self.service_url
165
 
        return ServerProxy(url, transport=self.transport)
 
159
        return xmlrpclib.ServerProxy(url, transport=self.transport)
166
160
 
167
161
    def send_request(self, method_name, method_params):
168
162
        proxy = self.get_proxy()
169
163
        method = getattr(proxy, method_name)
170
164
        try:
171
165
            result = method(*method_params)
172
 
        except ProtocolError as e:
 
166
        except xmlrpclib.ProtocolError as e:
173
167
            if e.errcode == 301:
174
168
                # TODO: This can give a ProtocolError representing a 301 error, whose
175
169
                # e.headers['location'] tells where to go and e.errcode==301; should
176
170
                # probably log something and retry on the new url.
177
171
                raise NotImplementedError("should resend request to %s, but this isn't implemented"
178
 
                                          % e.headers.get('Location', 'NO-LOCATION-PRESENT'))
 
172
                        % e.headers.get('Location', 'NO-LOCATION-PRESENT'))
179
173
            else:
180
174
                # we don't want to print the original message because its
181
175
                # str representation includes the plaintext password.
182
176
                # TODO: print more headers to help in tracking down failures
183
177
                raise errors.BzrError("xmlrpc protocol error connecting to %s: %s %s"
184
 
                                      % (self.service_url, e.errcode, e.errmsg))
 
178
                        % (self.service_url, e.errcode, e.errmsg))
185
179
        except socket.gaierror as e:
186
180
            raise errors.ConnectionError(
187
181
                "Could not resolve '%s'" % self.domain,
191
185
    @property
192
186
    def domain(self):
193
187
        if self._lp_instance is None:
194
 
            instance = DEFAULT_INSTANCE
 
188
            instance = self.DEFAULT_INSTANCE
195
189
        else:
196
190
            instance = self._lp_instance
197
 
        return LAUNCHPAD_DOMAINS[instance]
 
191
        return self.LAUNCHPAD_DOMAINS[instance]
198
192
 
199
193
    def _guess_branch_path(self, branch_url, _request_factory=None):
200
194
        scheme, hostinfo, path = urlsplit(branch_url)[:3]
204
198
            resolve = _request_factory(path)
205
199
            try:
206
200
                result = resolve.submit(self)
207
 
            except Fault as fault:
 
201
            except xmlrpclib.Fault as fault:
208
202
                raise InvalidURL(branch_url, str(fault))
209
203
            branch_url = result['urls'][0]
210
204
            path = urlsplit(branch_url)[2]
211
205
        else:
212
 
            if hostinfo not in LAUNCHPAD_BAZAAR_DOMAINS:
 
206
            domains = (
 
207
                'bazaar.%s' % domain
 
208
                for domain in self.LAUNCHPAD_DOMAINS.values())
 
209
            if hostinfo not in domains:
213
210
                raise NotLaunchpadBranch(branch_url)
214
211
        return path.lstrip('/')
215
212