40
44
hooks = LocationHooks()
42
def parse_rcp_location(location):
43
"""Convert a rcp-style location to a URL.
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
50
m = re.match('^(?P<user>[^@:/]+@)?(?P<host>[^/:]+):(?P<path>.*)$', location)
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,
60
def rcp_location_to_url(location, scheme='ssh'):
61
"""Convert a rcp-style location to a URL.
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
68
(host, user, path) = parse_rcp_location(location)
69
quoted_user = urlutils.quote(user) if user else None
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))
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')
83
(username, hostname) = parts[2].split('@', 1)
89
return (scheme, hostname, username, path)
92
def cvs_to_url(location):
93
"""Convert a CVS pserver location string to a URL.
95
:param location: pserver URL
96
:return: A cvs+pserver URL
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,
105
quoted_path=urlutils.quote(path)))
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
119
if not isinstance(location, str):
58
if not isinstance(location, string_types):
120
59
raise AssertionError("location not a byte or unicode string")
122
if location.startswith(':pserver:'):
123
return cvs_to_url(location)
125
60
from .directory_service import directories
126
61
location = directories.dereference(location, purpose)
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)
137
location = location.decode('ascii')
73
location = location.decode('ascii')
139
75
if location.startswith("file:") and not location.startswith("file://"):
140
76
return urlutils.join(urlutils.local_path_to_url("."), location[5:])
143
url = rcp_location_to_url(location, scheme="ssh")
149
78
if not urlutils.is_url(location):
150
79
return urlutils.local_path_to_url(location)