diff options
author | Raoul Hidalgo Charman <raoul.hidalgocharman@codethink.co.uk> | 2019-04-11 17:34:03 +0100 |
---|---|---|
committer | Tom Pollard <tom.pollard@codethink.co.uk> | 2019-04-26 12:55:12 +0100 |
commit | 080edf8967c85262ec713cc2e9a5ff340ca0b24f (patch) | |
tree | 105f401906461938d9b8fb22091286834dcc9839 | |
parent | d19d31cf8600a73e2b63ece4284288e745c852fb (diff) | |
download | buildstream-080edf8967c85262ec713cc2e9a5ff340ca0b24f.tar.gz |
Move _remove_ref method to utils module
Part of #974
-rw-r--r-- | buildstream/_cas/cascache.py | 55 | ||||
-rw-r--r-- | buildstream/utils.py | 46 |
2 files changed, 50 insertions, 51 deletions
diff --git a/buildstream/_cas/cascache.py b/buildstream/_cas/cascache.py index 265ee584b..10e8b3e56 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 7d6db0b67..f65528688 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 |