/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: 2019-03-05 07:32:38 UTC
  • mto: (7290.1.21 work)
  • mto: This revision was merged to the branch mainline in revision 7311.
  • Revision ID: jelmer@jelmer.uk-20190305073238-zlqn981opwnqsmzi
Add appveyor configuration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""UI location string handling."""
19
19
 
20
 
import re
 
20
from __future__ import absolute_import
21
21
 
22
22
from . import (
23
23
    urlutils,
24
24
    )
25
25
from .hooks import Hooks
 
26
from .sixish import (
 
27
    PY3,
 
28
    string_types,
 
29
    )
26
30
 
27
31
 
28
32
class LocationHooks(Hooks):
39
43
 
40
44
hooks = LocationHooks()
41
45
 
42
 
def parse_rcp_location(location):
43
 
    """Convert a rcp-style location to a URL.
44
 
 
45
 
    :param location: Location to convert, e.g. "foo:bar"
46
 
    :param scheme: URL scheme to return, defaults to "ssh"
47
 
    :return: A URL, e.g. "ssh://foo/bar"
48
 
    :raises ValueError: if this is not a RCP-style URL
49
 
    """
50
 
    m = re.match('^(?P<user>[^@:/]+@)?(?P<host>[^/:]+):(?P<path>.*)$', location)
51
 
    if not m:
52
 
        raise ValueError("Not a RCP URL")
53
 
    if m.group('path').startswith('//'):
54
 
        raise ValueError("Not a RCP URL: already looks like a URL")
55
 
    return (m.group('host'),
56
 
            m.group('user')[:-1] if m.group('user') else None,
57
 
            m.group('path'))
58
 
 
59
 
 
60
 
def rcp_location_to_url(location, scheme='ssh'):
61
 
    """Convert a rcp-style location to a URL.
62
 
 
63
 
    :param location: Location to convert, e.g. "foo:bar"
64
 
    :param scheme: URL scheme to return, defaults to "ssh"
65
 
    :return: A URL, e.g. "ssh://foo/bar"
66
 
    :raises ValueError: if this is not a RCP-style URL
67
 
    """
68
 
    (host, user, path) = parse_rcp_location(location)
69
 
    quoted_user = urlutils.quote(user) if user else None
70
 
    url = urlutils.URL(
71
 
        scheme=scheme, quoted_user=quoted_user,
72
 
        port=None, quoted_password=None,
73
 
        quoted_host=urlutils.quote(host),
74
 
        quoted_path=urlutils.quote(path))
75
 
    return str(url)
76
 
 
77
 
 
78
 
def parse_cvs_location(location):
79
 
    parts = location.split(':')
80
 
    if parts[0] or parts[1] not in ('pserver', 'ssh'):
81
 
        raise ValueError('not a valid pserver location string')
82
 
    try:
83
 
        (username, hostname) = parts[2].split('@', 1)
84
 
    except IndexError:
85
 
        hostname = parts[2]
86
 
        username = None
87
 
    scheme = parts[1]
88
 
    path = parts[3]
89
 
    return (scheme, hostname, username, path)
90
 
 
91
 
 
92
 
def cvs_to_url(location):
93
 
    """Convert a CVS pserver location string to a URL.
94
 
 
95
 
    :param location: pserver URL
96
 
    :return: A cvs+pserver URL
97
 
    """
98
 
    (scheme, host, user, path) = parse_cvs_location(location)
99
 
    return str(urlutils.URL(
100
 
        scheme='cvs+' + scheme,
101
 
        quoted_user=urlutils.quote(user) if user else None,
102
 
        quoted_host=urlutils.quote(host),
103
 
        quoted_password=None,
104
 
        port=None,
105
 
        quoted_path=urlutils.quote(path)))
106
 
 
107
46
 
108
47
def location_to_url(location, purpose=None):
109
48
    """Determine a fully qualified URL from a location string.
116
55
    :raise InvalidURL: If the location is already a URL, but not valid.
117
56
    :return: Byte string with resulting URL
118
57
    """
119
 
    if not isinstance(location, str):
 
58
    if not isinstance(location, string_types):
120
59
        raise AssertionError("location not a byte or unicode string")
121
 
 
122
 
    if location.startswith(':pserver:'):
123
 
        return cvs_to_url(location)
124
 
 
125
60
    from .directory_service import directories
126
61
    location = directories.dereference(location, purpose)
127
62
 
130
65
        location = location.encode('ascii')
131
66
    except UnicodeError:
132
67
        if urlutils.is_url(location):
133
 
            raise urlutils.InvalidURL(
134
 
                path=location, extra='URLs must be properly escaped')
 
68
            raise urlutils.InvalidURL(path=location,
 
69
                                      extra='URLs must be properly escaped')
135
70
        location = urlutils.local_path_to_url(location)
136
71
    else:
137
 
        location = location.decode('ascii')
 
72
        if PY3:
 
73
            location = location.decode('ascii')
138
74
 
139
75
    if location.startswith("file:") and not location.startswith("file://"):
140
76
        return urlutils.join(urlutils.local_path_to_url("."), location[5:])
141
77
 
142
 
    try:
143
 
        url = rcp_location_to_url(location, scheme="ssh")
144
 
    except ValueError:
145
 
        pass
146
 
    else:
147
 
        return url
148
 
 
149
78
    if not urlutils.is_url(location):
150
79
        return urlutils.local_path_to_url(location)
151
80