mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
fix(plugin_store): 修复插件商店的安装与卸载逻辑
- 优化了插件安装、更新和移除的逻辑 - 调整了插件路径的处理方式,支持更灵活的安装位置 - 重构了 `install_plugin_with_repo` 方法,使用 `StorePluginInfo` 对象作为参数 - 修复了一些潜在的路径问题和模块命名问题
This commit is contained in:
parent
fb0a9813e1
commit
a08700927b
@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import random
|
import random
|
||||||
import shutil
|
import shutil
|
||||||
@ -183,6 +184,8 @@ class StoreManager:
|
|||||||
StorePluginInfo: 插件信息
|
StorePluginInfo: 插件信息
|
||||||
bool: 是否是外部插件
|
bool: 是否是外部插件
|
||||||
"""
|
"""
|
||||||
|
plugin_list: list[StorePluginInfo]
|
||||||
|
extra_plugin_list: list[StorePluginInfo]
|
||||||
plugin_list, extra_plugin_list = await cls.get_data()
|
plugin_list, extra_plugin_list = await cls.get_data()
|
||||||
plugin_info = None
|
plugin_info = None
|
||||||
is_external = False
|
is_external = False
|
||||||
@ -206,6 +209,10 @@ class StoreManager:
|
|||||||
if is_remove:
|
if is_remove:
|
||||||
if plugin_info.module not in modules:
|
if plugin_info.module not in modules:
|
||||||
raise PluginStoreException(f"插件 {plugin_info.name} 未安装,无法移除")
|
raise PluginStoreException(f"插件 {plugin_info.name} 未安装,无法移除")
|
||||||
|
if plugin_obj := await PluginInfo.get_or_none(
|
||||||
|
name=plugin_info.name, module=plugin_info.module
|
||||||
|
):
|
||||||
|
plugin_info.module_path = plugin_obj.module_path
|
||||||
return plugin_info, is_external
|
return plugin_info, is_external
|
||||||
|
|
||||||
if is_update:
|
if is_update:
|
||||||
@ -237,9 +244,7 @@ class StoreManager:
|
|||||||
plugin_info.github_url = f"{github_url_split[0]}/tree/{version_split[1]}"
|
plugin_info.github_url = f"{github_url_split[0]}/tree/{version_split[1]}"
|
||||||
logger.info(f"正在安装插件 {plugin_info.name}...", LOG_COMMAND)
|
logger.info(f"正在安装插件 {plugin_info.name}...", LOG_COMMAND)
|
||||||
await cls.install_plugin_with_repo(
|
await cls.install_plugin_with_repo(
|
||||||
plugin_info.github_url,
|
plugin_info,
|
||||||
plugin_info.module_path,
|
|
||||||
plugin_info.is_dir,
|
|
||||||
is_external,
|
is_external,
|
||||||
source,
|
source,
|
||||||
)
|
)
|
||||||
@ -248,9 +253,7 @@ class StoreManager:
|
|||||||
@classmethod
|
@classmethod
|
||||||
async def install_plugin_with_repo(
|
async def install_plugin_with_repo(
|
||||||
cls,
|
cls,
|
||||||
github_url: str,
|
plugin_info: StorePluginInfo,
|
||||||
module_path: str,
|
|
||||||
is_dir: bool,
|
|
||||||
is_external: bool = False,
|
is_external: bool = False,
|
||||||
source: str | None = None,
|
source: str | None = None,
|
||||||
):
|
):
|
||||||
@ -267,18 +270,26 @@ class StoreManager:
|
|||||||
repo_type = RepoType.ALIYUN
|
repo_type = RepoType.ALIYUN
|
||||||
elif source == "git":
|
elif source == "git":
|
||||||
repo_type = RepoType.GITHUB
|
repo_type = RepoType.GITHUB
|
||||||
replace_module_path = module_path.replace(".", "/")
|
module_path = plugin_info.module_path
|
||||||
plugin_name = module_path.split(".")[-1]
|
is_dir = plugin_info.is_dir
|
||||||
|
github_url = plugin_info.github_url
|
||||||
|
assert github_url
|
||||||
|
replace_module_path = module_path.replace(".", "/").lstrip("/")
|
||||||
|
plugin_name = module_path.split(".")[-1] or plugin_info.module
|
||||||
if is_dir:
|
if is_dir:
|
||||||
files = await RepoFileManager.list_directory_files(
|
files = await RepoFileManager.list_directory_files(
|
||||||
github_url, replace_module_path, repo_type=repo_type
|
github_url, replace_module_path, repo_type=repo_type
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
files = [RepoFileInfo(path=f"{replace_module_path}.py", is_dir=False)]
|
files = [RepoFileInfo(path=f"{replace_module_path}.py", is_dir=False)]
|
||||||
local_path = BASE_PATH / "plugins" if is_external else BASE_PATH
|
if not is_external:
|
||||||
|
target_dir = BASE_PATH
|
||||||
|
elif is_dir:
|
||||||
target_dir = BASE_PATH / "plugins" / plugin_name
|
target_dir = BASE_PATH / "plugins" / plugin_name
|
||||||
|
else:
|
||||||
|
target_dir = BASE_PATH / "plugins"
|
||||||
files = [file for file in files if not file.is_dir]
|
files = [file for file in files if not file.is_dir]
|
||||||
download_files = [(file.path, local_path / file.path) for file in files]
|
download_files = [(file.path, target_dir / file.path) for file in files]
|
||||||
result = await RepoFileManager.download_files(
|
result = await RepoFileManager.download_files(
|
||||||
github_url,
|
github_url,
|
||||||
download_files,
|
download_files,
|
||||||
@ -298,7 +309,7 @@ class StoreManager:
|
|||||||
|
|
||||||
is_install_req = False
|
is_install_req = False
|
||||||
for requirement_path in requirement_paths:
|
for requirement_path in requirement_paths:
|
||||||
requirement_file = local_path / requirement_path.path
|
requirement_file = target_dir / requirement_path.path
|
||||||
if requirement_file.exists():
|
if requirement_file.exists():
|
||||||
is_install_req = True
|
is_install_req = True
|
||||||
await VirtualEnvPackageManager.install_requirement(requirement_file)
|
await VirtualEnvPackageManager.install_requirement(requirement_file)
|
||||||
@ -341,13 +352,11 @@ class StoreManager:
|
|||||||
str: 返回消息
|
str: 返回消息
|
||||||
"""
|
"""
|
||||||
plugin_info, _ = await cls.get_plugin_by_value(index_or_module, is_remove=True)
|
plugin_info, _ = await cls.get_plugin_by_value(index_or_module, is_remove=True)
|
||||||
path = BASE_PATH
|
module_path = plugin_info.module_path
|
||||||
if plugin_info.github_url:
|
module = module_path.split(".")[-1]
|
||||||
path = BASE_PATH / "plugins"
|
path = BASE_PATH.parent / Path(module_path.replace(".", os.sep))
|
||||||
for p in plugin_info.module_path.split("."):
|
|
||||||
path = path / p
|
|
||||||
if not plugin_info.is_dir:
|
if not plugin_info.is_dir:
|
||||||
path = Path(f"{path}.py")
|
path = path.parent / f"{module}.py"
|
||||||
if not path.exists():
|
if not path.exists():
|
||||||
return f"插件 {plugin_info.name} 不存在..."
|
return f"插件 {plugin_info.name} 不存在..."
|
||||||
logger.debug(f"尝试移除插件 {plugin_info.name} 文件: {path}", LOG_COMMAND)
|
logger.debug(f"尝试移除插件 {plugin_info.name} 文件: {path}", LOG_COMMAND)
|
||||||
@ -356,7 +365,7 @@ class StoreManager:
|
|||||||
shutil.rmtree(path, onerror=win_on_rm_error)
|
shutil.rmtree(path, onerror=win_on_rm_error)
|
||||||
else:
|
else:
|
||||||
path.unlink()
|
path.unlink()
|
||||||
await PluginInitManager.remove(f"zhenxun.{plugin_info.module_path}")
|
await PluginInitManager.remove(module_path)
|
||||||
return f"插件 {plugin_info.name} 移除成功! 重启后生效"
|
return f"插件 {plugin_info.name} 移除成功! 重启后生效"
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -423,9 +432,7 @@ class StoreManager:
|
|||||||
if plugin_info.github_url is None:
|
if plugin_info.github_url is None:
|
||||||
plugin_info.github_url = DEFAULT_GITHUB_URL
|
plugin_info.github_url = DEFAULT_GITHUB_URL
|
||||||
await cls.install_plugin_with_repo(
|
await cls.install_plugin_with_repo(
|
||||||
plugin_info.github_url,
|
plugin_info,
|
||||||
plugin_info.module_path,
|
|
||||||
plugin_info.is_dir,
|
|
||||||
is_external,
|
is_external,
|
||||||
)
|
)
|
||||||
return f"插件 {plugin_info.name} 更新成功! 重启后生效"
|
return f"插件 {plugin_info.name} 更新成功! 重启后生效"
|
||||||
@ -473,9 +480,7 @@ class StoreManager:
|
|||||||
plugin_info.github_url = DEFAULT_GITHUB_URL
|
plugin_info.github_url = DEFAULT_GITHUB_URL
|
||||||
is_external = False
|
is_external = False
|
||||||
await cls.install_plugin_with_repo(
|
await cls.install_plugin_with_repo(
|
||||||
plugin_info.github_url,
|
plugin_info,
|
||||||
plugin_info.module_path,
|
|
||||||
plugin_info.is_dir,
|
|
||||||
is_external,
|
is_external,
|
||||||
)
|
)
|
||||||
update_success_list.append(plugin_info.name)
|
update_success_list.append(plugin_info.name)
|
||||||
|
|||||||
@ -87,7 +87,7 @@ class PluginInitManager:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def remove(cls, module_path: str):
|
async def remove(cls, module_path: str):
|
||||||
"""运行指定插件安装方法"""
|
"""运行指定插件移除方法"""
|
||||||
if model := cls.plugins.get(module_path):
|
if model := cls.plugins.get(module_path):
|
||||||
if model.remove:
|
if model.remove:
|
||||||
class_ = model.class_()
|
class_ = model.class_()
|
||||||
|
|||||||
@ -326,7 +326,7 @@ class RepoFileManager:
|
|||||||
|
|
||||||
# 获取仓库树信息
|
# 获取仓库树信息
|
||||||
strategy = GitHubStrategy()
|
strategy = GitHubStrategy()
|
||||||
strategy.body = await GitHubStrategy.parse_repo_info(repo_info)
|
strategy.body = await strategy.parse_repo_info(repo_info)
|
||||||
|
|
||||||
# 处理目录路径,确保格式正确
|
# 处理目录路径,确保格式正确
|
||||||
if directory_path and not directory_path.endswith("/") and recursive:
|
if directory_path and not directory_path.endswith("/") and recursive:
|
||||||
@ -480,7 +480,7 @@ class RepoFileManager:
|
|||||||
target_dir: Path | None = None,
|
target_dir: Path | None = None,
|
||||||
) -> FileDownloadResult:
|
) -> FileDownloadResult:
|
||||||
"""
|
"""
|
||||||
下载单个文件
|
下载多个文件
|
||||||
|
|
||||||
参数:
|
参数:
|
||||||
repo_url: 仓库URL
|
repo_url: 仓库URL
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user