48
46
from ...i18n import gettext
49
from .lp_registration import (
50
InvalidLaunchpadInstance,
49
class LaunchpadlibMissing(errors.DependencyNotPresent):
51
_fmt = ("launchpadlib is required for Launchpad API access. "
52
"Please install the launchpadlib package.")
54
def __init__(self, e):
55
super(LaunchpadlibMissing, self).__init__(
54
59
import launchpadlib
55
60
except ImportError as e:
56
raise errors.DependencyNotPresent('launchpadlib', e)
61
raise LaunchpadlibMissing(e)
58
63
from launchpadlib.launchpad import (
62
66
from launchpadlib import uris
64
68
# Declare the minimum version of launchpadlib that we need in order to work.
65
# 1.6.0 is the version of launchpadlib packaged in Ubuntu 10.04, the most
66
# recent Ubuntu LTS release supported on the desktop at the time of writing.
67
MINIMUM_LAUNCHPADLIB_VERSION = (1, 6, 0)
69
MINIMUM_LAUNCHPADLIB_VERSION = (1, 6, 3)
72
# We use production as the default because edge has been deprecated circa
73
# 2010-11 (see bug https://bugs.launchpad.net/bzr/+bug/583667)
74
DEFAULT_INSTANCE = 'production'
77
'production': 'launchpad.net',
78
'staging': 'staging.launchpad.net',
79
'qastaging': 'qastaging.launchpad.net',
80
'demo': 'demo.launchpad.net',
81
'dev': 'launchpad.test',
84
LAUNCHPAD_BAZAAR_DOMAINS = [
86
for domain in LAUNCHPAD_DOMAINS.values()]
70
89
def get_cache_directory():
97
116
return staging_root.replace('staging', 'qastaging')
100
def _get_api_url(service):
101
"""Return the root URL of the Launchpad API.
103
e.g. For the 'staging' Launchpad service, this function returns
104
launchpadlib.launchpad.STAGING_SERVICE_ROOT.
106
:param service: A `LaunchpadService` object.
107
:return: A URL as a string.
109
if service._lp_instance is None:
110
lp_instance = service.DEFAULT_INSTANCE
112
lp_instance = service._lp_instance
114
return lookup_service_root(lp_instance)
116
raise InvalidLaunchpadInstance(lp_instance)
119
119
class NoLaunchpadBranch(errors.BzrError):
120
120
_fmt = 'No launchpad branch could be found for branch "%(url)s".'
123
123
errors.BzrError.__init__(self, branch=branch, url=branch.base)
126
def login(service, timeout=None, proxy_info=None,
127
version=Launchpad.DEFAULT_VERSION):
126
def connect_launchpad(base_url, timeout=None, proxy_info=None,
127
version=Launchpad.DEFAULT_VERSION):
128
128
"""Log in to the Launchpad API.
130
130
:return: The root `Launchpad` object from launchpadlib.
132
132
if proxy_info is None:
133
134
proxy_info = httplib2.proxy_info_from_environment('https')
134
135
cache_directory = get_cache_directory()
135
launchpad = Launchpad.login_with(
136
'bzr', _get_api_url(service), cache_directory, timeout=timeout,
136
return Launchpad.login_with(
137
'breezy', base_url, cache_directory, timeout=timeout,
137
138
proxy_info=proxy_info, version=version)
138
# XXX: Work-around a minor security bug in launchpadlib < 1.6.3, which
139
# would create this directory with default umask.
140
osutils.chmod_if_possible(cache_directory, 0o700)
144
142
class LaunchpadBranch(object):
208
206
def tweak_url(url, launchpad):
209
207
"""Adjust a URL to work with staging, if needed."""
210
if str(launchpad._root_uri) == STAGING_SERVICE_ROOT:
208
if str(launchpad._root_uri) == uris.STAGING_SERVICE_ROOT:
211
209
return url.replace('bazaar.launchpad.net',
212
210
'bazaar.staging.launchpad.net')
213
211
elif str(launchpad._root_uri) == lookup_service_root('qastaging'):
254
252
dev_focus = lp_branch.project.development_focus
255
253
if dev_focus is None:
256
254
raise errors.BzrError(gettext('%s has no development focus.') %
257
lp_branch.bzr_identity)
255
lp_branch.bzr_identity)
258
256
target = dev_focus.branch
259
257
if target is None:
260
258
raise errors.BzrError(gettext(
261
'development focus %s has no branch.') % dev_focus)
259
'development focus %s has no branch.') % dev_focus)
262
260
elif lp_branch.sourcepackage is not None:
263
261
target = lp_branch.sourcepackage.getBranch(pocket="Release")
264
262
if target is None:
267
265
lp_branch.sourcepackage)
269
267
raise errors.BzrError(gettext(
270
'%s has no associated product or source package.') %
271
lp_branch.bzr_identity)
268
'%s has no associated product or source package.') %
269
lp_branch.bzr_identity)
272
270
return LaunchpadBranch(target, target.bzr_identity)
274
272
def update_lp(self):
282
280
self.lp.bzr_identity)
284
282
graph = self.bzr.repository.get_graph()
285
if not graph.is_ancestor(self.lp.last_scanned_id,
283
if not graph.is_ancestor(osutils.safe_utf8(self.lp.last_scanned_id),
286
284
self.bzr.last_revision()):
287
285
raise errors.DivergedBranches(self.bzr, self.push_bzr)
288
286
trace.note(gettext('Pushing to %s') % self.lp.bzr_identity)