mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-02-12 15:06:35 +08:00
fix 恢复插件后安装依赖
This commit is contained in:
@@ -11,6 +11,7 @@ from app.schemas import Notification, MessageChannel
|
||||
from app.utils.http import RequestUtils
|
||||
from app.utils.system import SystemUtils
|
||||
from app.helper.system import SystemHelper
|
||||
from app.helper.plugin import PluginHelper
|
||||
from version import FRONTEND_VERSION, APP_VERSION
|
||||
|
||||
|
||||
@@ -135,6 +136,13 @@ class SystemChain(ChainBase):
|
||||
shutil.rmtree(target_path)
|
||||
shutil.copytree(item, target_path)
|
||||
logger.info(f"已恢复插件目录: {item.name}")
|
||||
# 安装依赖
|
||||
requirements_file = target_path / "requirements.txt"
|
||||
if requirements_file.exists():
|
||||
logger.info(f"正在安装插件 {item.name} 的依赖...")
|
||||
success, message = PluginHelper.pip_install_with_fallback(requirements_file)
|
||||
if not success:
|
||||
logger.warn(f"插件 {item.name} 依赖安装失败: {message}")
|
||||
restored_count += 1
|
||||
# 如果是文件
|
||||
elif item.is_file():
|
||||
|
||||
@@ -112,14 +112,11 @@ class PluginHelper(metaclass=Singleton):
|
||||
package_version = settings.VERSION_FLAG
|
||||
|
||||
# 优先检查指定版本的插件,即 package.v(x).json 文件中是否存在该插件,如果存在,返回该版本号
|
||||
plugins = self.get_plugins(repo_url, package_version)
|
||||
if pid in plugins:
|
||||
if pid in (self.get_plugins(repo_url, package_version) or []):
|
||||
return package_version
|
||||
|
||||
# 如果指定版本的插件不存在,检查全局 package.json 文件,查看插件是否兼容指定的版本
|
||||
global_plugins = self.get_plugins(repo_url)
|
||||
plugin = global_plugins.get(pid, None)
|
||||
|
||||
plugin = (self.get_plugins(repo_url) or {}).get(pid, None)
|
||||
# 检查插件是否明确支持当前指定的版本(如 v2 或 v3),如果支持,返回空字符串表示使用 package.json(v1)
|
||||
if plugin and plugin.get(package_version) is True:
|
||||
return ""
|
||||
@@ -402,8 +399,7 @@ class PluginHelper(metaclass=Singleton):
|
||||
with open(requirements_file_path, "w", encoding="utf-8") as f:
|
||||
f.write(requirements_txt)
|
||||
|
||||
success, message = self.__pip_install_with_fallback(requirements_file_path)
|
||||
return success, message
|
||||
return self.pip_install_with_fallback(requirements_file_path)
|
||||
|
||||
return True, "" # 如果 requirements.txt 为空,视作成功
|
||||
|
||||
@@ -420,7 +416,7 @@ class PluginHelper(metaclass=Singleton):
|
||||
# 检查是否存在 requirements.txt 文件
|
||||
if requirements_file.exists():
|
||||
logger.info(f"{pid} 存在依赖,开始尝试安装依赖")
|
||||
success, error_message = self.__pip_install_with_fallback(requirements_file)
|
||||
success, error_message = self.pip_install_with_fallback(requirements_file)
|
||||
if success:
|
||||
return True, True, ""
|
||||
else:
|
||||
@@ -478,7 +474,7 @@ class PluginHelper(metaclass=Singleton):
|
||||
shutil.rmtree(plugin_dir, ignore_errors=True)
|
||||
|
||||
@staticmethod
|
||||
def __pip_install_with_fallback(requirements_file: Path) -> Tuple[bool, str]:
|
||||
def pip_install_with_fallback(requirements_file: Path) -> Tuple[bool, str]:
|
||||
"""
|
||||
使用自动降级策略安装依赖,并确保新安装的包可被动态导入
|
||||
:param requirements_file: 依赖的 requirements.txt 文件路径
|
||||
@@ -599,7 +595,6 @@ class PluginHelper(metaclass=Singleton):
|
||||
def install_dependencies(self, dependencies: List[str]) -> Tuple[bool, str]:
|
||||
"""
|
||||
安装指定的依赖项列表
|
||||
|
||||
:param dependencies: 需要安装或更新的依赖项列表
|
||||
:return: (success, message)
|
||||
"""
|
||||
@@ -614,12 +609,12 @@ class PluginHelper(metaclass=Singleton):
|
||||
with open(requirements_temp_file, "w", encoding="utf-8") as f:
|
||||
for dep in dependencies:
|
||||
f.write(dep + "\n")
|
||||
|
||||
# 使用自动降级策略安装依赖
|
||||
success, message = self.__pip_install_with_fallback(requirements_temp_file)
|
||||
# 删除临时文件
|
||||
requirements_temp_file.unlink()
|
||||
return success, message
|
||||
try:
|
||||
# 使用自动降级策略安装依赖
|
||||
return self.pip_install_with_fallback(requirements_temp_file)
|
||||
finally:
|
||||
# 删除临时文件
|
||||
requirements_temp_file.unlink()
|
||||
except Exception as e:
|
||||
logger.error(f"安装依赖项时发生错误:{e}")
|
||||
return False, f"安装依赖项时发生错误:{e}"
|
||||
|
||||
Reference in New Issue
Block a user