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
LAUNCHPAD_INSTANCE = {
44
'production': 'https://xmlrpc.launchpad.net/bazaar/',
45
'edge': 'https://xmlrpc.edge.launchpad.net/bazaar/',
46
'staging': 'https://xmlrpc.staging.launchpad.net/bazaar/',
47
'demo': 'https://xmlrpc.demo.launchpad.net/bazaar/',
48
'dev': 'http://xmlrpc.launchpad.dev/bazaar/',
50
DEFAULT_SERVICE_URL = LAUNCHPAD_INSTANCE['production']
53
registrant_email = None
54
registrant_password = None
57
def __init__(self, transport=None, lp_instance=None):
58
"""Construct a new service talking to the launchpad rpc server"""
59
self._lp_instance = lp_instance
61
uri_type = urllib.splittype(self.service_url)[0]
62
if uri_type == 'https':
63
transport = xmlrpclib.SafeTransport()
65
transport = xmlrpclib.Transport()
66
transport.user_agent = 'bzr/%s (xmlrpclib/%s)' \
67
% (_bzrlib_version, xmlrpclib.__version__)
68
self.transport = transport
72
def service_url(self):
73
"""Return the http or https url for the xmlrpc server.
75
This does not include the username/password credentials.
77
key = 'BZR_LP_XMLRPC_URL'
79
return os.environ[key]
80
elif self._lp_instance is not None:
81
return self.LAUNCHPAD_INSTANCE[self._lp_instance]
83
return self.DEFAULT_SERVICE_URL
85
def get_proxy(self, authenticated):
86
"""Return the proxy for XMLRPC requests."""
88
# auth info must be in url
89
# TODO: if there's no registrant email perhaps we should
90
# just connect anonymously?
91
scheme, hostinfo, path = urlsplit(self.service_url)[:3]
92
assert '@' not in hostinfo
93
assert self.registrant_email is not None
94
assert self.registrant_password is not None
95
# TODO: perhaps fully quote the password to make it very slightly
97
# TODO: can we perhaps add extra Authorization headers
98
# directly to the request, rather than putting this into
99
# the url? perhaps a bit more secure against accidentally
100
# revealing it. std66 s3.2.1 discourages putting the
101
# password in the url.
102
hostinfo = '%s:%s@%s' % (urllib.quote(self.registrant_email),
103
urllib.quote(self.registrant_password),
105
url = urlunsplit((scheme, hostinfo, path, '', ''))
107
url = self.service_url
108
return xmlrpclib.ServerProxy(url, transport=self.transport)
110
def gather_user_credentials(self):
111
"""Get the password from the user."""
112
the_config = config.GlobalConfig()
113
self.registrant_email = the_config.user_email()
114
if self.registrant_password is None:
115
auth = config.AuthenticationConfig()
116
scheme, hostinfo = urlsplit(self.service_url)[:2]
117
prompt = 'launchpad.net password for %s: ' % \
118
self.registrant_email
119
# We will reuse http[s] credentials if we can, prompt user
121
self.registrant_password = auth.get_password(scheme, hostinfo,
122
self.registrant_email,
125
def send_request(self, method_name, method_params, authenticated):
126
proxy = self.get_proxy(authenticated)
128
method = getattr(proxy, method_name)
130
result = method(*method_params)
131
except xmlrpclib.ProtocolError, e:
133
# TODO: This can give a ProtocolError representing a 301 error, whose
134
# e.headers['location'] tells where to go and e.errcode==301; should
135
# probably log something and retry on the new url.
136
raise NotImplementedError("should resend request to %s, but this isn't implemented"
137
% e.headers.get('Location', 'NO-LOCATION-PRESENT'))
139
# we don't want to print the original message because its
140
# str representation includes the plaintext password.
141
# TODO: print more headers to help in tracking down failures
142
raise errors.BzrError("xmlrpc protocol error connecting to %s: %s %s"
143
% (self.service_url, e.errcode, e.errmsg))
147
class BaseRequest(object):
148
"""Base request for talking to a XMLRPC server."""
150
# Set this to the XMLRPC method name.
152
_authenticated = True
154
def _request_params(self):
155
"""Return the arguments to pass to the method"""
156
raise NotImplementedError(self._request_params)
158
def submit(self, service):
159
"""Submit request to Launchpad XMLRPC server.
161
:param service: LaunchpadService indicating where to send
162
the request and the authentication credentials.
164
return service.send_request(self._methodname, self._request_params(),
168
class DryRunLaunchpadService(LaunchpadService):
169
"""Service that just absorbs requests without sending to server.
171
The dummy service does not need authentication.
174
def send_request(self, method_name, method_params, authenticated):
177
def gather_user_credentials(self):
181
class BranchRegistrationRequest(BaseRequest):
182
"""Request to tell Launchpad about a bzr branch."""
184
_methodname = 'register_branch'
186
def __init__(self, branch_url,
189
branch_description='',
194
self.branch_url = branch_url
196
self.branch_name = branch_name
198
self.branch_name = self._find_default_branch_name(self.branch_url)
199
self.branch_title = branch_title
200
self.branch_description = branch_description
201
self.author_email = author_email
202
self.product_name = product_name
204
def _request_params(self):
205
"""Return xmlrpc request parameters"""
206
# This must match the parameter tuple expected by Launchpad for this
208
return (self.branch_url,
211
self.branch_description,
216
def _find_default_branch_name(self, branch_url):
217
i = branch_url.rfind('/')
218
return branch_url[i+1:]
221
class BranchBugLinkRequest(BaseRequest):
222
"""Request to link a bzr branch in Launchpad to a bug."""
224
_methodname = 'link_branch_to_bug'
226
def __init__(self, branch_url, bug_id):
229
self.branch_url = branch_url
231
def _request_params(self):
232
"""Return xmlrpc request parameters"""
233
# This must match the parameter tuple expected by Launchpad for this
235
return (self.branch_url, self.bug_id, '')
238
class ResolveLaunchpadPathRequest(BaseRequest):
239
"""Request to resolve the path component of an lp: URL."""
241
_methodname = 'resolve_lp_path'
242
_authenticated = False
244
def __init__(self, path):
248
def _request_params(self):
249
"""Return xmlrpc request parameters"""