]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/repo.py
repo: make repository classes closeable and close client for remotes
[bup.git] / lib / bup / repo.py
index 189192fbef86fe92da5493c7e0ef8166995d97e3..97547d539e481b776e915b05a2efe1d5e9929802 100644 (file)
@@ -11,6 +11,18 @@ class LocalRepo:
         self._cp = git.cp(self.repo_dir)
         self.rev_list = partial(git.rev_list, repo_dir=self.repo_dir)
 
+    def close(self):
+        pass
+
+    def __del__(self):
+        self.close()
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, type, value, traceback):
+        self.close()
+
     def cat(self, ref):
         """If ref does not exist, yield (None, None, None).  Otherwise yield
         (oidx, type, size), and then all of the data associated with
@@ -41,6 +53,20 @@ class RemoteRepo:
         self.client = client.Client(address)
         self.rev_list = self.client.rev_list
 
+    def close(self):
+        if self.client:
+            self.client.close()
+            self.client = None
+
+    def __del__(self):
+        self.close()
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, type, value, traceback):
+        self.close()
+
     def cat(self, ref):
         """If ref does not exist, yield (None, None, None).  Otherwise yield
         (oidx, type, size), and then all of the data associated with