diff --git a/main.py b/main.py index 60125cf9a..1afb47964 100644 --- a/main.py +++ b/main.py @@ -23,32 +23,23 @@ logo_tmpl = """ """ def make_necessary_dirs(): + ''' + 创建必要的目录。 + ''' os.makedirs("data/config", exist_ok=True) os.makedirs("temp", exist_ok=True) -def main(): +def update_dept(): + ''' + 更新依赖库。 + ''' + # 获取 Python 可执行文件路径 + py = sys.executable + # 更新依赖库 + mirror = "https://mirrors.aliyun.com/pypi/simple/" + os.system(f"{py} -m pip install -r requirements.txt -i {mirror}") - logger = LogManager.GetLogger( - log_name='astrbot-core', - out_to_console=True, - # HTTPpost_url='http://localhost:6185/api/log', - # http_mode = True, - custom_formatter=Formatter('[%(asctime)s| %(name)s - %(levelname)s|%(filename)s:%(lineno)d]: %(message)s', datefmt="%H:%M:%S") - ) - logger.info(logo_tmpl) - - # 设置代理 - from util.cmd_config import CmdConfig - cc = CmdConfig() - http_proxy = cc.get("http_proxy") - https_proxy = cc.get("https_proxy") - logger.info(f"使用代理: {http_proxy}, {https_proxy}") - if http_proxy: - os.environ['HTTP_PROXY'] = http_proxy - if https_proxy: - os.environ['HTTPS_PROXY'] = https_proxy - os.environ['NO_PROXY'] = 'https://api.sgroup.qq.com' - +def main(): try: import botpy, logging import astrbot.core as bot_core @@ -87,6 +78,26 @@ def check_env(): exit() if __name__ == "__main__": + logger = LogManager.GetLogger( + log_name='astrbot-core', + out_to_console=True, + custom_formatter=Formatter('[%(asctime)s| %(name)s - %(levelname)s|%(filename)s:%(lineno)d]: %(message)s', datefmt="%H:%M:%S") + ) + logger.info(logo_tmpl) + + # 设置代理 + from util.cmd_config import CmdConfig + cc = CmdConfig() + http_proxy = cc.get("http_proxy") + https_proxy = cc.get("https_proxy") + logger.info(f"使用代理: {http_proxy}, {https_proxy}") + if http_proxy: + os.environ['HTTP_PROXY'] = http_proxy + if https_proxy: + os.environ['HTTPS_PROXY'] = https_proxy + os.environ['NO_PROXY'] = 'https://api.sgroup.qq.com' + + update_dept() check_env() t = threading.Thread(target=main, daemon=True) t.start() diff --git a/util/plugin_util.py b/util/plugin_util.py index d8bf57a60..4da9a89fc 100644 --- a/util/plugin_util.py +++ b/util/plugin_util.py @@ -1,7 +1,7 @@ ''' 插件工具函数 ''' -import os +import os, sys import inspect import shutil import stat @@ -12,9 +12,13 @@ try: except ImportError: pass from types import ModuleType -from pip._internal import main as pipmain from type.plugin import * from type.register import * +from SparkleLogging.utils.core import LogManager +from logging import Logger + +logger: Logger = LogManager.GetLogger(log_name='astrbot-core') + # 找出模块里所有的类名 @@ -56,29 +60,35 @@ def get_modules(path): def get_plugin_store_path(): - if os.path.exists("addons/plugins"): - return "addons/plugins" - elif os.path.exists("QQChannelChatGPT/addons/plugins"): - return "QQChannelChatGPT/addons/plugins" - elif os.path.exists("AstrBot/addons/plugins"): - return "AstrBot/addons/plugins" - else: - raise FileNotFoundError("插件文件夹不存在。") - + plugin_dir = os.path.abspath(os.path.join(os.path.abspath(__file__), "../../addons/plugins")) + return plugin_dir def get_plugin_modules(): plugins = [] try: - if os.path.exists("addons/plugins"): - plugins = get_modules("addons/plugins") + plugin_dir = get_plugin_store_path() + if os.path.exists(plugin_dir): + plugins = get_modules(plugin_dir) return plugins - elif os.path.exists("QQChannelChatGPT/addons/plugins"): - plugins = get_modules("QQChannelChatGPT/addons/plugins") - return plugins - else: - return None except BaseException as e: raise e + +def check_plugin_dept_update(cached_plugins: RegisteredPlugins, target_plugin: str = None): + plugin_dir = get_plugin_store_path() + if not os.path.exists(plugin_dir): + return False + to_update = [] + if target_plugin: + to_update.append(target_plugin) + else: + for p in cached_plugins: + to_update.append(p.root_dir_name) + for p in to_update: + plugin_path = os.path.join(plugin_dir, p) + if os.path.exists(os.path.join(plugin_path, "requirements.txt")): + pth = os.path.join(plugin_path, "requirements.txt") + logger.info(f"正在检查更新插件 {p} 的依赖: {pth}") + update_plugin_dept(os.path.join(plugin_path, "requirements.txt")) def plugin_reload(cached_plugins: RegisteredPlugins): @@ -97,6 +107,8 @@ def plugin_reload(cached_plugins: RegisteredPlugins): module_path = plugin['module_path'] root_dir_name = plugin['pname'] + check_plugin_dept_update(cached_plugins, root_dir_name) + module = __import__("addons.plugins." + root_dir_name + "." + p, fromlist=[p]) @@ -144,6 +156,11 @@ def plugin_reload(cached_plugins: RegisteredPlugins): return True, None else: return False, fail_rec + +def update_plugin_dept(path): + mirror = "https://mirrors.aliyun.com/pypi/simple/" + py = sys.executable + os.system(f"{py} -m pip install -r {path} -i {mirror} --quiet") def install_plugin(repo_url: str, cached_plugins: RegisteredPlugins): @@ -155,15 +172,12 @@ def install_plugin(repo_url: str, cached_plugins: RegisteredPlugins): d = repo_url.split("/")[-1] # 转换非法字符:- d = d.replace("-", "_") + d = d.lower() # 转换为小写 # 创建文件夹 plugin_path = os.path.join(ppath, d) if os.path.exists(plugin_path): remove_dir(plugin_path) Repo.clone_from(repo_url, to_path=plugin_path, branch='master') - # 读取插件的requirements.txt - if os.path.exists(os.path.join(plugin_path, "requirements.txt")): - if pipmain(['install', '-r', os.path.join(plugin_path, "requirements.txt"), '--quiet']) != 0: - raise Exception("插件的依赖安装失败, 需要您手动 pip 安装对应插件的依赖。") ok, err = plugin_reload(cached_plugins) if not ok: raise Exception(err) @@ -198,11 +212,6 @@ def update_plugin(plugin_name: str, cached_plugins: RegisteredPlugins): plugin_path = os.path.join(ppath, root_dir_name) repo = Repo(path=plugin_path) repo.remotes.origin.pull() - # 读取插件的requirements.txt - if os.path.exists(os.path.join(plugin_path, "requirements.txt")): - print("正在安装插件依赖...") - if pipmain(['install', '-r', os.path.join(plugin_path, "requirements.txt")]) != 0: - raise Exception("插件依赖安装失败, 需要您手动pip安装对应插件的依赖。") ok, err = plugin_reload(cached_plugins) if not ok: raise Exception(err)