/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: Max Bowsher
  • Date: 2011-02-01 01:34:58 UTC
  • mto: This revision was merged to the branch mainline in revision 5639.
  • Revision ID: maxb@f2s.com-20110201013458-kkp1y64bvce58ziq
PPA documentation update.

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
    )
 
50
try:
 
51
    from launchpadlib import uris
 
52
except ImportError:
 
53
    # Create a minimal object so the getattr() calls below fail gently and
 
54
    # provide default values
 
55
    uris = object()
50
56
 
 
57
LPNET_SERVICE_ROOT = getattr(uris, 'LPNET_SERVICE_ROOT',
 
58
                             'https://api.launchpad.net/beta/')
 
59
QASTAGING_SERVICE_ROOT = getattr(uris, 'QASTAGING_SERVICE_ROOT',
 
60
                                 'https://api.qastaging.launchpad.net/')
51
61
 
52
62
# Declare the minimum version of launchpadlib that we need in order to work.
53
63
# 1.5.1 is the version of launchpadlib packaged in Ubuntu 9.10, the most
75
85
 
76
86
 
77
87
LAUNCHPAD_API_URLS = {
78
 
    'production': 'https://api.launchpad.net/beta/',
79
 
    'edge': EDGE_SERVICE_ROOT,
 
88
    'production': LPNET_SERVICE_ROOT,
 
89
    'qastaging': QASTAGING_SERVICE_ROOT,
80
90
    'staging': STAGING_SERVICE_ROOT,
81
91
    'dev': 'https://api.launchpad.dev/beta/',
82
92
    }
85
95
def _get_api_url(service):
86
96
    """Return the root URL of the Launchpad API.
87
97
 
88
 
    e.g. For the 'edge' Launchpad service, this function returns
89
 
    launchpadlib.launchpad.EDGE_SERVICE_ROOT.
 
98
    e.g. For the 'staging' Launchpad service, this function returns
 
99
    launchpadlib.launchpad.STAGING_SERVICE_ROOT.
90
100
 
91
101
    :param service: A `LaunchpadService` object.
92
102
    :return: A URL as a string.
101
111
        raise InvalidLaunchpadInstance(lp_instance)
102
112
 
103
113
 
 
114
class NoLaunchpadBranch(errors.BzrError):
 
115
    _fmt = 'No launchpad branch could be found for branch "%(url)s".'
 
116
 
 
117
    def __init__(self, branch):
 
118
        errors.BzrError.__init__(self, branch=branch, url=branch.base)
 
119
 
 
120
 
104
121
def login(service, timeout=None, proxy_info=None):
105
122
    """Log in to the Launchpad API.
106
123
 
179
196
    @staticmethod
180
197
    def tweak_url(url, launchpad):
181
198
        """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')
 
199
        if str(launchpad._root_uri) == STAGING_SERVICE_ROOT:
 
200
            return url.replace('bazaar.launchpad.net',
 
201
                               'bazaar.staging.launchpad.net')
 
202
        elif str(launchpad._root_uri) == QASTAGING_SERVICE_ROOT:
 
203
            return url.replace('bazaar.launchpad.net',
 
204
                               'bazaar.qastaging.launchpad.net')
 
205
        return url
188
206
 
189
207
    @classmethod
190
 
    def from_bzr(cls, launchpad, bzr_branch):
 
208
    def from_bzr(cls, launchpad, bzr_branch, create_missing=True):
191
209
        """Find a Launchpad branch from a bzr branch."""
192
210
        check_update = True
193
211
        for url in cls.candidate_urls(bzr_branch):
198
216
            if lp_branch is not None:
199
217
                break
200
218
        else:
 
219
            if not create_missing:
 
220
                raise NoLaunchpadBranch(bzr_branch)
201
221
            lp_branch = cls.create_now(launchpad, bzr_branch)
202
222
            check_update = False
203
223
        return cls(lp_branch, bzr_branch.base, bzr_branch, check_update)
215
235
            raise errors.BzrError('%s is not registered on Launchpad' % url)
216
236
        return lp_branch
217
237
 
218
 
    def get_dev_focus(self):
219
 
        """Return the 'LaunchpadBranch' for the dev focus of this one."""
 
238
    def get_target(self):
 
239
        """Return the 'LaunchpadBranch' for the target of this one."""
220
240
        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)
 
241
        if lp_branch.project is not None:
 
242
            dev_focus = lp_branch.project.development_focus
 
243
            if dev_focus is None:
 
244
                raise errors.BzrError('%s has no development focus.' %
 
245
                                  lp_branch.bzr_identity)
 
246
            target = dev_focus.branch
 
247
            if target is None:
 
248
                raise errors.BzrError('development focus %s has no branch.' % dev_focus)
 
249
        elif lp_branch.sourcepackage is not None:
 
250
            target = lp_branch.sourcepackage.getBranch(pocket="Release")
 
251
            if target is None:
 
252
                raise errors.BzrError('source package %s has no branch.' %
 
253
                                      lp_branch.sourcepackage)
 
254
        else:
 
255
            raise errors.BzrError('%s has no associated product or source package.' %
 
256
                                  lp_branch.bzr_identity)
 
257
        return LaunchpadBranch(target, target.bzr_identity)
229
258
 
230
259
    def update_lp(self):
231
260
        """Update the Launchpad copy of this branch."""
280
309
        if lp_branch:
281
310
            return lp_branch
282
311
    raise NotLaunchpadBranch(url)
 
312
 
 
313
 
 
314
def canonical_url(object):
 
315
    """Return the canonical URL for a branch."""
 
316
    scheme, netloc, path, params, query, fragment = urlparse.urlparse(
 
317
        str(object.self_link))
 
318
    path = '/'.join(path.split('/')[2:])
 
319
    netloc = netloc.replace('api.', 'code.')
 
320
    return urlparse.urlunparse((scheme, netloc, path, params, query,
 
321
                                fragment))