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.
29
from urllib.parse import (
33
except ImportError: # python < 3
34
from urlparse import (
25
from urllib.parse import (
47
38
from ...i18n import gettext
48
from .lp_registration import (
49
InvalidLaunchpadInstance,
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__(
53
51
import launchpadlib
54
52
except ImportError as e:
55
raise errors.DependencyNotPresent('launchpadlib', e)
53
raise LaunchpadlibMissing(e)
55
from launchpadlib.credentials import RequestTokenAuthorizationEngine
57
56
from launchpadlib.launchpad import (
61
59
from launchpadlib import uris
63
61
# Declare the minimum version of launchpadlib that we need in order to work.
64
# 1.6.0 is the version of launchpadlib packaged in Ubuntu 10.04, the most
65
# recent Ubuntu LTS release supported on the desktop at the time of writing.
66
MINIMUM_LAUNCHPADLIB_VERSION = (1, 6, 0)
62
MINIMUM_LAUNCHPADLIB_VERSION = (1, 6, 3)
69
65
def get_cache_directory():
70
66
"""Return the directory to cache launchpadlib objects in."""
71
return osutils.pathjoin(config.config_dir(), 'launchpad')
67
return osutils.pathjoin(bedding.cache_dir(), 'launchpad')
74
70
def parse_launchpadlib_version(version_number):
96
92
return staging_root.replace('staging', 'qastaging')
99
def _get_api_url(service):
100
"""Return the root URL of the Launchpad API.
102
e.g. For the 'staging' Launchpad service, this function returns
103
launchpadlib.launchpad.STAGING_SERVICE_ROOT.
105
:param service: A `LaunchpadService` object.
106
:return: A URL as a string.
108
if service._lp_instance is None:
109
lp_instance = service.DEFAULT_INSTANCE
111
lp_instance = service._lp_instance
113
return lookup_service_root(lp_instance)
115
raise InvalidLaunchpadInstance(lp_instance)
118
95
class NoLaunchpadBranch(errors.BzrError):
119
96
_fmt = 'No launchpad branch could be found for branch "%(url)s".'
122
99
errors.BzrError.__init__(self, branch=branch, url=branch.base)
125
def login(service, timeout=None, proxy_info=None,
126
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):
127
112
"""Log in to the Launchpad API.
129
114
:return: The root `Launchpad` object from launchpadlib.
131
116
if proxy_info is None:
132
118
proxy_info = httplib2.proxy_info_from_environment('https')
133
cache_directory = get_cache_directory()
134
launchpad = Launchpad.login_with(
135
'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,
136
129
proxy_info=proxy_info, version=version)
137
# XXX: Work-around a minor security bug in launchpadlib < 1.6.3, which
138
# would create this directory with default umask.
139
osutils.chmod_if_possible(cache_directory, 0o700)
143
133
class LaunchpadBranch(object):
207
197
def tweak_url(url, launchpad):
208
198
"""Adjust a URL to work with staging, if needed."""
209
if str(launchpad._root_uri) == STAGING_SERVICE_ROOT:
199
if str(launchpad._root_uri) == uris.STAGING_SERVICE_ROOT:
210
200
return url.replace('bazaar.launchpad.net',
211
201
'bazaar.staging.launchpad.net')
212
202
elif str(launchpad._root_uri) == lookup_service_root('qastaging'):