bim.purge_hdf5_cache to skip currently loaded cache

Previously it would fail with:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'bonsai\\bim\\data\\cache\\988c190483ae6bab7cf6b00cee78694d.h5'
This commit is contained in:
Andrej730
2024-08-27 11:59:56 +05:00
parent 27aee31adf
commit 48f0dc6a00
3 changed files with 12 additions and 2 deletions
@@ -440,9 +440,11 @@ class SelectExpressFile(bpy.types.Operator):
class PurgeHdf5Cache(bpy.types.Operator):
bl_idname = "bim.purge_hdf5_cache"
bl_label = "Purge HDF5 Cache"
bl_description = "Clean up HDF5 cache files except the ones that currently loaded"
def execute(self, context):
core.purge_hdf5_cache(tool.Debug)
self.report({"INFO"}, "HDF5 cache purged.")
return {"FINISHED"}
+4 -1
View File
@@ -44,7 +44,10 @@ class Debug(bonsai.core.tool.Debug):
cache_dir = os.path.join(bpy.context.scene.BIMProperties.data_dir, "cache")
filelist = [f for f in os.listdir(cache_dir) if f.endswith(".h5")]
for f in filelist:
os.remove(os.path.join(cache_dir, f))
try:
os.remove(os.path.join(cache_dir, f))
except PermissionError:
pass
@classmethod
def debug_geometry(
+6 -1
View File
@@ -57,5 +57,10 @@ class TestPurgeHdf5Cache(NewFile):
test_file = cache_dir / "test.h5"
test_file.parent.mkdir(parents=True, exist_ok=True)
test_file.touch()
# Ensure it can skip currently loaded cache.
loaded_file_path = test_file.with_stem("test_loaded")
loaded_file = open(loaded_file_path, "w")
subject.purge_hdf5_cache()
assert not [f for f in cache_dir.iterdir() if f.suffix == ".h5"]
assert [f for f in cache_dir.iterdir() if f.suffix == ".h5"] == [loaded_file_path]