diff --git a/updater.py b/updater.py index d23e9eb..32bf71e 100644 --- a/updater.py +++ b/updater.py @@ -1,69 +1,111 @@ -import requests, gdown, zipfile, os, ctypes, tempfile, shutil, subprocess, sys, json, logging +import requests, gdown, zipfile, os, tempfile, shutil, subprocess, sys, json, logging, webbrowser from tkinter import messagebox, filedialog + def terminate(): logger.error("Terminating the program") sys.exit() + def get_appdata_path(): - appdata_path = os.getenv('APPDATA') - folder_path = os.path.join(appdata_path, 'Keychron mice updater') - + appdata_path = os.getenv("APPDATA") + folder_path = os.path.join(appdata_path, "Keychron mice updater") + if not os.path.exists(folder_path): os.makedirs(folder_path) - + return folder_path + def setup_logger(): - folder_path = get_appdata_path() - log_file_path = os.path.join(folder_path, 'updater.log') + log_file_path = os.path.join(get_appdata_path(), "updater.log") logging.basicConfig( filename=log_file_path, - format='%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)d - %(message)s', + format="%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)d - %(message)s", level=logging.INFO, - encoding='utf-8' + encoding="utf-8", ) logger = logging.getLogger(__name__) return logger + logger = setup_logger() + +def own_update_manager(): + try: + online_GH_version = float( + requests.get( + "https://api.github.com/repos/Pyenb/Keychron_mice_software_updater/releases/latest" + ) + .json()["tag_name"] + .replace("v", "") + ) + if online_GH_version > VERSION: + logger.info("New version of the updater available") + result = messagebox.askquestion( + "New updater version available", + f"Version {online_GH_version} of the updater is available. Do you want to check it out?", + ) + if result == "yes": + logger.info("User selected to update the updater") + webbrowser.open( + "https://github.com/Pyenb/Keychron_mice_software_updater/releases/latest" + ) + else: + logger.info("User selected not to update the updater") + except Exception as e: + logging.error(f"Failed to check for new updater version: {e}") + + def config_manager(): - folder_path = get_appdata_path() - config_file = os.path.join(folder_path, "config.json") + config_file = os.path.join(get_appdata_path(), "config.json") if os.path.exists(config_file): with open(config_file, "r") as f: install_path = json.load(f)["install_path"] - if os.path.exists(os.path.join(install_path, 'config.xml')): + if os.path.exists(os.path.join(install_path, "config.xml")): return install_path else: - logger.warning('config.xml not found in install_path') + logger.warning(f"config.xml not found in {install_path}") install_path = get_install_path() with open(config_file, "w") as f: json.dump({"install_path": install_path}, f) - logger.info('install_path written to config.json') + logger.info("install_path written to config.json") return install_path + def get_install_path(): default_path = r"C:\Program Files (x86)\Keychron" - if not os.path.exists(os.path.join(default_path, 'config.xml')): - logger.warning(f"Keychron software is not installed in the default location: {default_path}") - messagebox.showerror("Error", f"Keychron software is not installed in the default location: {default_path}") - messagebox.showinfo("Info", "Please select the Keychron software installation folder") + if not os.path.exists(os.path.join(default_path, "config.xml")): + logger.warning( + f"Keychron software is not installed in the default location: {default_path}" + ) + messagebox.showerror( + "Error", + f"Keychron software is not installed in the default location: {default_path}", + ) + messagebox.showinfo( + "Info", "Please select the Keychron software installation folder" + ) install_path = filedialog.askdirectory().replace("/", "\\") logger.info(f"User selected installation folder: {install_path}") return install_path return default_path + def get_installed_version(install_path): try: with open(install_path + "\\config.xml") as f: - installed_version = f.read().split('