/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: Vincent Ladeuil
  • Date: 2012-01-18 14:09:19 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120118140919-rlvdrhpc0nq1lbwi
Change set/remove to require a lock for the branch config files.

This means that tests (or any plugin for that matter) do not requires an
explicit lock on the branch anymore to change a single option. This also
means the optimisation becomes "opt-in" and as such won't be as
spectacular as it may be and/or harder to get right (nothing fails
anymore).

This reduces the diff by ~300 lines.

Code/tests that were updating more than one config option is still taking
a lock to at least avoid some IOs and demonstrate the benefits through
the decreased number of hpss calls.

The duplication between BranchStack and BranchOnlyStack will be removed
once the same sharing is in place for local config files, at which point
the Stack class itself may be able to host the changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009-2012 Canonical Ltd
 
1
# Copyright (C) 2009, 2010 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
23
23
# needed by a command that uses it.
24
24
 
25
25
 
26
 
import httplib2
27
26
import os
28
27
import re
29
28
import urlparse
50
49
    STAGING_SERVICE_ROOT,
51
50
    Launchpad,
52
51
    )
53
 
from launchpadlib import uris
 
52
 
54
53
 
55
54
# Declare the minimum version of launchpadlib that we need in order to work.
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)
 
55
# 1.5.1 is the version of launchpadlib packaged in Ubuntu 9.10, the most
 
56
# recent Ubuntu release at the time of writing.
 
57
MINIMUM_LAUNCHPADLIB_VERSION = (1, 5, 1)
59
58
 
60
59
 
61
60
def get_cache_directory():
77
76
            installed_version, installed_version)
78
77
 
79
78
 
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')
 
79
# The older versions of launchpadlib only provided service root constants for
 
80
# edge and staging, whilst newer versions drop edge. Therefore service root
 
81
# URIs for which we do not always have constants are derived from the staging
 
82
# one, which does always exist.
 
83
#
 
84
# It is necessary to derive, rather than use hardcoded URIs because
 
85
# launchpadlib <= 1.5.4 requires service root URIs that end in a path of
 
86
# /beta/, whilst launchpadlib >= 1.5.5 requires service root URIs with no path
 
87
# info.
 
88
#
 
89
# Once we have a hard dependency on launchpadlib >= 1.5.4 we can replace all of
 
90
# bzr's local knowledge of individual Launchpad instances with use of the
 
91
# launchpadlib.uris module.
 
92
LAUNCHPAD_API_URLS = {
 
93
    'production': STAGING_SERVICE_ROOT.replace('api.staging.launchpad.net',
 
94
        'api.launchpad.net'),
 
95
    'qastaging': STAGING_SERVICE_ROOT.replace('api.staging.launchpad.net',
 
96
        'api.qastaging.launchpad.net'),
 
97
    'staging': STAGING_SERVICE_ROOT,
 
98
    'dev': STAGING_SERVICE_ROOT.replace('api.staging.launchpad.net',
 
99
        'api.launchpad.dev'),
 
100
    }
88
101
 
89
102
 
90
103
def _get_api_url(service):
101
114
    else:
102
115
        lp_instance = service._lp_instance
103
116
    try:
104
 
        return lookup_service_root(lp_instance)
105
 
    except ValueError:
 
117
        return LAUNCHPAD_API_URLS[lp_instance]
 
118
    except KeyError:
106
119
        raise InvalidLaunchpadInstance(lp_instance)
107
120
 
108
121
 
113
126
        errors.BzrError.__init__(self, branch=branch, url=branch.base)
114
127
 
115
128
 
116
 
def login(service, timeout=None, proxy_info=None,
117
 
          version=Launchpad.DEFAULT_VERSION):
 
129
def login(service, timeout=None, proxy_info=None):
118
130
    """Log in to the Launchpad API.
119
131
 
120
132
    :return: The root `Launchpad` object from launchpadlib.
121
133
    """
122
 
    if proxy_info is None:
123
 
        proxy_info = httplib2.proxy_info_from_environment('https')
124
134
    cache_directory = get_cache_directory()
125
135
    launchpad = Launchpad.login_with(
126
136
        'bzr', _get_api_url(service), cache_directory, timeout=timeout,
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.
 
137
        proxy_info=proxy_info)
 
138
    # XXX: Work-around a minor security bug in launchpadlib 1.5.1, which would
 
139
    # create this directory with default umask.
130
140
    osutils.chmod_if_possible(cache_directory, 0700)
131
141
    return launchpad
132
142
 
200
210
        if str(launchpad._root_uri) == STAGING_SERVICE_ROOT:
201
211
            return url.replace('bazaar.launchpad.net',
202
212
                               'bazaar.staging.launchpad.net')
203
 
        elif str(launchpad._root_uri) == lookup_service_root('qastaging'):
 
213
        elif str(launchpad._root_uri) == LAUNCHPAD_API_URLS['qastaging']:
204
214
            return url.replace('bazaar.launchpad.net',
205
215
                               'bazaar.qastaging.launchpad.net')
206
216
        return url