Commit ba6c96e8 authored by richardmaw-codethink's avatar richardmaw-codethink Committed by Jürg Billeter

CasBasedDirectory: Change constructor to take a CASCache instead of a Context

The Context was only used to obtain a reference to the CASCache
and set the unused cas_directory field.
parent d4f12184
......@@ -182,7 +182,7 @@ class SandboxRemote(Sandbox):
# to replace the sandbox's virtual directory with that. Creating a new virtual directory object
# from another hash will be interesting, though...
new_dir = CasBasedDirectory(self._get_context(), ref=dir_digest)
new_dir = CasBasedDirectory(self._get_context().artifactcache.cas, ref=dir_digest)
self._set_virtual_directory(new_dir)
def run(self, command, flags, *, cwd=None, env=None):
......@@ -191,7 +191,7 @@ class SandboxRemote(Sandbox):
if isinstance(upload_vdir, FileBasedDirectory):
# Make a new temporary directory to put source in
upload_vdir = CasBasedDirectory(self._get_context(), ref=None)
upload_vdir = CasBasedDirectory(self._get_context().artifactcache.cas, ref=None)
upload_vdir.import_files(self.get_virtual_directory()._get_underlying_directory())
upload_vdir.recalculate_hash()
......
......@@ -156,7 +156,7 @@ class Sandbox():
"""
if self._vdir is None or self._never_cache_vdirs:
if 'BST_CAS_DIRECTORIES' in os.environ:
self._vdir = CasBasedDirectory(self.__context, ref=None)
self._vdir = CasBasedDirectory(self.__context.artifactcache.cas, ref=None)
else:
self._vdir = FileBasedDirectory(self._root)
return self._vdir
......
......@@ -249,13 +249,11 @@ class CasBasedDirectory(Directory):
_pb2_path_sep = "/"
_pb2_absolute_path_prefix = "/"
def __init__(self, context, ref=None, parent=None, common_name="untitled", filename=None):
self.context = context
self.cas_directory = os.path.join(context.artifactdir, 'cas')
def __init__(self, cas_cache, ref=None, parent=None, common_name="untitled", filename=None):
self.filename = filename
self.common_name = common_name
self.pb2_directory = remote_execution_pb2.Directory()
self.cas_cache = context.artifactcache.cas
self.cas_cache = cas_cache
if ref:
with open(self.cas_cache.objpath(ref), 'rb') as f:
self.pb2_directory.ParseFromString(f.read())
......@@ -270,7 +268,7 @@ class CasBasedDirectory(Directory):
if self._directory_read:
return
for entry in self.pb2_directory.directories:
buildStreamDirectory = CasBasedDirectory(self.context, ref=entry.digest,
buildStreamDirectory = CasBasedDirectory(self.cas_cache, ref=entry.digest,
parent=self, filename=entry.name)
self.index[entry.name] = IndexEntry(entry, buildstream_object=buildStreamDirectory)
for entry in self.pb2_directory.files:
......@@ -333,7 +331,7 @@ class CasBasedDirectory(Directory):
.format(name, str(self), type(newdir)))
dirnode = self._find_pb2_entry(name)
else:
newdir = CasBasedDirectory(self.context, parent=self, filename=name)
newdir = CasBasedDirectory(self.cas_cache, parent=self, filename=name)
dirnode = self.pb2_directory.directories.add()
dirnode.name = name
......
......@@ -225,7 +225,7 @@ def _test_push_directory(user_config_file, project_dir, artifact_dir, artifact_d
if cas.has_push_remotes():
# Create a CasBasedDirectory from local CAS cache content
directory = CasBasedDirectory(context, ref=artifact_digest)
directory = CasBasedDirectory(context.artifactcache.cas, ref=artifact_digest)
# Push the CasBasedDirectory object
cas.push_directory(project, directory)
......
......@@ -3,7 +3,7 @@ import pytest
from buildstream._exceptions import ErrorDomain
from buildstream._context import Context
from buildstream._artifactcache.cascache import CASCache
from buildstream.storage._casbaseddirectory import CasBasedDirectory
from buildstream.storage._filebaseddirectory import FileBasedDirectory
......@@ -17,9 +17,8 @@ def setup_backend(backend_class, tmpdir):
if backend_class == FileBasedDirectory:
return backend_class(os.path.join(tmpdir, "vdir"))
else:
context = Context()
context.artifactdir = os.path.join(tmpdir, "cas")
return backend_class(context)
cas_cache = CASCache(tmpdir)
return backend_class(cas_cache)
@pytest.mark.parametrize("backend", [
......
......@@ -15,18 +15,6 @@ from buildstream import utils
# These are comparitive tests that check that FileBasedDirectory and
# CasBasedDirectory act identically.
class FakeArtifactCache():
def __init__(self):
self.cas = None
class FakeContext():
def __init__(self):
self.artifactdir = ''
self.artifactcache = FakeArtifactCache()
# This is a set of example file system contents. It's a set of trees
# which are either expected to be problematic or were found to be
# problematic during random testing.
......@@ -120,8 +108,8 @@ def file_contents_are(path, contents):
return file_contents(path) == contents
def create_new_casdir(root_number, fake_context, tmpdir):
d = CasBasedDirectory(fake_context)
def create_new_casdir(root_number, cas_cache, tmpdir):
d = CasBasedDirectory(cas_cache)
d.import_files(os.path.join(tmpdir, "content", "root{}".format(root_number)))
assert d.ref.hash != empty_hash_ref
return d
......@@ -175,20 +163,19 @@ def directory_not_empty(path):
def _import_test(tmpdir, original, overlay, generator_function, verify_contents=False):
fake_context = FakeContext()
fake_context.artifactcache.cas = CASCache(tmpdir)
cas_cache = CASCache(tmpdir)
# Create some fake content
generator_function(original, tmpdir)
if original != overlay:
generator_function(overlay, tmpdir)
d = create_new_casdir(original, fake_context, tmpdir)
d = create_new_casdir(original, cas_cache, tmpdir)
duplicate_cas = create_new_casdir(original, fake_context, tmpdir)
duplicate_cas = create_new_casdir(original, cas_cache, tmpdir)
assert duplicate_cas.ref.hash == d.ref.hash
d2 = create_new_casdir(overlay, fake_context, tmpdir)
d2 = create_new_casdir(overlay, cas_cache, tmpdir)
d.import_files(d2)
export_dir = os.path.join(tmpdir, "output-{}-{}".format(original, overlay))
roundtrip_dir = os.path.join(tmpdir, "roundtrip-{}-{}".format(original, overlay))
......@@ -247,15 +234,14 @@ def test_random_cas_import(cli, tmpdir, original):
def _listing_test(tmpdir, root, generator_function):
fake_context = FakeContext()
fake_context.artifactcache.cas = CASCache(tmpdir)
cas_cache = CASCache(tmpdir)
# Create some fake content
generator_function(root, tmpdir)
d = create_new_filedir(root, tmpdir)
filelist = list(d.list_relative_paths())
d2 = create_new_casdir(root, fake_context, tmpdir)
d2 = create_new_casdir(root, cas_cache, tmpdir)
filelist2 = list(d2.list_relative_paths())
assert filelist == filelist2
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment