From fead5a87a8e7f9613993963cade3b09588593fa9 Mon Sep 17 00:00:00 2001 From: Natalie Johnson <98086483+Copaan@users.noreply.github.com> Date: Wed, 21 Aug 2024 16:37:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20data=5Fsource.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin_shop/data_source.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/zhenxun/builtin_plugins/plugin_shop/data_source.py b/zhenxun/builtin_plugins/plugin_shop/data_source.py index 324104e5..c868998f 100644 --- a/zhenxun/builtin_plugins/plugin_shop/data_source.py +++ b/zhenxun/builtin_plugins/plugin_shop/data_source.py @@ -1,5 +1,7 @@ import shutil from pathlib import Path +import subprocess +import os import nonebot import ujson as json @@ -83,6 +85,20 @@ async def download_file(url: str): f.write(r.text) +def install_requirement(plugin_path: Path): + requirement_path = plugin_path / "requirement.txt" + + if not requirement_path.exists(): + logger.debug(f"No requirement.txt found for plugin: {plugin_path.name}", "插件管理") + return + + try: + result = subprocess.run(["pip", "install", "-r", str(requirement_path)], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + logger.debug(f"Successfully installed dependencies for plugin: {plugin_path.name}. Output:\n{result.stdout}", "插件管理") + except subprocess.CalledProcessError as e: + logger.error(f"Failed to install dependencies for plugin: {plugin_path.name}. Error:\n{e.stderr}") + + class ShopManage: type2name = { @@ -171,6 +187,11 @@ class ShopManage: return "插件下载地址构建失败..." logger.debug(f"尝试下载插件 URL: {url_path}", "插件管理") await download_file(DOWNLOAD_URL.format(url_path)) + + # 安装依赖 + plugin_path = BASE_PATH / "/".join(module_path_split) + install_requirement(plugin_path) + return f"插件 {plugin_key} 安装成功!" @classmethod