# Licensed under a 3-clause BSD style license - see LICENSE.rstimportosfrom.importutil
[docs]classBuildCache:""" Build cache Data is cached in a directory tree:: {self._path}/ {self._path}/{commit_hash}/* {self._path}/{commit_hash}.timestamp If the timestamp file is missing, the subdirectory is ignored (and subject to cleanup). The cache cleanup retains the latest ``build_cache_size`` items that have a valid timestamp file. The timestamp files are created by ``self.finalize_cache_dir(commmit_hash)``, which also triggers a cache cleanup. The finalization should be called only after package is installed successfully, keeping in mind that ``build_cache_size`` may be 0. """def__init__(self,conf,root):
[docs]def_get_cache_dir(self,commit_hash):""" Get the cache dir and timestamp file corresponding to a given commit hash. """path=os.path.join(self._path,commit_hash)stamp=path+".timestamp"returnpath,stamp
[docs]def_get_cache_contents(self):""" Return list of commit hash directories in the cache (containing wheels or not), sorted by decreasing timestamp """ifnotos.path.isdir(self._path):return[]defsort_key(name):path,stamp=self._get_cache_dir(name)try:returnos.stat(stamp).st_mtimeexceptOSError:return0names=os.listdir(self._path)names.sort(key=sort_key,reverse=True)returnnames
[docs]def_cleanup_build_cache(self):# First remove items without timestampifos.path.isdir(self._path):names=os.listdir(self._path)fornameinnames:path,stamp=self._get_cache_dir(name)ifnotos.path.exists(stamp):self._remove_cache_dir(name)# Then remove old itemsnames=self._get_cache_contents()fornameinnames[self._cache_size:]:self._remove_cache_dir(name)