MAJOR=1
MINOR=5
DEVNAME=zero
DEVMODE=0666
  7   ˆl–Ü4ý( ©”u( 	•íÀà™S�ÿ�þ^	^Y¹DGÃ Â�ÏÈ„e‘6çÇÂ¿Ä ?÷     # Copyright (C) 2010, 2011 Canonical Ltd
# Copyright (C) 2018 Breezy Developers
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Support for Launchpad."""

import re
import shutil
import tempfile
from typing import Optional, List, Any

from ...forge import (
    Forge,
    LabelsUnsupported,
    MergeProposal,
    MergeProposalBuilder,
    MergeProposalExists,
    UnsupportedForge,
    TitleUnsupported,
    AutoMergeUnsupported,
    )

from ... import (
    branch as _mod_branch,
    controldir,
    errors,
    hooks,
    urlutils,
    )
from ...trace import mutter
from ...git.urls import git_url_to_bzr_url
from ...lazy_import import lazy_import
lazy_import(globals(), """
from breezy.plugins.launchpad import (
    lp_api,
    uris as lp_uris,
    )

""")
from ...transport import get_transport


DEFAULT_PREFERRED_SCHEMES = ['ssh', 'http']

BZR_SCHEME_MAP = {
    'ssh': 'bzr+ssh://bazaar.launchpad.net/',
    'http': 'https://bazaar.launchpad.net/',
    'https': 'https://bazaar.launchpad.net/',
}

GIT_SCHEME_MAP = {
    'ssh': 'git+ssh://git.launchpad.net/',
    'http': 'https://git.launchpad.net/',
    'https': 'https://git.launchpad.net/',
}


# TODO(jelmer): Make selection of launchpad staging a configuration option.

def status_to_lp_mp_statuses(status):
    statuses = []
    if status in ('open', 'all'):
        statuses.extend([
            'Work in progress',
            'Needs review',
            'Approved',
            'Code failed to merge',
            'Queued'])
    if status in ('closed', 'all'):
        statuses.extend(['Rejected', 'Superseded'])
    if status in ('merged', 'all'):
        statuses.append('Merged')
    return statuses


def plausible_launchpad_url(url):
    if url is None:
        return False
    if url.startswith('lp:'):
        return True
    regex = re.compile(r'([a-z]*\+)*(bzr\+ssh|http|ssh|git|https)'
                       r'://(bazaar|git).*\.launchpad\.net')
    return bool(regex.match(url))


class WebserviceFailure(Exception):

    def __init__(self, message):
        self.message = message


def _call_webservice(call, *args, **kwargs):
    """Make a call to the webservice, wrapping failures.

    :param call: The call to make.
    :param *args: *args for the call.
    :param **kwargs: **kwargs for the call.
    :return: The result of calling call(*args, *kwargs).
    """
    from lazr.restfulclient import errors as restful_errors
    try:
        return call(*args, **kwargs)
    except restful_errors.HTTPError as e:
        error_lines = []
        for line in e.content.splitlines():
            if line.startswith(b'Traceback (most recent call last):'):
                break
            error_lines.append(line)
        raise WebserviceFailure(b''.join(error_lines))


class LaunchpadMergeProposal(MergeProposal):

    supports_auto_merge = False

    def __init__(self, mp):
        self._mp = mp

    def get_web_url(self):
        return self._mp.web_link

    def get_source_branch_url(self, *, preferred_schemes=None):
        # TODO(jelmer): Honor preferred_schemes
        if self._mp.source_branch:
            return self._mp.source_branch.bzr_identity
        else:
            return git_url_to_bzr_url(
                self._mp.source_git_repository.git_identity,
                ref=self._mp.source_git_path.encode('utf