mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-05 03:39:59 +08:00
✨ feat(env): 支持git更新
This commit is contained in:
@@ -104,25 +104,16 @@ class MemberUpdateManage:
|
||||
exist_member_list.append(member.id)
|
||||
if data_list[0]:
|
||||
try:
|
||||
await GroupInfoUser.bulk_create(data_list[0], 30)
|
||||
await GroupInfoUser.bulk_create(
|
||||
data_list[0], 30, ignore_conflicts=True
|
||||
)
|
||||
logger.debug(
|
||||
f"创建用户数据 {len(data_list[0])} 条",
|
||||
"更新群组成员信息",
|
||||
target=group_id,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"批量创建用户数据失败: {e},开始进行逐个存储",
|
||||
"更新群组成员信息",
|
||||
)
|
||||
for u in data_list[0]:
|
||||
try:
|
||||
await u.save()
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"创建用户 {u.user_name}({u.user_id}) 数据失败: {e}",
|
||||
"更新群组成员信息",
|
||||
)
|
||||
logger.error("批量创建用户数据失败", "更新群组成员信息", e=e)
|
||||
if data_list[1]:
|
||||
await GroupInfoUser.bulk_update(data_list[1], ["user_name"], 30)
|
||||
logger.debug(
|
||||
|
||||
@@ -32,15 +32,23 @@ __plugin_meta__ = PluginMetadata(
|
||||
检查更新真寻最新版本,包括了自动更新
|
||||
资源文件大小一般在130mb左右,除非必须更新一般仅更新代码文件
|
||||
指令:
|
||||
检查更新 [main|release|resource|webui] ?[-r]
|
||||
检查更新 [main|release|resource|webui] ?[-r] ?[-f] ?[-z] ?[-t]
|
||||
main: main分支
|
||||
release: 最新release
|
||||
resource: 资源文件
|
||||
webui: webui文件
|
||||
-r: 下载资源文件,一般在更新main或release时使用
|
||||
-f: 强制更新,一般用于更新main时使用(仅git更新时有效)
|
||||
-s: 更新源,为 git 或 ali(默认使用ali)
|
||||
-z: 下载zip文件进行更新(仅git有效)
|
||||
-t: 更新方式,git或download(默认使用git)
|
||||
git: 使用git pull(推荐)
|
||||
download: 通过commit hash比较文件后下载更新(仅git有效)
|
||||
|
||||
示例:
|
||||
检查更新 main
|
||||
检查更新 main -r
|
||||
检查更新 main -f
|
||||
检查更新 release -r
|
||||
检查更新 resource
|
||||
检查更新 webui
|
||||
@@ -57,6 +65,10 @@ _matcher = on_alconna(
|
||||
"检查更新",
|
||||
Args["ver_type?", ["main", "release", "resource", "webui"]],
|
||||
Option("-r|--resource", action=store_true, help_text="下载资源文件"),
|
||||
Option("-f|--force", action=store_true, help_text="强制更新"),
|
||||
Option("-s", Args["source?", ["git", "ali"]], help_text="更新源"),
|
||||
Option("-z|--zip", action=store_true, help_text="下载zip文件"),
|
||||
Option("-t", Args["update_type?", ["git", "download"]], help_text="更新方式"),
|
||||
),
|
||||
priority=1,
|
||||
block=True,
|
||||
@@ -71,6 +83,10 @@ async def _(
|
||||
session: Uninfo,
|
||||
ver_type: Match[str],
|
||||
resource: Query[bool] = Query("resource", False),
|
||||
force: Query[bool] = Query("force", False),
|
||||
source: Query[str] = Query("source", "ali"),
|
||||
zip: Query[bool] = Query("zip", False),
|
||||
update_type: Query[str] = Query("update_type", "git"),
|
||||
):
|
||||
result = ""
|
||||
await MessageUtils.build_message("正在进行检查更新...").send(reply_to=True)
|
||||
@@ -80,7 +96,15 @@ async def _(
|
||||
logger.info("查看当前版本...", "检查更新", session=session)
|
||||
await MessageUtils.build_message(result).finish()
|
||||
try:
|
||||
result = await UpdateManager.update(bot, session.user.id, ver_type.result)
|
||||
result = await UpdateManager.update(
|
||||
bot,
|
||||
session.user.id,
|
||||
ver_type.result,
|
||||
force.result,
|
||||
source.result,
|
||||
zip.result,
|
||||
update_type.result,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error("版本更新失败...", "检查更新", session=session, e=e)
|
||||
await MessageUtils.build_message(f"更新版本失败...e: {e}").finish()
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import tarfile
|
||||
import zipfile
|
||||
|
||||
@@ -12,7 +11,9 @@ from zhenxun.services.log import logger
|
||||
from zhenxun.utils.github_utils import GithubUtils
|
||||
from zhenxun.utils.github_utils.models import RepoInfo
|
||||
from zhenxun.utils.http_utils import AsyncHttpx
|
||||
from zhenxun.utils.manager.virtual_env_package_manager import VirtualEnvPackageManager
|
||||
from zhenxun.utils.platform import PlatformUtils
|
||||
from zhenxun.utils.repo_utils import AliyunRepoManager, GithubRepoManager
|
||||
|
||||
from .config import (
|
||||
BACKUP_PATH,
|
||||
@@ -22,6 +23,7 @@ from .config import (
|
||||
DEFAULT_GITHUB_URL,
|
||||
DOWNLOAD_GZ_FILE,
|
||||
DOWNLOAD_ZIP_FILE,
|
||||
GIT_GITHUB_URL,
|
||||
PYPROJECT_FILE,
|
||||
PYPROJECT_FILE_STRING,
|
||||
PYPROJECT_LOCK_FILE,
|
||||
@@ -35,26 +37,6 @@ from .config import (
|
||||
)
|
||||
|
||||
|
||||
def install_requirement():
|
||||
requirement_path = (REQ_TXT_FILE).absolute()
|
||||
|
||||
if not requirement_path.exists():
|
||||
logger.debug(
|
||||
f"没有找到zhenxun的requirement.txt,目标路径为{requirement_path}", COMMAND
|
||||
)
|
||||
return
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["pip", "install", "-r", str(requirement_path)],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
logger.debug(f"成功安装真寻依赖,日志:\n{result.stdout}", COMMAND)
|
||||
except subprocess.CalledProcessError as e:
|
||||
logger.error(f"安装真寻依赖失败,错误:\n{e.stderr}", COMMAND, e=e)
|
||||
|
||||
|
||||
@run_sync
|
||||
def _file_handle(latest_version: str | None):
|
||||
"""文件移动操作
|
||||
@@ -133,7 +115,6 @@ def _file_handle(latest_version: str | None):
|
||||
if latest_version:
|
||||
with open(VERSION_FILE, "w", encoding="utf8") as f:
|
||||
f.write(f"__version__: {latest_version}")
|
||||
install_requirement()
|
||||
|
||||
|
||||
class UpdateManager:
|
||||
@@ -185,17 +166,7 @@ class UpdateManager:
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def update(cls, bot: Bot, user_id: str, version_type: str) -> str:
|
||||
"""更新操作
|
||||
|
||||
参数:
|
||||
bot: Bot
|
||||
user_id: 用户id
|
||||
version_type: 更新版本类型
|
||||
|
||||
返回:
|
||||
str | None: 返回消息
|
||||
"""
|
||||
async def __zip_update(cls, version_type: str):
|
||||
logger.info("开始下载真寻最新版文件....", COMMAND)
|
||||
cur_version = cls.__get_version()
|
||||
url = None
|
||||
@@ -222,11 +193,6 @@ class UpdateManager:
|
||||
f"开始更新版本:{cur_version} -> {new_version} | 下载链接:{url}",
|
||||
COMMAND,
|
||||
)
|
||||
await PlatformUtils.send_superuser(
|
||||
bot,
|
||||
f"检测真寻已更新,版本更新:{cur_version} -> {new_version}\n开始更新...",
|
||||
user_id,
|
||||
)
|
||||
download_file = (
|
||||
DOWNLOAD_GZ_FILE if version_type == "release" else DOWNLOAD_ZIP_FILE
|
||||
)
|
||||
@@ -243,6 +209,65 @@ class UpdateManager:
|
||||
logger.debug("下载真寻最新版文件失败...", COMMAND)
|
||||
return ""
|
||||
|
||||
@classmethod
|
||||
async def update(
|
||||
cls,
|
||||
bot: Bot,
|
||||
user_id: str,
|
||||
version_type: str,
|
||||
force: bool,
|
||||
source: str,
|
||||
zip: bool,
|
||||
update_type: str,
|
||||
) -> str:
|
||||
"""更新操作
|
||||
|
||||
参数:
|
||||
bot: Bot
|
||||
user_id: 用户id
|
||||
version_type: 更新版本类型
|
||||
force: 是否强制更新
|
||||
source: 更新源
|
||||
zip: 是否下载zip文件
|
||||
update_type: 更新方式
|
||||
|
||||
返回:
|
||||
str | None: 返回消息
|
||||
"""
|
||||
cur_version = cls.__get_version()
|
||||
await PlatformUtils.send_superuser(
|
||||
bot,
|
||||
f"检测真寻已更新,当前版本:{cur_version}\n开始更新...",
|
||||
user_id,
|
||||
)
|
||||
if zip:
|
||||
return await cls.__zip_update(version_type)
|
||||
elif source == "git":
|
||||
result = await GithubRepoManager.update(
|
||||
GIT_GITHUB_URL,
|
||||
BASE_PATH,
|
||||
use_git=update_type == "git",
|
||||
force=force,
|
||||
)
|
||||
else:
|
||||
result = await AliyunRepoManager.update(
|
||||
GIT_GITHUB_URL,
|
||||
BASE_PATH,
|
||||
force=force,
|
||||
)
|
||||
if not result.success:
|
||||
return f"版本更新失败...错误: {result.error_message}"
|
||||
await PlatformUtils.send_superuser(
|
||||
bot, "真寻更新完成,开始安装依赖...", user_id
|
||||
)
|
||||
await VirtualEnvPackageManager.install_requirement(REQ_TXT_FILE)
|
||||
return (
|
||||
f"版本更新完成!\n"
|
||||
f"版本: {cur_version} -> {result.new_version}\n"
|
||||
f"变更文件个数: {len(result.changed_files)}\n"
|
||||
"请重新启动真寻以完成更新!"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def __get_version(cls) -> str:
|
||||
"""获取当前版本
|
||||
|
||||
@@ -2,6 +2,8 @@ from pathlib import Path
|
||||
|
||||
from zhenxun.configs.path_config import TEMP_PATH
|
||||
|
||||
GIT_GITHUB_URL = "https://github.com/zhenxun-org/zhenxun_bot.git"
|
||||
|
||||
DEFAULT_GITHUB_URL = "https://github.com/HibiKier/zhenxun_bot/tree/main"
|
||||
RELEASE_URL = "https://api.github.com/repos/HibiKier/zhenxun_bot/releases/latest"
|
||||
|
||||
|
||||
@@ -40,14 +40,14 @@ def row_style(column: str, text: str) -> RowStyle:
|
||||
return style
|
||||
|
||||
|
||||
def install_requirement(plugin_path: Path):
|
||||
async def install_requirement(plugin_path: Path):
|
||||
requirement_files = ["requirement.txt", "requirements.txt"]
|
||||
requirement_paths = [plugin_path / file for file in requirement_files]
|
||||
|
||||
if existing_requirements := next(
|
||||
(path for path in requirement_paths if path.exists()), None
|
||||
):
|
||||
VirtualEnvPackageManager.install_requirement(existing_requirements)
|
||||
await VirtualEnvPackageManager.install_requirement(existing_requirements)
|
||||
|
||||
|
||||
class StoreManager:
|
||||
@@ -288,7 +288,7 @@ class StoreManager:
|
||||
if not success:
|
||||
raise Exception("插件依赖文件下载失败")
|
||||
logger.debug(f"插件依赖文件列表: {req_paths}", LOG_COMMAND)
|
||||
install_requirement(plugin_path)
|
||||
await install_requirement(plugin_path)
|
||||
except ValueError as e:
|
||||
logger.warning("未获取到依赖文件路径...", e=e)
|
||||
return True
|
||||
|
||||
@@ -38,10 +38,11 @@ async def _(setting: Setting) -> Result:
|
||||
password = Config.get_config("web-ui", "password")
|
||||
if password or BotConfig.db_url:
|
||||
return Result.fail("配置已存在,请先删除DB_URL内容和前端密码再进行设置。")
|
||||
env_file = Path() / ".env.dev"
|
||||
env_file = Path() / ".env.example"
|
||||
if not env_file.exists():
|
||||
return Result.fail("配置文件.env.dev不存在。")
|
||||
env_text = env_file.read_text(encoding="utf-8")
|
||||
to_env_file = Path() / ".env.dev"
|
||||
if setting.db_url:
|
||||
if setting.db_url.startswith("sqlite"):
|
||||
base_dir = Path().resolve()
|
||||
@@ -78,7 +79,7 @@ async def _(setting: Setting) -> Result:
|
||||
if setting.username:
|
||||
Config.set_config("web-ui", "username", setting.username)
|
||||
Config.set_config("web-ui", "password", setting.password, True)
|
||||
env_file.write_text(env_text, encoding="utf-8")
|
||||
to_env_file.write_text(env_text, encoding="utf-8")
|
||||
if BAT_FILE.exists():
|
||||
for file in os.listdir(Path()):
|
||||
if file.startswith(FILE_NAME):
|
||||
|
||||
@@ -229,9 +229,9 @@ async def _(payload: InstallDependenciesPayload) -> Result:
|
||||
if not payload.dependencies:
|
||||
return Result.fail("依赖列表不能为空")
|
||||
if payload.handle_type == "install":
|
||||
result = VirtualEnvPackageManager.install(payload.dependencies)
|
||||
result = await VirtualEnvPackageManager.install(payload.dependencies)
|
||||
else:
|
||||
result = VirtualEnvPackageManager.uninstall(payload.dependencies)
|
||||
result = await VirtualEnvPackageManager.uninstall(payload.dependencies)
|
||||
return Result.ok(result)
|
||||
except Exception as e:
|
||||
logger.error(f"{router.prefix}/install_dependencies 调用错误", "WebUi", e=e)
|
||||
|
||||
Reference in New Issue
Block a user