From 804889f1de8d7ceb5c2e7259365d78e0314c8839 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sun, 14 May 2023 17:30:05 +0800 Subject: [PATCH 1/3] =?UTF-8?q?perf:=20=E5=8A=A0=E8=BD=BD=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E7=9A=84=E8=BE=93=E5=87=BA=E6=94=B9=E4=B8=BAdebug?= =?UTF-8?q?=E7=BA=A7=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/plugin/host.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/plugin/host.py b/pkg/plugin/host.py index af37c24c..8d242801 100644 --- a/pkg/plugin/host.py +++ b/pkg/plugin/host.py @@ -79,7 +79,7 @@ def walk_plugin_path(module, prefix='', path_prefix=''): __current_module_path__ = "plugins/"+path_prefix + item.name + '.py' importlib.import_module(module.__name__ + '.' + item.name) - logging.info('加载模块: plugins/{} 成功'.format(path_prefix + item.name + '.py')) + logging.debug('加载模块: plugins/{} 成功'.format(path_prefix + item.name + '.py')) except: logging.error('加载模块: plugins/{} 失败: {}'.format(path_prefix + item.name + '.py', sys.exc_info())) traceback.print_exc() From d85e8401264b0da8cd4ffcfc311a456111787f52 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sun, 14 May 2023 18:41:20 +0800 Subject: [PATCH 2/3] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E6=9B=B4=E6=96=B0=E6=93=8D=E4=BD=9C=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=9B=B4=E6=96=B0=E5=8D=95=E4=B8=AA=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/plugin/host.py | 41 ++++++++++++++++++++++++++ pkg/qqbot/cmds/plugin/plugin.py | 51 ++++++++++++++++----------------- 2 files changed, 65 insertions(+), 27 deletions(-) diff --git a/pkg/plugin/host.py b/pkg/plugin/host.py index 8d242801..4249c37e 100644 --- a/pkg/plugin/host.py +++ b/pkg/plugin/host.py @@ -8,6 +8,7 @@ import sys import shutil import traceback +import pkg.utils.updater as updater import pkg.utils.context as context import pkg.plugin.switch as switch import pkg.plugin.settings as settings @@ -177,6 +178,43 @@ def uninstall_plugin(plugin_name: str) -> str: return "plugins/"+plugin_path +def update_plugin(plugin_name: str): + """更新插件""" + # 检查是否有远程地址记录 + target_plugin_dir = "plugins/" + __plugins__[plugin_name]['path'].replace("\\", "/").split("plugins/")[1].split("/")[0] + + remote_url = updater.get_remote_url(target_plugin_dir) + if remote_url == "https://github.com/RockChinQ/QChatGPT" or remote_url == "https://gitee.com/RockChin/QChatGPT" \ + or remote_url == "" or remote_url is None or remote_url == "http://github.com/RockChinQ/QChatGPT" or remote_url == "http://gitee.com/RockChin/QChatGPT": + raise Exception("插件没有远程地址记录,无法更新") + + # 把远程clone到temp/plugins/update/插件名 + logging.info("克隆插件储存库: {}".format(remote_url)) + + from dulwich import porcelain + clone_target_dir = "temp/plugins/update/"+target_plugin_dir.split("/")[-1]+"/" + + if os.path.exists(clone_target_dir): + shutil.rmtree(clone_target_dir) + + if not os.path.exists(clone_target_dir): + os.makedirs(clone_target_dir) + repo = porcelain.clone(remote_url, clone_target_dir, checkout=True) + + # 检查此目录是否包含requirements.txt + if os.path.exists(clone_target_dir+"requirements.txt"): + logging.info("检测到requirements.txt,正在安装依赖") + import pkg.utils.pkgmgr + pkg.utils.pkgmgr.install_requirements(clone_target_dir+"requirements.txt") + + import pkg.utils.log as log + log.reset_logging() + + # 将temp/plugins/update/插件名 覆盖到 plugins/插件名 + shutil.rmtree(target_plugin_dir) + + shutil.copytree(clone_target_dir, target_plugin_dir) + class EventContext: """事件上下文""" eid = 0 @@ -344,3 +382,6 @@ class PluginHost: event_context.__return_value__)) return event_context + +if __name__ == "__main__": + pass diff --git a/pkg/qqbot/cmds/plugin/plugin.py b/pkg/qqbot/cmds/plugin/plugin.py index 02cba4c9..1ec186d0 100644 --- a/pkg/qqbot/cmds/plugin/plugin.py +++ b/pkg/qqbot/cmds/plugin/plugin.py @@ -97,37 +97,34 @@ class PluginUpdateCommand(AbstractCommandNode): plugin_list = plugin_host.__plugins__ reply = [] - def closure(): - try: - import pkg.utils.context - updated = [] - for key in plugin_list: - plugin = plugin_list[key] - if updater.is_repo("/".join(plugin['path'].split('/')[:-1])): - success = updater.pull_latest("/".join(plugin['path'].split('/')[:-1])) - if success: - updated.append(plugin['name']) - # 检查是否有requirements.txt - pkg.utils.context.get_qqbot_manager().notify_admin("正在安装依赖...") - for key in plugin_list: - plugin = plugin_list[key] - if os.path.exists("/".join(plugin['path'].split('/')[:-1])+"/requirements.txt"): - logging.info("{}检测到requirements.txt,安装依赖".format(plugin['name'])) - import pkg.utils.pkgmgr - pkg.utils.pkgmgr.install_requirements("/".join(plugin['path'].split('/')[:-1])+"/requirements.txt") + if len(ctx.crt_params) > 0: + def closure(): + try: + import pkg.utils.context + + updated = [] - import pkg.utils.log as log - log.reset_logging() + if ctx.crt_params[0] == 'all': + for key in plugin_list: + plugin_host.update_plugin(key) + updated.append(key) + else: + if ctx.crt_params[0] in plugin_list: + plugin_host.update_plugin(ctx.crt_params[0]) + updated.append(ctx.crt_params[0]) + else: + raise Exception("未找到插件: {}".format(ctx.crt_params[0])) - pkg.utils.context.get_qqbot_manager().notify_admin("已更新插件: {}".format(", ".join(updated))) - except Exception as e: - logging.error("插件更新失败:{}".format(e)) - pkg.utils.context.get_qqbot_manager().notify_admin("插件更新失败:{} 请尝试手动更新插件".format(e)) + pkg.utils.context.get_qqbot_manager().notify_admin("已更新插件: {}, 请发送 !reload 重载插件".format(", ".join(updated))) + except Exception as e: + logging.error("插件更新失败:{}".format(e)) + pkg.utils.context.get_qqbot_manager().notify_admin("插件更新失败:{} 请尝试手动更新插件".format(e)) - - threading.Thread(target=closure).start() - reply = ["[bot]正在更新所有插件,请勿重复发起..."] + reply = ["[bot]正在更新插件,请勿重复发起..."] + threading.Thread(target=closure).start() + else: + reply = ["[bot]请指定要更新的插件, 或使用 !plugin update all 更新所有插件"] return True, reply From 45042fe7d451c2a80ae2ec399168cd52bdc054fc Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sun, 14 May 2023 18:44:14 +0800 Subject: [PATCH 3/3] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=91=BD=E4=BB=A4wiki?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- res/wiki/插件使用.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/res/wiki/插件使用.md b/res/wiki/插件使用.md index a5172ed1..f0db3f12 100644 --- a/res/wiki/插件使用.md +++ b/res/wiki/插件使用.md @@ -23,15 +23,16 @@ QChatGPT 插件使用Wiki ## 管理 -### !plugin 指令 +### !plugin 命令 ``` -!plugin 列出所有已安装的插件 +!plugin 列出所有已安装的插件 !plugin get <储存库地址> 从Git储存库安装插件(需要管理员权限) -!plugin update 更新所有插件(需要管理员权限,仅支持从储存库安装的插件) -!plugin del <插件名> 删除插件(需要管理员权限) -!plugin on <插件名> 启用插件(需要管理员权限) -!plugin off <插件名> 禁用插件(需要管理员权限) +!plugin update all 更新所有插件(需要管理员权限,仅支持从储存库安装的插件) +!plugin update <插件名> 更新指定插件 +!plugin del <插件名> 删除插件(需要管理员权限) +!plugin on <插件名> 启用插件(需要管理员权限) +!plugin off <插件名> 禁用插件(需要管理员权限) ``` ### 控制插件执行顺序