You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by no...@apache.org on 2020/12/29 12:25:18 UTC

[buildstream] 08/13: Move _remove_ref method to utils module

This is an automated email from the ASF dual-hosted git repository.

not-in-ldap pushed a commit to branch tpollard/prototemp
in repository https://gitbox.apache.org/repos/asf/buildstream.git

commit 080edf8967c85262ec713cc2e9a5ff340ca0b24f
Author: Raoul Hidalgo Charman <ra...@codethink.co.uk>
AuthorDate: Thu Apr 11 17:34:03 2019 +0100

    Move _remove_ref method to utils module
    
    Part of #974
---
 buildstream/_cas/cascache.py | 55 ++++----------------------------------------
 buildstream/utils.py         | 46 ++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 51 deletions(-)

diff --git a/buildstream/_cas/cascache.py b/buildstream/_cas/cascache.py
index 265ee58..10e8b3e 100644
--- a/buildstream/_cas/cascache.py
+++ b/buildstream/_cas/cascache.py
@@ -21,7 +21,6 @@ import hashlib
 import itertools
 import os
 import stat
-import errno
 import uuid
 import contextlib
 
@@ -568,7 +567,10 @@ class CASCache():
     def remove(self, ref, *, defer_prune=False):
 
         # Remove cache ref
-        self._remove_ref(ref)
+        try:
+            utils._remove_ref(os.path.join(self.casdir, 'refs', 'heads'), ref)
+        except FileNotFoundError:
+            raise CASCacheError("Could not find ref '{}'".format(ref))
 
         if not defer_prune:
             pruned = self.prune()
@@ -753,55 +755,6 @@ class CASCache():
     def _refpath(self, ref):
         return os.path.join(self.casdir, 'refs', 'heads', ref)
 
-    # _remove_ref()
-    #
-    # Removes a ref.
-    #
-    # This also takes care of pruning away directories which can
-    # be removed after having removed the given ref.
-    #
-    # Args:
-    #    ref (str): The ref to remove
-    #
-    # Raises:
-    #    (CASCacheError): If the ref didnt exist, or a system error
-    #                     occurred while removing it
-    #
-    def _remove_ref(self, ref):
-
-        # Remove the ref itself
-        refpath = self._refpath(ref)
-        try:
-            os.unlink(refpath)
-        except FileNotFoundError as e:
-            raise CASCacheError("Could not find ref '{}'".format(ref)) from e
-
-        # Now remove any leading directories
-        basedir = os.path.join(self.casdir, 'refs', 'heads')
-        components = list(os.path.split(ref))
-        while components:
-            components.pop()
-            refdir = os.path.join(basedir, *components)
-
-            # Break out once we reach the base
-            if refdir == basedir:
-                break
-
-            try:
-                os.rmdir(refdir)
-            except FileNotFoundError:
-                # The parent directory did not exist, but it's
-                # parent directory might still be ready to prune
-                pass
-            except OSError as e:
-                if e.errno == errno.ENOTEMPTY:
-                    # The parent directory was not empty, so we
-                    # cannot prune directories beyond this point
-                    break
-
-                # Something went wrong here
-                raise CASCacheError("System error while removing ref '{}': {}".format(ref, e)) from e
-
     # _commit_directory():
     #
     # Adds local directory to content addressable store.
diff --git a/buildstream/utils.py b/buildstream/utils.py
index 7d6db0b..f655286 100644
--- a/buildstream/utils.py
+++ b/buildstream/utils.py
@@ -1321,3 +1321,49 @@ def _list_directory(base_path, *, glob_expr=None):
             if not glob_expr or fnmatch(relative_path, glob_expr):
                 # Obtain the mtime (the time a file was last modified)
                 yield (os.path.getmtime(ref_path), relative_path)
+
+
+# remove_ref()
+#
+# Removes a ref
+#
+# This also takes care of pruning away directories which can
+# be removed after having removed the given ref.
+#
+# Args:
+#    basedir (str): Path of base directory the ref is in
+#    ref (str): The ref to remove
+#
+# Raises:
+#    (CASCacheError): If the ref didnt exist, or a system error
+#                     occurred while removing it
+#
+def _remove_ref(basedir, ref):
+    # Remove the ref itself
+    refpath = os.path.join(basedir, ref)
+    os.unlink(refpath)
+
+    # Now remove any leading directories
+    components = list(os.path.split(ref))
+    while components:
+        components.pop()
+        refdir = os.path.join(basedir, *components)
+
+        # Break out once we reach the base
+        if refdir == basedir:
+            break
+
+        try:
+            os.rmdir(refdir)
+        except FileNotFoundError:
+            # The parent directory did not exist, but it's
+            # parent directory might still be ready to prune
+            pass
+        except OSError as e:
+            if e.errno == errno.ENOTEMPTY:
+                # The parent directory was not empty, so we
+                # cannot prune directories beyond this point
+                break
+
+            # Something went wrong here
+            raise BstError("System error while removing ref '{}': {}".format(ref, e)) from e