/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
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
# XXX: Do some sort of version check for 1.5.1 or greater.
24
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
25
import os
4505.6.16 by Jonathan Lange
Work on Windows, I think.
26
import sys
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
27
28
from bzrlib import (
29
    errors,
4505.6.16 by Jonathan Lange
Work on Windows, I think.
30
    osutils,
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
31
    trace,
4505.6.16 by Jonathan Lange
Work on Windows, I think.
32
    win32utils,
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
33
    )
34
from bzrlib.plugins.launchpad.lp_registration import (
35
    InvalidLaunchpadInstance,
36
    NotLaunchpadBranch,
37
    )
38
39
from launchpadlib.launchpad import (
40
    EDGE_SERVICE_ROOT,
41
    STAGING_SERVICE_ROOT,
42
    Launchpad,
43
    )
44
45
4505.6.15 by Jonathan Lange
Baby steps: Move the cache directory stuff into a function.
46
def get_cache_directory():
47
    """Return the directory to cache launchpadlib objects in."""
4505.6.16 by Jonathan Lange
Work on Windows, I think.
48
    if sys.platform == 'win32':
49
        base = win32utils.get_appdata_location_unicode()
50
        if base is None:
51
            base = os.environ.get('HOME', None)
52
        if base is None:
53
            raise errors.BzrError('You must have one of BZR_HOME, APPDATA,'
54
                                  ' or HOME set')
55
    else:
56
        base = os.path.expanduser('~/.cache')
57
    return osutils.pathjoin(base, 'launchpadlib')
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
58
59
60
LAUNCHPAD_API_URLS = {
4505.6.20 by Jonathan Lange
Use the real production URL.
61
    'production': 'https://api.launchpad.net/beta/',
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
62
    'edge': EDGE_SERVICE_ROOT,
63
    'staging': STAGING_SERVICE_ROOT,
64
    'dev': 'https://api.launchpad.dev/beta/',
65
    }
66
67
68
def _get_api_url(service):
69
    """Return the root URL of the Launchpad API.
70
71
    e.g. For the 'edge' Launchpad service, this function returns
72
    launchpadlib.launchpad.EDGE_SERVICE_ROOT.
73
74
    :param service: A `LaunchpadService` object.
75
    :return: A URL as a string.
76
    """
77
    if service._lp_instance is None:
78
        lp_instance = service.DEFAULT_INSTANCE
79
    else:
80
        lp_instance = service._lp_instance
81
    try:
82
        return LAUNCHPAD_API_URLS[lp_instance]
83
    except KeyError:
84
        raise InvalidLaunchpadInstance(lp_instance)
85
86
87
def login(service, timeout=None, proxy_info=None):
88
    """Log in to the Launchpad API.
89
90
    :return: The root `Launchpad` object from launchpadlib.
91
    """
4505.6.19 by Jonathan Lange
Delete swathes of code because we can rely on a version of launchpadlib
92
    cache_directory = get_cache_directory()
93
    launchpad = Launchpad.login_with(
94
        'bzr', _get_api_url(service), cache_directory, timeout=timeout,
95
        proxy_info=proxy_info)
96
    # XXX: Work-around a minor security bug in launchpadlib 1.5.1, which would
97
    # create this directory with default umask.
98
    os.chmod(cache_directory, 0700)
4505.6.9 by Jonathan Lange
Add some XXX comments based on the review.
99
    # XXX: Why does this set the private member of a class?
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
100
    launchpad._service = service
101
    return launchpad
102
103
104
def load_branch(launchpad, branch):
105
    """Return the launchpadlib Branch object corresponding to 'branch'.
106
107
    :param launchpad: The root `Launchpad` object from launchpadlib.
108
    :param branch: A `bzrlib.branch.Branch`.
109
    :raise NotLaunchpadBranch: If we cannot determine the Launchpad URL of
110
        `branch`.
111
    :return: A launchpadlib Branch object.
112
    """
4505.6.9 by Jonathan Lange
Add some XXX comments based on the review.
113
    # XXX: Why does this need service and _guess_branch_path?
4505.6.6 by Jonathan Lange
Add a command to mirror Launchpad branches now.
114
    service = launchpad._service
115
    for url in branch.get_public_branch(), branch.get_push_location():
116
        if url is None:
117
            continue
118
        try:
119
            path = service._guess_branch_path(url)
120
        except (errors.InvalidURL, NotLaunchpadBranch):
121
            pass
122
        else:
123
            trace.mutter('Guessing path: %s', path)
124
            uri = launchpad._root_uri.append(path)
125
            uri_str = str(uri)
126
            trace.mutter('Guessing url: %s', uri_str)
127
            return launchpad.load(uri_str)
128
    raise NotLaunchpadBranch(url)