Refactor update process

This commit is contained in:
Hoang Hong Quan
2025-02-27 11:19:17 +07:00
parent 0ae4c5267b
commit 2e9bc2c429
+156 -33
View File
@@ -15,63 +15,186 @@ class Updater:
self.sha_version = os.path.join(os.path.dirname(os.path.realpath(__file__)), "sha_version.txt") self.sha_version = os.path.join(os.path.dirname(os.path.realpath(__file__)), "sha_version.txt")
self.download_repo_url = "https://github.com/lzhoang2801/OpCore-Simplify/archive/refs/heads/main.zip" self.download_repo_url = "https://github.com/lzhoang2801/OpCore-Simplify/archive/refs/heads/main.zip"
self.temporary_dir = tempfile.mkdtemp() self.temporary_dir = tempfile.mkdtemp()
self.current_step = 0
def get_current_sha_version(self): def get_current_sha_version(self):
current_sha_version = self.utils.read_file(self.sha_version) print("Checking current version...")
try:
current_sha_version = self.utils.read_file(self.sha_version)
if not current_sha_version: if not current_sha_version:
print("SHA version information is missing in the sha_version.txt file.\n") print("SHA version information is missing.")
return "0506fb67111d5c5230bf59b826c318ea4251dfc4" return "missing_sha_version"
return current_sha_version.decode() return current_sha_version.decode()
except Exception as e:
print("Error reading current SHA version: {}".format(str(e)))
return "error_reading_sha_version"
def get_latest_sha_version(self): def get_latest_sha_version(self):
latest_commit = self.github.get_latest_commit("lzhoang2801", "OpCore-Simplify") or {} print("Fetching latest version from GitHub...")
try:
latest_commit = self.github.get_latest_commit("lzhoang2801", "OpCore-Simplify")
if latest_commit and latest_commit.get("sha"):
return latest_commit.get("sha")
except Exception as e:
print("Error fetching latest SHA version: {}".format(str(e)))
return None
return latest_commit.get("sha") or "0506fb67111d5c5230bf59b826c318ea4251dfc4" print("Could not fetch latest commit information from GitHub.")
return None
def download_update(self): def download_update(self):
self.utils.create_folder(self.temporary_dir) self.current_step += 1
file_path = os.path.join(self.temporary_dir, os.path.basename(self.download_repo_url)) print("")
self.fetcher.download_and_save_file(self.download_repo_url, file_path) print("Step {}: Creating temporary directory...".format(self.current_step))
self.utils.extract_zip_file(file_path) try:
self.utils.create_folder(self.temporary_dir)
print(" Temporary directory created.")
self.current_step += 1
print("Step {}: Downloading update package...".format(self.current_step))
print(" ", end="")
file_path = os.path.join(self.temporary_dir, os.path.basename(self.download_repo_url))
self.fetcher.download_and_save_file(self.download_repo_url, file_path)
if os.path.exists(file_path) and os.path.getsize(file_path) > 0:
print(" Update package downloaded ({:.1f} KB)".format(os.path.getsize(file_path)/1024))
self.current_step += 1
print("Step {}: Extracting files...".format(self.current_step))
self.utils.extract_zip_file(file_path)
print(" Files extracted successfully")
return True
else:
print(" Download failed or file is empty")
return False
except Exception as e:
print(" Error during download/extraction: {}".format(str(e)))
return False
def update_files(self): def update_files(self):
target_dir = os.path.join(self.temporary_dir, "main", "OpCore-Simplify-main") self.current_step += 1
file_paths = self.utils.find_matching_paths(target_dir, type_filter="file") print("Step {}: Updating files...".format(self.current_step))
try:
target_dir = os.path.join(self.temporary_dir, "OpCore-Simplify-main")
if not os.path.exists(target_dir):
target_dir = os.path.join(self.temporary_dir, "main", "OpCore-Simplify-main")
for path, type in file_paths: if not os.path.exists(target_dir):
source = os.path.join(target_dir, path) print(" Could not locate extracted files directory")
destination = source.replace(target_dir, os.path.dirname(os.path.realpath(__file__))) return False
self.utils.create_folder(os.path.dirname(destination))
shutil.move(source, destination) file_paths = self.utils.find_matching_paths(target_dir, type_filter="file")
if ".command" in os.path.splitext(path)[-1] and os.name != "nt":
self.run({ total_files = len(file_paths)
"args":["chmod", "+x", destination] print(" Found {} files to update".format(total_files))
})
shutil.rmtree(self.temporary_dir) updated_count = 0
for index, (path, type) in enumerate(file_paths, start=1):
source = os.path.join(target_dir, path)
destination = source.replace(target_dir, os.path.dirname(os.path.realpath(__file__)))
self.utils.create_folder(os.path.dirname(destination))
print(" Updating [{}/{}]: {}".format(index, total_files, os.path.basename(path)), end="\r")
try:
shutil.move(source, destination)
updated_count += 1
if ".command" in os.path.splitext(path)[-1] and os.name != "nt":
self.run({
"args": ["chmod", "+x", destination]
})
except Exception as e:
print(" Failed to update {}: {}".format(path, str(e)))
print("")
print(" Successfully updated {}/{} files".format(updated_count, total_files))
self.current_step += 1
print("Step {}: Cleaning up temporary files...".format(self.current_step))
shutil.rmtree(self.temporary_dir)
print(" Cleanup complete")
return True
except Exception as e:
print(" Error during file update: {}".format(str(e)))
return False
def save_latest_sha_version(self, latest_sha): def save_latest_sha_version(self, latest_sha):
self.utils.write_file(self.sha_version, latest_sha.encode()) try:
self.utils.write_file(self.sha_version, latest_sha.encode())
self.current_step += 1
print("Step {}: Version information updated.".format(self.current_step))
return True
except Exception as e:
print("Failed to save version information: {}".format(str(e)))
return False
def run_update(self): def run_update(self):
self.utils.head("Check for Updates") self.utils.head("Check for Updates")
print("") print("")
current_sha_version = self.get_current_sha_version() current_sha_version = self.get_current_sha_version()
latest_sha_version = self.get_latest_sha_version() latest_sha_version = self.get_latest_sha_version()
print("Current script SHA version: {}".format(current_sha_version))
print("Latest script SHA version: {}".format(latest_sha_version))
print("") print("")
if latest_sha_version is None:
print("Could not verify the latest version from GitHub.")
print("Current script SHA version: {}".format(current_sha_version))
print("Please check your internet connection and try again later.")
print("")
user_input = self.utils.request_input("Do you want to skip the update process? (Y/n): ").strip().lower() or "y"
if user_input == 'y':
print("")
print("Update process skipped.")
return False
else:
print("")
print("Continuing with update using default version check...")
latest_sha_version = "update_forced_by_user"
else:
print("Current script SHA version: {}".format(current_sha_version))
print("Latest script SHA version: {}".format(latest_sha_version))
print("")
if latest_sha_version != current_sha_version: if latest_sha_version != current_sha_version:
print("Update available!")
print("Updating from version {} to {}".format(current_sha_version, latest_sha_version)) print("Updating from version {} to {}".format(current_sha_version, latest_sha_version))
print("") print("")
self.download_update() print("Starting update process...")
self.update_files()
self.save_latest_sha_version(latest_sha_version) if not self.download_update():
print("\n") print("")
print(" Update failed: Could not download or extract update package")
if os.path.exists(self.temporary_dir):
self.current_step += 1
print("Step {}: Cleaning up temporary files...".format(self.current_step))
shutil.rmtree(self.temporary_dir)
print(" Cleanup complete")
return False
if not self.update_files():
print("")
print(" Update failed: Could not update files")
return False
if not self.save_latest_sha_version(latest_sha_version):
print("")
print(" Update completed but version information could not be saved")
print("")
print("Update completed successfully!")
print("")
print("The program needs to restart to complete the update process.") print("The program needs to restart to complete the update process.")
print("\n") return True
else: else:
print("You are already using the latest version") print("You are already using the latest version")
return False
return latest_sha_version != current_sha_version