/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 breezy/plugins/launchpad/lp_api.py

  • Committer: Jelmer Vernooij
  • Date: 2018-02-18 21:42:57 UTC
  • mto: This revision was merged to the branch mainline in revision 6859.
  • Revision ID: jelmer@jelmer.uk-20180218214257-jpevutp1wa30tz3v
Update TODO to reference Breezy, not Bazaar.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
22
24
 
23
25
 
 
26
import httplib2
 
27
import os
24
28
import re
25
 
from urllib.parse import (
26
 
    urlparse,
27
 
    urlunparse,
28
 
    )
 
29
try:
 
30
    from urllib.parse import (
 
31
        urlparse,
 
32
        urlunparse,
 
33
        )
 
34
except ImportError:  # python < 3
 
35
    from urlparse import (
 
36
        urlparse,
 
37
        urlunparse,
 
38
        )
29
39
 
30
40
from ... import (
31
41
    branch,
32
 
    bedding,
 
42
    config,
33
43
    errors,
34
44
    osutils,
35
45
    trace,
36
46
    transport,
37
47
    )
38
48
from ...i18n import gettext
39
 
 
40
 
 
41
 
class LaunchpadlibMissing(errors.DependencyNotPresent):
42
 
 
43
 
    _fmt = ("launchpadlib is required for Launchpad API access. "
44
 
            "Please install the launchpadlib package.")
45
 
 
46
 
    def __init__(self, e):
47
 
        super(LaunchpadlibMissing, self).__init__(
48
 
            'launchpadlib', e)
 
49
from .lp_registration import (
 
50
    InvalidLaunchpadInstance,
 
51
    )
49
52
 
50
53
try:
51
54
    import launchpadlib
52
55
except ImportError as e:
53
 
    raise LaunchpadlibMissing(e)
 
56
    raise errors.DependencyNotPresent('launchpadlib', e)
54
57
 
55
 
from launchpadlib.credentials import RequestTokenAuthorizationEngine
56
58
from launchpadlib.launchpad import (
 
59
    STAGING_SERVICE_ROOT,
57
60
    Launchpad,
58
61
    )
59
62
from launchpadlib import uris
60
63
 
61
64
# Declare the minimum version of launchpadlib that we need in order to work.
62
 
MINIMUM_LAUNCHPADLIB_VERSION = (1, 6, 3)
 
65
# 1.6.0 is the version of launchpadlib packaged in Ubuntu 10.04, the most
 
66
# recent Ubuntu LTS release supported on the desktop at the time of writing.
 
67
MINIMUM_LAUNCHPADLIB_VERSION = (1, 6, 0)
63
68
 
64
69
 
65
70
def get_cache_directory():
66
71
    """Return the directory to cache launchpadlib objects in."""
67
 
    return osutils.pathjoin(bedding.cache_dir(), 'launchpad')
 
72
    return osutils.pathjoin(config.config_dir(), 'launchpad')
68
73
 
69
74
 
70
75
def parse_launchpadlib_version(version_number):
92
97
        return staging_root.replace('staging', 'qastaging')
93
98
 
94
99
 
 
100
def _get_api_url(service):
 
101
    """Return the root URL of the Launchpad API.
 
102
 
 
103
    e.g. For the 'staging' Launchpad service, this function returns
 
104
    launchpadlib.launchpad.STAGING_SERVICE_ROOT.
 
105
 
 
106
    :param service: A `LaunchpadService` object.
 
107
    :return: A URL as a string.
 
108
    """
 
109
    if service._lp_instance is None:
 
110
        lp_instance = service.DEFAULT_INSTANCE
 
111
    else:
 
112
        lp_instance = service._lp_instance
 
113
    try:
 
114
        return lookup_service_root(lp_instance)
 
115
    except ValueError:
 
116
        raise InvalidLaunchpadInstance(lp_instance)
 
117
 
 
118
 
95
119
class NoLaunchpadBranch(errors.BzrError):
96
120
    _fmt = 'No launchpad branch could be found for branch "%(url)s".'
97
121
 
99
123
        errors.BzrError.__init__(self, branch=branch, url=branch.base)
100
124
 
101
125
 
102
 
def get_auth_engine(base_url):
103
 
    return Launchpad.authorization_engine_factory(base_url, 'breezy')
104
 
 
105
 
 
106
 
def get_credential_store():
107
 
    return Launchpad.credential_store_factory(None)
108
 
 
109
 
 
110
 
def connect_launchpad(base_url, timeout=None, proxy_info=None,
111
 
                      version=Launchpad.DEFAULT_VERSION):
 
126
def login(service, timeout=None, proxy_info=None,
 
127
          version=Launchpad.DEFAULT_VERSION):
112
128
    """Log in to the Launchpad API.
113
129
 
114
130
    :return: The root `Launchpad` object from launchpadlib.
115
131
    """
116
132
    if proxy_info is None:
117
 
        import httplib2
118
133
        proxy_info = httplib2.proxy_info_from_environment('https')
119
 
    try:
120
 
        cache_directory = get_cache_directory()
121
 
    except EnvironmentError:
122
 
        cache_directory = None
123
 
    credential_store = get_credential_store()
124
 
    authorization_engine = get_auth_engine(base_url)
125
 
    return Launchpad.login_with(
126
 
        'breezy', base_url, cache_directory, timeout=timeout,
127
 
        credential_store=credential_store,
128
 
        authorization_engine=authorization_engine,
 
134
    cache_directory = get_cache_directory()
 
135
    launchpad = Launchpad.login_with(
 
136
        'bzr', _get_api_url(service), cache_directory, timeout=timeout,
129
137
        proxy_info=proxy_info, version=version)
130
 
 
 
138
    # XXX: Work-around a minor security bug in launchpadlib < 1.6.3, which
 
139
    # would create this directory with default umask.
 
140
    osutils.chmod_if_possible(cache_directory, 0o700)
 
141
    return launchpad
131
142
 
132
143
 
133
144
class LaunchpadBranch(object):
171
182
            return False
172
183
        if url.startswith('lp:'):
173
184
            return True
174
 
        regex = re.compile('([a-z]*\\+)*(bzr\\+ssh|http)'
 
185
        regex = re.compile('([a-z]*\+)*(bzr\+ssh|http)'
175
186
                           '://bazaar.*.launchpad.net')
176
187
        return bool(regex.match(url))
177
188
 
196
207
    @staticmethod
197
208
    def tweak_url(url, launchpad):
198
209
        """Adjust a URL to work with staging, if needed."""
199
 
        if str(launchpad._root_uri) == uris.STAGING_SERVICE_ROOT:
 
210
        if str(launchpad._root_uri) == STAGING_SERVICE_ROOT:
200
211
            return url.replace('bazaar.launchpad.net',
201
212
                               'bazaar.staging.launchpad.net')
202
213
        elif str(launchpad._root_uri) == lookup_service_root('qastaging'):
233
244
        lp_branch = launchpad.branches.getByUrl(url=url)
234
245
        if lp_branch is None:
235
246
            raise errors.BzrError(gettext('%s is not registered on Launchpad') %
236
 
                                  url)
 
247
                                                                            url)
237
248
        return lp_branch
238
249
 
239
250
    def get_target(self):
243
254
            dev_focus = lp_branch.project.development_focus
244
255
            if dev_focus is None:
245
256
                raise errors.BzrError(gettext('%s has no development focus.') %
246
 
                                      lp_branch.bzr_identity)
 
257
                                  lp_branch.bzr_identity)
247
258
            target = dev_focus.branch
248
259
            if target is None:
249
260
                raise errors.BzrError(gettext(
250
 
                    'development focus %s has no branch.') % dev_focus)
 
261
                        'development focus %s has no branch.') % dev_focus)
251
262
        elif lp_branch.sourcepackage is not None:
252
263
            target = lp_branch.sourcepackage.getBranch(pocket="Release")
253
264
            if target is None:
256
267
                                      lp_branch.sourcepackage)
257
268
        else:
258
269
            raise errors.BzrError(gettext(
259
 
                '%s has no associated product or source package.') %
260
 
                lp_branch.bzr_identity)
 
270
                        '%s has no associated product or source package.') %
 
271
                                  lp_branch.bzr_identity)
261
272
        return LaunchpadBranch(target, target.bzr_identity)
262
273
 
263
274
    def update_lp(self):
271
282
                               self.lp.bzr_identity)
272
283
                    return
273
284
                graph = self.bzr.repository.get_graph()
274
 
                if not graph.is_ancestor(osutils.safe_utf8(self.lp.last_scanned_id),
 
285
                if not graph.is_ancestor(self.lp.last_scanned_id,
275
286
                                         self.bzr.last_revision()):
276
287
                    raise errors.DivergedBranches(self.bzr, self.push_bzr)
277
288
                trace.note(gettext('Pushing to %s') % self.lp.bzr_identity)