Rev 2481: Naive implementation of transport reuse by Transport.get_transport(). in file:///v/home/vila/src/bugs/111702/

Vincent Ladeuil v.ladeuil+lp at free.fr
Thu May 3 10:59:49 BST 2007


At file:///v/home/vila/src/bugs/111702/

------------------------------------------------------------
revno: 2481
revision-id: v.ladeuil+lp at free.fr-20070503095947-k2o8rnp5ug6uriu1
parent: v.ladeuil+lp at free.fr-20070502203101-prcuwowzkz5qh0gn
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: 111702
timestamp: Thu 2007-05-03 11:59:47 +0200
message:
  Naive implementation of transport reuse by Transport.get_transport().
  
  * transport/__init__.py:
  (get_transport): Accept a list of reusable transports as a
  parameter.
  
  * tests/test_transport.py:
  (TestReusedTransports): Some basic tests.
modified:
  bzrlib/tests/test_transport.py testtransport.py-20050718175618-e5cdb99f4555ddce
  bzrlib/transport/__init__.py   transport.py-20050711165921-4978aa7ce1285ad5
-------------- next part --------------
=== modified file 'bzrlib/tests/test_transport.py'
--- a/bzrlib/tests/test_transport.py	2007-04-26 09:07:38 +0000
+++ b/bzrlib/tests/test_transport.py	2007-05-03 09:59:47 +0000
@@ -592,3 +592,16 @@
         # make sure we reach the root
         t = t.clone('..')
         self.assertEquals(t.base, 'file://HOST/')
+
+
+class TestReusedTransports(TestCase):
+
+    def test_reuse_same_transport(self):
+        t = get_transport('http://foo/')
+        t2 = get_transport('http://foo/', transports=[t])
+        self.assertIs(t, t2)
+
+    def test_don_t_reuse_different_transport(self):
+        t = get_transport('http://foo/')
+        t2 = get_transport('http://bar/', transports=[t])
+        self.assertIsNot(t, t2)

=== modified file 'bzrlib/transport/__init__.py'
--- a/bzrlib/transport/__init__.py	2007-05-02 14:36:55 +0000
+++ b/bzrlib/transport/__init__.py	2007-05-03 09:59:47 +0000
@@ -1038,13 +1038,12 @@
 urlunescape = urlutils.unescape
 _urlRE = re.compile(r'^(?P<proto>[^:/\\]+)://(?P<path>.*)$')
 
-
-def get_transport(base):
+def get_transport(base, transports=[]):
     """Open a transport to access a URL or directory.
 
-    base is either a URL or a directory name.  
+    :param base: either a URL or a directory name.
+    :param transports: optional reusable transport list.
     """
-
     if base is None:
         base = '.'
     last_err = None
@@ -1067,21 +1066,34 @@
         # Only local paths can be Unicode
         base = convert_path_to_url(base,
             'URLs must be properly escaped (protocol: %s)')
-    
-    for proto, factory_list in transport_list_registry.iteritems():
-        if proto is not None and base.startswith(proto):
-            t, last_err = _try_transport_factories(base, factory_list)
-            if t:
-                return t
-
-    # We tried all the different protocols, now try one last time
-    # as a local protocol
-    base = convert_path_to_url(base, 'Unsupported protocol: %s')
-
-    # The default handler is the filesystem handler, stored as protocol None
-    return _try_transport_factories(base,
-                    transport_list_registry.get(None))[0]
-                                                   
+
+    transport = None
+    for t in transports:
+        if t.base == base:
+            transport = t
+            break
+
+    if transport is None:
+        for proto, factory_list in transport_list_registry.iteritems():
+            if proto is not None and base.startswith(proto):
+                transport, last_err = _try_transport_factories(base,
+                                                               factory_list)
+                if transport:
+                    break
+
+    if transport is None:
+        # We tried all the different protocols, now try one last
+        # time as a local protocol
+        base = convert_path_to_url(base, 'Unsupported protocol: %s')
+
+        # The default handler is the filesystem handler, stored
+        # as protocol None
+        factory_list = transport_list_registry.get(None)
+        transport, last_err = _try_transport_factories(base, factory_list)
+
+    return transport
+
+
 def do_catching_redirections(action, transport, redirected):
     """Execute an action with given transport catching redirections.
 



More information about the bazaar-commits mailing list