video-stream/program/updater.py

78 lines
2.9 KiB
Python
Raw Normal View History

2021-11-15 10:54:11 +00:00
import os
2021-12-01 15:31:53 +00:00
import re
2021-11-15 10:54:11 +00:00
import sys
2021-12-01 15:31:53 +00:00
import asyncio
import subprocess
from asyncio import sleep
2021-11-15 10:54:11 +00:00
from git import Repo
from pyrogram.types import Message
from driver.filters import command
from pyrogram import Client, filters
from os import system, execle, environ
from driver.decorators import sudo_users_only
from git.exc import InvalidGitRepositoryError
from config import UPSTREAM_REPO, BOT_USERNAME
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)
@Client.on_message(command(["update", f"update@{BOT_USERNAME}"]) & ~filters.edited)
2021-11-15 22:22:53 +00:00
@sudo_users_only
2021-11-15 10:54:11 +00:00
async def update_repo(_, message: Message):
chat_id = message.chat.id
msg = await message.reply("🔄 `processing update...`")
update_avail = updater()
if update_avail:
await msg.edit("✅ update finished\n\n• bot restarted, back active again in 1 minutes.")
system("git pull -f && pip3 install -r requirements.txt")
execle(sys.executable, sys.executable, "main.py", environ)
return
2021-11-15 12:41:48 +00:00
await msg.edit("bot is **up-to-date** with [main](https://github.com/levina-lab/video-stream/tree/main)", disable_web_page_preview=True)
2021-11-15 22:22:53 +00:00
@Client.on_message(command(["restart", f"restart@{BOT_USERNAME}"]) & ~filters.edited)
@sudo_users_only
async def restart_bot(_, message: Message):
2021-12-02 05:44:30 +00:00
msg = await message.reply("`restarting bot...`")
args = [sys.executable, "main.py"]
await msg.edit("✅ bot restarted\n\n• now you can use this bot again.")
execle(sys.executable, *args, environ)
return