40
46
hooks = LocationHooks()
42
def parse_rcp_location(location):
49
def rcp_location_to_url(location, scheme='ssh'):
43
50
"""Convert a rcp-style location to a URL.
45
52
:param location: Location to convert, e.g. "foo:bar"
46
:param scheme: URL scheme to return, defaults to "ssh"
53
:param schenme: URL scheme to return, defaults to "ssh"
47
54
:return: A URL, e.g. "ssh://foo/bar"
48
55
:raises ValueError: if this is not a RCP-style URL
52
59
raise ValueError("Not a RCP URL")
53
60
if m.group('path').startswith('//'):
54
61
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
62
quoted_user = urlutils.quote(m.group('user')[:-1]) if m.group('user') else None
70
63
url = urlutils.URL(
71
64
scheme=scheme, quoted_user=quoted_user,
72
65
port=None, quoted_password=None,
73
quoted_host=urlutils.quote(host),
74
quoted_path=urlutils.quote(path))
66
quoted_host=urlutils.quote(m.group('host')),
67
quoted_path=urlutils.quote(m.group('path')))
78
def parse_cvs_location(location):
71
def pserver_to_url(location):
72
"""Convert a CVS pserver location string to a URL.
74
:param location: pserver URL
75
:return: A cvs+pserver URL
79
77
parts = location.split(':')
80
if parts[0] or parts[1] not in ('pserver', 'ssh'):
78
if parts[0] or parts[1] != 'pserver':
81
79
raise ValueError('not a valid pserver location string')
83
81
(username, hostname) = parts[2].split('@', 1)
85
83
hostname = parts[2]
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
85
return str(urlutils.URL(
100
scheme='cvs+' + scheme,
101
quoted_user=urlutils.quote(user) if user else None,
102
quoted_host=urlutils.quote(host),
87
quoted_user=urlutils.quote(username) if username else None,
88
quoted_host=urlutils.quote(hostname),
103
89
quoted_password=None,
105
quoted_path=urlutils.quote(path)))
91
quoted_path=urlutils.quote(parts[3])))
108
94
def location_to_url(location, purpose=None):
116
102
:raise InvalidURL: If the location is already a URL, but not valid.
117
103
:return: Byte string with resulting URL
119
if not isinstance(location, str):
105
if not isinstance(location, string_types):
120
106
raise AssertionError("location not a byte or unicode string")
122
108
if location.startswith(':pserver:'):
123
return cvs_to_url(location)
109
return pserver_to_url(location)
125
111
from .directory_service import directories
126
112
location = directories.dereference(location, purpose)