From b2b6f0d4daf32be6f7be068239a17426e411fc32 Mon Sep 17 00:00:00 2001 From: korrykatti Date: Sun, 7 Apr 2024 18:41:47 +0530 Subject: [PATCH] windows error handler --- main.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 2a2c2eb..b8408bc 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ from bs4 import BeautifulSoup import subprocess import os +import stat import shutil def read_version(): @@ -12,6 +13,29 @@ def read_version(): version = config_data.get('version', '0.0.0') return version + +# Define the onerror handler function +def onerror(func, path, exc_info): + """ + Error handler for ``shutil.rmtree``. + + If the error is due to an access error (read only file) + it attempts to add write permission and then retries. + + If the error is for another reason it re-raises the error. + + Usage : ``shutil.rmtree(path, onerror=onerror)`` + """ + # Is the error an access error? + if not os.access(path, os.W_OK): + # Attempt to add write permission + os.chmod(path, stat.S_IWUSR) + # Retry the operation + func(path) + else: + # If the error is for another reason, re-raise the error + raise + def get_external_version(url): response = requests.get(url) if response.status_code == 200: @@ -26,10 +50,8 @@ def close_window(window): window.destroy() def compare_versions(): - # Delete the "update" folder if it exists - update_folder = "update" - if os.path.exists(update_folder): - shutil.rmtree(update_folder) + # Call the function to delete the update folder + delete_update_folder() local_version = read_version() external_url = 'https://korrykatti.github.io/others/thunder/version.html' # Replace this with the actual URL of the external website