/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/plugins/launchpad/lp_api.py

  • Committer: John Arbash Meinel
  • Date: 2009-12-22 16:28:47 UTC
  • mto: This revision was merged to the branch mainline in revision 4922.
  • Revision ID: john@arbash-meinel.com-20091222162847-tvnsc69to4l4uf5r
Implement a permute_for_extension helper.

Use it for all of the 'simple' extension permutations.
It basically permutes all tests in the current module, by setting TestCase.module.
Which works well for most of our extension tests. Some had more advanced
handling of permutations (extra permutations, custom vars, etc.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
# 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
 
 
23
 
 
24
import os
 
25
 
 
26
from bzrlib import (
 
27
    config,
 
28
    errors,
 
29
    osutils,
 
30
    )
 
31
from bzrlib.plugins.launchpad.lp_registration import (
 
32
    InvalidLaunchpadInstance,
 
33
    NotLaunchpadBranch,
 
34
    )
 
35
 
 
36
try:
 
37
    import launchpadlib
 
38
except ImportError, e:
 
39
    raise errors.DependencyNotPresent('launchpadlib', e)
 
40
 
 
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
 
 
53
 
 
54
def get_cache_directory():
 
55
    """Return the directory to cache launchpadlib objects in."""
 
56
    return osutils.pathjoin(config.config_dir(), 'launchpad')
 
57
 
 
58
 
 
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
 
 
73
LAUNCHPAD_API_URLS = {
 
74
    'production': 'https://api.launchpad.net/beta/',
 
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
    """
 
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)
 
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
    """
 
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.
 
131
    for url in branch.get_public_branch(), branch.get_push_location():
 
132
        lp_branch = launchpad.branches.getByUrl(url=url)
 
133
        if lp_branch:
 
134
            return lp_branch
 
135
    raise NotLaunchpadBranch(url)