/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/location.py

  • Committer: Jelmer Vernooij
  • Date: 2020-09-02 16:35:18 UTC
  • mto: (7490.40.109 work)
  • mto: This revision was merged to the branch mainline in revision 7526.
  • Revision ID: jelmer@jelmer.uk-20200902163518-sy9f4unbboljphgu
Handle duplicate directories entries for git.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006-2010 Canonical Ltd
 
2
# Copyright (C) 2018 Breezy Developers
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 
 
18
"""UI location string handling."""
 
19
 
 
20
from __future__ import absolute_import
 
21
 
 
22
import re
 
23
 
 
24
from . import (
 
25
    urlutils,
 
26
    )
 
27
from .hooks import Hooks
 
28
from .sixish import (
 
29
    PY3,
 
30
    string_types,
 
31
    )
 
32
 
 
33
 
 
34
class LocationHooks(Hooks):
 
35
    """Dictionary mapping hook name to a list of callables for location hooks.
 
36
    """
 
37
 
 
38
    def __init__(self):
 
39
        Hooks.__init__(self, "breezy.location", "hooks")
 
40
        self.add_hook(
 
41
            'rewrite_url',
 
42
            "Possibly rewrite a URL. Called with a URL to rewrite and the "
 
43
            "purpose of the URL.", (3, 0))
 
44
 
 
45
 
 
46
hooks = LocationHooks()
 
47
 
 
48
def parse_rcp_location(location):
 
49
    """Convert a rcp-style location to a URL.
 
50
 
 
51
    :param location: Location to convert, e.g. "foo:bar"
 
52
    :param scheme: URL scheme to return, defaults to "ssh"
 
53
    :return: A URL, e.g. "ssh://foo/bar"
 
54
    :raises ValueError: if this is not a RCP-style URL
 
55
    """
 
56
    m = re.match('^(?P<user>[^@:/]+@)?(?P<host>[^/:]+):(?P<path>.*)$', location)
 
57
    if not m:
 
58
        raise ValueError("Not a RCP URL")
 
59
    if m.group('path').startswith('//'):
 
60
        raise ValueError("Not a RCP URL: already looks like a URL")
 
61
    return (m.group('host'),
 
62
            m.group('user')[:-1] if m.group('user') else None,
 
63
            m.group('path'))
 
64
 
 
65
 
 
66
def rcp_location_to_url(location, scheme='ssh'):
 
67
    """Convert a rcp-style location to a URL.
 
68
 
 
69
    :param location: Location to convert, e.g. "foo:bar"
 
70
    :param scheme: URL scheme to return, defaults to "ssh"
 
71
    :return: A URL, e.g. "ssh://foo/bar"
 
72
    :raises ValueError: if this is not a RCP-style URL
 
73
    """
 
74
    (host, user, path) = parse_rcp_location(location)
 
75
    quoted_user = urlutils.quote(user) if user else None
 
76
    url = urlutils.URL(
 
77
        scheme=scheme, quoted_user=quoted_user,
 
78
        port=None, quoted_password=None,
 
79
        quoted_host=urlutils.quote(host),
 
80
        quoted_path=urlutils.quote(path))
 
81
    return str(url)
 
82
 
 
83
 
 
84
def parse_cvs_location(location):
 
85
    parts = location.split(':')
 
86
    if parts[0] or parts[1] not in ('pserver', 'ssh'):
 
87
        raise ValueError('not a valid pserver location string')
 
88
    try:
 
89
        (username, hostname) = parts[2].split('@', 1)
 
90
    except IndexError:
 
91
        hostname = parts[2]
 
92
        username = None
 
93
    scheme = parts[1]
 
94
    path = parts[3]
 
95
    return (scheme, hostname, username, path)
 
96
 
 
97
 
 
98
def cvs_to_url(location):
 
99
    """Convert a CVS pserver location string to a URL.
 
100
 
 
101
    :param location: pserver URL
 
102
    :return: A cvs+pserver URL
 
103
    """
 
104
    (scheme, host, user, path) = parse_cvs_location(location)
 
105
    return str(urlutils.URL(
 
106
        scheme='cvs+' + scheme,
 
107
        quoted_user=urlutils.quote(user) if user else None,
 
108
        quoted_host=urlutils.quote(host),
 
109
        quoted_password=None,
 
110
        port=None,
 
111
        quoted_path=urlutils.quote(path)))
 
112
 
 
113
 
 
114
def location_to_url(location, purpose=None):
 
115
    """Determine a fully qualified URL from a location string.
 
116
 
 
117
    This will try to interpret location as both a URL and a directory path. It
 
118
    will also lookup the location in directories.
 
119
 
 
120
    :param location: Unicode or byte string object with a location
 
121
    :param purpose: Intended method of access (None, 'read' or 'write')
 
122
    :raise InvalidURL: If the location is already a URL, but not valid.
 
123
    :return: Byte string with resulting URL
 
124
    """
 
125
    if not isinstance(location, string_types):
 
126
        raise AssertionError("location not a byte or unicode string")
 
127
 
 
128
    if location.startswith(':pserver:'):
 
129
        return cvs_to_url(location)
 
130
 
 
131
    from .directory_service import directories
 
132
    location = directories.dereference(location, purpose)
 
133
 
 
134
    # Catch any URLs which are passing Unicode rather than ASCII
 
135
    try:
 
136
        location = location.encode('ascii')
 
137
    except UnicodeError:
 
138
        if urlutils.is_url(location):
 
139
            raise urlutils.InvalidURL(
 
140
                path=location, extra='URLs must be properly escaped')
 
141
        location = urlutils.local_path_to_url(location)
 
142
    else:
 
143
        if PY3:
 
144
            location = location.decode('ascii')
 
145
 
 
146
    if location.startswith("file:") and not location.startswith("file://"):
 
147
        return urlutils.join(urlutils.local_path_to_url("."), location[5:])
 
148
 
 
149
    try:
 
150
        url = rcp_location_to_url(location, scheme="ssh")
 
151
    except ValueError:
 
152
        pass
 
153
    else:
 
154
        return url
 
155
 
 
156
    if not urlutils.is_url(location):
 
157
        return urlutils.local_path_to_url(location)
 
158
 
 
159
    for hook in hooks['rewrite_url']:
 
160
        location = hook(location, purpose=purpose)
 
161
 
 
162
    return location