1
  X   ˆl–Ü4ý( ©”u( 	•íÀà™S�ÿ�þC#Ž,Ü¢#á�a	HçÄƒu±9Ã_ŠI|¥�+Wé3ž¯a–Ým_JJi?u8 ¸ ÜiÕ1hßÂ f    # Copyright (C) 2007-2011 Canonical Ltd
#
# 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

"""Directory lookup that uses Launchpad."""

from urllib.parse import urlsplit

from ... import (
    debug,
    errors,
    trace,
    transport,
    )
from ...urlutils import InvalidURL
from ...i18n import gettext

from .uris import (
    DEFAULT_INSTANCE,
    LAUNCHPAD_DOMAINS,
    LPNET_SERVICE_ROOT,
    )
from .account import get_lp_login


# As breezy.transport.remote may not be loaded yet, make sure bzr+ssh
# is counted as a netloc protocol.
transport.register_urlparse_netloc_protocol('bzr+ssh')
transport.register_urlparse_netloc_protocol('lp')
transport.register_urlparse_netloc_protocol('lp+bzr')


def _requires_launchpad_login(scheme, netloc, path, query,
                              fragment):
    """Does the URL require a Launchpad login in order to be reached?

    The URL is specified by its parsed components, as returned from
    urlsplit.
    """
    return (scheme in ('bzr+ssh', 'sftp')
            and (netloc.endswith('launchpad.net') or
                 netloc.endswith('launchpad.test')))


def _expand_user(path, url, lp_login):
    if path.startswith('~/'):
        if lp_login is None:
            raise InvalidURL(path=url,
                             extra='Cannot resolve "~" to your username.'
                             ' See "bzr help launchpad-login"')
        path = '~' + lp_login + path[1:]
    return path


def _update_url_scheme(url):
    # Do ubuntu: and debianlp: expansions.
    scheme, netloc, path, query, fragment = urlsplit(url)
    if scheme in ('ubuntu', 'debianlp'):
        if scheme == 'ubuntu':
            distro = 'ubuntu'
        elif scheme == 'debianlp':
            distro = 'debian'
            # No shortcuts for Debian distroseries.
        else:
            raise AssertionError('scheme should be ubuntu: or debianlp:')
        # Split the path.  It's either going to be 'project' or
        # 'series/project', but recognize that it may be a series we don't
        # know about.
        path_parts = path.split('/')
        if len(path_parts) == 1:
            # It's just a project name.
            lp_url_template = 'lp:%(distro)s/%(project)s'
            project = path_parts[0]
            series = None
        elif len(path_parts) == 2:
            # It's a series and project.
            lp_url_template = 'lp:%(distro)s/%(series)s/%(project)s'
            series, project = path_parts
        else:
            # There are either 0 or > 2 path parts, neither of which is
            # supported for these schemes.
            raise InvalidURL('Bad path: %s' % url)
        # Hack the url and let the following do the final resolution.
        url = lp_url_template % dict(
            distro=distro,
            series=series,
            project=project)
        scheme, netloc, path, query, fragment = urlsplit(url)
    elif scheme == 'lp+bzr':
        scheme = 'lp'
    return url, path


def _resolve_via_api(path, url, api_base_url=LPNET_SERVICE_ROOT):
    from .lp_api import connect_launchpad
    lp = connect_launchpad(api_base_url, version='devel')
    lp_branch = lp.branches.getByPath(path=path)
    if not lp_branch:
        raise InvalidURL("Unknown Launchpad path: %s" % path)
    return {'urls': [lp_branch.composePublicURL(scheme='bzr+ssh