~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Martin Pool
  • Date: 2005-11-04 01:46:31 UTC
  • mto: (1185.33.49 bzr.dev)
  • mto: This revision was merged to the branch mainline in revision 1512.
  • Revision ID: mbp@sourcefrog.net-20051104014631-750e0ad4172c952c
Make biobench directly executable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
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
 
 
17
 
 
18
 
from getpass import getpass
19
 
import os
20
 
from urlparse import urlsplit, urlunsplit
21
 
import urllib
22
 
import xmlrpclib
23
 
 
24
 
from bzrlib import (
25
 
    config,
26
 
    errors,
27
 
    __version__ as _bzrlib_version,
28
 
    )
29
 
 
30
 
# for testing, do
31
 
'''
32
 
export BZR_LP_XMLRPC_URL=http://xmlrpc.staging.launchpad.net/bazaar/
33
 
'''
34
 
 
35
 
class InvalidLaunchpadInstance(errors.BzrError):
36
 
 
37
 
    _fmt = "%(lp_instance)s is not a valid Launchpad instance."
38
 
 
39
 
    def __init__(self, lp_instance):
40
 
        errors.BzrError.__init__(self, lp_instance=lp_instance)
41
 
 
42
 
 
43
 
class LaunchpadService(object):
44
 
    """A service to talk to Launchpad via XMLRPC.
45
 
 
46
 
    See http://bazaar-vcs.org/Specs/LaunchpadRpc for the methods we can call.
47
 
    """
48
 
 
49
 
    # NB: these should always end in a slash to avoid xmlrpclib appending
50
 
    # '/RPC2'
51
 
    # We use edge as the default because:
52
 
    # Beta users get redirected to it
53
 
    # All users can use it
54
 
    # There is a bug in the launchpad side where redirection causes an OOPS.
55
 
    LAUNCHPAD_INSTANCE = {
56
 
        'production': 'https://xmlrpc.launchpad.net/bazaar/',
57
 
        'edge': 'https://xmlrpc.edge.launchpad.net/bazaar/',
58
 
        'staging': 'https://xmlrpc.staging.launchpad.net/bazaar/',
59
 
        'demo': 'https://xmlrpc.demo.launchpad.net/bazaar/',
60
 
        'dev': 'http://xmlrpc.launchpad.dev/bazaar/',
61
 
        }
62
 
    DEFAULT_SERVICE_URL = LAUNCHPAD_INSTANCE['edge']
63
 
 
64
 
    transport = None
65
 
    registrant_email = None
66
 
    registrant_password = None
67
 
 
68
 
 
69
 
    def __init__(self, transport=None, lp_instance=None):
70
 
        """Construct a new service talking to the launchpad rpc server"""
71
 
        self._lp_instance = lp_instance
72
 
        if transport is None:
73
 
            uri_type = urllib.splittype(self.service_url)[0]
74
 
            if uri_type == 'https':
75
 
                transport = xmlrpclib.SafeTransport()
76
 
            else:
77
 
                transport = xmlrpclib.Transport()
78
 
            transport.user_agent = 'bzr/%s (xmlrpclib/%s)' \
79
 
                    % (_bzrlib_version, xmlrpclib.__version__)
80
 
        self.transport = transport
81
 
 
82
 
 
83
 
    @property
84
 
    def service_url(self):
85
 
        """Return the http or https url for the xmlrpc server.
86
 
 
87
 
        This does not include the username/password credentials.
88
 
        """
89
 
        key = 'BZR_LP_XMLRPC_URL'
90
 
        if key in os.environ:
91
 
            return os.environ[key]
92
 
        elif self._lp_instance is not None:
93
 
            try:
94
 
                return self.LAUNCHPAD_INSTANCE[self._lp_instance]
95
 
            except KeyError:
96
 
                raise InvalidLaunchpadInstance(self._lp_instance)
97
 
        else:
98
 
            return self.DEFAULT_SERVICE_URL
99
 
 
100
 
    def get_proxy(self, authenticated):
101
 
        """Return the proxy for XMLRPC requests."""
102
 
        if authenticated:
103
 
            # auth info must be in url
104
 
            # TODO: if there's no registrant email perhaps we should
105
 
            # just connect anonymously?
106
 
            scheme, hostinfo, path = urlsplit(self.service_url)[:3]
107
 
            assert '@' not in hostinfo
108
 
            assert self.registrant_email is not None
109
 
            assert self.registrant_password is not None
110
 
            # TODO: perhaps fully quote the password to make it very slightly
111
 
            # obscured
112
 
            # TODO: can we perhaps add extra Authorization headers
113
 
            # directly to the request, rather than putting this into
114
 
            # the url?  perhaps a bit more secure against accidentally
115
 
            # revealing it.  std66 s3.2.1 discourages putting the
116
 
            # password in the url.
117
 
            hostinfo = '%s:%s@%s' % (urllib.quote(self.registrant_email),
118
 
                                     urllib.quote(self.registrant_password),
119
 
                                     hostinfo)
120
 
            url = urlunsplit((scheme, hostinfo, path, '', ''))
121
 
        else:
122
 
            url = self.service_url
123
 
        return xmlrpclib.ServerProxy(url, transport=self.transport)
124
 
 
125
 
    def gather_user_credentials(self):
126
 
        """Get the password from the user."""
127
 
        the_config = config.GlobalConfig()
128
 
        self.registrant_email = the_config.user_email()
129
 
        if self.registrant_password is None:
130
 
            auth = config.AuthenticationConfig()
131
 
            scheme, hostinfo = urlsplit(self.service_url)[:2]
132
 
            prompt = 'launchpad.net password for %s: ' % \
133
 
                    self.registrant_email
134
 
            # We will reuse http[s] credentials if we can, prompt user
135
 
            # otherwise
136
 
            self.registrant_password = auth.get_password(scheme, hostinfo,
137
 
                                                         self.registrant_email,
138
 
                                                         prompt=prompt)
139
 
 
140
 
    def send_request(self, method_name, method_params, authenticated):
141
 
        proxy = self.get_proxy(authenticated)
142
 
        assert method_name
143
 
        method = getattr(proxy, method_name)
144
 
        try:
145
 
            result = method(*method_params)
146
 
        except xmlrpclib.ProtocolError, e:
147
 
            if e.errcode == 301:
148
 
                # TODO: This can give a ProtocolError representing a 301 error, whose
149
 
                # e.headers['location'] tells where to go and e.errcode==301; should
150
 
                # probably log something and retry on the new url.
151
 
                raise NotImplementedError("should resend request to %s, but this isn't implemented"
152
 
                        % e.headers.get('Location', 'NO-LOCATION-PRESENT'))
153
 
            else:
154
 
                # we don't want to print the original message because its
155
 
                # str representation includes the plaintext password.
156
 
                # TODO: print more headers to help in tracking down failures
157
 
                raise errors.BzrError("xmlrpc protocol error connecting to %s: %s %s"
158
 
                        % (self.service_url, e.errcode, e.errmsg))
159
 
        return result
160
 
 
161
 
 
162
 
class BaseRequest(object):
163
 
    """Base request for talking to a XMLRPC server."""
164
 
 
165
 
    # Set this to the XMLRPC method name.
166
 
    _methodname = None
167
 
    _authenticated = True
168
 
 
169
 
    def _request_params(self):
170
 
        """Return the arguments to pass to the method"""
171
 
        raise NotImplementedError(self._request_params)
172
 
 
173
 
    def submit(self, service):
174
 
        """Submit request to Launchpad XMLRPC server.
175
 
 
176
 
        :param service: LaunchpadService indicating where to send
177
 
            the request and the authentication credentials.
178
 
        """
179
 
        return service.send_request(self._methodname, self._request_params(),
180
 
                                    self._authenticated)
181
 
 
182
 
 
183
 
class DryRunLaunchpadService(LaunchpadService):
184
 
    """Service that just absorbs requests without sending to server.
185
 
    
186
 
    The dummy service does not need authentication.
187
 
    """
188
 
 
189
 
    def send_request(self, method_name, method_params, authenticated):
190
 
        pass
191
 
 
192
 
    def gather_user_credentials(self):
193
 
        pass
194
 
 
195
 
 
196
 
class BranchRegistrationRequest(BaseRequest):
197
 
    """Request to tell Launchpad about a bzr branch."""
198
 
 
199
 
    _methodname = 'register_branch'
200
 
 
201
 
    def __init__(self, branch_url,
202
 
                 branch_name='',
203
 
                 branch_title='',
204
 
                 branch_description='',
205
 
                 author_email='',
206
 
                 product_name='',
207
 
                 ):
208
 
        if not branch_url:
209
 
            raise errors.InvalidURL(branch_url, "You need to specify a non-empty branch URL.")
210
 
        self.branch_url = branch_url
211
 
        if branch_name:
212
 
            self.branch_name = branch_name
213
 
        else:
214
 
            self.branch_name = self._find_default_branch_name(self.branch_url)
215
 
        self.branch_title = branch_title
216
 
        self.branch_description = branch_description
217
 
        self.author_email = author_email
218
 
        self.product_name = product_name
219
 
 
220
 
    def _request_params(self):
221
 
        """Return xmlrpc request parameters"""
222
 
        # This must match the parameter tuple expected by Launchpad for this
223
 
        # method
224
 
        return (self.branch_url,
225
 
                self.branch_name,
226
 
                self.branch_title,
227
 
                self.branch_description,
228
 
                self.author_email,
229
 
                self.product_name,
230
 
               )
231
 
 
232
 
    def _find_default_branch_name(self, branch_url):
233
 
        i = branch_url.rfind('/')
234
 
        return branch_url[i+1:]
235
 
 
236
 
 
237
 
class BranchBugLinkRequest(BaseRequest):
238
 
    """Request to link a bzr branch in Launchpad to a bug."""
239
 
 
240
 
    _methodname = 'link_branch_to_bug'
241
 
 
242
 
    def __init__(self, branch_url, bug_id):
243
 
        assert branch_url
244
 
        self.bug_id = bug_id
245
 
        self.branch_url = branch_url
246
 
 
247
 
    def _request_params(self):
248
 
        """Return xmlrpc request parameters"""
249
 
        # This must match the parameter tuple expected by Launchpad for this
250
 
        # method
251
 
        return (self.branch_url, self.bug_id, '')
252
 
 
253
 
 
254
 
class ResolveLaunchpadPathRequest(BaseRequest):
255
 
    """Request to resolve the path component of an lp: URL."""
256
 
 
257
 
    _methodname = 'resolve_lp_path'
258
 
    _authenticated = False
259
 
 
260
 
    def __init__(self, path):
261
 
        if not path:
262
 
            raise errors.InvalidURL(path=path,
263
 
                                    extra="You must specify a product.")
264
 
        self.path = path
265
 
 
266
 
    def _request_params(self):
267
 
        """Return xmlrpc request parameters"""
268
 
        return (self.path,)