40
40
hooks = LocationHooks()
43
def rcp_location_to_url(location, scheme='ssh'):
42
def parse_rcp_location(location):
44
43
"""Convert a rcp-style location to a URL.
46
45
:param location: Location to convert, e.g. "foo:bar"
47
:param schenme: URL scheme to return, defaults to "ssh"
46
:param scheme: URL scheme to return, defaults to "ssh"
48
47
:return: A URL, e.g. "ssh://foo/bar"
49
48
:raises ValueError: if this is not a RCP-style URL
53
52
raise ValueError("Not a RCP URL")
54
53
if m.group('path').startswith('//'):
55
54
raise ValueError("Not a RCP URL: already looks like a URL")
56
quoted_user = urlutils.quote(m.group('user')[:-1]) if m.group('user') else None
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
57
70
url = urlutils.URL(
58
71
scheme=scheme, quoted_user=quoted_user,
59
72
port=None, quoted_password=None,
60
quoted_host=urlutils.quote(m.group('host')),
61
quoted_path=urlutils.quote(m.group('path')))
73
quoted_host=urlutils.quote(host),
74
quoted_path=urlutils.quote(path))
65
def pserver_to_url(location):
66
"""Convert a CVS pserver location string to a URL.
68
:param location: pserver URL
69
:return: A cvs+pserver URL
78
def parse_cvs_location(location):
71
79
parts = location.split(':')
72
if parts[0] or parts[1] != 'pserver':
73
raise ValueError('not a valid pserver location string')
80
if parts[0] or parts[1] not in ('pserver', 'ssh', 'extssh'):
81
raise ValueError('not a valid CVS location string')
75
83
(username, hostname) = parts[2].split('@', 1)
77
85
hostname = parts[2]
88
if scheme == 'extssh':
91
return (scheme, hostname, username, path)
94
def cvs_to_url(location):
95
"""Convert a CVS pserver location string to a URL.
97
:param location: pserver URL
98
:return: A cvs+pserver URL
100
(scheme, host, user, path) = parse_cvs_location(location)
79
101
return str(urlutils.URL(
81
quoted_user=urlutils.quote(username) if username else None,
82
quoted_host=urlutils.quote(hostname),
102
scheme='cvs+' + scheme,
103
quoted_user=urlutils.quote(user) if user else None,
104
quoted_host=urlutils.quote(host),
83
105
quoted_password=None,
85
quoted_path=urlutils.quote(parts[3])))
107
quoted_path=urlutils.quote(path)))
88
110
def location_to_url(location, purpose=None):
99
121
if not isinstance(location, str):
100
122
raise AssertionError("location not a byte or unicode string")
102
if location.startswith(':pserver:'):
103
return pserver_to_url(location)
124
if location.startswith(':pserver:') or location.startswith(':extssh:'):
125
return cvs_to_url(location)
105
127
from .directory_service import directories
106
128
location = directories.dereference(location, purpose)