diff options
author | Tristan Maat <tristan.maat@codethink.co.uk> | 2019-11-26 17:19:15 +0000 |
---|---|---|
committer | Tristan Maat <tristan.maat@codethink.co.uk> | 2019-12-03 10:45:08 +0000 |
commit | 845a2fdb4ff786c9e9c7441ba321387124a25354 (patch) | |
tree | 6534dd727c43f20a2381ae0df9d430716e4b93c7 /src | |
parent | 320b3f2efed977bc11903e5c981a17f7a709022c (diff) | |
download | buildstream-845a2fdb4ff786c9e9c7441ba321387124a25354.tar.gz |
_*cache.py: Standardize cache basedirs
Diffstat (limited to 'src')
-rw-r--r-- | src/buildstream/_artifactcache.py | 22 | ||||
-rw-r--r-- | src/buildstream/_basecache.py | 7 | ||||
-rw-r--r-- | src/buildstream/_sourcecache.py | 12 |
3 files changed, 21 insertions, 20 deletions
diff --git a/src/buildstream/_artifactcache.py b/src/buildstream/_artifactcache.py index 3ad288962..02dd21d41 100644 --- a/src/buildstream/_artifactcache.py +++ b/src/buildstream/_artifactcache.py @@ -146,12 +146,12 @@ class ArtifactCache(BaseCache): super().__init__(context) # create artifact directory - self.artifactdir = context.artifactdir - os.makedirs(self.artifactdir, exist_ok=True) + self._basedir = context.artifactdir + os.makedirs(self._basedir, exist_ok=True) def update_mtime(self, ref): try: - os.utime(os.path.join(self.artifactdir, ref)) + os.utime(os.path.join(self._basedir, ref)) except FileNotFoundError as e: raise ArtifactError("Couldn't find artifact: {}".format(ref)) from e @@ -176,7 +176,7 @@ class ArtifactCache(BaseCache): def contains(self, element, key): ref = element.get_artifact_name(key) - return os.path.exists(os.path.join(self.artifactdir, ref)) + return os.path.exists(os.path.join(self._basedir, ref)) # list_artifacts(): # @@ -189,7 +189,7 @@ class ArtifactCache(BaseCache): # ([str]) - A list of artifact names as generated in LRU order # def list_artifacts(self, *, glob=None): - return [ref for _, ref in sorted(list(self._list_refs_mtimes(self.artifactdir, glob_expr=glob)))] + return [ref for _, ref in sorted(list(self._list_refs_mtimes(self._basedir, glob_expr=glob)))] # remove(): # @@ -202,7 +202,7 @@ class ArtifactCache(BaseCache): # def remove(self, ref): try: - self._remove_ref(ref, self.artifactdir) + self._remove_ref(ref) except CacheError as e: raise ArtifactError("{}".format(e)) from e @@ -410,8 +410,8 @@ class ArtifactCache(BaseCache): oldref = element.get_artifact_name(oldkey) newref = element.get_artifact_name(newkey) - if not os.path.exists(os.path.join(self.artifactdir, newref)): - os.link(os.path.join(self.artifactdir, oldref), os.path.join(self.artifactdir, newref)) + if not os.path.exists(os.path.join(self._basedir, newref)): + os.link(os.path.join(self._basedir, oldref), os.path.join(self._basedir, newref)) # get_artifact_logs(): # @@ -514,7 +514,7 @@ class ArtifactCache(BaseCache): # (iter): Iterator over directories digests available from artifacts. # def _reachable_directories(self): - for root, _, files in os.walk(self.artifactdir): + for root, _, files in os.walk(self._basedir): for artifact_file in files: artifact = artifact_pb2.Artifact() with open(os.path.join(root, artifact_file), "r+b") as f: @@ -532,7 +532,7 @@ class ArtifactCache(BaseCache): # (iter): Iterator over single file digests in artifacts # def _reachable_digests(self): - for root, _, files in os.walk(self.artifactdir): + for root, _, files in os.walk(self._basedir): for artifact_file in files: artifact = artifact_pb2.Artifact() with open(os.path.join(root, artifact_file), "r+b") as f: @@ -707,7 +707,7 @@ class ArtifactCache(BaseCache): return None # Write the artifact proto to cache - artifact_path = os.path.join(self.artifactdir, artifact_name) + artifact_path = os.path.join(self._basedir, artifact_name) os.makedirs(os.path.dirname(artifact_path), exist_ok=True) with utils.save_file_atomic(artifact_path, mode="wb") as f: f.write(artifact.SerializeToString()) diff --git a/src/buildstream/_basecache.py b/src/buildstream/_basecache.py index a7d06f27e..dff7742e7 100644 --- a/src/buildstream/_basecache.py +++ b/src/buildstream/_basecache.py @@ -62,6 +62,8 @@ class BaseCache: self._has_fetch_remotes = False self._has_push_remotes = False + self._basedir = None + # has_open_grpc_channels(): # # Return whether there are gRPC channel instances. This is used to safeguard @@ -439,15 +441,14 @@ class BaseCache: # # Args: # ref (str): The ref to remove - # basedir (str): Path of base directory the ref is in # # Raises: # (CASCacheError): If the ref didnt exist, or a system error # occurred while removing it # - def _remove_ref(self, ref, basedir): + def _remove_ref(self, ref): try: - utils._remove_path_with_parents(basedir, ref) + utils._remove_path_with_parents(self._basedir, ref) except FileNotFoundError as e: raise CacheError("Could not find ref '{}'".format(ref)) from e except OSError as e: diff --git a/src/buildstream/_sourcecache.py b/src/buildstream/_sourcecache.py index 03e2d1830..221694e94 100644 --- a/src/buildstream/_sourcecache.py +++ b/src/buildstream/_sourcecache.py @@ -129,8 +129,8 @@ class SourceCache(BaseCache): def __init__(self, context): super().__init__(context) - self.sourcerefdir = os.path.join(context.cachedir, "source_protos") - os.makedirs(self.sourcerefdir, exist_ok=True) + self._basedir = os.path.join(context.cachedir, "source_protos") + os.makedirs(self._basedir, exist_ok=True) # list_sources() # @@ -140,7 +140,7 @@ class SourceCache(BaseCache): # ([str]): iterable over all source refs # def list_sources(self): - return [ref for _, ref in self._list_refs_mtimes(self.sourcerefdir)] + return [ref for _, ref in self._list_refs_mtimes(self._basedir)] # contains() # @@ -326,7 +326,7 @@ class SourceCache(BaseCache): return pushed_index and pushed_storage def _remove_source(self, ref, *, defer_prune=False): - return self.cas.remove(ref, basedir=self.sourcerefdir, defer_prune=defer_prune) + return self.cas.remove(ref, basedir=self._basedir, defer_prune=defer_prune) def _store_source(self, ref, digest): source_proto = source_pb2.Source() @@ -351,10 +351,10 @@ class SourceCache(BaseCache): raise SourceCacheError("Attempted to access unavailable source: {}".format(e)) from e def _source_path(self, ref): - return os.path.join(self.sourcerefdir, ref) + return os.path.join(self._basedir, ref) def _reachable_directories(self): - for root, _, files in os.walk(self.sourcerefdir): + for root, _, files in os.walk(self._basedir): for source_file in files: source = source_pb2.Source() with open(os.path.join(root, source_file), "r+b") as f: |