25
import bzrlib.errors as errors
27
__version__ as _bzrlib_version,
29
32
export BZR_LP_XMLRPC_URL=http://xmlrpc.staging.launchpad.net/bazaar/
35
class InvalidLaunchpadInstance(errors.BzrError):
37
_fmt = "%(lp_instance)s is not a valid Launchpad instance."
39
def __init__(self, lp_instance):
40
errors.BzrError.__init__(self, lp_instance=lp_instance)
32
43
class LaunchpadService(object):
33
44
"""A service to talk to Launchpad via XMLRPC.
35
46
See http://bazaar-vcs.org/Specs/LaunchpadRpc for the methods we can call.
38
# NB: this should always end in a slash to avoid xmlrpclib appending
49
# NB: these should always end in a slash to avoid xmlrpclib appending
40
DEFAULT_SERVICE_URL = 'https://xmlrpc.launchpad.net/bazaar/'
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/',
62
DEFAULT_SERVICE_URL = LAUNCHPAD_INSTANCE['edge']
43
65
registrant_email = None
44
66
registrant_password = None
47
def __init__(self, transport=None):
69
def __init__(self, transport=None, lp_instance=None):
48
70
"""Construct a new service talking to the launchpad rpc server"""
71
self._lp_instance = lp_instance
49
72
if transport is None:
50
73
uri_type = urllib.splittype(self.service_url)[0]
51
74
if uri_type == 'https':
66
89
key = 'BZR_LP_XMLRPC_URL'
67
90
if key in os.environ:
68
91
return os.environ[key]
92
elif self._lp_instance is not None:
94
return self.LAUNCHPAD_INSTANCE[self._lp_instance]
96
raise InvalidLaunchpadInstance(self._lp_instance)
70
98
return self.DEFAULT_SERVICE_URL
100
def get_proxy(self, authenticated):
73
101
"""Return the proxy for XMLRPC requests."""
74
# auth info must be in url
75
# TODO: if there's no registrant email perhaps we should just connect
77
scheme, hostinfo, path = urlsplit(self.service_url)[:3]
78
assert '@' not in hostinfo
79
assert self.registrant_email is not None
80
assert self.registrant_password is not None
81
# TODO: perhaps fully quote the password to make it very slightly
83
# TODO: can we perhaps add extra Authorization headers directly to the
84
# request, rather than putting this into the url? perhaps a bit more
85
# secure against accidentally revealing it. std66 s3.2.1 discourages putting
86
# the password in the url.
87
hostinfo = '%s:%s@%s' % (urllib.quote(self.registrant_email),
88
urllib.quote(self.registrant_password),
90
url = urlunsplit((scheme, hostinfo, path, '', ''))
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]
108
raise AssertionError(hostinfo)
109
if self.registrant_email is None:
110
raise AssertionError()
111
if self.registrant_password is None:
112
raise AssertionError()
113
# TODO: perhaps fully quote the password to make it very slightly
115
# TODO: can we perhaps add extra Authorization headers
116
# directly to the request, rather than putting this into
117
# the url? perhaps a bit more secure against accidentally
118
# revealing it. std66 s3.2.1 discourages putting the
119
# password in the url.
120
hostinfo = '%s:%s@%s' % (urllib.quote(self.registrant_email),
121
urllib.quote(self.registrant_password),
123
url = urlunsplit((scheme, hostinfo, path, '', ''))
125
url = self.service_url
91
126
return xmlrpclib.ServerProxy(url, transport=self.transport)
93
128
def gather_user_credentials(self):
94
129
"""Get the password from the user."""
95
config = bzrlib.config.GlobalConfig()
96
self.registrant_email = config.user_email()
130
the_config = config.GlobalConfig()
131
self.registrant_email = the_config.user_email()
97
132
if self.registrant_password is None:
133
auth = config.AuthenticationConfig()
134
scheme, hostinfo = urlsplit(self.service_url)[:2]
98
135
prompt = 'launchpad.net password for %s: ' % \
99
136
self.registrant_email
100
self.registrant_password = getpass(prompt)
137
# We will reuse http[s] credentials if we can, prompt user
139
self.registrant_password = auth.get_password(scheme, hostinfo,
140
self.registrant_email,
102
def send_request(self, method_name, method_params):
103
proxy = self.get_proxy()
143
def send_request(self, method_name, method_params, authenticated):
144
proxy = self.get_proxy(authenticated)
105
145
method = getattr(proxy, method_name)
107
147
result = method(*method_params)
208
250
# This must match the parameter tuple expected by Launchpad for this
210
252
return (self.branch_url, self.bug_id, '')
255
class ResolveLaunchpadPathRequest(BaseRequest):
256
"""Request to resolve the path component of an lp: URL."""
258
_methodname = 'resolve_lp_path'
259
_authenticated = False
261
def __init__(self, path):
263
raise errors.InvalidURL(path=path,
264
extra="You must specify a product.")
267
def _request_params(self):
268
"""Return xmlrpc request parameters"""