From 811edd2ca5d5cfc35dd10eceb128567ff06d7bcd Mon Sep 17 00:00:00 2001 From: Benex254 Date: Mon, 5 Aug 2024 09:47:00 +0300 Subject: [PATCH] feat(updater):implement basic script to update app --- fastanime/Utility/app_updater.py | 103 +++++++++++++++++++++++++++++++ fastanime/__init__.py | 9 ++- 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 fastanime/Utility/app_updater.py diff --git a/fastanime/Utility/app_updater.py b/fastanime/Utility/app_updater.py new file mode 100644 index 0000000..a4b4226 --- /dev/null +++ b/fastanime/Utility/app_updater.py @@ -0,0 +1,103 @@ +import pathlib +import re +import shlex +import shutil +import sys +from subprocess import PIPE, Popen + +import requests +from rich import print + +from .. import APP_NAME, AUTHOR, GIT_REPO, REPO, __version__ + +API_URL = f"https://api.{GIT_REPO}/repos/{AUTHOR}/{APP_NAME}/releases/latest" + + +def check_for_updates(): + USER_AGENT = f"{APP_NAME} user" + request = requests.get( + API_URL, + headers={ + "User-Agent": USER_AGENT, + "X-GitHub-Api-Version": "2022-11-28", + "Accept": "application/vnd.github+json", + }, + ) + + if request.status_code == 200: + release_json = request.json() + return (release_json["tag_name"] == __version__, release_json) + else: + print(request.text) + return (False, {}) + + +def is_git_repo(author, repository): + # Check if the current directory contains a .git folder + if not pathlib.Path("./.git").exists(): + return False + + repository_qualname = f"{author}/{repository}" + + # Read the .git/config file to find the remote repository URL + config_path = pathlib.Path("./.git/config") + if not config_path.exists(): + return False + + with open(config_path, "r") as git_config: + git_config_content = git_config.read() + + # Use regex to find the repository URL in the config file + repo_name_pattern = r"\[remote \"origin\"\]\s+url = .*\/([^/]+\/[^/]+)\.git" + match = re.search(repo_name_pattern, git_config_content) + + if match is None: + return False + + # Extract the repository name and compare with the expected repository_qualname + config_repo_name = match.group(1) + return config_repo_name == repository_qualname + + +def update_app(): + is_latest, release_json = check_for_updates() + if is_latest: + print("[green]App is up to date[/]") + return + tag_name = release_json["tag_name"] + + print("[cyan]Updating app to version %s[/]" % tag_name) + if is_git_repo(AUTHOR, APP_NAME): + executable = shutil.which("git") + args = [ + executable, + "pull", + ] + + print(f"Pulling latest changes from the repository via git: {shlex.join(args)}") + + if not executable: + return print("[red]Cannot find git.[/]") + + process = Popen( + args, + stdout=PIPE, + stderr=PIPE, + ) + + process.communicate() + else: + executable = sys.executable + + app_package_url = f"https://{REPO}/releases/download/{tag_name}/fastanime-{tag_name.replace("v","")}.tar.gz" + args = [ + executable, + "-m", + "pip", + "install", + app_package_url, + "--user", + "--no-warn-script-location", + ] + process = Popen(args) + process.communicate() diff --git a/fastanime/__init__.py b/fastanime/__init__.py index 8a6f39c..47e5e06 100644 --- a/fastanime/__init__.py +++ b/fastanime/__init__.py @@ -11,13 +11,15 @@ install(show_locals=True) logger = logging.getLogger(__name__) # initiate constants -__version__ = "0.3.0" +__version__ = "v0.30.0" PLATFORM = platform() APP_NAME = "FastAnime" AUTHOR = "Benex254" GIT_REPO = "github.com" REPO = f"{GIT_REPO}/{AUTHOR}/{APP_NAME}" +USER_NAME = os.environ.get("USERNAME", f"{APP_NAME} user") + dirs = PlatformDirs(appname=APP_NAME, appauthor=AUTHOR, ensure_exists=True) @@ -41,6 +43,11 @@ USER_DOWNLOADS_DIR = dirs.user_downloads_dir def FastAnime(gui=False): + if "--update" in sys.argv: + from .Utility.app_updater import update_app + + update_app() + sys.argv.remove("--update") if "--gui" in sys.argv: gui = True sys.argv.remove("--gui")