def Purge(self, max_age=None, cache_size=None): """Attempts to clean up the cache contents. Is a no-op if cache lock is not acquirable. Args: max_age: Overrides the __init__ max_age for this one purge. Mostly intended for unittests. cache_size: Overrides the __init__ cache_size for this one purge. Mostly intended for unittests. """ max_age = self._max_age if max_age is None else max_age cache_size = self._cache_size if cache_size is None else cache_size try: # Prevent other changes while we purge the cache. with self._PurgeLock(shared=False, blocking=False): # Purge files based on age, if specified. if max_age is not None: now = time.time() for f in utils.ListdirFullpath(self._file_dir): if (now - os.path.getmtime(f)) > max_age: os.unlink(f) # Purge files based on size, if specified. if cache_size is not None: # Find cache files, and sort them so the oldest are first. # This defines which ones we will purge first. cache_files = utils.ListdirFullpath(self._file_dir) cache_files.sort(key=os.path.getmtime) sizes = [os.path.getsize(f) for f in cache_files] total_size = sum(sizes) # Remove files until we are small enough to fit. for f, size in zip(cache_files, sizes): if total_size < cache_size: break total_size -= size os.unlink(f) # Just remove all lock files. They will be recreated as needed. shutil.rmtree(self._lock_dir) os.makedirs(self._lock_dir) except locking.LockNotAcquiredError: # If we can't get an exclusive lock on the file, it's in use, leave it. pass
def testListdirFullpath(self): file_a = os.path.join(self.tempdir, 'a') file_b = os.path.join(self.tempdir, 'b') osutils.Touch(file_a) osutils.Touch(file_b) self.assertEqual(sorted(utils.ListdirFullpath(self.tempdir)), [file_a, file_b])
def testListdirFullpath(self): file_a = os.path.join(self.tempdir, 'a') file_b = os.path.join(self.tempdir, 'b') with file(file_a, 'w+'): pass with file(file_b, 'w+'): pass self.assertEqual(sorted(utils.ListdirFullpath(self.tempdir)), [file_a, file_b])