From 4278d2b8efc1e26ad0f472c4f4fffb10fe22d6d9 Mon Sep 17 00:00:00 2001 From: lanvent Date: Fri, 7 Apr 2023 23:31:07 +0800 Subject: [PATCH] feat: add updatep command --- plugins/godcmd/godcmd.py | 10 ++++++++++ plugins/plugin_manager.py | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/plugins/godcmd/godcmd.py b/plugins/godcmd/godcmd.py index 5b91d32..46f11af 100644 --- a/plugins/godcmd/godcmd.py +++ b/plugins/godcmd/godcmd.py @@ -104,6 +104,11 @@ ADMIN_COMMANDS = { "args": ["插件名"], "desc": "卸载指定插件", }, + "updatep": { + "alias": ["updatep", "更新插件"], + "args": ["插件名"], + "desc": "更新指定插件", + }, "debug": { "alias": ["debug", "调试模式", "DEBUG"], "desc": "开启机器调试日志", @@ -336,6 +341,11 @@ class Godcmd(Plugin): ok, result = False, "请提供插件名" else: ok, result = PluginManager().uninstall_plugin(args[0]) + elif cmd == "updatep": + if len(args) != 1: + ok, result = False, "请提供插件名" + else: + ok, result = PluginManager().update_plugin(args[0]) logger.debug("[Godcmd] admin command: %s by %s" % (cmd, user)) else: ok, result = False, "需要管理员权限才能执行该指令" diff --git a/plugins/plugin_manager.py b/plugins/plugin_manager.py index 460ecec..619edea 100644 --- a/plugins/plugin_manager.py +++ b/plugins/plugin_manager.py @@ -243,6 +243,30 @@ class PluginManager: logger.error("Failed to install plugin, {}".format(e)) return False, "安装插件失败,"+str(e) + def update_plugin(self, name:str): + try: + import common.package_manager as pkgmgr + pkgmgr.check_dulwich() + except Exception as e: + logger.error("Failed to install plugin, {}".format(e)) + return False, "无法导入dulwich,更新插件失败" + from dulwich import porcelain + name = name.upper() + if name not in self.plugins: + return False, "插件不存在" + if name in ["HELLO","GODCMD","ROLE","TOOL","BDUNIT","BANWORDS","FINISH","DUNGEON"]: + return False, "预置插件无法更新,请更新主程序仓库" + dirname = self.plugins[name].path + try: + porcelain.pull(dirname, "origin") + if os.path.exists(os.path.join(dirname,"requirements.txt")): + logger.info("detect requirements.txt,installing...") + pkgmgr.install_requirements(os.path.join(dirname,"requirements.txt")) + return True, "更新插件成功,请重新运行程序" + except Exception as e: + logger.error("Failed to update plugin, {}".format(e)) + return False, "更新插件失败,"+str(e) + def uninstall_plugin(self, name:str): name = name.upper() if name not in self.plugins: