17
17
"""Tools for dealing with the Launchpad API."""
19
from __future__ import absolute_import
19
21
# Importing this module will be expensive, since it imports launchpadlib and
20
22
# its dependencies. However, our plan is to only load this module when it is
21
23
# needed by a command that uses it.
25
from urllib.parse import (
38
39
from ...i18n import gettext
41
class LaunchpadlibMissing(errors.DependencyNotPresent):
43
_fmt = ("launchpadlib is required for Launchpad API access. "
44
"Please install the launchpadlib package.")
46
def __init__(self, e):
47
super(LaunchpadlibMissing, self).__init__(
40
from .lp_registration import (
41
InvalidLaunchpadInstance,
51
45
import launchpadlib
52
46
except ImportError as e:
53
raise LaunchpadlibMissing(e)
47
raise errors.DependencyNotPresent('launchpadlib', e)
55
from launchpadlib.credentials import RequestTokenAuthorizationEngine
56
49
from launchpadlib.launchpad import (
59
53
from launchpadlib import uris
61
55
# Declare the minimum version of launchpadlib that we need in order to work.
62
MINIMUM_LAUNCHPADLIB_VERSION = (1, 6, 3)
56
# 1.6.0 is the version of launchpadlib packaged in Ubuntu 10.04, the most
57
# recent Ubuntu LTS release supported on the desktop at the time of writing.
58
MINIMUM_LAUNCHPADLIB_VERSION = (1, 6, 0)
65
61
def get_cache_directory():
66
62
"""Return the directory to cache launchpadlib objects in."""
67
return osutils.pathjoin(bedding.cache_dir(), 'launchpad')
63
return osutils.pathjoin(config.config_dir(), 'launchpad')
70
66
def parse_launchpadlib_version(version_number):
92
88
return staging_root.replace('staging', 'qastaging')
91
def _get_api_url(service):
92
"""Return the root URL of the Launchpad API.
94
e.g. For the 'staging' Launchpad service, this function returns
95
launchpadlib.launchpad.STAGING_SERVICE_ROOT.
97
:param service: A `LaunchpadService` object.
98
:return: A URL as a string.
100
if service._lp_instance is None:
101
lp_instance = service.DEFAULT_INSTANCE
103
lp_instance = service._lp_instance
105
return lookup_service_root(lp_instance)
107
raise InvalidLaunchpadInstance(lp_instance)
95
110
class NoLaunchpadBranch(errors.BzrError):
96
111
_fmt = 'No launchpad branch could be found for branch "%(url)s".'
99
114
errors.BzrError.__init__(self, branch=branch, url=branch.base)
102
def get_auth_engine(base_url):
103
return Launchpad.authorization_engine_factory(base_url, 'breezy')
106
def get_credential_store():
107
return Launchpad.credential_store_factory(None)
110
def connect_launchpad(base_url, timeout=None, proxy_info=None,
111
version=Launchpad.DEFAULT_VERSION):
117
def login(service, timeout=None, proxy_info=None,
118
version=Launchpad.DEFAULT_VERSION):
112
119
"""Log in to the Launchpad API.
114
121
:return: The root `Launchpad` object from launchpadlib.
116
123
if proxy_info is None:
118
124
proxy_info = httplib2.proxy_info_from_environment('https')
120
cache_directory = get_cache_directory()
121
except EnvironmentError:
122
cache_directory = None
123
credential_store = get_credential_store()
124
authorization_engine = get_auth_engine(base_url)
125
return Launchpad.login_with(
126
'breezy', base_url, cache_directory, timeout=timeout,
127
credential_store=credential_store,
128
authorization_engine=authorization_engine,
125
cache_directory = get_cache_directory()
126
launchpad = Launchpad.login_with(
127
'bzr', _get_api_url(service), cache_directory, timeout=timeout,
129
128
proxy_info=proxy_info, version=version)
129
# XXX: Work-around a minor security bug in launchpadlib < 1.6.3, which
130
# would create this directory with default umask.
131
osutils.chmod_if_possible(cache_directory, 0o700)
133
135
class LaunchpadBranch(object):
172
174
if url.startswith('lp:'):
174
regex = re.compile('([a-z]*\\+)*(bzr\\+ssh|http)'
176
regex = re.compile('([a-z]*\+)*(bzr\+ssh|http)'
175
177
'://bazaar.*.launchpad.net')
176
178
return bool(regex.match(url))
197
199
def tweak_url(url, launchpad):
198
200
"""Adjust a URL to work with staging, if needed."""
199
if str(launchpad._root_uri) == uris.STAGING_SERVICE_ROOT:
201
if str(launchpad._root_uri) == STAGING_SERVICE_ROOT:
200
202
return url.replace('bazaar.launchpad.net',
201
203
'bazaar.staging.launchpad.net')
202
204
elif str(launchpad._root_uri) == lookup_service_root('qastaging'):
243
245
dev_focus = lp_branch.project.development_focus
244
246
if dev_focus is None:
245
247
raise errors.BzrError(gettext('%s has no development focus.') %
246
lp_branch.bzr_identity)
248
lp_branch.bzr_identity)
247
249
target = dev_focus.branch
248
250
if target is None:
249
251
raise errors.BzrError(gettext(
250
'development focus %s has no branch.') % dev_focus)
252
'development focus %s has no branch.') % dev_focus)
251
253
elif lp_branch.sourcepackage is not None:
252
254
target = lp_branch.sourcepackage.getBranch(pocket="Release")
253
255
if target is None:
256
258
lp_branch.sourcepackage)
258
260
raise errors.BzrError(gettext(
259
'%s has no associated product or source package.') %
260
lp_branch.bzr_identity)
261
'%s has no associated product or source package.') %
262
lp_branch.bzr_identity)
261
263
return LaunchpadBranch(target, target.bzr_identity)
263
265
def update_lp(self):
264
266
"""Update the Launchpad copy of this branch."""
265
267
if not self._check_update:
267
with self.bzr.lock_read():
268
271
if self.lp.last_scanned_id is not None:
269
272
if self.bzr.last_revision() == self.lp.last_scanned_id:
270
273
trace.note(gettext('%s is already up-to-date.') %
271
274
self.lp.bzr_identity)
273
276
graph = self.bzr.repository.get_graph()
274
if not graph.is_ancestor(osutils.safe_utf8(self.lp.last_scanned_id),
277
if not graph.is_ancestor(self.lp.last_scanned_id,
275
278
self.bzr.last_revision()):
276
279
raise errors.DivergedBranches(self.bzr, self.push_bzr)
277
280
trace.note(gettext('Pushing to %s') % self.lp.bzr_identity)
278
281
self.bzr.push(self.push_bzr)
280
285
def find_lca_tree(self, other):
281
286
"""Find the revision tree for the LCA of this branch and other.
292
297
def canonical_url(object):
293
298
"""Return the canonical URL for a branch."""
294
scheme, netloc, path, params, query, fragment = urlparse(
299
scheme, netloc, path, params, query, fragment = urlparse.urlparse(
295
300
str(object.self_link))
296
301
path = '/'.join(path.split('/')[2:])
297
302
netloc = netloc.replace('api.', 'code.')
298
return urlunparse((scheme, netloc, path, params, query, fragment))
303
return urlparse.urlunparse((scheme, netloc, path, params, query,