1
# Copyright (C) 2010 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."""
19
# Importing this module will be expensive, since it imports launchpadlib and
20
# its dependencies. However, our plan is to only load this module when it is
21
# needed by a command that uses it.
31
from bzrlib.plugins.launchpad.lp_registration import (
32
InvalidLaunchpadInstance,
38
except ImportError, e:
39
raise errors.DependencyNotPresent('launchpadlib', e)
41
from launchpadlib.launchpad import (
48
# Declare the minimum version of launchpadlib that we need in order to work.
49
# 1.5.1 is the version of launchpadlib packaged in Ubuntu 9.10, the most
50
# recent Ubuntu release at the time of writing.
51
MINIMUM_LAUNCHPADLIB_VERSION = (1, 5, 1)
54
def get_cache_directory():
55
"""Return the directory to cache launchpadlib objects in."""
56
return osutils.pathjoin(config.config_dir(), 'launchpad')
59
def parse_launchpadlib_version(version_number):
60
"""Parse a version number of the style used by launchpadlib."""
61
return tuple(map(int, version_number.split('.')))
64
def check_launchpadlib_compatibility():
65
"""Raise an error if launchpadlib has the wrong version number."""
66
installed_version = parse_launchpadlib_version(launchpadlib.__version__)
67
if installed_version < MINIMUM_LAUNCHPADLIB_VERSION:
68
raise errors.IncompatibleAPI(
69
'launchpadlib', MINIMUM_LAUNCHPADLIB_VERSION,
70
installed_version, installed_version)
73
LAUNCHPAD_API_URLS = {
74
'production': 'https://api.launchpad.net/beta/',
75
'edge': EDGE_SERVICE_ROOT,
76
'staging': STAGING_SERVICE_ROOT,
77
'dev': 'https://api.launchpad.dev/beta/',
81
def _get_api_url(service):
82
"""Return the root URL of the Launchpad API.
84
e.g. For the 'edge' Launchpad service, this function returns
85
launchpadlib.launchpad.EDGE_SERVICE_ROOT.
87
:param service: A `LaunchpadService` object.
88
:return: A URL as a string.
90
if service._lp_instance is None:
91
lp_instance = service.DEFAULT_INSTANCE
93
lp_instance = service._lp_instance
95
return LAUNCHPAD_API_URLS[lp_instance]
97
raise InvalidLaunchpadInstance(lp_instance)
100
def login(service, timeout=None, proxy_info=None):
101
"""Log in to the Launchpad API.
103
:return: The root `Launchpad` object from launchpadlib.
105
cache_directory = get_cache_directory()
106
launchpad = Launchpad.login_with(
107
'bzr', _get_api_url(service), cache_directory, timeout=timeout,
108
proxy_info=proxy_info)
109
# XXX: Work-around a minor security bug in launchpadlib 1.5.1, which would
110
# create this directory with default umask.
111
os.chmod(cache_directory, 0700)
115
def load_branch(launchpad, branch):
116
"""Return the launchpadlib Branch object corresponding to 'branch'.
118
:param launchpad: The root `Launchpad` object from launchpadlib.
119
:param branch: A `bzrlib.branch.Branch`.
120
:raise NotLaunchpadBranch: If we cannot determine the Launchpad URL of
122
:return: A launchpadlib Branch object.
124
# XXX: This duplicates the "What are possible URLs for the branch that
125
# Launchpad might recognize" logic found in cmd_lp_open.
127
# XXX: This makes multiple roundtrips to Launchpad for what is
128
# conceptually a single operation -- get me the branches that match these
129
# URLs. Unfortunately, Launchpad's support for such operations is poor, so
130
# we have to allow multiple roundtrips.
131
for url in branch.get_public_branch(), branch.get_push_location():
132
lp_branch = launchpad.branches.getByUrl(url=url)
135
raise NotLaunchpadBranch(url)