From 58faf0100996bca0cfea8bc4798fab31f25cc0b0 Mon Sep 17 00:00:00 2001 From: Igor Bogoslavskyi Date: Sun, 23 Jan 2022 12:33:36 +0100 Subject: [PATCH] Introduce a more thorough clean method --- Default.sublime-commands | 4 +- EasyClangComplete.py | 46 +++++++++++------------ plugin/flags_sources/cmake_file.py | 13 ++++++- plugin/utils/tools.py | 4 +- plugin/view_config/view_config_manager.py | 8 ++++ 5 files changed, 44 insertions(+), 31 deletions(-) diff --git a/Default.sublime-commands b/Default.sublime-commands index 7811a4ae..eb536d4c 100644 --- a/Default.sublime-commands +++ b/Default.sublime-commands @@ -18,8 +18,8 @@ "command": "ecc_show_all_errors" }, { - "caption": "ECC: Clean current CMake cache", - "command": "clean_cmake" + "caption": "ECC: Clean internal cache", + "command": "clean_internal_cache" }, { "caption": "ECC: Show popup info", diff --git a/EasyClangComplete.py b/EasyClangComplete.py index 2d4cfd08..be1b8e74 100644 --- a/EasyClangComplete.py +++ b/EasyClangComplete.py @@ -83,8 +83,14 @@ def plugin_loaded(): We need it to initialize all the different classes that encapsulate functionality. We can only properly init them after sublime api is available.""" + log.debug("Reloading the plugin") module_reloader.ModuleReloader.reload_all(ignore_string='singleton') GenericCache.clear_all_caches() + # Clear the old cache directory. + cache_dir = File.get_temp_dir() + if path.exists(cache_dir): + log.debug("Removing the build cache folder %s", cache_dir) + shutil.rmtree(cache_dir, ignore_errors=True) handle_plugin_loaded_function() @@ -179,36 +185,26 @@ def passthrough(view, trigger): settings.force_unix_includes) -class CleanCmakeCommand(sublime_plugin.TextCommand): - """Command that cleans cmake build directory.""" +class CleanInternalCacheCommand(sublime_plugin.TextCommand): + """Command that cleans the internal build directory and all caches.""" def run(self, edit): - """Run clean command. - - Detects if there is a CMakeLists.txt associated to current view and - cleans all related information in case there is one. - """ + """Run clean command.""" if not SublBridge.is_valid_view(self.view): + log.debug("Cannot clean build for view: '%s'", + self.view.file_name()) return - import gc - file_path = self.view.file_name() - cmake_cache = CMakeFileCache() - try: - cmake_file_path = cmake_cache[file_path] - log.debug("Cleaning file: '%s'", cmake_file_path) - del cmake_cache[file_path] - del cmake_cache[cmake_file_path] - EasyClangComplete.view_config_manager.clear_for_view( + log.debug("Clearing config for view: '%s'", self.view.file_name()) + EasyClangComplete.view_config_manager.clear_for_view( self.view.buffer_id()) - # Better safe than sorry. Cleanup! - gc.collect() - temp_proj_dir = CMakeFile.unique_folder_name(cmake_file_path) - if path.exists(temp_proj_dir): - log.debug("Cleaning build directory: '%s'", temp_proj_dir) - shutil.rmtree(temp_proj_dir, ignore_errors=True) - except KeyError: - log.debug("Nothing to clean") - + cache_dir = File.get_temp_dir() + if path.exists(cache_dir): + log.debug("Removing the build cache folder %s", cache_dir) + shutil.rmtree(cache_dir, ignore_errors=True) + else: + log.warning("Cloud not clean folder: %s", cache_dir) + GenericCache().clear_all_caches() + EasyClangComplete.view_config_manager.clear() class GenerateBazelCompDbCommand(sublime_plugin.TextCommand): """Command that generates a compilation database with bazel.""" diff --git a/plugin/flags_sources/cmake_file.py b/plugin/flags_sources/cmake_file.py index 471e056b..785f8f8e 100644 --- a/plugin/flags_sources/cmake_file.py +++ b/plugin/flags_sources/cmake_file.py @@ -142,6 +142,15 @@ def get_flags(self, file_path=None, search_scope=None): flags = db.get_flags(file_path, db_search_scope) return flags + @staticmethod + def get_cache_dir(): + """Get all builds folder name. + + Returns: + str: Path to a temp folder where all cache is stored. + """ + return File.get_temp_dir('cmake_builds') + @staticmethod def unique_folder_name(cmake_path): """Get unique build folder name. @@ -152,8 +161,8 @@ def unique_folder_name(cmake_path): Returns: str: Path to a unique temp folder. """ - return File.get_temp_dir('cmake_builds', - Tools.get_unique_str(cmake_path)) + return File.get_temp_dir('cmake_builds', + Tools.get_unique_str(cmake_path)) @staticmethod def __prepend_prefix_paths(prefix_paths): diff --git a/plugin/utils/tools.py b/plugin/utils/tools.py index 854c7b43..2d8b5ef4 100644 --- a/plugin/utils/tools.py +++ b/plugin/utils/tools.py @@ -64,9 +64,9 @@ def run_command(command, shell=False, cwd=path.curdir, env=environ, output_text = e.output.decode("utf-8") _log.debug("Command finished with code: %s", e.returncode) _log.debug("Command output: \n%s", output_text) - except OSError: + except OSError as e: _log.debug( - "Executable file not found executing: {}".format(command)) + "Error while executing command: %s", e.strerror) return output_text @staticmethod diff --git a/plugin/view_config/view_config_manager.py b/plugin/view_config/view_config_manager.py index 1362b95e..72b3a721 100644 --- a/plugin/view_config/view_config_manager.py +++ b/plugin/view_config/view_config_manager.py @@ -104,6 +104,14 @@ def load_for_view(self, view, settings): log.error("Traceback: %s", tb) return None + def clear(self): + """Clear all cache.""" + import gc + log.debug("Trying to clean all view cache") + with self.__rlock: + self.__cache.clear() + gc.collect() # Explicitly collect garbage. + def clear_for_view(self, v_id): """Clear config for a view id.""" assert isinstance(v_id, int), "View id should be an int."