~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-08-17 18:13:57 UTC
  • mfrom: (5268.7.29 transport-segments)
  • Revision ID: pqm@pqm.ubuntu.com-20110817181357-y5q5eth1hk8bl3om
(jelmer) Allow specifying the colocated branch to use in the branch URL,
 and retrieving the branch name using ControlDir._get_selected_branch.
 (Jelmer Vernooij)

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
16
16
 
17
17
"""Tools for dealing with the Launchpad API."""
18
18
 
19
 
from __future__ import absolute_import
20
 
 
21
19
# Importing this module will be expensive, since it imports launchpadlib and
22
20
# its dependencies. However, our plan is to only load this module when it is
23
21
# needed by a command that uses it.
24
22
 
25
23
 
26
 
import httplib2
27
24
import os
28
25
import re
29
26
import urlparse
36
33
    trace,
37
34
    transport,
38
35
    )
39
 
from bzrlib.i18n import gettext
40
36
from bzrlib.plugins.launchpad.lp_registration import (
41
37
    InvalidLaunchpadInstance,
42
38
    )
50
46
    STAGING_SERVICE_ROOT,
51
47
    Launchpad,
52
48
    )
53
 
from launchpadlib import uris
 
49
 
54
50
 
55
51
# 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)
 
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)
59
55
 
60
56
 
61
57
def get_cache_directory():
77
73
            installed_version, installed_version)
78
74
 
79
75
 
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')
 
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
    }
88
98
 
89
99
 
90
100
def _get_api_url(service):
101
111
    else:
102
112
        lp_instance = service._lp_instance
103
113
    try:
104
 
        return lookup_service_root(lp_instance)
105
 
    except ValueError:
 
114
        return LAUNCHPAD_API_URLS[lp_instance]
 
115
    except KeyError:
106
116
        raise InvalidLaunchpadInstance(lp_instance)
107
117
 
108
118
 
113
123
        errors.BzrError.__init__(self, branch=branch, url=branch.base)
114
124
 
115
125
 
116
 
def login(service, timeout=None, proxy_info=None,
117
 
          version=Launchpad.DEFAULT_VERSION):
 
126
def login(service, timeout=None, proxy_info=None):
118
127
    """Log in to the Launchpad API.
119
128
 
120
129
    :return: The root `Launchpad` object from launchpadlib.
121
130
    """
122
 
    if proxy_info is None:
123
 
        proxy_info = httplib2.proxy_info_from_environment('https')
124
131
    cache_directory = get_cache_directory()
125
132
    launchpad = Launchpad.login_with(
126
133
        '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.
130
 
    osutils.chmod_if_possible(cache_directory, 0700)
 
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)
131
138
    return launchpad
132
139
 
133
140
 
200
207
        if str(launchpad._root_uri) == STAGING_SERVICE_ROOT:
201
208
            return url.replace('bazaar.launchpad.net',
202
209
                               'bazaar.staging.launchpad.net')
203
 
        elif str(launchpad._root_uri) == lookup_service_root('qastaging'):
 
210
        elif str(launchpad._root_uri) == LAUNCHPAD_API_URLS['qastaging']:
204
211
            return url.replace('bazaar.launchpad.net',
205
212
                               'bazaar.qastaging.launchpad.net')
206
213
        return url
228
235
        """Create a Bazaar branch on Launchpad for the supplied branch."""
229
236
        url = cls.tweak_url(bzr_branch.get_push_location(), launchpad)
230
237
        if not cls.plausible_launchpad_url(url):
231
 
            raise errors.BzrError(gettext('%s is not registered on Launchpad') %
 
238
            raise errors.BzrError('%s is not registered on Launchpad' %
232
239
                                  bzr_branch.base)
233
240
        bzr_branch.create_clone_on_transport(transport.get_transport(url))
234
241
        lp_branch = launchpad.branches.getByUrl(url=url)
235
242
        if lp_branch is None:
236
 
            raise errors.BzrError(gettext('%s is not registered on Launchpad') %
237
 
                                                                            url)
 
243
            raise errors.BzrError('%s is not registered on Launchpad' % url)
238
244
        return lp_branch
239
245
 
240
246
    def get_target(self):
243
249
        if lp_branch.project is not None:
244
250
            dev_focus = lp_branch.project.development_focus
245
251
            if dev_focus is None:
246
 
                raise errors.BzrError(gettext('%s has no development focus.') %
 
252
                raise errors.BzrError('%s has no development focus.' %
247
253
                                  lp_branch.bzr_identity)
248
254
            target = dev_focus.branch
249
255
            if target is None:
250
 
                raise errors.BzrError(gettext(
251
 
                        'development focus %s has no branch.') % dev_focus)
 
256
                raise errors.BzrError('development focus %s has no branch.' % dev_focus)
252
257
        elif lp_branch.sourcepackage is not None:
253
258
            target = lp_branch.sourcepackage.getBranch(pocket="Release")
254
259
            if target is None:
255
 
                raise errors.BzrError(gettext(
256
 
                                      'source package %s has no branch.') %
 
260
                raise errors.BzrError('source package %s has no branch.' %
257
261
                                      lp_branch.sourcepackage)
258
262
        else:
259
 
            raise errors.BzrError(gettext(
260
 
                        '%s has no associated product or source package.') %
 
263
            raise errors.BzrError('%s has no associated product or source package.' %
261
264
                                  lp_branch.bzr_identity)
262
265
        return LaunchpadBranch(target, target.bzr_identity)
263
266
 
269
272
        try:
270
273
            if self.lp.last_scanned_id is not None:
271
274
                if self.bzr.last_revision() == self.lp.last_scanned_id:
272
 
                    trace.note(gettext('%s is already up-to-date.') %
 
275
                    trace.note('%s is already up-to-date.' %
273
276
                               self.lp.bzr_identity)
274
277
                    return
275
278
                graph = self.bzr.repository.get_graph()
276
279
                if not graph.is_ancestor(self.lp.last_scanned_id,
277
280
                                         self.bzr.last_revision()):
278
281
                    raise errors.DivergedBranches(self.bzr, self.push_bzr)
279
 
                trace.note(gettext('Pushing to %s') % self.lp.bzr_identity)
 
282
                trace.note('Pushing to %s' % self.lp.bzr_identity)
280
283
            self.bzr.push(self.push_bzr)
281
284
        finally:
282
285
            self.bzr.unlock()