Rev 2477: Add Transport.ensure_base() in http://bzr.arbash-meinel.com/branches/bzr/0.17-dev/single_connect_for_push_75721

John Arbash Meinel john at arbash-meinel.com
Wed May 2 15:37:09 BST 2007


At http://bzr.arbash-meinel.com/branches/bzr/0.17-dev/single_connect_for_push_75721

------------------------------------------------------------
revno: 2477
revision-id: john at arbash-meinel.com-20070502143655-id25373m3lgue8ke
parent: john at arbash-meinel.com-20070501224141-23intuz4dabm0j73
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: single_connect_for_push_75721
timestamp: Wed 2007-05-02 09:36:55 -0500
message:
  Add Transport.ensure_base()
modified:
  bzrlib/tests/test_transport_implementations.py test_transport_implementations.py-20051227111451-f97c5c7d5c49fce7
  bzrlib/transport/__init__.py   transport.py-20050711165921-4978aa7ce1285ad5
-------------- next part --------------
=== modified file 'bzrlib/tests/test_transport_implementations.py'
--- a/bzrlib/tests/test_transport_implementations.py	2007-04-23 05:03:44 +0000
+++ b/bzrlib/tests/test_transport_implementations.py	2007-05-02 14:36:55 +0000
@@ -61,6 +61,38 @@
         """Check that transport.get(relpath).read() == content."""
         self.assertEqualDiff(content, transport.get(relpath).read())
 
+    def test_ensure_base_missing(self):
+        """.ensure_base() should create the directory if it doesn't exist"""
+        t = self.get_transport()
+        t_a = t.clone('a')
+        if t_a.is_readonly():
+            self.assertRaises(TransportNotPossible,
+                              t_a.ensure_base)
+            return
+        self.assertTrue(t_a.ensure_base())
+        self.assertTrue(t.has('a'))
+
+    def test_ensure_base_exists(self):
+        """.ensure_base() should just be happy if it already exists"""
+        t = self.get_transport()
+        if t.is_readonly():
+            return
+
+        t.mkdir('a')
+        t_a = t.clone('a')
+        # ensure_base returns False if it didn't create the base
+        self.assertFalse(t_a.ensure_base())
+
+    def test_ensure_base_missing_parent(self):
+        """.ensure_base() will fail if the parent dir doesn't exist"""
+        t = self.get_transport()
+        if t.is_readonly():
+            return
+
+        t_a = t.clone('a')
+        t_b = t_a.clone('b')
+        self.assertRaises(NoSuchFile, t_b.ensure_base)
+
     def test_has(self):
         t = self.get_transport()
 

=== modified file 'bzrlib/transport/__init__.py'
--- a/bzrlib/transport/__init__.py	2007-04-26 09:07:38 +0000
+++ b/bzrlib/transport/__init__.py	2007-05-02 14:36:55 +0000
@@ -276,6 +276,22 @@
         """
         raise NotImplementedError(self.clone)
 
+    def ensure_base(self):
+        """Ensure that the directory this transport references exists.
+
+        This will create a directory if it doesn't exist.
+        :return: True if the directory was created, False otherwise.
+        """
+        # The default implementation just uses "Easier to ask for forgiveness
+        # than permission". We attempt to create the directory, and just
+        # suppress a FileExists exception.
+        try:
+            self.mkdir('.')
+        except errors.FileExists:
+            return False
+        else:
+            return True
+
     def should_cache(self):
         """Return True if the data pulled across should be cached locally.
         """



More information about the bazaar-commits mailing list