diff --git a/program/updater.py b/program/updater.py index d002c23..6e1067c 100644 --- a/program/updater.py +++ b/program/updater.py @@ -18,73 +18,73 @@ along with this program. If not, see updates for [{ac_br}]:" - 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šŸ’¬ {c.count()} šŸ—“ [{c.committed_datetime.strftime(d_form)}]\n" - f"[{c.summary}] šŸ‘Øā€šŸ’» {c.author}" - ) - 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