parameters: (cache_path, cache_size_gb, clean_ratio)
return_statement: return True
docstring: Prune the cache.
docstring_summary: Prune the cache.
docstring_tokens: Prune the cache .
function: def prune_cache(cache_path, cache_size_gb, clean_ratio):
      """Prune the cache."""
      # This function is taken as is from waf, with the interface cleaned up and some minor
      # stylistic changes.
  
      cache_size = cache_size_gb * GIGBYTES
  
      (total_size, contents) = collect_cache_contents(cache_path)
  
      LOGGER.info("cache size %d, quota %d", total_size, cache_size)
  
      if total_size >= cache_size:
          LOGGER.info("trimming the cache since %d > %d", total_size, cache_size)
  
          # make a list to sort the folders' by timestamp
          contents.sort(key=lambda x: x.time, reverse=True)  # sort by timestamp
  
          # now that the contents of things to delete is sorted by timestamp in reverse order, we
          # just delete things until the total_size falls below the target cache size ratio.
          while total_size >= cache_size * clean_ratio:
              if not contents:
                  LOGGER.error("cache size is over quota, and there are no files in "
                               "the queue to delete.")
                  return False
  
              cache_item = contents.pop()
  
              # check the atime again just to make sure something wasn't accessed while
              # we pruning other files.
              if cache_item.time < os.stat(cache_item.path).st_atime:
                  continue
  
              to_remove = cache_item.path + ".del"
              try:
                  os.rename(cache_item.path, to_remove)
              except Exception as err:  # pylint: disable=broad-except
                  # another process may have already cleared the file.
                  LOGGER.warning("Unable to rename %s : %s", cache_item, err)
              else:
                  try:
                      if os.path.isdir(to_remove):
                          shutil.rmtree(to_remove)
                      else:
                          os.remove(to_remove)
                      total_size -= cache_item.size
                  except Exception as err:  # pylint: disable=broad-except
                      # this should not happen, but who knows?
                      LOGGER.error("error [%s, %s] removing file '%s', "
                                   "please report this error", err, type(err), to_remove)
  
          LOGGER.info("total cache size at the end of pruning: %d", total_size)
          return True
      LOGGER.info("cache size (%d) is currently within boundaries", total_size)
      return True
function_tokens: def prune_cache ( cache_path , cache_size_gb , clean_ratio ) : # This function is taken as is from waf, with the interface cleaned up and some minor # stylistic changes. cache_size = cache_size_gb * GIGBYTES ( total_size , contents ) = collect_cache_contents ( cache_path ) LOGGER . info ( "cache size %d, quota %d" , total_size , cache_size ) if total_size >= cache_size : LOGGER . info ( "trimming the cache since %d > %d" , total_size , cache_size ) # make a list to sort the folders' by timestamp contents . sort ( key = lambda x : x . time , reverse = True ) # sort by timestamp # now that the contents of things to delete is sorted by timestamp in reverse order, we # just delete things until the total_size falls below the target cache size ratio. while total_size >= cache_size * clean_ratio : if not contents : LOGGER . error ( "cache size is over quota, and there are no files in " "the queue to delete." ) return False cache_item = contents . pop ( ) # check the atime again just to make sure something wasn't accessed while # we pruning other files. if cache_item . time < os . stat ( cache_item . path ) . st_atime : continue to_remove = cache_item . path + ".del" try : os . rename ( cache_item . path , to_remove ) except Exception as err : # pylint: disable=broad-except # another process may have already cleared the file. LOGGER . warning ( "Unable to rename %s : %s" , cache_item , err ) else : try : if os . path . isdir ( to_remove ) : shutil . rmtree ( to_remove ) else : os . remove ( to_remove ) total_size -= cache_item . size except Exception as err : # pylint: disable=broad-except # this should not happen, but who knows? LOGGER . error ( "error [%s, %s] removing file '%s', " "please report this error" , err , type ( err ) , to_remove ) LOGGER . info ( "total cache size at the end of pruning: %d" , total_size ) return True LOGGER . info ( "cache size (%d) is currently within boundaries" , total_size ) return True