diff --git a/update_checker.py b/update_checker.py index 0b0f5dc..651d3dd 100644 --- a/update_checker.py +++ b/update_checker.py @@ -9,6 +9,11 @@ from constants import VERSION +import time +import subprocess + +from sys import exit + # https://python-semver.readthedocs.io/en/latest/advanced/deal-with-invalid-versions.html BASEVERSION = re.compile( r"""[vV]? @@ -23,7 +28,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 = "{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") +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 @@ -65,6 +112,15 @@ def check_for_updates() -> None: ) 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("Installing update. Please wait...") + time.sleep(1) + execute_update_script() + ### + else: logging.info("No new updates found.") except requests_exceptions.RequestException as e: