Merge lp:~jelmer/brz/location into lp:brz

Proposed by Jelmer Vernooij
Status: Merged
Approved by: Jelmer Vernooij
Approved revision: no longer in the source branch.
Merge reported by: The Breezy Bot
Merged at revision: not available
Proposed branch: lp:~jelmer/brz/location
Merge into: lp:brz
Diff against target: 316 lines (+145/-89)
6 files modified
breezy/builtins.py (+2/-2)
breezy/location.py (+64/-0)
breezy/tests/__init__.py (+1/-0)
breezy/tests/test_location.py (+75/-0)
breezy/tests/test_transport.py (+0/-50)
breezy/transport/__init__.py (+3/-37)
To merge this branch: bzr merge lp:~jelmer/brz/location
Reviewer Review Type Date Requested Status
Martin Packman Approve
Review via email: mp+359384@code.launchpad.net

Commit message

Move location_to_url to its own module.

Description of the change

Move location_to_url to its own module.

To post a comment you must log in.
Revision history for this message
Martin Packman (gz) wrote :

Yes, this makes sense, especially if we want to try using a fancier type here later.

review: Approve
Revision history for this message
The Breezy Bot (the-breezy-bot) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'breezy/builtins.py'
2--- breezy/builtins.py 2018-11-21 03:20:30 +0000
3+++ breezy/builtins.py 2018-12-10 01:47:19 +0000
4@@ -5625,12 +5625,12 @@
5
6 def run(self, listen=None, port=None, inet=False, directory=None,
7 allow_writes=False, protocol=None, client_timeout=None):
8- from . import transport
9+ from . import location, transport
10 if directory is None:
11 directory = osutils.getcwd()
12 if protocol is None:
13 protocol = transport.transport_server_registry.get()
14- url = transport.location_to_url(directory)
15+ url = location.location_to_url(directory)
16 if not allow_writes:
17 url = 'readonly+' + url
18 t = transport.get_transport_from_url(url)
19
20=== added file 'breezy/location.py'
21--- breezy/location.py 1970-01-01 00:00:00 +0000
22+++ breezy/location.py 2018-12-10 01:47:19 +0000
23@@ -0,0 +1,64 @@
24+# Copyright (C) 2006-2010 Canonical Ltd
25+# Copyright (C) 2018 Breezy Developers
26+#
27+# This program is free software; you can redistribute it and/or modify
28+# it under the terms of the GNU General Public License as published by
29+# the Free Software Foundation; either version 2 of the License, or
30+# (at your option) any later version.
31+#
32+# This program is distributed in the hope that it will be useful,
33+# but WITHOUT ANY WARRANTY; without even the implied warranty of
34+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35+# GNU General Public License for more details.
36+#
37+# You should have received a copy of the GNU General Public License
38+# along with this program; if not, write to the Free Software
39+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
40+
41+"""UI location string handling."""
42+
43+from __future__ import absolute_import
44+
45+from . import (
46+ urlutils,
47+ )
48+from .sixish import (
49+ PY3,
50+ string_types,
51+ )
52+
53+
54+def location_to_url(location):
55+ """Determine a fully qualified URL from a location string.
56+
57+ This will try to interpret location as both a URL and a directory path. It
58+ will also lookup the location in directories.
59+
60+ :param location: Unicode or byte string object with a location
61+ :raise InvalidURL: If the location is already a URL, but not valid.
62+ :return: Byte string with resulting URL
63+ """
64+ if not isinstance(location, string_types):
65+ raise AssertionError("location not a byte or unicode string")
66+ from .directory_service import directories
67+ location = directories.dereference(location)
68+
69+ # Catch any URLs which are passing Unicode rather than ASCII
70+ try:
71+ location = location.encode('ascii')
72+ except UnicodeError:
73+ if urlutils.is_url(location):
74+ raise urlutils.InvalidURL(path=location,
75+ extra='URLs must be properly escaped')
76+ location = urlutils.local_path_to_url(location)
77+ else:
78+ if PY3:
79+ location = location.decode('ascii')
80+
81+ if location.startswith("file:") and not location.startswith("file://"):
82+ return urlutils.join(urlutils.local_path_to_url("."), location[5:])
83+
84+ if not urlutils.is_url(location):
85+ return urlutils.local_path_to_url(location)
86+
87+ return location
88
89=== modified file 'breezy/tests/__init__.py'
90--- breezy/tests/__init__.py 2018-11-17 16:53:10 +0000
91+++ breezy/tests/__init__.py 2018-12-10 01:47:19 +0000
92@@ -4127,6 +4127,7 @@
93 'breezy.tests.test_lazy_import',
94 'breezy.tests.test_lazy_regex',
95 'breezy.tests.test_library_state',
96+ 'breezy.tests.test_location',
97 'breezy.tests.test_lock',
98 'breezy.tests.test_lockable_files',
99 'breezy.tests.test_lockdir',
100
101=== added file 'breezy/tests/test_location.py'
102--- breezy/tests/test_location.py 1970-01-01 00:00:00 +0000
103+++ breezy/tests/test_location.py 2018-12-10 01:47:19 +0000
104@@ -0,0 +1,75 @@
105+# Copyright (C) 2005-2011, 2015, 2016 Canonical Ltd
106+#
107+# This program is free software; you can redistribute it and/or modify
108+# it under the terms of the GNU General Public License as published by
109+# the Free Software Foundation; either version 2 of the License, or
110+# (at your option) any later version.
111+#
112+# This program is distributed in the hope that it will be useful,
113+# but WITHOUT ANY WARRANTY; without even the implied warranty of
114+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
115+# GNU General Public License for more details.
116+#
117+# You should have received a copy of the GNU General Public License
118+# along with this program; if not, write to the Free Software
119+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
120+
121+"""Tests for breezy.location."""
122+
123+from .. import (
124+ osutils,
125+ tests,
126+ urlutils,
127+ )
128+from ..directory_service import directories
129+from ..location import (
130+ location_to_url,
131+ )
132+
133+
134+class SomeDirectory(object):
135+
136+ def look_up(self, name, url):
137+ return "http://bar"
138+
139+
140+class TestLocationToUrl(tests.TestCase):
141+
142+ def get_base_location(self):
143+ path = osutils.abspath('/foo/bar')
144+ if path.startswith('/'):
145+ url = 'file://%s' % (path,)
146+ else:
147+ # On Windows, abspaths start with the drive letter, so we have to
148+ # add in the extra '/'
149+ url = 'file:///%s' % (path,)
150+ return path, url
151+
152+ def test_regular_url(self):
153+ self.assertEqual("file://foo", location_to_url("file://foo"))
154+
155+ def test_directory(self):
156+ directories.register("bar:", SomeDirectory, "Dummy directory")
157+ self.addCleanup(directories.remove, "bar:")
158+ self.assertEqual("http://bar", location_to_url("bar:"))
159+
160+ def test_unicode_url(self):
161+ self.assertRaises(urlutils.InvalidURL, location_to_url,
162+ b"http://fo/\xc3\xaf".decode("utf-8"))
163+
164+ def test_unicode_path(self):
165+ path, url = self.get_base_location()
166+ location = path + b"\xc3\xaf".decode("utf-8")
167+ url += '%C3%AF'
168+ self.assertEqual(url, location_to_url(location))
169+
170+ def test_path(self):
171+ path, url = self.get_base_location()
172+ self.assertEqual(url, location_to_url(path))
173+
174+ def test_relative_file_url(self):
175+ self.assertEqual(urlutils.local_path_to_url(".") + "/bar",
176+ location_to_url("file:bar"))
177+
178+ def test_absolute_file_url(self):
179+ self.assertEqual("file:///bar", location_to_url("file:/bar"))
180
181=== modified file 'breezy/tests/test_transport.py'
182--- breezy/tests/test_transport.py 2018-11-12 01:41:38 +0000
183+++ breezy/tests/test_transport.py 2018-12-10 01:47:19 +0000
184@@ -28,7 +28,6 @@
185 transport,
186 urlutils,
187 )
188-from ..directory_service import directories
189 from ..sixish import (
190 BytesIO,
191 )
192@@ -37,7 +36,6 @@
193 fakenfs,
194 http,
195 local,
196- location_to_url,
197 memory,
198 pathfilter,
199 readonly,
200@@ -1107,51 +1105,3 @@
201 result = http.unhtml_roughly(fake_html)
202 self.assertEqual(len(result), 1000)
203 self.assertStartsWith(result, " something!")
204-
205-
206-class SomeDirectory(object):
207-
208- def look_up(self, name, url):
209- return "http://bar"
210-
211-
212-class TestLocationToUrl(tests.TestCase):
213-
214- def get_base_location(self):
215- path = osutils.abspath('/foo/bar')
216- if path.startswith('/'):
217- url = 'file://%s' % (path,)
218- else:
219- # On Windows, abspaths start with the drive letter, so we have to
220- # add in the extra '/'
221- url = 'file:///%s' % (path,)
222- return path, url
223-
224- def test_regular_url(self):
225- self.assertEqual("file://foo", location_to_url("file://foo"))
226-
227- def test_directory(self):
228- directories.register("bar:", SomeDirectory, "Dummy directory")
229- self.addCleanup(directories.remove, "bar:")
230- self.assertEqual("http://bar", location_to_url("bar:"))
231-
232- def test_unicode_url(self):
233- self.assertRaises(urlutils.InvalidURL, location_to_url,
234- b"http://fo/\xc3\xaf".decode("utf-8"))
235-
236- def test_unicode_path(self):
237- path, url = self.get_base_location()
238- location = path + b"\xc3\xaf".decode("utf-8")
239- url += '%C3%AF'
240- self.assertEqual(url, location_to_url(location))
241-
242- def test_path(self):
243- path, url = self.get_base_location()
244- self.assertEqual(url, location_to_url(path))
245-
246- def test_relative_file_url(self):
247- self.assertEqual(urlutils.local_path_to_url(".") + "/bar",
248- location_to_url("file:bar"))
249-
250- def test_absolute_file_url(self):
251- self.assertEqual("file:///bar", location_to_url("file:/bar"))
252
253=== modified file 'breezy/transport/__init__.py'
254--- breezy/transport/__init__.py 2018-11-18 19:48:57 +0000
255+++ breezy/transport/__init__.py 2018-12-10 01:47:19 +0000
256@@ -37,6 +37,7 @@
257
258 from breezy import (
259 errors,
260+ location as _mod_location,
261 osutils,
262 ui,
263 urlutils,
264@@ -1507,42 +1508,6 @@
265 raise NotImplementedError(self.disconnect)
266
267
268-def location_to_url(location):
269- """Determine a fully qualified URL from a location string.
270-
271- This will try to interpret location as both a URL and a directory path. It
272- will also lookup the location in directories.
273-
274- :param location: Unicode or byte string object with a location
275- :raise InvalidURL: If the location is already a URL, but not valid.
276- :return: Byte string with resulting URL
277- """
278- if not isinstance(location, string_types):
279- raise AssertionError("location not a byte or unicode string")
280- from breezy.directory_service import directories
281- location = directories.dereference(location)
282-
283- # Catch any URLs which are passing Unicode rather than ASCII
284- try:
285- location = location.encode('ascii')
286- except UnicodeError:
287- if urlutils.is_url(location):
288- raise urlutils.InvalidURL(path=location,
289- extra='URLs must be properly escaped')
290- location = urlutils.local_path_to_url(location)
291- else:
292- if PY3:
293- location = location.decode('ascii')
294-
295- if location.startswith("file:") and not location.startswith("file://"):
296- return urlutils.join(urlutils.local_path_to_url("."), location[5:])
297-
298- if not urlutils.is_url(location):
299- return urlutils.local_path_to_url(location)
300-
301- return location
302-
303-
304 def get_transport_from_path(path, possible_transports=None):
305 """Open a transport for a local path.
306
307@@ -1601,7 +1566,8 @@
308 """
309 if base is None:
310 base = '.'
311- return get_transport_from_url(location_to_url(base), possible_transports)
312+ return get_transport_from_url(
313+ _mod_location.location_to_url(base), possible_transports)
314
315
316 def _try_transport_factories(base, factory_list):

Subscribers

People subscribed via source and target branches