]> arthur.barton.de Git - bup.git/commitdiff
repo: make repository classes closeable and close client for remotes
authorRob Browning <rlb@defaultvalue.org>
Sat, 31 Mar 2018 20:10:45 +0000 (15:10 -0500)
committerRob Browning <rlb@defaultvalue.org>
Sat, 31 Mar 2018 22:41:41 +0000 (17:41 -0500)
Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
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