From a953479ebf0120b8123d24da5cab525a99dc0044 Mon Sep 17 00:00:00 2001 From: Vladless Date: Sat, 14 Jun 2025 06:23:31 +0300 Subject: [PATCH] make get_git_commit_number work under systemd --- bot.py | 57 ++++++++++++++++++++++++++++++++++++++++--------- cli_launcher.py | 20 +++++++++++------ 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/bot.py b/bot.py index 13334c64..868592b7 100644 --- a/bot.py +++ b/bot.py @@ -1,4 +1,5 @@ import traceback +import os from aiogram import Bot, Dispatcher from aiogram.client.default import DefaultBotProperties @@ -21,26 +22,65 @@ dp = Dispatcher(bot=bot, storage=storage) def get_git_commit_number() -> str: repo_url = "https://github.com/Vladless/Solo_bot" + cwd = os.path.abspath(os.path.dirname(__file__)) + + if not os.path.isdir(os.path.join(cwd, ".git")): + cwd = "/root/Prod/Solo_bot" + + env = os.environ.copy() + env["GIT_DIR"] = os.path.join(cwd, ".git") + env["GIT_WORK_TREE"] = cwd try: local_number = subprocess.check_output( - ["git", "rev-list", "--count", "HEAD"] + ["git", "rev-list", "--count", "HEAD"], + cwd=cwd, + env=env ).decode().strip() local_hash = subprocess.check_output( - ["git", "rev-parse", "HEAD"] + ["git", "rev-parse", "HEAD"], + cwd=cwd, + env=env ).decode().strip() - except Exception: - return "\n(Требуется обновление через CLI)" + + try: + branch = subprocess.check_output( + ["git", "rev-parse", "--abbrev-ref", "HEAD"], + cwd=cwd, + env=env + ).decode().strip() + + if branch == "HEAD": + describe = subprocess.check_output( + ["git", "describe", "--tags", "--exact-match"], + cwd=cwd, + env=env, + stderr=subprocess.DEVNULL + ).decode().strip() + + if describe.startswith("v") or "release" in describe.lower(): + branch = "main" + else: + branch = "dev" + except Exception: + branch = "dev" + + except Exception as e: + return f"\n(Требуется обновление через CLI: {e})" try: remote_commit = subprocess.check_output( - ["git", "ls-remote", "origin", "refs/heads/dev"] + ["git", "ls-remote", "origin", f"refs/heads/{branch}"], + cwd=cwd, + env=env ).decode() remote_hash = remote_commit.split()[0] remote_number = subprocess.check_output( - ["git", "rev-list", "--count", remote_hash] + ["git", "rev-list", "--count", remote_hash], + cwd=cwd, + env=env ).decode().strip() if local_hash == remote_hash: @@ -53,10 +93,7 @@ def get_git_commit_number() -> str: ) except Exception: - return ( - f"\n(commit " - f"#{local_number} / actual commit unknown)" - ) + return "" version = f"v4.3-Release{get_git_commit_number()}" diff --git a/cli_launcher.py b/cli_launcher.py index 1a921053..fdd42af2 100755 --- a/cli_launcher.py +++ b/cli_launcher.py @@ -101,6 +101,7 @@ def clean_project_dir_safe(update_buttons=False, update_img=False): preserved_paths = { os.path.join(PROJECT_DIR, "config.py"), os.path.join(PROJECT_DIR, "handlers", "texts.py"), + os.path.join(PROJECT_DIR, ".git"), } if not update_buttons: @@ -205,9 +206,7 @@ def update_from_beta(): f"[cyan]🔢 Локальная версия: {local_version} | Последняя в dev: {remote_version}[/cyan]" ) if local_version == remote_version: - if not Confirm.ask( - "[yellow]❗ Версия актуальна. Обновить всё равно?[/yellow]" - ): + if not Confirm.ask("[yellow]❗ Версия актуальна. Обновить всё равно?[/yellow]"): return else: console.print("[red]⚠️ Не удалось определить версии.[/red]") @@ -229,7 +228,8 @@ def update_from_beta(): os.chdir(PROJECT_DIR) console.print("[cyan]📅 Клонируем временный репозиторий...[/cyan]") subprocess.run(["rm", "-rf", TEMP_DIR]) - if os.system(f"git clone -b dev {GITHUB_REPO} {TEMP_DIR}") != 0: + + if os.system(f"git clone --depth=1000000 -b dev {GITHUB_REPO} {TEMP_DIR}") != 0: console.print("[red]❌ Ошибка при клонировании. Обновление отменено.[/red]") return @@ -243,6 +243,10 @@ def update_from_beta(): exclude_options += "--exclude=handlers/buttons.py " subprocess.run(f"rsync -a {exclude_options} {TEMP_DIR}/ {PROJECT_DIR}/", shell=True) + + if os.path.exists(os.path.join(TEMP_DIR, ".git")): + subprocess.run(["cp", "-r", os.path.join(TEMP_DIR, ".git"), PROJECT_DIR]) + subprocess.run(["rm", "-rf", TEMP_DIR]) fix_permissions() @@ -298,7 +302,7 @@ def update_from_release(): console.print(f"[cyan]📥 Клонируем релиз {tag_name} во временную папку...[/cyan]") subprocess.run(["rm", "-rf", TEMP_DIR]) subprocess.run( - f"git clone --depth 1 --branch {tag_name} {GITHUB_REPO} {TEMP_DIR}", + f"git clone --branch {tag_name} {GITHUB_REPO} {TEMP_DIR}", shell=True, check=True, ) @@ -314,6 +318,10 @@ def update_from_release(): exclude_options += "--exclude=handlers/buttons.py " subprocess.run(f"rsync -a {exclude_options} {TEMP_DIR}/ {PROJECT_DIR}/", shell=True) + + if os.path.exists(os.path.join(TEMP_DIR, ".git")): + subprocess.run(["cp", "-r", os.path.join(TEMP_DIR, ".git"), PROJECT_DIR]) + subprocess.run(["rm", "-rf", TEMP_DIR]) fix_permissions() @@ -351,7 +359,7 @@ def show_update_menu(): def show_menu(): table = Table( - title="Solobot CLI v0.1.8", title_style="bold magenta", header_style="bold blue" + title="Solobot CLI v0.2.0", title_style="bold magenta", header_style="bold blue" ) table.add_column("№", justify="center", style="cyan", no_wrap=True) table.add_column("Операция", style="white")