[Notes] [Git][BuildStream/buildstream][tristan/cas-cleanup-improve] _cas/cascache.py: Cleanup directories when removing refs



Title: GitLab

Tristan Van Berkom pushed to branch tristan/cas-cleanup-improve at BuildStream / buildstream

Commits:

1 changed file:

Changes:

  • buildstream/_cas/cascache.py
    ... ... @@ -545,11 +545,7 @@ class CASCache():
    545 545
         def remove(self, ref, *, defer_prune=False):
    
    546 546
     
    
    547 547
             # Remove cache ref
    
    548
    -        refpath = self._refpath(ref)
    
    549
    -        if not os.path.exists(refpath):
    
    550
    -            raise CASCacheError("Could not find ref '{}'".format(ref))
    
    551
    -
    
    552
    -        os.unlink(refpath)
    
    548
    +        self._remove_ref(ref)
    
    553 549
     
    
    554 550
             if not defer_prune:
    
    555 551
                 pruned = self.prune()
    
    ... ... @@ -626,6 +622,55 @@ class CASCache():
    626 622
         def _refpath(self, ref):
    
    627 623
             return os.path.join(self.casdir, 'refs', 'heads', ref)
    
    628 624
     
    
    625
    +    # _remove_ref()
    
    626
    +    #
    
    627
    +    # Removes a ref.
    
    628
    +    #
    
    629
    +    # This also takes care of pruning away directories which can
    
    630
    +    # be removed after having removed the given ref.
    
    631
    +    #
    
    632
    +    # Args:
    
    633
    +    #    ref (str): The ref to remove
    
    634
    +    #
    
    635
    +    # Raises:
    
    636
    +    #    (CASCacheError): If the ref didnt exist, or a system error
    
    637
    +    #                     occurred while removing it
    
    638
    +    #
    
    639
    +    def _remove_ref(self, ref):
    
    640
    +
    
    641
    +        # Remove the ref itself
    
    642
    +        refpath = self._refpath(ref)
    
    643
    +        try:
    
    644
    +            os.unlink(refpath)
    
    645
    +        except FileNotFoundError:
    
    646
    +            raise CASCacheError("Could not find ref '{}'".format(ref)) from e
    
    647
    +
    
    648
    +        basedir = os.path.join(self.casdir, 'refs', 'heads')
    
    649
    +        components = list(os.path.split(ref))
    
    650
    +
    
    651
    +        while components:
    
    652
    +            components.pop()
    
    653
    +            refdir = os.path.join(basedir, *components)
    
    654
    +
    
    655
    +            # Break out once we reach the base
    
    656
    +            if refdir == basedir:
    
    657
    +                break
    
    658
    +
    
    659
    +            try:
    
    660
    +                os.rmdir(refdir)
    
    661
    +            except FileNotFoundError:
    
    662
    +                # The parent directory did not exist, but it's
    
    663
    +                # parent directory might still be ready to prune
    
    664
    +                pass
    
    665
    +            except OSError as e:
    
    666
    +                if e.errno == errno.ENOTEMPTY:
    
    667
    +                    # The parent directory was not empty, so we
    
    668
    +                    # cannot prune directories beyond this point
    
    669
    +                    break
    
    670
    +
    
    671
    +                # Something went wrong here
    
    672
    +                raise CASCacheError("System error while removing ref '{}': {}".format(ref, e)) from e
    
    673
    +
    
    629 674
         # _commit_directory():
    
    630 675
         #
    
    631 676
         # Adds local directory to content addressable store.
    



  • [Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]