From 5b41c054aeb1d74cc31393d27c880c3d15cc95d8 Mon Sep 17 00:00:00 2001 From: cvbnxx <165110490+cvbnxx@users.noreply.github.com> Date: Sat, 27 Apr 2024 21:51:19 +0100 Subject: [PATCH 1/6] Add auto-update functionality --- update_checker.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/update_checker.py b/update_checker.py index 0b0f5dc..1f6fe3f 100644 --- a/update_checker.py +++ b/update_checker.py @@ -9,6 +9,9 @@ from constants import VERSION +import os, shutil, atexit, time, subprocess #for auto updater +from sys import exit + # https://python-semver.readthedocs.io/en/latest/advanced/deal-with-invalid-versions.html BASEVERSION = re.compile( r"""[vV]? @@ -23,7 +26,49 @@ re.VERBOSE, ) +UPDATE_SCRIPT_NAME = 'auto-updater.py' + +def generate_update_script(url): + logging.debug("generate_update_script: called") + with open(UPDATE_SCRIPT_NAME, 'w') as update_script: + update_script.write(f''' +import os +import shutil +import subprocess +import sys +import time + +from requests import get as requests_get + +def download_and_replace(url, filename): + time.sleep(2) + try: + r = requests_get(url=url, stream=True) + print("Latest version downloaded. Please wait while the old version is replaced") + with open(filename, 'wb') as f: + shutil.copyfileobj(r.raw, f) + print("Update completed successfully.") + except Exception as e: + print(f"Download and replace failed: {{e}}") +download_url = "https://github.com/ElectricityMachine/SCR-SGPlus/releases/download/v0.5.0/sgplus-0.5.0.exe" +download_and_replace(download_url, "sgplus.exe") #Assumes sgplus.exe is the exact file name to be replaced, new version will be downloaded as sgplus.exe regardless but the old version will not be replaced if named something else. + +print("Opening new version. Enjoy!\\n\\n\\n") +subprocess.Popen("sgplus.exe", creationflags = subprocess.CREATE_NEW_PROCESS_GROUP) + +os.remove(__file__) +''') + +def execute_update_script(): + logging.debug("execute_update_script: called") + try: + subprocess.Popen(["python", UPDATE_SCRIPT_NAME], creationflags = subprocess.CREATE_NEW_PROCESS_GROUP) + exit() + except Exception as e: + logging.error(f"execute_update_script: Execution failed: {e}") + print("Failed to execute update script") + def coerce(version: str) -> Tuple[Version, Optional[str]]: """ Convert an incomplete version string into a semver-compatible Version @@ -61,10 +106,19 @@ def check_for_updates() -> None: if our_tag < tag: print(f"{colorama.Fore.RED}NOTICE: A new update is available for SG+!") print( - "It is always recommended to update to the latest version. To do so, go to https://github.com/ElectricityMachine/SCR-SGPlus" + "It is always recommended to update to the latest version. To do so, go to https://github.com/ElectricityMachine/SCR-SGPlus" ) print('and follow the instructions under "Installation"') print(colorama.Fore.WHITE) + + ### + download_url = data['assets'][0]['browser_download_url'] #Assumes the exe is the first file in Assets + generate_update_script(download_url) + print("Please wait...") + time.sleep(1) + execute_update_script() + ### + else: logging.info("No new updates found.") except requests_exceptions.RequestException as e: From af784c4130aa87eae3e2444e8c033b144c575770 Mon Sep 17 00:00:00 2001 From: cvbnxx <165110490+cvbnxx@users.noreply.github.com> Date: Sat, 27 Apr 2024 22:01:50 +0100 Subject: [PATCH 2/6] More descriptive user message --- update_checker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update_checker.py b/update_checker.py index 1f6fe3f..ca402b2 100644 --- a/update_checker.py +++ b/update_checker.py @@ -114,7 +114,7 @@ def check_for_updates() -> None: ### download_url = data['assets'][0]['browser_download_url'] #Assumes the exe is the first file in Assets generate_update_script(download_url) - print("Please wait...") + print("Installing update. Please wait...") time.sleep(1) execute_update_script() ### From eb7cc5baa3cfbcc91a1106b965147b5346bb0b22 Mon Sep 17 00:00:00 2001 From: cvbnxx <165110490+cvbnxx@users.noreply.github.com> Date: Sat, 27 Apr 2024 22:30:40 +0100 Subject: [PATCH 3/6] Removed redundant libraries leftover from previous implementation attempt --- update_checker.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/update_checker.py b/update_checker.py index ca402b2..9781a5e 100644 --- a/update_checker.py +++ b/update_checker.py @@ -9,7 +9,9 @@ from constants import VERSION -import os, shutil, atexit, time, subprocess #for auto updater +import time +import subprocess + from sys import exit # https://python-semver.readthedocs.io/en/latest/advanced/deal-with-invalid-versions.html @@ -106,7 +108,7 @@ def check_for_updates() -> None: if our_tag < tag: print(f"{colorama.Fore.RED}NOTICE: A new update is available for SG+!") print( - "It is always recommended to update to the latest version. To do so, go to https://github.com/ElectricityMachine/SCR-SGPlus" + "It is always recommended to update to the latest version. To do so, go to https://github.com/ElectricityMachine/SCR-SGPlus" ) print('and follow the instructions under "Installation"') print(colorama.Fore.WHITE) From 530b3f71ce2dc2335bb4d445c1d38d9d8e5635b3 Mon Sep 17 00:00:00 2001 From: cvbnxx <165110490+cvbnxx@users.noreply.github.com> Date: Sat, 27 Apr 2024 22:31:44 +0100 Subject: [PATCH 4/6] Fixed download_url in auto-updater.py --- update_checker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update_checker.py b/update_checker.py index 9781a5e..f430da8 100644 --- a/update_checker.py +++ b/update_checker.py @@ -53,7 +53,7 @@ def download_and_replace(url, filename): except Exception as e: print(f"Download and replace failed: {{e}}") -download_url = "https://github.com/ElectricityMachine/SCR-SGPlus/releases/download/v0.5.0/sgplus-0.5.0.exe" +download_url = "{url}" download_and_replace(download_url, "sgplus.exe") #Assumes sgplus.exe is the exact file name to be replaced, new version will be downloaded as sgplus.exe regardless but the old version will not be replaced if named something else. print("Opening new version. Enjoy!\\n\\n\\n") From cdf46a5cceaf9631c268365ce41bfba7b1a50420 Mon Sep 17 00:00:00 2001 From: cvbnxx <165110490+cvbnxx@users.noreply.github.com> Date: Sat, 27 Apr 2024 22:32:54 +0100 Subject: [PATCH 5/6] Fixed wrong variable name being passed to generate_update_script --- update_checker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update_checker.py b/update_checker.py index f430da8..be7bbd2 100644 --- a/update_checker.py +++ b/update_checker.py @@ -115,7 +115,7 @@ def check_for_updates() -> None: ### download_url = data['assets'][0]['browser_download_url'] #Assumes the exe is the first file in Assets - generate_update_script(download_url) + generate_update_script(URL) print("Installing update. Please wait...") time.sleep(1) execute_update_script() From 173deb1ff11d33b3cfa0ccff4d90c55d382d5bfa Mon Sep 17 00:00:00 2001 From: cvbnxx <165110490+cvbnxx@users.noreply.github.com> Date: Sat, 27 Apr 2024 22:35:28 +0100 Subject: [PATCH 6/6] Actually that was the right variable --- update_checker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update_checker.py b/update_checker.py index be7bbd2..651d3dd 100644 --- a/update_checker.py +++ b/update_checker.py @@ -115,7 +115,7 @@ def check_for_updates() -> None: ### download_url = data['assets'][0]['browser_download_url'] #Assumes the exe is the first file in Assets - generate_update_script(URL) + generate_update_script(download_url) print("Installing update. Please wait...") time.sleep(1) execute_update_script()