~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Patch Queue Manager
  • Date: 2015-12-17 18:39:00 UTC
  • mfrom: (6606.1.2 fix-float)
  • Revision ID: pqm@pqm.ubuntu.com-20151217183900-0719du2uv1kwu3lc
(vila) Inline testtools private method to fix an issue in xenial (the
 private implementation has changed in an backward incompatible way).
 (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2009-2012 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Tools for dealing with the Launchpad API."""
18
18
 
 
19
from __future__ import absolute_import
 
20
 
19
21
# Importing this module will be expensive, since it imports launchpadlib and
20
22
# its dependencies. However, our plan is to only load this module when it is
21
23
# needed by a command that uses it.
22
24
 
23
25
 
 
26
import httplib2
24
27
import os
25
28
import re
26
29
import urlparse
47
50
    STAGING_SERVICE_ROOT,
48
51
    Launchpad,
49
52
    )
50
 
 
 
53
from launchpadlib import uris
51
54
 
52
55
# Declare the minimum version of launchpadlib that we need in order to work.
53
 
# 1.5.1 is the version of launchpadlib packaged in Ubuntu 9.10, the most
54
 
# recent Ubuntu release at the time of writing.
55
 
MINIMUM_LAUNCHPADLIB_VERSION = (1, 5, 1)
 
56
# 1.6.0 is the version of launchpadlib packaged in Ubuntu 10.04, the most
 
57
# recent Ubuntu LTS release supported on the desktop at the time of writing.
 
58
MINIMUM_LAUNCHPADLIB_VERSION = (1, 6, 0)
56
59
 
57
60
 
58
61
def get_cache_directory():
74
77
            installed_version, installed_version)
75
78
 
76
79
 
77
 
# The older versions of launchpadlib only provided service root constants for
78
 
# edge and staging, whilst newer versions drop edge. Therefore service root
79
 
# URIs for which we do not always have constants are derived from the staging
80
 
# one, which does always exist.
81
 
#
82
 
# It is necessary to derive, rather than use hardcoded URIs because
83
 
# launchpadlib <= 1.5.4 requires service root URIs that end in a path of
84
 
# /beta/, whilst launchpadlib >= 1.5.5 requires service root URIs with no path
85
 
# info.
86
 
#
87
 
# Once we have a hard dependency on launchpadlib >= 1.5.4 we can replace all of
88
 
# bzr's local knowledge of individual Launchpad instances with use of the
89
 
# launchpadlib.uris module.
90
 
LAUNCHPAD_API_URLS = {
91
 
    'production': STAGING_SERVICE_ROOT.replace('api.staging.launchpad.net',
92
 
        'api.launchpad.net'),
93
 
    'qastaging': STAGING_SERVICE_ROOT.replace('api.staging.launchpad.net',
94
 
        'api.qastaging.launchpad.net'),
95
 
    'staging': STAGING_SERVICE_ROOT,
96
 
    'dev': STAGING_SERVICE_ROOT.replace('api.staging.launchpad.net',
97
 
        'api.launchpad.dev'),
98
 
    }
 
80
def lookup_service_root(service_root):
 
81
    try:
 
82
        return uris.lookup_service_root(service_root)
 
83
    except ValueError:
 
84
        if service_root != 'qastaging':
 
85
            raise
 
86
        staging_root = uris.lookup_service_root('staging')
 
87
        return staging_root.replace('staging', 'qastaging')
99
88
 
100
89
 
101
90
def _get_api_url(service):
112
101
    else:
113
102
        lp_instance = service._lp_instance
114
103
    try:
115
 
        return LAUNCHPAD_API_URLS[lp_instance]
116
 
    except KeyError:
 
104
        return lookup_service_root(lp_instance)
 
105
    except ValueError:
117
106
        raise InvalidLaunchpadInstance(lp_instance)
118
107
 
119
108
 
124
113
        errors.BzrError.__init__(self, branch=branch, url=branch.base)
125
114
 
126
115
 
127
 
def login(service, timeout=None, proxy_info=None):
 
116
def login(service, timeout=None, proxy_info=None,
 
117
          version=Launchpad.DEFAULT_VERSION):
128
118
    """Log in to the Launchpad API.
129
119
 
130
120
    :return: The root `Launchpad` object from launchpadlib.
131
121
    """
 
122
    if proxy_info is None:
 
123
        proxy_info = httplib2.proxy_info_from_environment('https')
132
124
    cache_directory = get_cache_directory()
133
125
    launchpad = Launchpad.login_with(
134
126
        'bzr', _get_api_url(service), cache_directory, timeout=timeout,
135
 
        proxy_info=proxy_info)
136
 
    # XXX: Work-around a minor security bug in launchpadlib 1.5.1, which would
137
 
    # create this directory with default umask.
138
 
    os.chmod(cache_directory, 0700)
 
127
        proxy_info=proxy_info, version=version)
 
128
    # XXX: Work-around a minor security bug in launchpadlib < 1.6.3, which
 
129
    # would create this directory with default umask.
 
130
    osutils.chmod_if_possible(cache_directory, 0700)
139
131
    return launchpad
140
132
 
141
133
 
208
200
        if str(launchpad._root_uri) == STAGING_SERVICE_ROOT:
209
201
            return url.replace('bazaar.launchpad.net',
210
202
                               'bazaar.staging.launchpad.net')
211
 
        elif str(launchpad._root_uri) == LAUNCHPAD_API_URLS['qastaging']:
 
203
        elif str(launchpad._root_uri) == lookup_service_root('qastaging'):
212
204
            return url.replace('bazaar.launchpad.net',
213
205
                               'bazaar.qastaging.launchpad.net')
214
206
        return url