/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: 2019-06-02 02:35:46 UTC
  • mfrom: (7309 work)
  • mto: This revision was merged to the branch mainline in revision 7319.
  • Revision ID: jelmer@jelmer.uk-20190602023546-lqco868tnv26d8ow
merge trunk.

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
28
26
import re
29
27
try:
30
28
    from urllib.parse import (
46
44
    transport,
47
45
    )
48
46
from ...i18n import gettext
49
 
from .lp_registration import (
50
 
    InvalidLaunchpadInstance,
51
 
    )
 
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)
52
57
 
53
58
try:
54
59
    import launchpadlib
55
60
except ImportError as e:
56
 
    raise errors.DependencyNotPresent('launchpadlib', e)
 
61
    raise LaunchpadlibMissing(e)
57
62
 
58
63
from launchpadlib.launchpad import (
59
 
    STAGING_SERVICE_ROOT,
60
64
    Launchpad,
61
65
    )
62
66
from launchpadlib import uris
63
67
 
64
68
# 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)
 
69
MINIMUM_LAUNCHPADLIB_VERSION = (1, 6, 3)
 
70
 
 
71
 
 
72
# We use production as the default because edge has been deprecated circa
 
73
# 2010-11 (see bug https://bugs.launchpad.net/bzr/+bug/583667)
 
74
DEFAULT_INSTANCE = 'production'
 
75
 
 
76
LAUNCHPAD_DOMAINS = {
 
77
    'production': 'launchpad.net',
 
78
    'staging': 'staging.launchpad.net',
 
79
    'qastaging': 'qastaging.launchpad.net',
 
80
    'demo': 'demo.launchpad.net',
 
81
    'dev': 'launchpad.test',
 
82
    }
 
83
 
 
84
LAUNCHPAD_BAZAAR_DOMAINS = [
 
85
    'bazaar.%s' % domain
 
86
    for domain in LAUNCHPAD_DOMAINS.values()]
68
87
 
69
88
 
70
89
def get_cache_directory():
97
116
        return staging_root.replace('staging', 'qastaging')
98
117
 
99
118
 
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
119
class NoLaunchpadBranch(errors.BzrError):
120
120
    _fmt = 'No launchpad branch could be found for branch "%(url)s".'
121
121
 
123
123
        errors.BzrError.__init__(self, branch=branch, url=branch.base)
124
124
 
125
125
 
126
 
def login(service, timeout=None, proxy_info=None,
127
 
          version=Launchpad.DEFAULT_VERSION):
 
126
def connect_launchpad(base_url, timeout=None, proxy_info=None,
 
127
                      version=Launchpad.DEFAULT_VERSION):
128
128
    """Log in to the Launchpad API.
129
129
 
130
130
    :return: The root `Launchpad` object from launchpadlib.
131
131
    """
132
132
    if proxy_info is None:
 
133
        import httplib2
133
134
        proxy_info = httplib2.proxy_info_from_environment('https')
134
135
    cache_directory = get_cache_directory()
135
 
    launchpad = Launchpad.login_with(
136
 
        'bzr', _get_api_url(service), cache_directory, timeout=timeout,
 
136
    return Launchpad.login_with(
 
137
        'breezy', base_url, cache_directory, timeout=timeout,
137
138
        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
 
139
 
142
140
 
143
141
 
144
142
class LaunchpadBranch(object):
207
205
    @staticmethod
208
206
    def tweak_url(url, launchpad):
209
207
        """Adjust a URL to work with staging, if needed."""
210
 
        if str(launchpad._root_uri) == STAGING_SERVICE_ROOT:
 
208
        if str(launchpad._root_uri) == uris.STAGING_SERVICE_ROOT:
211
209
            return url.replace('bazaar.launchpad.net',
212
210
                               'bazaar.staging.launchpad.net')
213
211
        elif str(launchpad._root_uri) == lookup_service_root('qastaging'):
244
242
        lp_branch = launchpad.branches.getByUrl(url=url)
245
243
        if lp_branch is None:
246
244
            raise errors.BzrError(gettext('%s is not registered on Launchpad') %
247
 
                                                                            url)
 
245
                                  url)
248
246
        return lp_branch
249
247
 
250
248
    def get_target(self):
254
252
            dev_focus = lp_branch.project.development_focus
255
253
            if dev_focus is None:
256
254
                raise errors.BzrError(gettext('%s has no development focus.') %
257
 
                                  lp_branch.bzr_identity)
 
255
                                      lp_branch.bzr_identity)
258
256
            target = dev_focus.branch
259
257
            if target is None:
260
258
                raise errors.BzrError(gettext(
261
 
                        'development focus %s has no branch.') % dev_focus)
 
259
                    'development focus %s has no branch.') % dev_focus)
262
260
        elif lp_branch.sourcepackage is not None:
263
261
            target = lp_branch.sourcepackage.getBranch(pocket="Release")
264
262
            if target is None:
267
265
                                      lp_branch.sourcepackage)
268
266
        else:
269
267
            raise errors.BzrError(gettext(
270
 
                        '%s has no associated product or source package.') %
271
 
                                  lp_branch.bzr_identity)
 
268
                '%s has no associated product or source package.') %
 
269
                lp_branch.bzr_identity)
272
270
        return LaunchpadBranch(target, target.bzr_identity)
273
271
 
274
272
    def update_lp(self):
282
280
                               self.lp.bzr_identity)
283
281
                    return
284
282
                graph = self.bzr.repository.get_graph()
285
 
                if not graph.is_ancestor(self.lp.last_scanned_id,
 
283
                if not graph.is_ancestor(osutils.safe_utf8(self.lp.last_scanned_id),
286
284
                                         self.bzr.last_revision()):
287
285
                    raise errors.DivergedBranches(self.bzr, self.push_bzr)
288
286
                trace.note(gettext('Pushing to %s') % self.lp.bzr_identity)