/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4797.32.2 by John Arbash Meinel
merge 2.1, resolving NEWS conflict.
1
# Copyright (C) 2009, 2010 Canonical Ltd
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
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
    STAGING_SERVICE_ROOT,
43
    Launchpad,
44
    )
45
46
47
# Declare the minimum version of launchpadlib that we need in order to work.
48
# 1.5.1 is the version of launchpadlib packaged in Ubuntu 9.10, the most
49
# recent Ubuntu release at the time of writing.
50
MINIMUM_LAUNCHPADLIB_VERSION = (1, 5, 1)
51
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
52
4505.6.15 by Jonathan Lange
Baby steps: Move the cache directory stuff into a function.
53
def get_cache_directory():
54
    """Return the directory to cache launchpadlib objects in."""
4505.6.24 by Jonathan Lange
Move cache directory to the Bazaar configuration directory.
55
    return osutils.pathjoin(config.config_dir(), 'launchpad')
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
56
57
4505.6.27 by Jonathan Lange
Add some tests to check for version compatibility. Drop tests for
58
def parse_launchpadlib_version(version_number):
59
    """Parse a version number of the style used by launchpadlib."""
60
    return tuple(map(int, version_number.split('.')))
61
62
63
def check_launchpadlib_compatibility():
64
    """Raise an error if launchpadlib has the wrong version number."""
65
    installed_version = parse_launchpadlib_version(launchpadlib.__version__)
66
    if installed_version < MINIMUM_LAUNCHPADLIB_VERSION:
67
        raise errors.IncompatibleAPI(
68
            'launchpadlib', MINIMUM_LAUNCHPADLIB_VERSION,
69
            installed_version, installed_version)
70
71
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
72
LAUNCHPAD_API_URLS = {
4505.6.20 by Jonathan Lange
Use the real production URL.
73
    'production': 'https://api.launchpad.net/beta/',
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
74
    'staging': STAGING_SERVICE_ROOT,
75
    'dev': 'https://api.launchpad.dev/beta/',
76
    }
77
78
79
def _get_api_url(service):
80
    """Return the root URL of the Launchpad API.
81
4797.76.5 by Vincent Ladeuil
Fix edge references in lp_api and more comments.
82
    e.g. For the 'staging' Launchpad service, this function returns
83
    launchpadlib.launchpad.STAGING_SERVICE_ROOT.
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
84
85
    :param service: A `LaunchpadService` object.
86
    :return: A URL as a string.
87
    """
88
    if service._lp_instance is None:
89
        lp_instance = service.DEFAULT_INSTANCE
90
    else:
91
        lp_instance = service._lp_instance
92
    try:
93
        return LAUNCHPAD_API_URLS[lp_instance]
94
    except KeyError:
95
        raise InvalidLaunchpadInstance(lp_instance)
96
97
98
def login(service, timeout=None, proxy_info=None):
99
    """Log in to the Launchpad API.
100
101
    :return: The root `Launchpad` object from launchpadlib.
102
    """
4505.6.19 by Jonathan Lange
Delete swathes of code because we can rely on a version of launchpadlib
103
    cache_directory = get_cache_directory()
104
    launchpad = Launchpad.login_with(
105
        'bzr', _get_api_url(service), cache_directory, timeout=timeout,
106
        proxy_info=proxy_info)
107
    # XXX: Work-around a minor security bug in launchpadlib 1.5.1, which would
108
    # create this directory with default umask.
109
    os.chmod(cache_directory, 0700)
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
110
    return launchpad
111
112
113
def load_branch(launchpad, branch):
114
    """Return the launchpadlib Branch object corresponding to 'branch'.
115
116
    :param launchpad: The root `Launchpad` object from launchpadlib.
117
    :param branch: A `bzrlib.branch.Branch`.
118
    :raise NotLaunchpadBranch: If we cannot determine the Launchpad URL of
119
        `branch`.
120
    :return: A launchpadlib Branch object.
121
    """
4505.6.22 by Jonathan Lange
Remove the ugly _service hack by using getByUrl.
122
    # XXX: This duplicates the "What are possible URLs for the branch that
123
    # Launchpad might recognize" logic found in cmd_lp_open.
124
125
    # XXX: This makes multiple roundtrips to Launchpad for what is
126
    # conceptually a single operation -- get me the branches that match these
127
    # URLs. Unfortunately, Launchpad's support for such operations is poor, so
128
    # we have to allow multiple roundtrips.
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
129
    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.
130
        lp_branch = launchpad.branches.getByUrl(url=url)
131
        if lp_branch:
132
            return lp_branch
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
133
    raise NotLaunchpadBranch(url)