def test_FSCache_404(): c = cache.FSCache("/tmp/labm8-cache-404") with pytest.raises(KeyError): c['foobar'] with pytest.raises(KeyError): del c['foobar'] assert not c.get("foobar") assert c.get("foobar", 5) == 5 c.clear()
def test_FSCache_dict_key(): c = cache.FSCache("/tmp/labm8-cache-dict") # create file system.echo("Hello, world!", "/tmp/labm8.test.remove.txt") # sanity check assert fs.read("/tmp/labm8.test.remove.txt") == ["Hello, world!"] # insert file into cache key = {'a': 5, "c": [1, 2, 3]} c[key] = "/tmp/labm8.test.remove.txt" # check file contents assert fs.read(c[key]) == ["Hello, world!"] c.clear()
def mkcache(*relative_path_components: str) -> cache.FSCache: """Instantiate a file system cache. If the cache does not exist, one is created. Args: *relative_path_components: Relative path of cache. Returns: A filesystem cache instance. """ return cache.FSCache(cachepath(*relative_path_components), escape_key=cache.escape_path)
def __init__(self, path: pathlib.Path): self.path = path.absolute() self.cache = cache.FSCache(self.path) self.corpus = NullCorpus() self.config = pbutil.FromFile(self.path / 'META.pbtxt', internal_pb2.ModelMeta()).config self.atomizer = atomizers.AtomizerBase.FromFile(self.path / 'atomizer') self.backend = { model_pb2.NetworkArchitecture.TENSORFLOW: tensorflow_backend.TensorFlowBackend, model_pb2.NetworkArchitecture.KERAS: keras_backend.KerasBackend, }[self.config.architecture.backend](self.config, self.cache, self.atomizer)
def test_FSCache_iter_len(): c = cache.FSCache("/tmp/labm8-fscache-iter", escape_key=cache.escape_path) c.clear() system.echo("Hello, world!", "/tmp/labm8.testfile.txt") c["foo"] = "/tmp/labm8.testfile.txt" for path in c: assert path == c.keypath("foo") system.echo("Hello, world!", "/tmp/labm8.testfile.txt") c["bar"] = "/tmp/labm8.testfile.txt" assert len(c) == 2 assert len(c.ls()) == 2 assert "bar" in c.ls() assert "foo" in c.ls() c.clear()
def test_FSCache_remove(): c = cache.FSCache("/tmp/labm8-cache-remove") # create file system.echo("Hello, world!", "/tmp/labm8.test.remove.txt") # sanity check assert fs.read("/tmp/labm8.test.remove.txt") == ["Hello, world!"] # insert file into cache c['foobar'] = "/tmp/labm8.test.remove.txt" # sanity check assert fs.read(c['foobar']) == ["Hello, world!"] # remove from cache del c['foobar'] with pytest.raises(KeyError): c['foobar'] assert not c.get("foobar") c.clear()
def compile_cpp_code(code): """ Compile C++ code to a dynamic library. Arguments: code (str): C++ socde. Returns: str: Path to binary. """ bincache = cache.FSCache(fs.path("~/.cache/visioncpp")) if bincache.get(code): logging.info("Found cached binary {}".format( fs.basename(bincache[code]))) else: check_for_computecpp() counter = {"val": 0} def progress(msg): text = "{}: {}".format(counter["val"], msg) if msg else "" if logging.getLogger().getEffectiveLevel() <= logging.INFO: end = "\n" else: end = "" print("\r\033[K {}".format(text), end=end) counter["val"] += 1 sys.stdout.flush() tmpdir = mkdtemp(prefix="visioncpp-") try: progress("compiling device code ...") stub = stub_file(code, dir=tmpdir) progress("compiling host code ...") host = host_compile(code, stub, dir=tmpdir) progress("linking executable ...") tmpbin = link(host, dir=tmpdir) progress("") bincache[code] = tmpbin except Exception as e: rmtree(tmpdir) raise e rmtree(tmpdir) return bincache[code]
def test_set_and_get(): fs.rm("/tmp/labm8-cache-set-and-get") c = cache.FSCache("/tmp/labm8-cache-set-and-get") # create file system.echo("Hello, world!", "/tmp/labm8.testfile.txt") # sanity check assert fs.read("/tmp/labm8.testfile.txt") == ["Hello, world!"] # insert file into cache c['foobar'] = "/tmp/labm8.testfile.txt" # file must be in cache assert fs.isfile(c.keypath("foobar")) # file must have been moved assert not fs.isfile("/tmp/labm8.testfile.txt") # check file contents assert fs.read(c['foobar']) == ["Hello, world!"] assert fs.read(c['foobar']) == fs.read(c.get('foobar')) c.clear()
def mkcache(*relative_path_components: list) -> cache.FSCache: """ Instantiae a file system cache. If the cache does not exist, one is created. Parameters ---------- *relative_path_components Relative path of cache. Returns ------- labm8.FSCache Filesystem cache. """ return cache.FSCache(cachepath(*relative_path_components), escape_key=cache.escape_path)
def test_FSCache_missing_key(): c = cache.FSCache("/tmp/labm8-missing-key") with pytest.raises(ValueError): c['foo'] = '/not/a/real/path' c.clear()
def test_FSCache_init_and_empty(): c = cache.FSCache("/tmp/labm8-cache-init-and-empty") assert fs.isdir("/tmp/labm8-cache-init-and-empty") c.clear() assert not fs.isdir("/tmp/labm8-cache-init-and-empty")