From 48f0dc6a005e58ecf362ccb79a063f482419c084 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Tue, 27 Aug 2024 11:59:56 +0500 Subject: [PATCH] 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' --- src/bonsai/bonsai/bim/module/debug/operator.py | 2 ++ src/bonsai/bonsai/tool/debug.py | 5 ++++- src/bonsai/test/tool/test_debug.py | 7 ++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/bonsai/bonsai/bim/module/debug/operator.py b/src/bonsai/bonsai/bim/module/debug/operator.py index c01546b0ab..85d5efd61d 100644 --- a/src/bonsai/bonsai/bim/module/debug/operator.py +++ b/src/bonsai/bonsai/bim/module/debug/operator.py @@ -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"} diff --git a/src/bonsai/bonsai/tool/debug.py b/src/bonsai/bonsai/tool/debug.py index 41923bd908..99bf884aa9 100644 --- a/src/bonsai/bonsai/tool/debug.py +++ b/src/bonsai/bonsai/tool/debug.py @@ -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( diff --git a/src/bonsai/test/tool/test_debug.py b/src/bonsai/test/tool/test_debug.py index 7f4c71f0e3..d0238b6592 100644 --- a/src/bonsai/test/tool/test_debug.py +++ b/src/bonsai/test/tool/test_debug.py @@ -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]