/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: 2011-02-11 16:15:39 UTC
  • mto: This revision was merged to the branch mainline in revision 5661.
  • Revision ID: v.ladeuil+lp@free.fr-20110211161539-26o5a28ihyemvuzg
Fix pqm failure on python2.4.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
import os
25
25
import re
 
26
import urlparse
26
27
 
27
28
from bzrlib import (
28
29
    branch,
43
44
    raise errors.DependencyNotPresent('launchpadlib', e)
44
45
 
45
46
from launchpadlib.launchpad import (
46
 
    EDGE_SERVICE_ROOT,
47
47
    STAGING_SERVICE_ROOT,
48
48
    Launchpad,
49
49
    )
74
74
            installed_version, installed_version)
75
75
 
76
76
 
 
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.
77
90
LAUNCHPAD_API_URLS = {
78
 
    'production': 'https://api.launchpad.net/beta/',
79
 
    'edge': EDGE_SERVICE_ROOT,
 
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'),
80
95
    'staging': STAGING_SERVICE_ROOT,
81
 
    'dev': 'https://api.launchpad.dev/beta/',
 
96
    'dev': STAGING_SERVICE_ROOT.replace('api.staging.launchpad.net',
 
97
        'api.launchpad.dev'),
82
98
    }
83
99
 
84
100
 
85
101
def _get_api_url(service):
86
102
    """Return the root URL of the Launchpad API.
87
103
 
88
 
    e.g. For the 'edge' Launchpad service, this function returns
89
 
    launchpadlib.launchpad.EDGE_SERVICE_ROOT.
 
104
    e.g. For the 'staging' Launchpad service, this function returns
 
105
    launchpadlib.launchpad.STAGING_SERVICE_ROOT.
90
106
 
91
107
    :param service: A `LaunchpadService` object.
92
108
    :return: A URL as a string.
101
117
        raise InvalidLaunchpadInstance(lp_instance)
102
118
 
103
119
 
 
120
class NoLaunchpadBranch(errors.BzrError):
 
121
    _fmt = 'No launchpad branch could be found for branch "%(url)s".'
 
122
 
 
123
    def __init__(self, branch):
 
124
        errors.BzrError.__init__(self, branch=branch, url=branch.base)
 
125
 
 
126
 
104
127
def login(service, timeout=None, proxy_info=None):
105
128
    """Log in to the Launchpad API.
106
129
 
179
202
    @staticmethod
180
203
    def tweak_url(url, launchpad):
181
204
        """Adjust a URL to work with staging, if needed."""
182
 
        if str(launchpad._root_uri) != STAGING_SERVICE_ROOT:
183
 
            return url
184
 
        if url is None:
185
 
            return None
186
 
        return url.replace('bazaar.launchpad.net',
187
 
                           'bazaar.staging.launchpad.net')
 
205
        if str(launchpad._root_uri) == STAGING_SERVICE_ROOT:
 
206
            return url.replace('bazaar.launchpad.net',
 
207
                               'bazaar.staging.launchpad.net')
 
208
        elif str(launchpad._root_uri) == LAUNCHPAD_API_URLS['qastaging']:
 
209
            return url.replace('bazaar.launchpad.net',
 
210
                               'bazaar.qastaging.launchpad.net')
 
211
        return url
188
212
 
189
213
    @classmethod
190
 
    def from_bzr(cls, launchpad, bzr_branch):
 
214
    def from_bzr(cls, launchpad, bzr_branch, create_missing=True):
191
215
        """Find a Launchpad branch from a bzr branch."""
192
216
        check_update = True
193
217
        for url in cls.candidate_urls(bzr_branch):
198
222
            if lp_branch is not None:
199
223
                break
200
224
        else:
 
225
            if not create_missing:
 
226
                raise NoLaunchpadBranch(bzr_branch)
201
227
            lp_branch = cls.create_now(launchpad, bzr_branch)
202
228
            check_update = False
203
229
        return cls(lp_branch, bzr_branch.base, bzr_branch, check_update)
215
241
            raise errors.BzrError('%s is not registered on Launchpad' % url)
216
242
        return lp_branch
217
243
 
218
 
    def get_dev_focus(self):
219
 
        """Return the 'LaunchpadBranch' for the dev focus of this one."""
 
244
    def get_target(self):
 
245
        """Return the 'LaunchpadBranch' for the target of this one."""
220
246
        lp_branch = self.lp
221
 
        if lp_branch.project is None:
222
 
            raise errors.BzrError('%s has no product.' %
223
 
                                  lp_branch.bzr_identity)
224
 
        dev_focus = lp_branch.project.development_focus.branch
225
 
        if dev_focus is None:
226
 
            raise errors.BzrError('%s has no development focus.' %
227
 
                                  lp_branch.bzr_identity)
228
 
        return LaunchpadBranch(dev_focus, dev_focus.bzr_identity)
 
247
        if lp_branch.project is not None:
 
248
            dev_focus = lp_branch.project.development_focus
 
249
            if dev_focus is None:
 
250
                raise errors.BzrError('%s has no development focus.' %
 
251
                                  lp_branch.bzr_identity)
 
252
            target = dev_focus.branch
 
253
            if target is None:
 
254
                raise errors.BzrError('development focus %s has no branch.' % dev_focus)
 
255
        elif lp_branch.sourcepackage is not None:
 
256
            target = lp_branch.sourcepackage.getBranch(pocket="Release")
 
257
            if target is None:
 
258
                raise errors.BzrError('source package %s has no branch.' %
 
259
                                      lp_branch.sourcepackage)
 
260
        else:
 
261
            raise errors.BzrError('%s has no associated product or source package.' %
 
262
                                  lp_branch.bzr_identity)
 
263
        return LaunchpadBranch(target, target.bzr_identity)
229
264
 
230
265
    def update_lp(self):
231
266
        """Update the Launchpad copy of this branch."""
280
315
        if lp_branch:
281
316
            return lp_branch
282
317
    raise NotLaunchpadBranch(url)
 
318
 
 
319
 
 
320
def canonical_url(object):
 
321
    """Return the canonical URL for a branch."""
 
322
    scheme, netloc, path, params, query, fragment = urlparse.urlparse(
 
323
        str(object.self_link))
 
324
    path = '/'.join(path.split('/')[2:])
 
325
    netloc = netloc.replace('api.', 'code.')
 
326
    return urlparse.urlunparse((scheme, netloc, path, params, query,
 
327
                                fragment))