/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-05-06 11:48:54 UTC
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180506114854-h4qd9ojaqy8wxjsd
Move .mailmap to root.

Show diffs side-by-side

added added

removed removed

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