/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: John Arbash Meinel
  • Date: 2011-04-20 09:46:28 UTC
  • mfrom: (5609.33.4 2.3)
  • mto: (5609.33.5 2.3)
  • mto: This revision was merged to the branch mainline in revision 5811.
  • Revision ID: john@arbash-meinel.com-20110420094628-l0bafq1lwb6ib1v2
Merge lp:bzr/2.3 @ 5640 so we can update the release notes (aka NEWS)

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
 
24
24
import os
 
25
import re
 
26
import urlparse
25
27
 
26
28
from bzrlib import (
 
29
    branch,
27
30
    config,
28
31
    errors,
29
32
    osutils,
 
33
    trace,
 
34
    transport,
30
35
    )
31
36
from bzrlib.plugins.launchpad.lp_registration import (
32
37
    InvalidLaunchpadInstance,
110
115
        raise InvalidLaunchpadInstance(lp_instance)
111
116
 
112
117
 
 
118
class NoLaunchpadBranch(errors.BzrError):
 
119
    _fmt = 'No launchpad branch could be found for branch "%(url)s".'
 
120
 
 
121
    def __init__(self, branch):
 
122
        errors.BzrError.__init__(self, branch=branch, url=branch.base)
 
123
 
 
124
 
113
125
def login(service, timeout=None, proxy_info=None):
114
126
    """Log in to the Launchpad API.
115
127
 
125
137
    return launchpad
126
138
 
127
139
 
 
140
class LaunchpadBranch(object):
 
141
    """Provide bzr and lp API access to a Launchpad branch."""
 
142
 
 
143
    def __init__(self, lp_branch, bzr_url, bzr_branch=None, check_update=True):
 
144
        """Constructor.
 
145
 
 
146
        :param lp_branch: The Launchpad branch.
 
147
        :param bzr_url: The URL of the Bazaar branch.
 
148
        :param bzr_branch: An instance of the Bazaar branch.
 
149
        """
 
150
        self.bzr_url = bzr_url
 
151
        self._bzr = bzr_branch
 
152
        self._push_bzr = None
 
153
        self._check_update = check_update
 
154
        self.lp = lp_branch
 
155
 
 
156
    @property
 
157
    def bzr(self):
 
158
        """Return the bzr branch for this branch."""
 
159
        if self._bzr is None:
 
160
            self._bzr = branch.Branch.open(self.bzr_url)
 
161
        return self._bzr
 
162
 
 
163
    @property
 
164
    def push_bzr(self):
 
165
        """Return the push branch for this branch."""
 
166
        if self._push_bzr is None:
 
167
            self._push_bzr = branch.Branch.open(self.lp.bzr_identity)
 
168
        return self._push_bzr
 
169
 
 
170
    @staticmethod
 
171
    def plausible_launchpad_url(url):
 
172
        """Is 'url' something that could conceivably be pushed to LP?
 
173
 
 
174
        :param url: A URL that may refer to a Launchpad branch.
 
175
        :return: A boolean.
 
176
        """
 
177
        if url is None:
 
178
            return False
 
179
        if url.startswith('lp:'):
 
180
            return True
 
181
        regex = re.compile('([a-z]*\+)*(bzr\+ssh|http)'
 
182
                           '://bazaar.*.launchpad.net')
 
183
        return bool(regex.match(url))
 
184
 
 
185
    @staticmethod
 
186
    def candidate_urls(bzr_branch):
 
187
        """Iterate through related URLs that might be Launchpad URLs.
 
188
 
 
189
        :param bzr_branch: A Bazaar branch to find URLs from.
 
190
        :return: a generator of URL strings.
 
191
        """
 
192
        url = bzr_branch.get_public_branch()
 
193
        if url is not None:
 
194
            yield url
 
195
        url = bzr_branch.get_push_location()
 
196
        if url is not None:
 
197
            yield url
 
198
        yield bzr_branch.base
 
199
 
 
200
    @staticmethod
 
201
    def tweak_url(url, launchpad):
 
202
        """Adjust a URL to work with staging, if needed."""
 
203
        if str(launchpad._root_uri) != STAGING_SERVICE_ROOT:
 
204
            return url
 
205
        if url is None:
 
206
            return None
 
207
        return url.replace('bazaar.launchpad.net',
 
208
                           'bazaar.staging.launchpad.net')
 
209
 
 
210
    @classmethod
 
211
    def from_bzr(cls, launchpad, bzr_branch, create_missing=True):
 
212
        """Find a Launchpad branch from a bzr branch."""
 
213
        check_update = True
 
214
        for url in cls.candidate_urls(bzr_branch):
 
215
            url = cls.tweak_url(url, launchpad)
 
216
            if not cls.plausible_launchpad_url(url):
 
217
                continue
 
218
            lp_branch = launchpad.branches.getByUrl(url=url)
 
219
            if lp_branch is not None:
 
220
                break
 
221
        else:
 
222
            if not create_missing:
 
223
                raise NoLaunchpadBranch(bzr_branch)
 
224
            lp_branch = cls.create_now(launchpad, bzr_branch)
 
225
            check_update = False
 
226
        return cls(lp_branch, bzr_branch.base, bzr_branch, check_update)
 
227
 
 
228
    @classmethod
 
229
    def create_now(cls, launchpad, bzr_branch):
 
230
        """Create a Bazaar branch on Launchpad for the supplied branch."""
 
231
        url = cls.tweak_url(bzr_branch.get_push_location(), launchpad)
 
232
        if not cls.plausible_launchpad_url(url):
 
233
            raise errors.BzrError('%s is not registered on Launchpad' %
 
234
                                  bzr_branch.base)
 
235
        bzr_branch.create_clone_on_transport(transport.get_transport(url))
 
236
        lp_branch = launchpad.branches.getByUrl(url=url)
 
237
        if lp_branch is None:
 
238
            raise errors.BzrError('%s is not registered on Launchpad' % url)
 
239
        return lp_branch
 
240
 
 
241
    def get_dev_focus(self):
 
242
        """Return the 'LaunchpadBranch' for the dev focus of this one."""
 
243
        lp_branch = self.lp
 
244
        if lp_branch.project is None:
 
245
            raise errors.BzrError('%s has no product.' %
 
246
                                  lp_branch.bzr_identity)
 
247
        dev_focus = lp_branch.project.development_focus.branch
 
248
        if dev_focus is None:
 
249
            raise errors.BzrError('%s has no development focus.' %
 
250
                                  lp_branch.bzr_identity)
 
251
        return LaunchpadBranch(dev_focus, dev_focus.bzr_identity)
 
252
 
 
253
    def update_lp(self):
 
254
        """Update the Launchpad copy of this branch."""
 
255
        if not self._check_update:
 
256
            return
 
257
        self.bzr.lock_read()
 
258
        try:
 
259
            if self.lp.last_scanned_id is not None:
 
260
                if self.bzr.last_revision() == self.lp.last_scanned_id:
 
261
                    trace.note('%s is already up-to-date.' %
 
262
                               self.lp.bzr_identity)
 
263
                    return
 
264
                graph = self.bzr.repository.get_graph()
 
265
                if not graph.is_ancestor(self.lp.last_scanned_id,
 
266
                                         self.bzr.last_revision()):
 
267
                    raise errors.DivergedBranches(self.bzr, self.push_bzr)
 
268
                trace.note('Pushing to %s' % self.lp.bzr_identity)
 
269
            self.bzr.push(self.push_bzr)
 
270
        finally:
 
271
            self.bzr.unlock()
 
272
 
 
273
    def find_lca_tree(self, other):
 
274
        """Find the revision tree for the LCA of this branch and other.
 
275
 
 
276
        :param other: Another LaunchpadBranch
 
277
        :return: The RevisionTree of the LCA of this branch and other.
 
278
        """
 
279
        graph = self.bzr.repository.get_graph(other.bzr.repository)
 
280
        lca = graph.find_unique_lca(self.bzr.last_revision(),
 
281
                                    other.bzr.last_revision())
 
282
        return self.bzr.repository.revision_tree(lca)
 
283
 
 
284
 
128
285
def load_branch(launchpad, branch):
129
286
    """Return the launchpadlib Branch object corresponding to 'branch'.
130
287
 
146
303
        if lp_branch:
147
304
            return lp_branch
148
305
    raise NotLaunchpadBranch(url)
 
306
 
 
307
 
 
308
def canonical_url(object):
 
309
    """Return the canonical URL for a branch."""
 
310
    scheme, netloc, path, params, query, fragment = urlparse.urlparse(
 
311
        str(object.self_link))
 
312
    path = '/'.join(path.split('/')[2:])
 
313
    netloc = netloc.replace('api.', 'code.')
 
314
    return urlparse.urlunparse((scheme, netloc, path, params, query,
 
315
                                fragment))