1
# Copyright (C) 2009 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tools for dealing with the Launchpad API."""
25
from bzrlib.plugins.launchpad.lp_registration import (
26
InvalidLaunchpadInstance,
30
from launchpadlib.credentials import Credentials
31
from launchpadlib.launchpad import (
36
from lazr.uri import URI
39
CACHE_DIRECTORY = os.path.expanduser('~/.launchpadlib/cache')
42
LAUNCHPAD_API_URLS = {
43
'production': EDGE_SERVICE_ROOT,
44
'edge': EDGE_SERVICE_ROOT,
45
'staging': STAGING_SERVICE_ROOT,
46
'dev': 'https://api.launchpad.dev/beta/',
50
def _get_api_url(service):
51
"""Return the root URL of the Launchpad API.
53
e.g. For the 'edge' Launchpad service, this function returns
54
launchpadlib.launchpad.EDGE_SERVICE_ROOT.
56
:param service: A `LaunchpadService` object.
57
:return: A URL as a string.
59
if service._lp_instance is None:
60
lp_instance = service.DEFAULT_INSTANCE
62
lp_instance = service._lp_instance
64
return LAUNCHPAD_API_URLS[lp_instance]
66
raise InvalidLaunchpadInstance(lp_instance)
69
def _get_credential_path(service):
70
"""Return the path to cached credentials for 'service'.
72
:param service: A `LaunchpadService` object.
73
:return: The path to a cached credentials file, which might not exist.
75
web_root_uri = URI(_get_api_url(service))
76
credential_name = 'creds-%s-bzr' % (web_root_uri.host)
77
return os.path.join(CACHE_DIRECTORY, credential_name)
80
def _login_from_cache(consumer_name, service_root, cache_dir,
81
credential_cache, timeout=None, proxy_info=None):
82
"""Use cached credentials if they exist, log in otherwise."""
84
credentials = Credentials.load_from_path(credential_cache)
85
except (OSError, IOError):
86
launchpad = Launchpad.get_token_and_login(
87
consumer_name, service_root, cache_dir, timeout, proxy_info)
88
launchpad.credentials.save_to_path(credential_cache)
90
access_key = credentials.access_token.key
91
access_secret = credentials.access_token.secret
92
launchpad = Launchpad.login(
93
consumer_name, access_key, access_secret, service_root,
94
cache_dir, timeout, proxy_info)
98
def login(service, timeout=None, proxy_info=None):
99
"""Log in to the Launchpad API.
101
:return: The root `Launchpad` object from launchpadlib.
103
credential_path = _get_credential_path(service)
104
launchpad = _login_from_cache(
105
'bzr', _get_api_url(service), CACHE_DIRECTORY, credential_path,
107
launchpad._service = service
111
def load_branch(launchpad, branch):
112
"""Return the launchpadlib Branch object corresponding to 'branch'.
114
:param launchpad: The root `Launchpad` object from launchpadlib.
115
:param branch: A `bzrlib.branch.Branch`.
116
:raise NotLaunchpadBranch: If we cannot determine the Launchpad URL of
118
:return: A launchpadlib Branch object.
120
service = launchpad._service
121
for url in branch.get_public_branch(), branch.get_push_location():
125
path = service._guess_branch_path(url)
126
except (errors.InvalidURL, NotLaunchpadBranch):
129
trace.mutter('Guessing path: %s', path)
130
uri = launchpad._root_uri.append(path)
132
trace.mutter('Guessing url: %s', uri_str)
133
return launchpad.load(uri_str)
134
raise NotLaunchpadBranch(url)