bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
7204.2.1
by Jelmer Vernooij
Move location_to_url to its own module. |
1 |
# Copyright (C) 2006-2010 Canonical Ltd
|
2 |
# Copyright (C) 2018 Breezy Developers
|
|
3 |
#
|
|
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.
|
|
8 |
#
|
|
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.
|
|
13 |
#
|
|
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
|
|
17 |
||
18 |
"""UI location string handling."""
|
|
19 |
||
20 |
from __future__ import absolute_import |
|
21 |
||
|
7265.6.2
by Jelmer Vernooij
Support rsync-style URLs. |
22 |
import re |
23 |
||
|
7204.2.1
by Jelmer Vernooij
Move location_to_url to its own module. |
24 |
from . import ( |
25 |
urlutils, |
|
26 |
)
|
|
|
7268.11.1
by Jelmer Vernooij
Add rewrite_url hook. |
27 |
from .hooks import Hooks |
|
7204.2.1
by Jelmer Vernooij
Move location_to_url to its own module. |
28 |
from .sixish import ( |
29 |
PY3, |
|
30 |
string_types, |
|
31 |
)
|
|
32 |
||
33 |
||
|
7268.11.1
by Jelmer Vernooij
Add rewrite_url hook. |
34 |
class LocationHooks(Hooks): |
35 |
"""Dictionary mapping hook name to a list of callables for location hooks. |
|
36 |
"""
|
|
37 |
||
38 |
def __init__(self): |
|
39 |
Hooks.__init__(self, "breezy.location", "hooks") |
|
40 |
self.add_hook( |
|
41 |
'rewrite_url', |
|
|
7268.11.2
by Jelmer Vernooij
Add purpose argument. |
42 |
"Possibly rewrite a URL. Called with a URL to rewrite and the "
|
43 |
"purpose of the URL.", (3, 0)) |
|
|
7268.11.1
by Jelmer Vernooij
Add rewrite_url hook. |
44 |
|
45 |
||
46 |
hooks = LocationHooks() |
|
47 |
||
48 |
||
|
7265.6.2
by Jelmer Vernooij
Support rsync-style URLs. |
49 |
def rcp_location_to_url(location, scheme='ssh'): |
50 |
"""Convert a rcp-style location to a URL. |
|
51 |
||
52 |
:param location: Location to convert, e.g. "foo:bar"
|
|
53 |
:param schenme: URL scheme to return, defaults to "ssh"
|
|
54 |
:return: A URL, e.g. "ssh://foo/bar"
|
|
55 |
:raises ValueError: if this is not a RCP-style URL
|
|
56 |
"""
|
|
57 |
m = re.match('^(?P<user>[^@:/]+@)?(?P<host>[^/:]+):(?P<path>.*)$', location) |
|
58 |
if not m: |
|
59 |
raise ValueError("Not a RCP URL") |
|
60 |
if m.group('path').startswith('//'): |
|
61 |
raise ValueError("Not a RCP URL: already looks like a URL") |
|
62 |
quoted_user = urlutils.quote(m.group('user')[:-1]) if m.group('user') else None |
|
63 |
url = urlutils.URL( |
|
64 |
scheme=scheme, quoted_user=quoted_user, |
|
65 |
port=None, quoted_password=None, |
|
66 |
quoted_host=urlutils.quote(m.group('host')), |
|
67 |
quoted_path=urlutils.quote(m.group('path'))) |
|
68 |
return str(url) |
|
69 |
||
70 |
||
|
7413.5.1
by Jelmer Vernooij
CVS pserver URLs indicate that the pserver protocol is not supported. |
71 |
def pserver_to_url(location): |
72 |
"""Convert a CVS pserver location string to a URL. |
|
73 |
||
74 |
:param location: pserver URL
|
|
75 |
:return: A cvs+pserver URL
|
|
76 |
"""
|
|
77 |
parts = location.split(':') |
|
78 |
if parts[0] or parts[1] != 'pserver': |
|
79 |
raise ValueError('not a valid pserver location string') |
|
80 |
try: |
|
81 |
(username, hostname) = parts[2].split('@', 1) |
|
82 |
except IndexError: |
|
83 |
hostname = parts[2] |
|
84 |
username = None |
|
85 |
return str(urlutils.URL( |
|
86 |
scheme='cvs+pserver', |
|
87 |
quoted_user=urlutils.quote(username) if username else None, |
|
88 |
quoted_host=urlutils.quote(hostname), |
|
89 |
quoted_password=None, |
|
90 |
port=None, |
|
91 |
quoted_path=urlutils.quote(parts[3]))) |
|
92 |
||
93 |
||
|
7268.11.2
by Jelmer Vernooij
Add purpose argument. |
94 |
def location_to_url(location, purpose=None): |
|
7204.2.1
by Jelmer Vernooij
Move location_to_url to its own module. |
95 |
"""Determine a fully qualified URL from a location string. |
96 |
||
97 |
This will try to interpret location as both a URL and a directory path. It
|
|
98 |
will also lookup the location in directories.
|
|
99 |
||
100 |
:param location: Unicode or byte string object with a location
|
|
|
7268.11.4
by Jelmer Vernooij
Actually pass in purpose in a couple of places. |
101 |
:param purpose: Intended method of access (None, 'read' or 'write')
|
|
7204.2.1
by Jelmer Vernooij
Move location_to_url to its own module. |
102 |
:raise InvalidURL: If the location is already a URL, but not valid.
|
103 |
:return: Byte string with resulting URL
|
|
104 |
"""
|
|
105 |
if not isinstance(location, string_types): |
|
106 |
raise AssertionError("location not a byte or unicode string") |
|
|
7413.5.1
by Jelmer Vernooij
CVS pserver URLs indicate that the pserver protocol is not supported. |
107 |
|
108 |
if location.startswith(':pserver:'): |
|
109 |
return pserver_to_url(location) |
|
110 |
||
|
7204.2.1
by Jelmer Vernooij
Move location_to_url to its own module. |
111 |
from .directory_service import directories |
|
7268.11.2
by Jelmer Vernooij
Add purpose argument. |
112 |
location = directories.dereference(location, purpose) |
|
7204.2.1
by Jelmer Vernooij
Move location_to_url to its own module. |
113 |
|
114 |
# Catch any URLs which are passing Unicode rather than ASCII
|
|
115 |
try: |
|
116 |
location = location.encode('ascii') |
|
117 |
except UnicodeError: |
|
118 |
if urlutils.is_url(location): |
|
|
7265.6.1
by Jelmer Vernooij
Reformatting. |
119 |
raise urlutils.InvalidURL( |
120 |
path=location, extra='URLs must be properly escaped') |
|
|
7204.2.1
by Jelmer Vernooij
Move location_to_url to its own module. |
121 |
location = urlutils.local_path_to_url(location) |
122 |
else: |
|
123 |
if PY3: |
|
124 |
location = location.decode('ascii') |
|
125 |
||
126 |
if location.startswith("file:") and not location.startswith("file://"): |
|
127 |
return urlutils.join(urlutils.local_path_to_url("."), location[5:]) |
|
128 |
||
|
7265.6.2
by Jelmer Vernooij
Support rsync-style URLs. |
129 |
try: |
130 |
url = rcp_location_to_url(location, scheme="ssh") |
|
131 |
except ValueError: |
|
132 |
pass
|
|
133 |
else: |
|
134 |
return url |
|
135 |
||
|
7204.2.1
by Jelmer Vernooij
Move location_to_url to its own module. |
136 |
if not urlutils.is_url(location): |
137 |
return urlutils.local_path_to_url(location) |
|
138 |
||
|
7268.11.1
by Jelmer Vernooij
Add rewrite_url hook. |
139 |
for hook in hooks['rewrite_url']: |
|
7268.11.2
by Jelmer Vernooij
Add purpose argument. |
140 |
location = hook(location, purpose=purpose) |
|
7268.11.1
by Jelmer Vernooij
Add rewrite_url hook. |
141 |
|
|
7204.2.1
by Jelmer Vernooij
Move location_to_url to its own module. |
142 |
return location |