[check] fixed the updater and restarter
This commit is contained in:
parent
471835a619
commit
6386e0a8ae
@ -18,73 +18,73 @@ along with this program. If not, see <https://www.gnu.org/licenses/licenses.html
|
||||
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
from git import Repo
|
||||
from os import system, execle, environ
|
||||
from git.exc import InvalidGitRepositoryError
|
||||
from git.exc import InvalidGitRepositoryError, GitCommandError
|
||||
|
||||
from pyrogram.types import Message
|
||||
from pyrogram import Client, filters
|
||||
|
||||
from program import LOGS
|
||||
from config import UPSTREAM_REPO, BOT_USERNAME
|
||||
from config import UPSTREAM_BRANCH, UPSTREAM_REPO, BOT_USERNAME
|
||||
|
||||
from driver.filters import command
|
||||
from driver.decorators import bot_creator
|
||||
|
||||
|
||||
def gen_chlog(repo, diff):
|
||||
upstream_repo_url = Repo().remotes[0].config_reader.get("url").replace(".git", "")
|
||||
ac_br = repo.active_branch.name
|
||||
ch_log = ""
|
||||
tldr_log = ""
|
||||
ch = f"<b>updates for <a href={upstream_repo_url}/tree/{ac_br}>[{ac_br}]</a>:</b>"
|
||||
ch_tl = f"updates for {ac_br}:"
|
||||
d_form = "%d/%m/%y || %H:%M"
|
||||
for c in repo.iter_commits(diff):
|
||||
ch_log += (
|
||||
f"\n\n💬 <b>{c.count()}</b> 🗓 <b>[{c.committed_datetime.strftime(d_form)}]</b>\n<b>"
|
||||
f"<a href={upstream_repo_url.rstrip('/')}/commit/{c}>[{c.summary}]</a></b> 👨💻 <code>{c.author}</code>"
|
||||
)
|
||||
tldr_log += f"\n\n💬 {c.count()} 🗓 [{c.committed_datetime.strftime(d_form)}]\n[{c.summary}] 👨💻 {c.author}"
|
||||
if ch_log:
|
||||
return str(ch + ch_log), str(ch_tl + tldr_log)
|
||||
return ch_log, tldr_log
|
||||
|
||||
|
||||
def updater():
|
||||
try:
|
||||
repo = Repo()
|
||||
except InvalidGitRepositoryError:
|
||||
repo = Repo.init()
|
||||
origin = repo.create_remote("upstream", UPSTREAM_REPO)
|
||||
origin.fetch()
|
||||
repo.create_head("main", origin.refs.main)
|
||||
repo.heads.main.set_tracking_branch(origin.refs.main)
|
||||
repo.heads.main.checkout(True)
|
||||
ac_br = repo.active_branch.name
|
||||
if "upstream" in repo.remotes:
|
||||
ups_rem = repo.remote("upstream")
|
||||
else:
|
||||
ups_rem = repo.create_remote("upstream", UPSTREAM_REPO)
|
||||
ups_rem.fetch(ac_br)
|
||||
changelog, tl_chnglog = gen_chlog(repo, f"HEAD..upstream/{ac_br}")
|
||||
return bool(changelog)
|
||||
from driver.database.dbqueue import get_active_chats, remove_active_chat
|
||||
|
||||
|
||||
@Client.on_message(command(["update", f"update@{BOT_USERNAME}"]) & ~filters.edited)
|
||||
@bot_creator
|
||||
async def update_bot(_, message: Message):
|
||||
chat_id = message.chat.id
|
||||
msg = await message.reply("❖ Checking updates...")
|
||||
update_avail = updater()
|
||||
if update_avail:
|
||||
await msg.edit("✅ Update finished !\n\n• Bot restarting, back active again in 1 minutes.")
|
||||
system("git pull -f && pip3 install --no-cache-dir -r requirements.txt")
|
||||
execle(sys.executable, sys.executable, "main.py", environ)
|
||||
msg = await bot.send_message(chat_id, "❖ Checking updates...")
|
||||
try:
|
||||
repo = Repo()
|
||||
except GitCommandError:
|
||||
await msg.edit("Git command error !")
|
||||
return
|
||||
await msg.edit(f"❖ bot is **up-to-date** with [main]({UPSTREAM_REPO}/tree/main) ❖", disable_web_page_preview=True)
|
||||
except InvalidGitRepositoryError:
|
||||
await msg.edit("❖ Checking Git updates...")
|
||||
repo = Repo.init()
|
||||
if "origin" in repo.remotes:
|
||||
origin = repo.remote("origin")
|
||||
else:
|
||||
origin = repo.create_remote("origin", UPSTREAM_REPO)
|
||||
origin.fetch()
|
||||
repo.create_head(UPSTREAM_BRANCH, origin.refs[UPSTREAM_BRANCH])
|
||||
repo.heads[UPSTREAM_BRANCH].set_tracking_branch(
|
||||
origin.refs[UPSTREAM_BRANCH]
|
||||
)
|
||||
repo.heads[UPSTREAM_BRANCH].checkout(True)
|
||||
try:
|
||||
repo.create_remote("origin", UPSTREAM_REPO)
|
||||
except BaseException:
|
||||
pass
|
||||
nrs = repo.remote("origin")
|
||||
nrs.fetch(UPSTREAM_BRANCH)
|
||||
try:
|
||||
nrs.pull(UPSTREAM_BRANCH)
|
||||
except GitCommandError:
|
||||
repo.git.reset("--hard", "FETCH_HEAD")
|
||||
await install_requirements(
|
||||
"pip3 install --no-cache-dir -r requirements.txt"
|
||||
)
|
||||
await msg.edit("Done, bot has updated !\n\n> now the bot will be rebooted to apply the update and will active again in 5-10 second(s).")
|
||||
return
|
||||
served_chats = []
|
||||
try:
|
||||
chats = await get_active_chats()
|
||||
for chat in chats:
|
||||
served_chats.append(int(chat["chat_id"]))
|
||||
except BaseException:
|
||||
pass
|
||||
for x in served_chats:
|
||||
try:
|
||||
await remove_active_chat(x)
|
||||
except BaseException:
|
||||
pass
|
||||
return
|
||||
os.system(f"kill -9 {os.getpid()} && python3 main.py")
|
||||
|
||||
|
||||
@Client.on_message(command(["restart", f"restart@{BOT_USERNAME}"]) & ~filters.edited)
|
||||
@ -92,7 +92,7 @@ async def update_bot(_, message: Message):
|
||||
async def restart_bot(_, message: Message):
|
||||
try:
|
||||
msg = await message.reply_text("❖ Restarting bot...")
|
||||
LOGS.info("[INFO]: BOT SERVER RESTARTED !!")
|
||||
LOGS.info("[INFO]: >> BOT SERVER RESTARTED <<")
|
||||
except BaseException as err:
|
||||
LOGS.info(f"[ERROR]: {err}")
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user