1
# Copyright (C) 2006-2010 Canonical Ltd
2
# Copyright (C) 2018 Breezy Developers
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.
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.
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
18
"""UI location string handling."""
25
from .hooks import Hooks
28
class LocationHooks(Hooks):
29
"""Dictionary mapping hook name to a list of callables for location hooks.
33
Hooks.__init__(self, "breezy.location", "hooks")
36
"Possibly rewrite a URL. Called with a URL to rewrite and the "
37
"purpose of the URL.", (3, 0))
40
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
def location_to_url(location, purpose=None):
109
"""Determine a fully qualified URL from a location string.
111
This will try to interpret location as both a URL and a directory path. It
112
will also lookup the location in directories.
114
:param location: Unicode or byte string object with a location
115
:param purpose: Intended method of access (None, 'read' or 'write')
116
:raise InvalidURL: If the location is already a URL, but not valid.
117
:return: Byte string with resulting URL
119
if not isinstance(location, str):
120
raise AssertionError("location not a byte or unicode string")
122
if location.startswith(':pserver:'):
123
return cvs_to_url(location)
125
from .directory_service import directories
126
location = directories.dereference(location, purpose)
128
# Catch any URLs which are passing Unicode rather than ASCII
130
location = location.encode('ascii')
132
if urlutils.is_url(location):
133
raise urlutils.InvalidURL(
134
path=location, extra='URLs must be properly escaped')
135
location = urlutils.local_path_to_url(location)
137
location = location.decode('ascii')
139
if location.startswith("file:") and not location.startswith("file://"):
140
return urlutils.join(urlutils.local_path_to_url("."), location[5:])
143
url = rcp_location_to_url(location, scheme="ssh")
149
if not urlutils.is_url(location):
150
return urlutils.local_path_to_url(location)
152
for hook in hooks['rewrite_url']:
153
location = hook(location, purpose=purpose)