17
17
"""Tools for dealing with the Launchpad API."""
19
from __future__ import absolute_import
21
19
# Importing this module will be expensive, since it imports launchpadlib and
22
20
# its dependencies. However, our plan is to only load this module when it is
23
21
# needed by a command that uses it.
25
from urllib.parse import (
39
from bzrlib.i18n import gettext
40
from bzrlib.plugins.launchpad.lp_registration import (
41
InvalidLaunchpadInstance,
38
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__(
45
51
import launchpadlib
46
except ImportError, e:
47
raise errors.DependencyNotPresent('launchpadlib', e)
52
except ImportError as e:
53
raise LaunchpadlibMissing(e)
55
from launchpadlib.credentials import RequestTokenAuthorizationEngine
49
56
from launchpadlib.launchpad import (
53
59
from launchpadlib import uris
55
61
# Declare the minimum version of launchpadlib that we need in order to work.
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)
62
MINIMUM_LAUNCHPADLIB_VERSION = (1, 6, 3)
61
65
def get_cache_directory():
62
66
"""Return the directory to cache launchpadlib objects in."""
63
return osutils.pathjoin(config.config_dir(), 'launchpad')
67
return osutils.pathjoin(bedding.cache_dir(), 'launchpad')
66
70
def parse_launchpadlib_version(version_number):
72
76
"""Raise an error if launchpadlib has the wrong version number."""
73
77
installed_version = parse_launchpadlib_version(launchpadlib.__version__)
74
78
if installed_version < MINIMUM_LAUNCHPADLIB_VERSION:
75
raise errors.IncompatibleAPI(
76
'launchpadlib', MINIMUM_LAUNCHPADLIB_VERSION,
77
installed_version, installed_version)
79
raise errors.DependencyNotPresent(
81
'At least launchpadlib %s is required, but installed version is %s'
82
% (MINIMUM_LAUNCHPADLIB_VERSION, installed_version))
80
85
def lookup_service_root(service_root):
87
92
return staging_root.replace('staging', 'qastaging')
90
def _get_api_url(service):
91
"""Return the root URL of the Launchpad API.
93
e.g. For the 'staging' Launchpad service, this function returns
94
launchpadlib.launchpad.STAGING_SERVICE_ROOT.
96
:param service: A `LaunchpadService` object.
97
:return: A URL as a string.
99
if service._lp_instance is None:
100
lp_instance = service.DEFAULT_INSTANCE
102
lp_instance = service._lp_instance
104
return lookup_service_root(lp_instance)
106
raise InvalidLaunchpadInstance(lp_instance)
109
95
class NoLaunchpadBranch(errors.BzrError):
110
96
_fmt = 'No launchpad branch could be found for branch "%(url)s".'
113
99
errors.BzrError.__init__(self, branch=branch, url=branch.base)
116
def login(service, timeout=None, proxy_info=None,
117
version=Launchpad.DEFAULT_VERSION):
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):
118
112
"""Log in to the Launchpad API.
120
114
:return: The root `Launchpad` object from launchpadlib.
122
116
if proxy_info is None:
123
118
proxy_info = httplib2.proxy_info_from_environment('https')
124
cache_directory = get_cache_directory()
125
launchpad = Launchpad.login_with(
126
'bzr', _get_api_url(service), cache_directory, timeout=timeout,
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,
127
129
proxy_info=proxy_info, version=version)
128
# XXX: Work-around a minor security bug in launchpadlib < 1.6.3, which
129
# would create this directory with default umask.
130
osutils.chmod_if_possible(cache_directory, 0700)
134
133
class LaunchpadBranch(object):
173
172
if url.startswith('lp:'):
175
regex = re.compile('([a-z]*\+)*(bzr\+ssh|http)'
174
regex = re.compile('([a-z]*\\+)*(bzr\\+ssh|http)'
176
175
'://bazaar.*.launchpad.net')
177
176
return bool(regex.match(url))
198
197
def tweak_url(url, launchpad):
199
198
"""Adjust a URL to work with staging, if needed."""
200
if str(launchpad._root_uri) == STAGING_SERVICE_ROOT:
199
if str(launchpad._root_uri) == uris.STAGING_SERVICE_ROOT:
201
200
return url.replace('bazaar.launchpad.net',
202
201
'bazaar.staging.launchpad.net')
203
202
elif str(launchpad._root_uri) == lookup_service_root('qastaging'):
244
243
dev_focus = lp_branch.project.development_focus
245
244
if dev_focus is None:
246
245
raise errors.BzrError(gettext('%s has no development focus.') %
247
lp_branch.bzr_identity)
246
lp_branch.bzr_identity)
248
247
target = dev_focus.branch
249
248
if target is None:
250
249
raise errors.BzrError(gettext(
251
'development focus %s has no branch.') % dev_focus)
250
'development focus %s has no branch.') % dev_focus)
252
251
elif lp_branch.sourcepackage is not None:
253
252
target = lp_branch.sourcepackage.getBranch(pocket="Release")
254
253
if target is None:
257
256
lp_branch.sourcepackage)
259
258
raise errors.BzrError(gettext(
260
'%s has no associated product or source package.') %
261
lp_branch.bzr_identity)
259
'%s has no associated product or source package.') %
260
lp_branch.bzr_identity)
262
261
return LaunchpadBranch(target, target.bzr_identity)
264
263
def update_lp(self):
265
264
"""Update the Launchpad copy of this branch."""
266
265
if not self._check_update:
267
with self.bzr.lock_read():
270
268
if self.lp.last_scanned_id is not None:
271
269
if self.bzr.last_revision() == self.lp.last_scanned_id:
272
270
trace.note(gettext('%s is already up-to-date.') %
273
271
self.lp.bzr_identity)
275
273
graph = self.bzr.repository.get_graph()
276
if not graph.is_ancestor(self.lp.last_scanned_id,
274
if not graph.is_ancestor(osutils.safe_utf8(self.lp.last_scanned_id),
277
275
self.bzr.last_revision()):
278
276
raise errors.DivergedBranches(self.bzr, self.push_bzr)
279
277
trace.note(gettext('Pushing to %s') % self.lp.bzr_identity)
280
278
self.bzr.push(self.push_bzr)
284
280
def find_lca_tree(self, other):
285
281
"""Find the revision tree for the LCA of this branch and other.
296
292
def canonical_url(object):
297
293
"""Return the canonical URL for a branch."""
298
scheme, netloc, path, params, query, fragment = urlparse.urlparse(
294
scheme, netloc, path, params, query, fragment = urlparse(
299
295
str(object.self_link))
300
296
path = '/'.join(path.split('/')[2:])
301
297
netloc = netloc.replace('api.', 'code.')
302
return urlparse.urlunparse((scheme, netloc, path, params, query,
298
return urlunparse((scheme, netloc, path, params, query, fragment))