~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: 2014-02-12 18:22:22 UTC
  • mfrom: (6589.2.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20140212182222-beouo25gaf1cny76
(vila) The XDG Base Directory Specification uses the XDG_CACHE_HOME,
 not XDG_CACHE_DIR. (Andrew Starr-Bochicchio)

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.
33
35
    trace,
34
36
    transport,
35
37
    )
 
38
from bzrlib.i18n import gettext
36
39
from bzrlib.plugins.launchpad.lp_registration import (
37
40
    InvalidLaunchpadInstance,
38
41
    )
46
49
    STAGING_SERVICE_ROOT,
47
50
    Launchpad,
48
51
    )
49
 
 
 
52
from launchpadlib import uris
50
53
 
51
54
# Declare the minimum version of launchpadlib that we need in order to work.
52
 
# 1.5.1 is the version of launchpadlib packaged in Ubuntu 9.10, the most
53
 
# recent Ubuntu release at the time of writing.
54
 
MINIMUM_LAUNCHPADLIB_VERSION = (1, 5, 1)
 
55
# 1.6.0 is the version of launchpadlib packaged in Ubuntu 10.04, the most
 
56
# recent Ubuntu LTS release supported on the desktop at the time of writing.
 
57
MINIMUM_LAUNCHPADLIB_VERSION = (1, 6, 0)
55
58
 
56
59
 
57
60
def get_cache_directory():
73
76
            installed_version, installed_version)
74
77
 
75
78
 
76
 
# The older versions of launchpadlib only provided service root constants for
77
 
# edge and staging, whilst newer versions drop edge. Therefore service root
78
 
# URIs for which we do not always have constants are derived from the staging
79
 
# one, which does always exist.
80
 
#
81
 
# It is necessary to derive, rather than use hardcoded URIs because
82
 
# launchpadlib <= 1.5.4 requires service root URIs that end in a path of
83
 
# /beta/, whilst launchpadlib >= 1.5.5 requires service root URIs with no path
84
 
# info.
85
 
#
86
 
# Once we have a hard dependency on launchpadlib >= 1.5.4 we can replace all of
87
 
# bzr's local knowledge of individual Launchpad instances with use of the
88
 
# launchpadlib.uris module.
89
 
LAUNCHPAD_API_URLS = {
90
 
    'production': STAGING_SERVICE_ROOT.replace('api.staging.launchpad.net',
91
 
        'api.launchpad.net'),
92
 
    'qastaging': STAGING_SERVICE_ROOT.replace('api.staging.launchpad.net',
93
 
        'api.qastaging.launchpad.net'),
94
 
    'staging': STAGING_SERVICE_ROOT,
95
 
    'dev': STAGING_SERVICE_ROOT.replace('api.staging.launchpad.net',
96
 
        'api.launchpad.dev'),
97
 
    }
 
79
def lookup_service_root(service_root):
 
80
    try:
 
81
        return uris.lookup_service_root(service_root)
 
82
    except ValueError:
 
83
        if service_root != 'qastaging':
 
84
            raise
 
85
        staging_root = uris.lookup_service_root('staging')
 
86
        return staging_root.replace('staging', 'qastaging')
98
87
 
99
88
 
100
89
def _get_api_url(service):
111
100
    else:
112
101
        lp_instance = service._lp_instance
113
102
    try:
114
 
        return LAUNCHPAD_API_URLS[lp_instance]
115
 
    except KeyError:
 
103
        return lookup_service_root(lp_instance)
 
104
    except ValueError:
116
105
        raise InvalidLaunchpadInstance(lp_instance)
117
106
 
118
107
 
123
112
        errors.BzrError.__init__(self, branch=branch, url=branch.base)
124
113
 
125
114
 
126
 
def login(service, timeout=None, proxy_info=None):
 
115
def login(service, timeout=None, proxy_info=None,
 
116
          version=Launchpad.DEFAULT_VERSION):
127
117
    """Log in to the Launchpad API.
128
118
 
129
119
    :return: The root `Launchpad` object from launchpadlib.
131
121
    cache_directory = get_cache_directory()
132
122
    launchpad = Launchpad.login_with(
133
123
        'bzr', _get_api_url(service), cache_directory, timeout=timeout,
134
 
        proxy_info=proxy_info)
135
 
    # XXX: Work-around a minor security bug in launchpadlib 1.5.1, which would
136
 
    # create this directory with default umask.
137
 
    os.chmod(cache_directory, 0700)
 
124
        proxy_info=proxy_info, version=version)
 
125
    # XXX: Work-around a minor security bug in launchpadlib < 1.6.3, which
 
126
    # would create this directory with default umask.
 
127
    osutils.chmod_if_possible(cache_directory, 0700)
138
128
    return launchpad
139
129
 
140
130
 
207
197
        if str(launchpad._root_uri) == STAGING_SERVICE_ROOT:
208
198
            return url.replace('bazaar.launchpad.net',
209
199
                               'bazaar.staging.launchpad.net')
210
 
        elif str(launchpad._root_uri) == LAUNCHPAD_API_URLS['qastaging']:
 
200
        elif str(launchpad._root_uri) == lookup_service_root('qastaging'):
211
201
            return url.replace('bazaar.launchpad.net',
212
202
                               'bazaar.qastaging.launchpad.net')
213
203
        return url
235
225
        """Create a Bazaar branch on Launchpad for the supplied branch."""
236
226
        url = cls.tweak_url(bzr_branch.get_push_location(), launchpad)
237
227
        if not cls.plausible_launchpad_url(url):
238
 
            raise errors.BzrError('%s is not registered on Launchpad' %
 
228
            raise errors.BzrError(gettext('%s is not registered on Launchpad') %
239
229
                                  bzr_branch.base)
240
230
        bzr_branch.create_clone_on_transport(transport.get_transport(url))
241
231
        lp_branch = launchpad.branches.getByUrl(url=url)
242
232
        if lp_branch is None:
243
 
            raise errors.BzrError('%s is not registered on Launchpad' % url)
 
233
            raise errors.BzrError(gettext('%s is not registered on Launchpad') %
 
234
                                                                            url)
244
235
        return lp_branch
245
236
 
246
237
    def get_target(self):
249
240
        if lp_branch.project is not None:
250
241
            dev_focus = lp_branch.project.development_focus
251
242
            if dev_focus is None:
252
 
                raise errors.BzrError('%s has no development focus.' %
 
243
                raise errors.BzrError(gettext('%s has no development focus.') %
253
244
                                  lp_branch.bzr_identity)
254
245
            target = dev_focus.branch
255
246
            if target is None:
256
 
                raise errors.BzrError('development focus %s has no branch.' % dev_focus)
 
247
                raise errors.BzrError(gettext(
 
248
                        'development focus %s has no branch.') % dev_focus)
257
249
        elif lp_branch.sourcepackage is not None:
258
250
            target = lp_branch.sourcepackage.getBranch(pocket="Release")
259
251
            if target is None:
260
 
                raise errors.BzrError('source package %s has no branch.' %
 
252
                raise errors.BzrError(gettext(
 
253
                                      'source package %s has no branch.') %
261
254
                                      lp_branch.sourcepackage)
262
255
        else:
263
 
            raise errors.BzrError('%s has no associated product or source package.' %
 
256
            raise errors.BzrError(gettext(
 
257
                        '%s has no associated product or source package.') %
264
258
                                  lp_branch.bzr_identity)
265
259
        return LaunchpadBranch(target, target.bzr_identity)
266
260
 
272
266
        try:
273
267
            if self.lp.last_scanned_id is not None:
274
268
                if self.bzr.last_revision() == self.lp.last_scanned_id:
275
 
                    trace.note('%s is already up-to-date.' %
 
269
                    trace.note(gettext('%s is already up-to-date.') %
276
270
                               self.lp.bzr_identity)
277
271
                    return
278
272
                graph = self.bzr.repository.get_graph()
279
273
                if not graph.is_ancestor(self.lp.last_scanned_id,
280
274
                                         self.bzr.last_revision()):
281
275
                    raise errors.DivergedBranches(self.bzr, self.push_bzr)
282
 
                trace.note('Pushing to %s' % self.lp.bzr_identity)
 
276
                trace.note(gettext('Pushing to %s') % self.lp.bzr_identity)
283
277
            self.bzr.push(self.push_bzr)
284
278
        finally:
285
279
            self.bzr.unlock()