浏览代码

Add ObjectStore.close.

Jelmer Vernooij 11 年之前
父节点
当前提交
7843dbb217
共有 3 个文件被更改,包括 18 次插入0 次删除
  1. 2 0
      NEWS
  2. 11 0
      dulwich/object_store.py
  3. 5 0
      dulwich/tests/test_object_store.py

+ 2 - 0
NEWS

@@ -34,6 +34,8 @@
   * Support running 'dulwich.server' and 'dulwich.web' using 'python -m'.
     (Jelmer Vernooij)
 
+  * Add ObjectStore.close(). (Jelmer Vernooij)
+
  API CHANGES
 
   * SSHVendor.connect_ssh has been renamed to SSHVendor.run_command.

+ 11 - 0
dulwich/object_store.py

@@ -247,6 +247,10 @@ class BaseObjectStore(object):
                 queue.extend(cmt.parents)
         return (commits, bases)
 
+    def close(self):
+        """Close any files opened by this object store."""
+        # Default implementation is a NO-OP
+
 
 class PackBasedObjectStore(BaseObjectStore):
 
@@ -293,6 +297,13 @@ class PackBasedObjectStore(BaseObjectStore):
         if self._pack_cache is not None:
             self._pack_cache.append(pack)
 
+    def close(self):
+        pack_cache = self._pack_cache
+        self._pack_cache = None
+        while pack_cache:
+            pack = pack_cache.pop()
+            pack.close()
+
     @property
     def packs(self):
         """List with pack objects."""

+ 5 - 0
dulwich/tests/test_object_store.py

@@ -188,6 +188,11 @@ class ObjectStoreTests(object):
         self.assertEqual((Blob.type_num, 'yummy data'),
                          self.store.get_raw(testobject.id))
 
+    def test_close(self):
+        # For now, just check that close doesn't barf.
+        self.store.add_object(testobject)
+        self.store.close()
+
 
 class MemoryObjectStoreTests(ObjectStoreTests, TestCase):