/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
19
import os
20
21
from bzrlib import (
22
    errors,
23
    trace,
24
    )
25
from bzrlib.plugins.launchpad.lp_registration import (
26
    InvalidLaunchpadInstance,
27
    NotLaunchpadBranch,
28
    )
29
30
from launchpadlib.credentials import Credentials
31
from launchpadlib.launchpad import (
32
    EDGE_SERVICE_ROOT,
33
    STAGING_SERVICE_ROOT,
34
    Launchpad,
35
    )
36
from lazr.uri import URI
37
38
39
CACHE_DIRECTORY = os.path.expanduser('~/.launchpadlib/cache')
40
41
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/',
47
    }
48
49
50
def _get_api_url(service):
51
    """Return the root URL of the Launchpad API.
52
53
    e.g. For the 'edge' Launchpad service, this function returns
54
    launchpadlib.launchpad.EDGE_SERVICE_ROOT.
55
56
    :param service: A `LaunchpadService` object.
57
    :return: A URL as a string.
58
    """
59
    if service._lp_instance is None:
60
        lp_instance = service.DEFAULT_INSTANCE
61
    else:
62
        lp_instance = service._lp_instance
63
    try:
64
        return LAUNCHPAD_API_URLS[lp_instance]
65
    except KeyError:
66
        raise InvalidLaunchpadInstance(lp_instance)
67
68
69
def _get_credential_path(service):
70
    """Return the path to cached credentials for 'service'.
71
72
    :param service: A `LaunchpadService` object.
73
    :return: The path to a cached credentials file, which might not exist.
74
    """
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)
78
79
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."""
83
    try:
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)
89
    else:
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)
95
    return launchpad
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
    """
103
    credential_path = _get_credential_path(service)
104
    launchpad = _login_from_cache(
105
        'bzr', _get_api_url(service), CACHE_DIRECTORY, credential_path,
106
        timeout, proxy_info)
107
    launchpad._service = service
108
    return launchpad
109
110
111
def load_branch(launchpad, branch):
112
    """Return the launchpadlib Branch object corresponding to 'branch'.
113
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
117
        `branch`.
118
    :return: A launchpadlib Branch object.
119
    """
120
    service = launchpad._service
121
    for url in branch.get_public_branch(), branch.get_push_location():
122
        if url is None:
123
            continue
124
        try:
125
            path = service._guess_branch_path(url)
126
        except (errors.InvalidURL, NotLaunchpadBranch):
127
            pass
128
        else:
129
            trace.mutter('Guessing path: %s', path)
130
            uri = launchpad._root_uri.append(path)
131
            uri_str = str(uri)
132
            trace.mutter('Guessing url: %s', uri_str)
133
            return launchpad.load(uri_str)
134
    raise NotLaunchpadBranch(url)