您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. # encoding:utf-8
  2. import importlib
  3. import importlib.util
  4. import json
  5. import os
  6. import sys
  7. from common.log import logger
  8. from common.singleton import singleton
  9. from common.sorted_dict import SortedDict
  10. from config import conf, write_plugin_config
  11. from .event import *
  12. @singleton
  13. class PluginManager:
  14. def __init__(self):
  15. self.plugins = SortedDict(lambda k, v: v.priority, reverse=True)
  16. self.listening_plugins = {}
  17. self.instances = {}
  18. self.pconf = {}
  19. self.current_plugin_path = None
  20. self.loaded = {}
  21. def register(self, name: str, desire_priority: int = 0, **kwargs):
  22. def wrapper(plugincls):
  23. plugincls.name = name
  24. plugincls.priority = desire_priority
  25. plugincls.desc = kwargs.get("desc")
  26. plugincls.author = kwargs.get("author")
  27. plugincls.path = self.current_plugin_path
  28. plugincls.version = kwargs.get("version") if kwargs.get("version") != None else "1.0"
  29. plugincls.namecn = kwargs.get("namecn") if kwargs.get("namecn") != None else name
  30. plugincls.hidden = kwargs.get("hidden") if kwargs.get("hidden") != None else False
  31. plugincls.enabled = True
  32. if self.current_plugin_path == None:
  33. raise Exception("Plugin path not set")
  34. self.plugins[name.upper()] = plugincls
  35. logger.info("Plugin %s_v%s registered, path=%s" % (name, plugincls.version, plugincls.path))
  36. return wrapper
  37. def save_config(self):
  38. with open("./plugins/plugins.json", "w", encoding="utf-8") as f:
  39. json.dump(self.pconf, f, indent=4, ensure_ascii=False)
  40. def load_config(self):
  41. logger.info("Loading plugins config...")
  42. modified = False
  43. if os.path.exists("./plugins/plugins.json"):
  44. with open("./plugins/plugins.json", "r", encoding="utf-8") as f:
  45. pconf = json.load(f)
  46. pconf["plugins"] = SortedDict(lambda k, v: v["priority"], pconf["plugins"], reverse=True)
  47. else:
  48. modified = True
  49. pconf = {"plugins": SortedDict(lambda k, v: v["priority"], reverse=True)}
  50. self.pconf = pconf
  51. if modified:
  52. self.save_config()
  53. return pconf
  54. @staticmethod
  55. def _load_all_config():
  56. """
  57. 背景: 目前插件配置存放于每个插件目录的config.json下,docker运行时不方便进行映射,故增加统一管理的入口,优先
  58. 加载 plugins/config.json,原插件目录下的config.json 不受影响
  59. 从 plugins/config.json 中加载所有插件的配置并写入 config.py 的全局配置中,供插件中使用
  60. 插件实例中通过 config.pconf(plugin_name) 即可获取该插件的配置
  61. """
  62. all_config_path = "./plugins/config.json"
  63. try:
  64. if os.path.exists(all_config_path):
  65. # read from all plugins config
  66. with open(all_config_path, "r", encoding="utf-8") as f:
  67. all_conf = json.load(f)
  68. logger.info(f"load all config from plugins/config.json: {all_conf}")
  69. # write to global config
  70. write_plugin_config(all_conf)
  71. except Exception as e:
  72. logger.error(e)
  73. def scan_plugins(self):
  74. logger.info("Scaning plugins ...")
  75. plugins_dir = "./plugins"
  76. raws = [self.plugins[name] for name in self.plugins]
  77. for plugin_name in os.listdir(plugins_dir):
  78. plugin_path = os.path.join(plugins_dir, plugin_name)
  79. if os.path.isdir(plugin_path):
  80. # 判断插件是否包含同名__init__.py文件
  81. main_module_path = os.path.join(plugin_path, "__init__.py")
  82. if os.path.isfile(main_module_path):
  83. # 导入插件
  84. import_path = "plugins.{}".format(plugin_name)
  85. try:
  86. self.current_plugin_path = plugin_path
  87. if plugin_path in self.loaded:
  88. if plugin_name.upper() != 'GODCMD':
  89. logger.info("reload module %s" % plugin_name)
  90. self.loaded[plugin_path] = importlib.reload(sys.modules[import_path])
  91. dependent_module_names = [name for name in sys.modules.keys() if name.startswith(import_path + ".")]
  92. for name in dependent_module_names:
  93. logger.info("reload module %s" % name)
  94. importlib.reload(sys.modules[name])
  95. else:
  96. self.loaded[plugin_path] = importlib.import_module(import_path)
  97. self.current_plugin_path = None
  98. except Exception as e:
  99. logger.warn("Failed to import plugin %s: %s" % (plugin_name, e))
  100. continue
  101. pconf = self.pconf
  102. news = [self.plugins[name] for name in self.plugins]
  103. new_plugins = list(set(news) - set(raws))
  104. modified = False
  105. for name, plugincls in self.plugins.items():
  106. rawname = plugincls.name
  107. if rawname not in pconf["plugins"]:
  108. modified = True
  109. logger.info("Plugin %s not found in pconfig, adding to pconfig..." % name)
  110. pconf["plugins"][rawname] = {
  111. "enabled": plugincls.enabled,
  112. "priority": plugincls.priority,
  113. }
  114. else:
  115. self.plugins[name].enabled = pconf["plugins"][rawname]["enabled"]
  116. self.plugins[name].priority = pconf["plugins"][rawname]["priority"]
  117. self.plugins._update_heap(name) # 更新下plugins中的顺序
  118. if modified:
  119. self.save_config()
  120. return new_plugins
  121. def refresh_order(self):
  122. for event in self.listening_plugins.keys():
  123. self.listening_plugins[event].sort(key=lambda name: self.plugins[name].priority, reverse=True)
  124. def activate_plugins(self): # 生成新开启的插件实例
  125. failed_plugins = []
  126. for name, plugincls in self.plugins.items():
  127. if plugincls.enabled:
  128. if 'GODCMD' in self.instances and name == 'GODCMD':
  129. continue
  130. # if name not in self.instances:
  131. try:
  132. instance = plugincls()
  133. except Exception as e:
  134. logger.warn("Failed to init %s, diabled. %s" % (name, e))
  135. self.disable_plugin(name)
  136. failed_plugins.append(name)
  137. continue
  138. self.instances[name] = instance
  139. for event in instance.handlers:
  140. if event not in self.listening_plugins:
  141. self.listening_plugins[event] = []
  142. self.listening_plugins[event].append(name)
  143. self.refresh_order()
  144. return failed_plugins
  145. def reload_plugin(self, name: str):
  146. name = name.upper()
  147. if name in self.instances:
  148. for event in self.listening_plugins:
  149. if name in self.listening_plugins[event]:
  150. self.listening_plugins[event].remove(name)
  151. del self.instances[name]
  152. self.activate_plugins()
  153. return True
  154. return False
  155. def load_plugins(self):
  156. self.load_config()
  157. self.scan_plugins()
  158. # 加载全量插件配置
  159. self._load_all_config()
  160. pconf = self.pconf
  161. logger.debug("plugins.json config={}".format(pconf))
  162. for name, plugin in pconf["plugins"].items():
  163. if name.upper() not in self.plugins:
  164. logger.error("Plugin %s not found, but found in plugins.json" % name)
  165. self.activate_plugins()
  166. def emit_event(self, e_context: EventContext, *args, **kwargs):
  167. if e_context.event in self.listening_plugins:
  168. for name in self.listening_plugins[e_context.event]:
  169. if self.plugins[name].enabled and e_context.action == EventAction.CONTINUE:
  170. logger.debug("Plugin %s triggered by event %s" % (name, e_context.event))
  171. instance = self.instances[name]
  172. instance.handlers[e_context.event](e_context, *args, **kwargs)
  173. if e_context.is_break():
  174. e_context["breaked_by"] = name
  175. logger.debug("Plugin %s breaked event %s" % (name, e_context.event))
  176. return e_context
  177. def set_plugin_priority(self, name: str, priority: int):
  178. name = name.upper()
  179. if name not in self.plugins:
  180. return False
  181. if self.plugins[name].priority == priority:
  182. return True
  183. self.plugins[name].priority = priority
  184. self.plugins._update_heap(name)
  185. rawname = self.plugins[name].name
  186. self.pconf["plugins"][rawname]["priority"] = priority
  187. self.pconf["plugins"]._update_heap(rawname)
  188. self.save_config()
  189. self.refresh_order()
  190. return True
  191. def enable_plugin(self, name: str):
  192. name = name.upper()
  193. if name not in self.plugins:
  194. return False, "插件不存在"
  195. if not self.plugins[name].enabled:
  196. self.plugins[name].enabled = True
  197. rawname = self.plugins[name].name
  198. self.pconf["plugins"][rawname]["enabled"] = True
  199. self.save_config()
  200. failed_plugins = self.activate_plugins()
  201. if name in failed_plugins:
  202. return False, "插件开启失败"
  203. return True, "插件已开启"
  204. return True, "插件已开启"
  205. def disable_plugin(self, name: str):
  206. name = name.upper()
  207. if name not in self.plugins:
  208. return False
  209. if self.plugins[name].enabled:
  210. self.plugins[name].enabled = False
  211. rawname = self.plugins[name].name
  212. self.pconf["plugins"][rawname]["enabled"] = False
  213. self.save_config()
  214. return True
  215. return True
  216. def list_plugins(self):
  217. return self.plugins
  218. def install_plugin(self, repo: str):
  219. try:
  220. import common.package_manager as pkgmgr
  221. pkgmgr.check_dulwich()
  222. except Exception as e:
  223. logger.error("Failed to install plugin, {}".format(e))
  224. return False, "无法导入dulwich,安装插件失败"
  225. import re
  226. from dulwich import porcelain
  227. logger.info("clone git repo: {}".format(repo))
  228. match = re.match(r"^(https?:\/\/|git@)([^\/:]+)[\/:]([^\/:]+)\/(.+).git$", repo)
  229. if not match:
  230. try:
  231. with open("./plugins/source.json", "r", encoding="utf-8") as f:
  232. source = json.load(f)
  233. if repo in source["repo"]:
  234. repo = source["repo"][repo]["url"]
  235. match = re.match(r"^(https?:\/\/|git@)([^\/:]+)[\/:]([^\/:]+)\/(.+).git$", repo)
  236. if not match:
  237. return False, "安装插件失败,source中的仓库地址不合法"
  238. else:
  239. return False, "安装插件失败,仓库地址不合法"
  240. except Exception as e:
  241. logger.error("Failed to install plugin, {}".format(e))
  242. return False, "安装插件失败,请检查仓库地址是否正确"
  243. dirname = os.path.join("./plugins", match.group(4))
  244. try:
  245. repo = porcelain.clone(repo, dirname, checkout=True)
  246. if os.path.exists(os.path.join(dirname, "requirements.txt")):
  247. logger.info("detect requirements.txt,installing...")
  248. pkgmgr.install_requirements(os.path.join(dirname, "requirements.txt"))
  249. return True, "安装插件成功,请使用 #scanp 命令扫描插件或重启程序,开启前请检查插件是否需要配置"
  250. except Exception as e:
  251. logger.error("Failed to install plugin, {}".format(e))
  252. return False, "安装插件失败," + str(e)
  253. def update_plugin(self, name: str):
  254. try:
  255. import common.package_manager as pkgmgr
  256. pkgmgr.check_dulwich()
  257. except Exception as e:
  258. logger.error("Failed to install plugin, {}".format(e))
  259. return False, "无法导入dulwich,更新插件失败"
  260. from dulwich import porcelain
  261. name = name.upper()
  262. if name not in self.plugins:
  263. return False, "插件不存在"
  264. if name in [
  265. "HELLO",
  266. "GODCMD",
  267. "ROLE",
  268. "TOOL",
  269. "BDUNIT",
  270. "BANWORDS",
  271. "FINISH",
  272. "DUNGEON",
  273. ]:
  274. return False, "预置插件无法更新,请更新主程序仓库"
  275. dirname = self.plugins[name].path
  276. try:
  277. porcelain.pull(dirname, "origin")
  278. if os.path.exists(os.path.join(dirname, "requirements.txt")):
  279. logger.info("detect requirements.txt,installing...")
  280. pkgmgr.install_requirements(os.path.join(dirname, "requirements.txt"))
  281. return True, "更新插件成功,请重新运行程序"
  282. except Exception as e:
  283. logger.error("Failed to update plugin, {}".format(e))
  284. return False, "更新插件失败," + str(e)
  285. def uninstall_plugin(self, name: str):
  286. name = name.upper()
  287. if name not in self.plugins:
  288. return False, "插件不存在"
  289. if name in self.instances:
  290. self.disable_plugin(name)
  291. dirname = self.plugins[name].path
  292. try:
  293. import shutil
  294. shutil.rmtree(dirname)
  295. rawname = self.plugins[name].name
  296. for event in self.listening_plugins:
  297. if name in self.listening_plugins[event]:
  298. self.listening_plugins[event].remove(name)
  299. del self.plugins[name]
  300. del self.pconf["plugins"][rawname]
  301. self.loaded[dirname] = None
  302. self.save_config()
  303. return True, "卸载插件成功"
  304. except Exception as e:
  305. logger.error("Failed to uninstall plugin, {}".format(e))
  306. return False, "卸载插件失败,请手动删除文件夹完成卸载," + str(e)