From 86eda7bdf8aaf3ba8ade72f05c292f7b87e8e535 Mon Sep 17 00:00:00 2001 From: Soulter <37870767+Soulter@users.noreply.github.com> Date: Sat, 13 May 2023 13:25:56 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/command/command.py | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/model/command/command.py b/model/command/command.py index 642f97028..a64ab0983 100644 --- a/model/command/command.py +++ b/model/command/command.py @@ -8,6 +8,7 @@ from model.provider.provider import Provider import json import util.plugin_util as putil import importlib +from pip._internal import main as pipmain PLATFORM_QQCHAN = 'qqchan' PLATFORM_GOCQ = 'gocq' @@ -38,14 +39,19 @@ class Command: if plugins != None: # print(f"[DEBUG] 当前加载的插件:{plugins}") for p in plugins: + # print(f"[Debug] 当前加载的插件:{self.cached_plugins}") try: if p in self.cached_plugins: - module = self.cached_plugins[p] + module = self.cached_plugins[p]["module"] + obj = self.cached_plugins[p]["clsobj"] else: module = __import__("addons.plugins." + p + "." + p, fromlist=[p]) - self.cached_plugins[p] = module - cls = putil.get_classes(p, module) - obj = getattr(module, cls[0])() + cls = putil.get_classes(p, module) + obj = getattr(module, cls[0])() + self.cached_plugins[p] = { + "module": module, + "clsobj": obj + } hit, res = obj.run(message, role, platform, message_obj) if hit: return True, res @@ -84,8 +90,16 @@ class Command: # 得到url的最后一段 d = l[2].split("/")[-1] # 创建文件夹 - os.mkdir(os.path.join(ppath, d)) - Repo.clone_from(l[2],to_path=os.path.join(ppath, d),branch='master') + plugin_path = os.path.join(ppath, d) + os.mkdir(plugin_path) + Repo.clone_from(l[2],to_path=plugin_path,branch='master') + + # 读取插件的requirements.txt + if os.path.exists(os.path.join(plugin_path, "requirements.txt")): + with open(os.path.join(plugin_path, "requirements.txt"), "r", encoding="utf-8") as f: + for line in f.readlines(): + pipmain(['install', line.strip()]) + return True, "插件拉取成功~", "plugin" except BaseException as e: return False, f"拉取插件失败,原因: {str(e)}", "plugin" @@ -98,7 +112,13 @@ class Command: elif l[1] == "reload": try: for pm in self.cached_plugins: - importlib.reload(self.cached_plugins[pm]) + module = self.cached_plugins[pm]["module"] + cls = putil.get_classes(pm, module) + obj = getattr(module, cls[0])() + self.cached_plugins[pm] = { + "module": module, + "clsobj": obj + } return True, "插件重载成功!", "plugin" except BaseException as e: return False, f"插件重载失败,原因: {str(e)}", "plugin"