2022-10-08 03:06:25 +00:00
|
|
|
import asyncio
|
2022-10-08 02:55:30 +00:00
|
|
|
import os
|
2022-10-08 03:06:25 +00:00
|
|
|
from sys import executable
|
2022-10-08 02:55:30 +00:00
|
|
|
|
|
|
|
from aiofiles import open as async_open
|
2023-03-14 01:27:22 +00:00
|
|
|
from telegram import Message, Update
|
2023-02-22 04:55:56 +00:00
|
|
|
from telegram.error import NetworkError
|
2023-03-14 01:27:22 +00:00
|
|
|
from telegram.ext import CallbackContext
|
2022-10-08 02:55:30 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
from core.plugin import Plugin, handler
|
2022-10-08 02:55:30 +00:00
|
|
|
from utils.helpers import execute
|
|
|
|
from utils.log import logger
|
|
|
|
|
2023-01-07 08:00:32 +00:00
|
|
|
try:
|
|
|
|
import ujson as jsonlib
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
import json as jsonlib
|
|
|
|
|
2022-10-08 02:55:30 +00:00
|
|
|
current_dir = os.getcwd()
|
|
|
|
|
|
|
|
UPDATE_DATA = os.path.join(current_dir, "data", "update.json")
|
|
|
|
|
|
|
|
|
|
|
|
class UpdatePlugin(Plugin):
|
2022-10-08 03:06:25 +00:00
|
|
|
def __init__(self):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.lock = asyncio.Lock()
|
2022-10-08 03:06:25 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
async def initialize(self) -> None:
|
2022-10-08 02:55:30 +00:00
|
|
|
if os.path.exists(UPDATE_DATA):
|
|
|
|
async with async_open(UPDATE_DATA) as file:
|
2023-01-07 08:00:32 +00:00
|
|
|
data = jsonlib.loads(await file.read())
|
2022-10-08 02:55:30 +00:00
|
|
|
try:
|
2023-03-14 01:27:22 +00:00
|
|
|
reply_text = Message.de_json(data, self.application.telegram.bot)
|
2022-10-08 02:55:30 +00:00
|
|
|
await reply_text.edit_text("重启成功")
|
2023-02-22 04:55:56 +00:00
|
|
|
except NetworkError as exc:
|
2023-03-14 01:27:22 +00:00
|
|
|
logger.error("编辑消息出现错误 %s", exc.message)
|
|
|
|
except jsonlib.JSONDecodeError:
|
|
|
|
logger.error("JSONDecodeError")
|
2023-02-22 04:55:56 +00:00
|
|
|
except KeyError as exc:
|
2023-03-14 01:27:22 +00:00
|
|
|
logger.error("编辑消息出现错误", exc_info=exc)
|
2022-10-08 02:55:30 +00:00
|
|
|
os.remove(UPDATE_DATA)
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
@handler.command("update", block=False, admin=True)
|
2022-10-08 02:55:30 +00:00
|
|
|
async def update(self, update: Update, context: CallbackContext):
|
|
|
|
user = update.effective_user
|
|
|
|
message = update.effective_message
|
2023-03-14 01:27:22 +00:00
|
|
|
args = self.get_args(context)
|
|
|
|
logger.info("用户 %s[%s] update命令请求", user.full_name, user.id)
|
|
|
|
if self.lock.locked():
|
2022-10-08 03:06:25 +00:00
|
|
|
await message.reply_text("程序正在更新 请勿重复操作")
|
2022-10-08 03:14:03 +00:00
|
|
|
return
|
2023-03-14 01:27:22 +00:00
|
|
|
async with self.lock:
|
2022-10-08 03:06:25 +00:00
|
|
|
reply_text = await message.reply_text("正在更新")
|
2022-12-25 13:26:08 +00:00
|
|
|
logger.info("正在更新代码")
|
2022-10-08 03:06:25 +00:00
|
|
|
await execute("git fetch --all")
|
|
|
|
if len(args) > 0:
|
|
|
|
await execute("git reset --hard origin/main")
|
|
|
|
await execute("git pull --all")
|
2023-09-09 14:10:58 +00:00
|
|
|
await execute("git submodule update")
|
|
|
|
if len(args) > 1:
|
2022-10-08 03:06:25 +00:00
|
|
|
await execute(f"{executable} -m poetry install --extras all")
|
2022-12-25 13:26:08 +00:00
|
|
|
logger.info("更新成功 正在重启")
|
2022-10-08 03:06:25 +00:00
|
|
|
await reply_text.edit_text("更新成功 正在重启")
|
2022-10-10 11:07:28 +00:00
|
|
|
async with async_open(UPDATE_DATA, mode="w", encoding="utf-8") as file:
|
2022-10-08 03:06:25 +00:00
|
|
|
await file.write(reply_text.to_json())
|
2022-10-08 02:55:30 +00:00
|
|
|
raise SystemExit
|