video-stream/program/updater.py

99 lines
3.5 KiB
Python
Raw Normal View History

2022-01-31 12:41:47 +00:00
import os
2022-02-10 21:56:59 +00:00
import re
2022-01-31 12:41:47 +00:00
import sys
import asyncio
2022-02-10 21:56:59 +00:00
import subprocess
from asyncio import sleep
2022-01-31 12:41:47 +00:00
from git import Repo
2022-02-10 21:56:59 +00:00
from os import system, execle, environ
from git.exc import InvalidGitRepositoryError
2022-02-11 06:43:58 +00:00
from pyrogram.types import Message
from pyrogram import Client, filters
2022-02-10 21:56:59 +00:00
from config import UPSTREAM_REPO, BOT_USERNAME
2022-01-31 12:41:47 +00:00
2022-02-11 06:43:58 +00:00
from driver.core import bot
from driver.filters import command
from driver.decorators import bot_creator
2022-02-10 21:56:59 +00:00
from driver.database.dbqueue import get_active_chats, remove_active_chat
2022-01-31 12:41:47 +00:00
2022-02-10 09:46:08 +00:00
2022-02-10 21:56:59 +00:00
def gen_chlog(repo, diff):
upstream_repo_url = Repo().remotes[0].config_reader.get("url").replace(".git", "")
ac_br = repo.active_branch.name
2022-02-11 00:42:06 +00:00
ch_log = ""
tldr_log = ""
2022-02-10 21:56:59 +00:00
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
2022-01-31 12:41:47 +00:00
2022-02-10 21:56:59 +00:00
def updater():
2022-01-31 12:41:47 +00:00
try:
repo = Repo()
except InvalidGitRepositoryError:
2022-02-10 21:56:59 +00:00
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:
2022-02-10 21:56:59 +00:00
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)
2022-01-31 12:41:47 +00:00
2022-02-10 21:56:59 +00:00
@Client.on_message(command(["update", f"update@{BOT_USERNAME}"]) & ~filters.edited)
@bot_creator
async def update_bot(_, message: Message):
chat_id = message.chat.id
2022-02-11 06:18:44 +00:00
msg = await message.reply("❖ Checking updates...")
2022-02-10 21:56:59 +00:00
update_avail = updater()
if update_avail:
await msg.edit("✅ Update finished !\n\n• Bot restarting, back active again in 1-2 minutes.")
2022-02-11 06:18:44 +00:00
system("git pull -f && pip3 install --no-cache-dir -r requirements.txt")
2022-02-10 21:56:59 +00:00
execle(sys.executable, sys.executable, "main.py", environ)
return
2022-02-11 06:18:44 +00:00
await msg.edit(f"❖ bot is **up-to-date** with [main]({UPSTREAM_REPO}/tree/main) ❖", disable_web_page_preview=True)
2022-02-10 21:56:59 +00:00
@Client.on_message(command(["restart", f"restart@{BOT_USERNAME}"]) & ~filters.edited)
@bot_creator
2022-01-31 12:41:47 +00:00
async def restart_bot(_, message: Message):
2022-02-10 21:56:59 +00:00
served_chats = []
try:
chats = await get_active_chats()
for chat in chats:
served_chats.append(int(chat["chat_id"]))
2022-02-11 06:18:44 +00:00
except BaseException as e:
print(e)
2022-02-10 21:56:59 +00:00
for x in served_chats:
try:
2022-02-10 21:56:59 +00:00
await bot.send_message(
x,
f"💡 Bot server has just restarted !\n\n• Sorry for the inconveniences due to bot maintenance.",
)
2022-02-10 21:56:59 +00:00
await remove_active_chat(x)
2022-02-11 06:18:44 +00:00
except BaseException as e:
print(e)
msg = await message.reply("❖ Restarting bot...")
2022-02-10 21:56:59 +00:00
args = [sys.executable, "main.py"]
await msg.edit("✅ Bot restarted\n\n• now bot is working again.")
execle(sys.executable, *args, environ)
return