From 6e7ec0ad8e46a37bbf509649fe6dcb3b309c5442 Mon Sep 17 00:00:00 2001 From: Saifeddine ALOUI Date: Mon, 9 Oct 2023 18:11:17 +0200 Subject: [PATCH] updated --- app.py | 56 ++++++++++++++++++++++++++++-------------------- update_script.py | 6 +++++- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/app.py b/app.py index 1c1c8039..8336b7bc 100644 --- a/app.py +++ b/app.py @@ -94,13 +94,43 @@ except Exception as ex: trace_exception(ex) run_update_script() - try: import mimetypes mimetypes.add_type('application/javascript', '.js') mimetypes.add_type('text/css', '.css') except: - ASCIIColors.yellow("Couldn't set mimetype") + ASCIIColors.yellow("Couldn't set mimetype") + +def check_update_(branch_name="main"): + try: + # Open the repository + repo_path = str(Path(__file__).parent) + ASCIIColors.yellow(f"Checking for updates from {repo_path}") + repo = git.Repo(repo_path) + + # Fetch updates from the remote for the specified branch + repo.remotes.origin.fetch(refspec=f"refs/heads/{branch_name}:refs/remotes/origin/{branch_name}") + + # Compare the local and remote commit IDs for the specified branch + local_commit = repo.head.commit + remote_commit = repo.remotes.origin.refs[branch_name].commit + + # Check if the local branch is behind the remote branch + is_behind = repo.is_ancestor(local_commit, remote_commit) + + ASCIIColors.yellow(f"update availability: {not is_behind}") + + # Return True if the local branch is behind the remote branch + return is_behind + except Exception as e: + # Handle any errors that may occur during the fetch process + # trace_exception(e) + return False + +if check_update_(): + run_update_script() + + log = logging.getLogger('werkzeug') @@ -138,27 +168,7 @@ def get_ip_address(): sock.close() -def check_update_(branch_name="main"): - try: - # Open the repository - repo_path = str(Path(__file__).parent) - ASCIIColors.yellow(f"Checking for updates from {repo_path}") - repo = git.Repo(repo_path) - - # Fetch updates from the remote for the specified branch - repo.remotes.origin.fetch(refspec=f"refs/heads/{branch_name}:refs/remotes/origin/{branch_name}") - - # Compare the local and remote commit IDs for the specified branch - local_commit = repo.head.commit - remote_commit = repo.remotes.origin.refs[branch_name].commit - - ASCIIColors.yellow(f"update availability: {local_commit != remote_commit}") - # Return True if there are updates, False otherwise - return local_commit != remote_commit - except Exception as e: - # Handle any errors that may occur during the fetch process - # trace_exception(e) - return False + def run_restart_script(args): diff --git a/update_script.py b/update_script.py index f410bb23..812fa2cb 100644 --- a/update_script.py +++ b/update_script.py @@ -4,14 +4,18 @@ import sys import git import subprocess import argparse +from pathlib import Path def run_git_pull(): try: - repo = git.Repo(".") + print("----------------> Updating the code <-----------------------") + repo = git.Repo(Path(__file__).parent) origin = repo.remotes.origin origin.pull() + return True except git.GitCommandError as e: print(f"Error during git pull: {e}") + return False def install_requirements(): try: