74
74
installed_version, installed_version)
77
# The older versions of launchpadlib only provided service root constants for
78
# edge and staging, whilst newer versions drop edge. Therefore service root
79
# URIs for which we do not always have constants are derived from the staging
80
# one, which does always exist.
82
# It is necessary to derive, rather than use hardcoded URIs because
83
# launchpadlib <= 1.5.4 requires service root URIs that end in a path of
84
# /beta/, whilst launchpadlib >= 1.5.5 requires service root URIs with no path
87
# Once we have a hard dependency on launchpadlib >= 1.5.4 we can replace all of
88
# bzr's local knowledge of individual Launchpad instances with use of the
89
# launchpadlib.uris module.
77
90
LAUNCHPAD_API_URLS = {
78
'production': 'https://api.launchpad.net/beta/',
79
'edge': EDGE_SERVICE_ROOT,
91
'production': STAGING_SERVICE_ROOT.replace('api.staging.launchpad.net',
93
'qastaging': STAGING_SERVICE_ROOT.replace('api.staging.launchpad.net',
94
'api.qastaging.launchpad.net'),
80
95
'staging': STAGING_SERVICE_ROOT,
81
'dev': 'https://api.launchpad.dev/beta/',
96
'dev': STAGING_SERVICE_ROOT.replace('api.staging.launchpad.net',
85
101
def _get_api_url(service):
86
102
"""Return the root URL of the Launchpad API.
88
e.g. For the 'edge' Launchpad service, this function returns
89
launchpadlib.launchpad.EDGE_SERVICE_ROOT.
104
e.g. For the 'staging' Launchpad service, this function returns
105
launchpadlib.launchpad.STAGING_SERVICE_ROOT.
91
107
:param service: A `LaunchpadService` object.
92
108
:return: A URL as a string.
101
117
raise InvalidLaunchpadInstance(lp_instance)
120
class NoLaunchpadBranch(errors.BzrError):
121
_fmt = 'No launchpad branch could be found for branch "%(url)s".'
123
def __init__(self, branch):
124
errors.BzrError.__init__(self, branch=branch, url=branch.base)
104
127
def login(service, timeout=None, proxy_info=None):
105
128
"""Log in to the Launchpad API.
180
203
def tweak_url(url, launchpad):
181
204
"""Adjust a URL to work with staging, if needed."""
182
if str(launchpad._root_uri) != STAGING_SERVICE_ROOT:
186
return url.replace('bazaar.launchpad.net',
187
'bazaar.staging.launchpad.net')
205
if str(launchpad._root_uri) == STAGING_SERVICE_ROOT:
206
return url.replace('bazaar.launchpad.net',
207
'bazaar.staging.launchpad.net')
208
elif str(launchpad._root_uri) == LAUNCHPAD_API_URLS['qastaging']:
209
return url.replace('bazaar.launchpad.net',
210
'bazaar.qastaging.launchpad.net')
190
def from_bzr(cls, launchpad, bzr_branch):
214
def from_bzr(cls, launchpad, bzr_branch, create_missing=True):
191
215
"""Find a Launchpad branch from a bzr branch."""
192
216
check_update = True
193
217
for url in cls.candidate_urls(bzr_branch):
198
222
if lp_branch is not None:
225
if not create_missing:
226
raise NoLaunchpadBranch(bzr_branch)
201
227
lp_branch = cls.create_now(launchpad, bzr_branch)
202
228
check_update = False
203
229
return cls(lp_branch, bzr_branch.base, bzr_branch, check_update)
215
241
raise errors.BzrError('%s is not registered on Launchpad' % url)
218
def get_dev_focus(self):
219
"""Return the 'LaunchpadBranch' for the dev focus of this one."""
244
def get_target(self):
245
"""Return the 'LaunchpadBranch' for the target of this one."""
220
246
lp_branch = self.lp
221
if lp_branch.project is None:
222
raise errors.BzrError('%s has no product.' %
223
lp_branch.bzr_identity)
224
dev_focus = lp_branch.project.development_focus.branch
225
if dev_focus is None:
226
raise errors.BzrError('%s has no development focus.' %
227
lp_branch.bzr_identity)
228
return LaunchpadBranch(dev_focus, dev_focus.bzr_identity)
247
if lp_branch.project is not None:
248
dev_focus = lp_branch.project.development_focus
249
if dev_focus is None:
250
raise errors.BzrError('%s has no development focus.' %
251
lp_branch.bzr_identity)
252
target = dev_focus.branch
254
raise errors.BzrError('development focus %s has no branch.' % dev_focus)
255
elif lp_branch.sourcepackage is not None:
256
target = lp_branch.sourcepackage.getBranch(pocket="Release")
258
raise errors.BzrError('source package %s has no branch.' %
259
lp_branch.sourcepackage)
261
raise errors.BzrError('%s has no associated product or source package.' %
262
lp_branch.bzr_identity)
263
return LaunchpadBranch(target, target.bzr_identity)
230
265
def update_lp(self):
231
266
"""Update the Launchpad copy of this branch."""
282
317
raise NotLaunchpadBranch(url)
320
def canonical_url(object):
321
"""Return the canonical URL for a branch."""
322
scheme, netloc, path, params, query, fragment = urlparse.urlparse(
323
str(object.self_link))
324
path = '/'.join(path.split('/')[2:])
325
netloc = netloc.replace('api.', 'code.')
326
return urlparse.urlunparse((scheme, netloc, path, params, query,