~bzr-pqm/bzr/bzr.dev

4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
1
# Copyright (C) 2009 Canonical Ltd
2
#
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.
7
#
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.
12
#
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
16
17
"""Tools for dealing with the Launchpad API."""
18
4505.6.11 by Jonathan Lange
Flag lp_api as a difficult import.
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.
22
4505.6.18 by Jonathan Lange
Another review comment to make a note of.
23
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
24
import os
25
26
from bzrlib import (
4505.6.24 by Jonathan Lange
Move cache directory to the Bazaar configuration directory.
27
    config,
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
28
    errors,
4505.6.16 by Jonathan Lange
Work on Windows, I think.
29
    osutils,
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
30
    )
31
from bzrlib.plugins.launchpad.lp_registration import (
32
    InvalidLaunchpadInstance,
33
    NotLaunchpadBranch,
34
    )
35
4505.6.25 by Jonathan Lange
Add a test to check what happens if launchpadlib not available.
36
try:
4505.6.27 by Jonathan Lange
Add some tests to check for version compatibility. Drop tests for
37
    import launchpadlib
4505.6.25 by Jonathan Lange
Add a test to check what happens if launchpadlib not available.
38
except ImportError, e:
39
    raise errors.DependencyNotPresent('launchpadlib', e)
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
40
4505.6.27 by Jonathan Lange
Add some tests to check for version compatibility. Drop tests for
41
from launchpadlib.launchpad import (
42
    EDGE_SERVICE_ROOT,
43
    STAGING_SERVICE_ROOT,
44
    Launchpad,
45
    )
46
47
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)
52
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
53
4505.6.15 by Jonathan Lange
Baby steps: Move the cache directory stuff into a function.
54
def get_cache_directory():
55
    """Return the directory to cache launchpadlib objects in."""
4505.6.24 by Jonathan Lange
Move cache directory to the Bazaar configuration directory.
56
    return osutils.pathjoin(config.config_dir(), 'launchpad')
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
57
58
4505.6.27 by Jonathan Lange
Add some tests to check for version compatibility. Drop tests for
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('.')))
62
63
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)
71
72
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
73
LAUNCHPAD_API_URLS = {
4505.6.20 by Jonathan Lange
Use the real production URL.
74
    'production': 'https://api.launchpad.net/beta/',
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
75
    'edge': EDGE_SERVICE_ROOT,
76
    'staging': STAGING_SERVICE_ROOT,
77
    'dev': 'https://api.launchpad.dev/beta/',
78
    }
79
80
81
def _get_api_url(service):
82
    """Return the root URL of the Launchpad API.
83
84
    e.g. For the 'edge' Launchpad service, this function returns
85
    launchpadlib.launchpad.EDGE_SERVICE_ROOT.
86
87
    :param service: A `LaunchpadService` object.
88
    :return: A URL as a string.
89
    """
90
    if service._lp_instance is None:
91
        lp_instance = service.DEFAULT_INSTANCE
92
    else:
93
        lp_instance = service._lp_instance
94
    try:
95
        return LAUNCHPAD_API_URLS[lp_instance]
96
    except KeyError:
97
        raise InvalidLaunchpadInstance(lp_instance)
98
99
100
def login(service, timeout=None, proxy_info=None):
101
    """Log in to the Launchpad API.
102
103
    :return: The root `Launchpad` object from launchpadlib.
104
    """
4505.6.19 by Jonathan Lange
Delete swathes of code because we can rely on a version of 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)
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
112
    return launchpad
113
114
115
def load_branch(launchpad, branch):
116
    """Return the launchpadlib Branch object corresponding to 'branch'.
117
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
121
        `branch`.
122
    :return: A launchpadlib Branch object.
123
    """
4505.6.22 by Jonathan Lange
Remove the ugly _service hack by using getByUrl.
124
    # XXX: This duplicates the "What are possible URLs for the branch that
125
    # Launchpad might recognize" logic found in cmd_lp_open.
126
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.
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
131
    for url in branch.get_public_branch(), branch.get_push_location():
4505.6.22 by Jonathan Lange
Remove the ugly _service hack by using getByUrl.
132
        lp_branch = launchpad.branches.getByUrl(url=url)
133
        if lp_branch:
134
            return lp_branch
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
135
    raise NotLaunchpadBranch(url)