fix(plugin): dependency dynamic refresh

This commit is contained in:
DDSRem
2025-05-24 09:23:47 +08:00
parent 525d17270f
commit 12760a70a1
2 changed files with 17 additions and 2 deletions
+16 -1
View File
@@ -2,6 +2,8 @@ import sys
import json import json
import shutil import shutil
import traceback import traceback
import site
import importlib
from pathlib import Path from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple, Set from typing import Any, Dict, List, Optional, Tuple, Set
@@ -452,7 +454,7 @@ class PluginHelper(metaclass=Singleton):
@staticmethod @staticmethod
def __pip_install_with_fallback(requirements_file: Path) -> Tuple[bool, str]: def __pip_install_with_fallback(requirements_file: Path) -> Tuple[bool, str]:
""" """
使用自动降级策略,PIP 安装依赖,优先级依次为镜像站、代理、直连 使用自动降级策略安装依赖,并确保新安装的包可被动态导入
:param requirements_file: 依赖的 requirements.txt 文件路径 :param requirements_file: 依赖的 requirements.txt 文件路径
:return: (是否成功, 错误信息) :return: (是否成功, 错误信息)
""" """
@@ -466,12 +468,25 @@ class PluginHelper(metaclass=Singleton):
strategies.append(("代理", base_cmd + ["--proxy", settings.PROXY_HOST])) strategies.append(("代理", base_cmd + ["--proxy", settings.PROXY_HOST]))
strategies.append(("直连", base_cmd)) strategies.append(("直连", base_cmd))
# 记录当前已安装的包,以便后续刷新
before_installation = set(sys.modules.keys())
# 遍历策略进行安装 # 遍历策略进行安装
for strategy_name, pip_command in strategies: for strategy_name, pip_command in strategies:
logger.debug(f"[PIP] 尝试使用策略:{strategy_name} 安装依赖,命令:{' '.join(pip_command)}") logger.debug(f"[PIP] 尝试使用策略:{strategy_name} 安装依赖,命令:{' '.join(pip_command)}")
success, message = SystemUtils.execute_with_subprocess(pip_command) success, message = SystemUtils.execute_with_subprocess(pip_command)
if success: if success:
logger.debug(f"[PIP] 策略:{strategy_name} 安装依赖成功,输出:{message}") logger.debug(f"[PIP] 策略:{strategy_name} 安装依赖成功,输出:{message}")
# 安装成功后刷新Python的模块系统
importlib.reload(site)
# 获取新安装的模块
current_modules = set(sys.modules.keys())
new_modules = current_modules - before_installation
# 重新加载新安装的模块
for module in new_modules:
if module in sys.modules:
del sys.modules[module]
logger.debug(f"[PIP] 已刷新导入系统,新加载的模块: {new_modules}")
return True, message return True, message
else: else:
logger.error(f"[PIP] 策略:{strategy_name} 安装依赖失败,错误信息:{message}") logger.error(f"[PIP] 策略:{strategy_name} 安装依赖失败,错误信息:{message}")
+1 -1
View File
@@ -188,7 +188,7 @@ class LoggerManager:
file_formatter = CustomFormatter(log_settings.LOG_FILE_FORMAT) file_formatter = CustomFormatter(log_settings.LOG_FILE_FORMAT)
file_handler.setFormatter(file_formatter) file_handler.setFormatter(file_formatter)
_logger.addHandler(file_handler) _logger.addHandler(file_handler)
# 禁止向父级log传递 # 禁止向父级log传递
_logger.propagate = False _logger.propagate = False