/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: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

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
 
 
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
 
import os
28
24
import re
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
 
        )
 
25
from urllib.parse import (
 
26
    urlparse,
 
27
    urlunparse,
 
28
    )
39
29
 
40
30
from ... import (
41
31
    branch,
42
 
    config,
 
32
    bedding,
43
33
    errors,
44
34
    osutils,
45
35
    trace,
46
36
    transport,
47
37
    )
48
38
from ...i18n import gettext
49
 
from .lp_registration import (
50
 
    InvalidLaunchpadInstance,
51
 
    )
 
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)
52
49
 
53
50
try:
54
51
    import launchpadlib
55
52
except ImportError as e:
56
 
    raise errors.DependencyNotPresent('launchpadlib', e)
 
53
    raise LaunchpadlibMissing(e)
57
54
 
58
55
from launchpadlib.launchpad import (
59
 
    STAGING_SERVICE_ROOT,
60
56
    Launchpad,
61
57
    )
62
58
from launchpadlib import uris
63
59
 
64
60
# Declare the minimum version of launchpadlib that we need in order to work.
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)
 
61
MINIMUM_LAUNCHPADLIB_VERSION = (1, 6, 3)
68
62
 
69
63
 
70
64
def get_cache_directory():
71
65
    """Return the directory to cache launchpadlib objects in."""
72
 
    return osutils.pathjoin(config.config_dir(), 'launchpad')
 
66
    return osutils.pathjoin(bedding.cache_dir(), 'launchpad')
73
67
 
74
68
 
75
69
def parse_launchpadlib_version(version_number):
97
91
        return staging_root.replace('staging', 'qastaging')
98
92
 
99
93
 
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
 
 
119
94
class NoLaunchpadBranch(errors.BzrError):
120
95
    _fmt = 'No launchpad branch could be found for branch "%(url)s".'
121
96
 
123
98
        errors.BzrError.__init__(self, branch=branch, url=branch.base)
124
99
 
125
100
 
126
 
def login(service, timeout=None, proxy_info=None,
127
 
          version=Launchpad.DEFAULT_VERSION):
 
101
def connect_launchpad(base_url, timeout=None, proxy_info=None,
 
102
                      version=Launchpad.DEFAULT_VERSION):
128
103
    """Log in to the Launchpad API.
129
104
 
130
105
    :return: The root `Launchpad` object from launchpadlib.
131
106
    """
132
107
    if proxy_info is None:
 
108
        import httplib2
133
109
        proxy_info = httplib2.proxy_info_from_environment('https')
134
 
    cache_directory = get_cache_directory()
135
 
    launchpad = Launchpad.login_with(
136
 
        'bzr', _get_api_url(service), cache_directory, timeout=timeout,
 
110
    try:
 
111
        cache_directory = get_cache_directory()
 
112
    except EnvironmentError:
 
113
        cache_directory = None
 
114
    return Launchpad.login_with(
 
115
        'breezy', base_url, cache_directory, timeout=timeout,
137
116
        proxy_info=proxy_info, version=version)
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
 
117
 
142
118
 
143
119
 
144
120
class LaunchpadBranch(object):
182
158
            return False
183
159
        if url.startswith('lp:'):
184
160
            return True
185
 
        regex = re.compile('([a-z]*\+)*(bzr\+ssh|http)'
 
161
        regex = re.compile('([a-z]*\\+)*(bzr\\+ssh|http)'
186
162
                           '://bazaar.*.launchpad.net')
187
163
        return bool(regex.match(url))
188
164
 
207
183
    @staticmethod
208
184
    def tweak_url(url, launchpad):
209
185
        """Adjust a URL to work with staging, if needed."""
210
 
        if str(launchpad._root_uri) == STAGING_SERVICE_ROOT:
 
186
        if str(launchpad._root_uri) == uris.STAGING_SERVICE_ROOT:
211
187
            return url.replace('bazaar.launchpad.net',
212
188
                               'bazaar.staging.launchpad.net')
213
189
        elif str(launchpad._root_uri) == lookup_service_root('qastaging'):
244
220
        lp_branch = launchpad.branches.getByUrl(url=url)
245
221
        if lp_branch is None:
246
222
            raise errors.BzrError(gettext('%s is not registered on Launchpad') %
247
 
                                                                            url)
 
223
                                  url)
248
224
        return lp_branch
249
225
 
250
226
    def get_target(self):
254
230
            dev_focus = lp_branch.project.development_focus
255
231
            if dev_focus is None:
256
232
                raise errors.BzrError(gettext('%s has no development focus.') %
257
 
                                  lp_branch.bzr_identity)
 
233
                                      lp_branch.bzr_identity)
258
234
            target = dev_focus.branch
259
235
            if target is None:
260
236
                raise errors.BzrError(gettext(
261
 
                        'development focus %s has no branch.') % dev_focus)
 
237
                    'development focus %s has no branch.') % dev_focus)
262
238
        elif lp_branch.sourcepackage is not None:
263
239
            target = lp_branch.sourcepackage.getBranch(pocket="Release")
264
240
            if target is None:
267
243
                                      lp_branch.sourcepackage)
268
244
        else:
269
245
            raise errors.BzrError(gettext(
270
 
                        '%s has no associated product or source package.') %
271
 
                                  lp_branch.bzr_identity)
 
246
                '%s has no associated product or source package.') %
 
247
                lp_branch.bzr_identity)
272
248
        return LaunchpadBranch(target, target.bzr_identity)
273
249
 
274
250
    def update_lp(self):
282
258
                               self.lp.bzr_identity)
283
259
                    return
284
260
                graph = self.bzr.repository.get_graph()
285
 
                if not graph.is_ancestor(self.lp.last_scanned_id,
 
261
                if not graph.is_ancestor(osutils.safe_utf8(self.lp.last_scanned_id),
286
262
                                         self.bzr.last_revision()):
287
263
                    raise errors.DivergedBranches(self.bzr, self.push_bzr)
288
264
                trace.note(gettext('Pushing to %s') % self.lp.bzr_identity)