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."""
20
from __future__ import absolute_import
27
from .hooks import Hooks
34
class LocationHooks(Hooks):
35
"""Dictionary mapping hook name to a list of callables for location hooks.
39
Hooks.__init__(self, "breezy.location", "hooks")
42
"Possibly rewrite a URL. Called with a URL to rewrite and the "
43
"purpose of the URL.", (3, 0))
46
hooks = LocationHooks()
48
def parse_rcp_location(location):
49
"""Convert a rcp-style location to a URL.
51
:param location: Location to convert, e.g. "foo:bar"
52
:param scheme: URL scheme to return, defaults to "ssh"
53
:return: A URL, e.g. "ssh://foo/bar"
54
:raises ValueError: if this is not a RCP-style URL
56
m = re.match('^(?P<user>[^@:/]+@)?(?P<host>[^/:]+):(?P<path>.*)$', location)
58
raise ValueError("Not a RCP URL")
59
if m.group('path').startswith('//'):
60
raise ValueError("Not a RCP URL: already looks like a URL")
61
return (m.group('host'),
62
m.group('user')[:-1] if m.group('user') else None,
66
def rcp_location_to_url(location, scheme='ssh'):
67
"""Convert a rcp-style location to a URL.
69
:param location: Location to convert, e.g. "foo:bar"
70
:param scheme: URL scheme to return, defaults to "ssh"
71
:return: A URL, e.g. "ssh://foo/bar"
72
:raises ValueError: if this is not a RCP-style URL
74
(host, user, path) = parse_rcp_location(location)
75
quoted_user = urlutils.quote(user) if user else None
77
scheme=scheme, quoted_user=quoted_user,
78
port=None, quoted_password=None,
79
quoted_host=urlutils.quote(host),
80
quoted_path=urlutils.quote(path))
84
def parse_cvs_location(location):
85
parts = location.split(':')
86
if parts[0] or parts[1] not in ('pserver', 'ssh'):
87
raise ValueError('not a valid pserver location string')
89
(username, hostname) = parts[2].split('@', 1)
95
return (scheme, hostname, username, path)
98
def cvs_to_url(location):
99
"""Convert a CVS pserver location string to a URL.
101
:param location: pserver URL
102
:return: A cvs+pserver URL
104
(scheme, host, user, path) = parse_cvs_location(location)
105
return str(urlutils.URL(
106
scheme='cvs+' + scheme,
107
quoted_user=urlutils.quote(user) if user else None,
108
quoted_host=urlutils.quote(host),
109
quoted_password=None,
111
quoted_path=urlutils.quote(path)))
114
def location_to_url(location, purpose=None):
115
"""Determine a fully qualified URL from a location string.
117
This will try to interpret location as both a URL and a directory path. It
118
will also lookup the location in directories.
120
:param location: Unicode or byte string object with a location
121
:param purpose: Intended method of access (None, 'read' or 'write')
122
:raise InvalidURL: If the location is already a URL, but not valid.
123
:return: Byte string with resulting URL
125
if not isinstance(location, string_types):
126
raise AssertionError("location not a byte or unicode string")
128
if location.startswith(':pserver:'):
129
return cvs_to_url(location)
131
from .directory_service import directories
132
location = directories.dereference(location, purpose)
134
# Catch any URLs which are passing Unicode rather than ASCII
136
location = location.encode('ascii')
138
if urlutils.is_url(location):
139
raise urlutils.InvalidURL(
140
path=location, extra='URLs must be properly escaped')
141
location = urlutils.local_path_to_url(location)
144
location = location.decode('ascii')
146
if location.startswith("file:") and not location.startswith("file://"):
147
return urlutils.join(urlutils.local_path_to_url("."), location[5:])
150
url = rcp_location_to_url(location, scheme="ssh")
156
if not urlutils.is_url(location):
157
return urlutils.local_path_to_url(location)
159
for hook in hooks['rewrite_url']:
160
location = hook(location, purpose=purpose)