1
# Copyright (C) 2006 Canonical Ltd
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.
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.
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
18
from getpass import getpass
20
from urlparse import urlsplit, urlunsplit
27
__version__ as _bzrlib_version,
32
export BZR_LP_XMLRPC_URL=http://xmlrpc.staging.launchpad.net/bazaar/
35
class LaunchpadService(object):
36
"""A service to talk to Launchpad via XMLRPC.
38
See http://bazaar-vcs.org/Specs/LaunchpadRpc for the methods we can call.
41
# NB: this should always end in a slash to avoid xmlrpclib appending
43
DEFAULT_SERVICE_URL = 'https://xmlrpc.launchpad.net/bazaar/'
46
registrant_email = None
47
registrant_password = None
50
def __init__(self, transport=None):
51
"""Construct a new service talking to the launchpad rpc server"""
53
uri_type = urllib.splittype(self.service_url)[0]
54
if uri_type == 'https':
55
transport = xmlrpclib.SafeTransport()
57
transport = xmlrpclib.Transport()
58
transport.user_agent = 'bzr/%s (xmlrpclib/%s)' \
59
% (_bzrlib_version, xmlrpclib.__version__)
60
self.transport = transport
64
def service_url(self):
65
"""Return the http or https url for the xmlrpc server.
67
This does not include the username/password credentials.
69
key = 'BZR_LP_XMLRPC_URL'
71
return os.environ[key]
73
return self.DEFAULT_SERVICE_URL
75
def get_proxy(self, authenticated):
76
"""Return the proxy for XMLRPC requests."""
78
# auth info must be in url
79
# TODO: if there's no registrant email perhaps we should
80
# just connect anonymously?
81
scheme, hostinfo, path = urlsplit(self.service_url)[:3]
82
assert '@' not in hostinfo
83
assert self.registrant_email is not None
84
assert self.registrant_password is not None
85
# TODO: perhaps fully quote the password to make it very slightly
87
# TODO: can we perhaps add extra Authorization headers
88
# directly to the request, rather than putting this into
89
# the url? perhaps a bit more secure against accidentally
90
# revealing it. std66 s3.2.1 discourages putting the
91
# password in the url.
92
hostinfo = '%s:%s@%s' % (urllib.quote(self.registrant_email),
93
urllib.quote(self.registrant_password),
95
url = urlunsplit((scheme, hostinfo, path, '', ''))
97
url = self.service_url
98
return xmlrpclib.ServerProxy(url, transport=self.transport)
100
def gather_user_credentials(self):
101
"""Get the password from the user."""
102
the_config = config.GlobalConfig()
103
self.registrant_email = the_config.user_email()
104
if self.registrant_password is None:
105
auth = config.AuthenticationConfig()
106
scheme, hostinfo = urlsplit(self.service_url)[:2]
107
prompt = 'launchpad.net password for %s: ' % \
108
self.registrant_email
109
# We will reuse http[s] credentials if we can, prompt user
111
self.registrant_password = auth.get_password(scheme, hostinfo,
112
self.registrant_email,
115
def send_request(self, method_name, method_params, authenticated):
116
proxy = self.get_proxy(authenticated)
118
method = getattr(proxy, method_name)
120
result = method(*method_params)
121
except xmlrpclib.ProtocolError, e:
123
# TODO: This can give a ProtocolError representing a 301 error, whose
124
# e.headers['location'] tells where to go and e.errcode==301; should
125
# probably log something and retry on the new url.
126
raise NotImplementedError("should resend request to %s, but this isn't implemented"
127
% e.headers.get('Location', 'NO-LOCATION-PRESENT'))
129
# we don't want to print the original message because its
130
# str representation includes the plaintext password.
131
# TODO: print more headers to help in tracking down failures
132
raise errors.BzrError("xmlrpc protocol error connecting to %s: %s %s"
133
% (self.service_url, e.errcode, e.errmsg))
137
class BaseRequest(object):
138
"""Base request for talking to a XMLRPC server."""
140
# Set this to the XMLRPC method name.
142
_authenticated = True
144
def _request_params(self):
145
"""Return the arguments to pass to the method"""
146
raise NotImplementedError(self._request_params)
148
def submit(self, service):
149
"""Submit request to Launchpad XMLRPC server.
151
:param service: LaunchpadService indicating where to send
152
the request and the authentication credentials.
154
return service.send_request(self._methodname, self._request_params(),
158
class DryRunLaunchpadService(LaunchpadService):
159
"""Service that just absorbs requests without sending to server.
161
The dummy service does not need authentication.
164
def send_request(self, method_name, method_params, authenticated):
167
def gather_user_credentials(self):
171
class BranchRegistrationRequest(BaseRequest):
172
"""Request to tell Launchpad about a bzr branch."""
174
_methodname = 'register_branch'
176
def __init__(self, branch_url,
179
branch_description='',
184
self.branch_url = branch_url
186
self.branch_name = branch_name
188
self.branch_name = self._find_default_branch_name(self.branch_url)
189
self.branch_title = branch_title
190
self.branch_description = branch_description
191
self.author_email = author_email
192
self.product_name = product_name
194
def _request_params(self):
195
"""Return xmlrpc request parameters"""
196
# This must match the parameter tuple expected by Launchpad for this
198
return (self.branch_url,
201
self.branch_description,
206
def _find_default_branch_name(self, branch_url):
207
i = branch_url.rfind('/')
208
return branch_url[i+1:]
211
class BranchBugLinkRequest(BaseRequest):
212
"""Request to link a bzr branch in Launchpad to a bug."""
214
_methodname = 'link_branch_to_bug'
216
def __init__(self, branch_url, bug_id):
219
self.branch_url = branch_url
221
def _request_params(self):
222
"""Return xmlrpc request parameters"""
223
# This must match the parameter tuple expected by Launchpad for this
225
return (self.branch_url, self.bug_id, '')
228
class ResolveLaunchpadPathRequest(BaseRequest):
229
"""Request to resolve the path component of an lp: URL."""
231
_methodname = 'resolve_lp_path'
232
_authenticated = False
234
def __init__(self, path):
238
def _request_params(self):
239
"""Return xmlrpc request parameters"""