/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
4797.84.2 by Max Bowsher
Expand comment.
72
# The older versions of launchpadlib only provided service root constants for
73
# edge and staging, whilst newer versions drop edge. Therefore service root
74
# URIs for which we do not always have constants are derived from the staging
75
# one, which does always exist.
76
#
77
# It is necessary to derive, rather than use hardcoded URIs because
78
# launchpadlib <= 1.5.4 requires service root URIs that end in a path of
79
# /beta/, whilst launchpadlib >= 1.5.5 requires service root URIs with no path
80
# info.
81
#
82
# Once we have a hard dependency on launchpadlib >= 1.5.4 we can replace all of
83
# bzr's local knowledge of individual Launchpad instances with use of the
84
# launchpadlib.uris module.
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
85
LAUNCHPAD_API_URLS = {
4797.84.1 by Max Bowsher
Fix broken communication with the Launchpad web service API when used with
86
    'production': STAGING_SERVICE_ROOT.replace('api.staging.launchpad.net',
87
        'api.launchpad.net'),
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
88
    'staging': STAGING_SERVICE_ROOT,
4797.84.1 by Max Bowsher
Fix broken communication with the Launchpad web service API when used with
89
    'dev': STAGING_SERVICE_ROOT.replace('api.staging.launchpad.net',
90
        'api.launchpad.dev'),
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
91
    }
92
93
94
def _get_api_url(service):
95
    """Return the root URL of the Launchpad API.
96
4797.76.5 by Vincent Ladeuil
Fix edge references in lp_api and more comments.
97
    e.g. For the 'staging' Launchpad service, this function returns
98
    launchpadlib.launchpad.STAGING_SERVICE_ROOT.
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
99
100
    :param service: A `LaunchpadService` object.
101
    :return: A URL as a string.
102
    """
103
    if service._lp_instance is None:
104
        lp_instance = service.DEFAULT_INSTANCE
105
    else:
106
        lp_instance = service._lp_instance
107
    try:
108
        return LAUNCHPAD_API_URLS[lp_instance]
109
    except KeyError:
110
        raise InvalidLaunchpadInstance(lp_instance)
111
112
113
def login(service, timeout=None, proxy_info=None):
114
    """Log in to the Launchpad API.
115
116
    :return: The root `Launchpad` object from launchpadlib.
117
    """
4505.6.19 by Jonathan Lange
Delete swathes of code because we can rely on a version of launchpadlib
118
    cache_directory = get_cache_directory()
119
    launchpad = Launchpad.login_with(
120
        'bzr', _get_api_url(service), cache_directory, timeout=timeout,
121
        proxy_info=proxy_info)
122
    # XXX: Work-around a minor security bug in launchpadlib 1.5.1, which would
123
    # create this directory with default umask.
124
    os.chmod(cache_directory, 0700)
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
125
    return launchpad
126
127
128
def load_branch(launchpad, branch):
129
    """Return the launchpadlib Branch object corresponding to 'branch'.
130
131
    :param launchpad: The root `Launchpad` object from launchpadlib.
132
    :param branch: A `bzrlib.branch.Branch`.
133
    :raise NotLaunchpadBranch: If we cannot determine the Launchpad URL of
134
        `branch`.
135
    :return: A launchpadlib Branch object.
136
    """
4505.6.22 by Jonathan Lange
Remove the ugly _service hack by using getByUrl.
137
    # XXX: This duplicates the "What are possible URLs for the branch that
138
    # Launchpad might recognize" logic found in cmd_lp_open.
139
140
    # XXX: This makes multiple roundtrips to Launchpad for what is
141
    # conceptually a single operation -- get me the branches that match these
142
    # URLs. Unfortunately, Launchpad's support for such operations is poor, so
143
    # we have to allow multiple roundtrips.
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
144
    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.
145
        lp_branch = launchpad.branches.getByUrl(url=url)
146
        if lp_branch:
147
            return lp_branch
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
148
    raise NotLaunchpadBranch(url)