Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Leviaria authored Sep 13, 2024
2 parents 6a4d346 + 778ad0b commit d9c2d27
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 13 deletions.
45 changes: 45 additions & 0 deletions clashroyalebuildabot/utils/git_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import subprocess
import sys
from time import sleep

from loguru import logger


def _is_branch_late() -> bool:
subprocess.run(
["git", "fetch"], check=True, capture_output=True, text=True
)

status = subprocess.run(
["git", "status", "-uno"],
capture_output=True,
text=True,
check=True,
).stdout
return "Your branch is up to date" not in status


def _check_and_pull_updates() -> None:
if not _is_branch_late():
return

should_update = input(
"New updates available. Do you want to update the bot? [y]/n: "
)
if should_update.lower() not in ["y", "yes", ""]:
return
subprocess.run(["git", "pull"], check=True, capture_output=True, text=True)


def check_and_pull_updates() -> None:
try:
_check_and_pull_updates()
except subprocess.CalledProcessError as e:
if "not a git repository" in e.stderr:
err = "We recommend getting the project using git."
err += "You won't be able to get any updates until you do."
logger.warning(err)
sleep(3)
return
logger.error(f"Error while checking / pulling updates: {e.stderr}")
sys.exit(1)
45 changes: 32 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from datetime import datetime
import signal
import sys
import threading
import time

from loguru import logger
from PyQt6.QtWidgets import QApplication

from clashroyalebuildabot.actions import ArchersAction
from clashroyalebuildabot.actions import BabyDragonAction
Expand All @@ -12,9 +14,28 @@
from clashroyalebuildabot.actions import MinipekkaAction
from clashroyalebuildabot.actions import MusketeerAction
from clashroyalebuildabot.actions import WitchAction
from clashroyalebuildabot.gui.main_window import MainWindow
from clashroyalebuildabot.gui.utils import load_config
from clashroyalebuildabot.utils.logger import setup_logger
from clashroyalebuildabot.bot import Bot

start_time = datetime.now()

logger.remove()
logger.add(
sys.stderr,
format="{time} {level} {message}",
backtrace=False,
diagnose=False,
)


def update_terminal_title():
while True:
elapsed_time = datetime.now() - start_time
hours, remainder = divmod(elapsed_time.total_seconds(), 3600)
minutes, seconds = divmod(remainder, 60)
formatted_time = f"{int(hours):02}:{int(minutes):02}:{int(seconds):02}"
sys.stdout.write(f"\x1b]2;{formatted_time} | BuildABot\x07")
sys.stdout.flush()
time.sleep(1)


def main():
Expand All @@ -29,19 +50,17 @@ def main():
WitchAction,
]
try:
config = load_config()

app = QApplication(sys.argv)
window = MainWindow(config, actions)
setup_logger(window, config)

window.show()
sys.exit(app.exec())
bot = Bot(actions=actions)
bot.run()
except Exception as e:
logger.error(f"An error occurred in main loop: {e}")
logger.error(f"An error occurred: {e}")
sys.exit(1)


if __name__ == "__main__":
signal.signal(signal.SIGINT, signal.SIG_DFL)

title_thread = threading.Thread(target=update_terminal_title, daemon=True)
title_thread.start()

main()

0 comments on commit d9c2d27

Please sign in to comment.