From ec950e0a97d076313aaed65f2390eccbe5c46079 Mon Sep 17 00:00:00 2001 From: thsrite Date: Fri, 21 Jun 2024 17:01:52 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=B8=85=E7=90=86=E6=97=A0=E6=95=88?= =?UTF-8?q?=E7=A1=AC=E9=93=BE=E6=8E=A5=E3=80=81=E6=9B=B4=E6=96=B0=E8=BD=AF?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=8C=87=E5=90=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/cloudassistant/__init__.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/plugins/cloudassistant/__init__.py b/plugins/cloudassistant/__init__.py index fc3b657..18c477c 100644 --- a/plugins/cloudassistant/__init__.py +++ b/plugins/cloudassistant/__init__.py @@ -466,6 +466,31 @@ class CloudAssistant(_PluginBase): return retcode + @staticmethod + def is_broken_symlink(path): + return os.path.islink(path) and not os.path.exists(path) + + def scan_and_remove_broken_symlinks(self, directory): + for root, dirs, files in os.walk(directory): + for name in dirs + files: + path = os.path.join(root, name) + if self.is_broken_symlink(path): + print(f"Removing broken symlink: {path}") + os.remove(path) + + @staticmethod + def update_symlink(target_from, target_to, directory): + for root, dirs, files in os.walk(directory): + for name in dirs + files: + path = os.path.join(root, name) + if os.path.islink(path): + current_target = os.readlink(path) + if current_target == target_from: + new_target = current_target.replace(target_from, target_to) + os.remove(path) + os.symlink(new_target, path) + print(f"Updated symlink: {path} -> {new_target}") + def get_state(self) -> bool: return self._enabled