From fe810e7ad041a488d9363894b2a652061e14df5f Mon Sep 17 00:00:00 2001 From: Cursor User Date: Wed, 18 Jun 2025 17:45:22 +0200 Subject: [PATCH] Major update script overhaul - robust conflict handling - Complete rewrite of update script with better error handling - Added automatic backup/restore of important config files - Robust cache cleanup (removes all .pyc and __pycache__ files) - Uses git reset --hard and clean for conflict-free updates - Force pull strategy prevents merge conflicts entirely - Preserves session files, config.py, and .env during updates - Windows-compatible commands for file operations - More reliable update process that handles any local changes --- update_script.py | 131 +++++++++++++++++++++++++++-------------------- 1 file changed, 75 insertions(+), 56 deletions(-) diff --git a/update_script.py b/update_script.py index 5973954..8e5f7c7 100644 --- a/update_script.py +++ b/update_script.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 """ -Ultroid Update Script -This script handles updating the bot while it's not running. +Ultroid Update Script - Improved Version +This script handles updating the bot while it's not running using a robust approach. """ import os @@ -15,14 +15,56 @@ def run_command(cmd, shell=True): try: result = subprocess.run(cmd, shell=shell, capture_output=True, text=True) print(f"Command: {cmd}") - print(f"Output: {result.stdout}") - if result.stderr: + if result.stdout.strip(): + print(f"Output: {result.stdout}") + if result.stderr.strip(): print(f"Error: {result.stderr}") return result.returncode == 0 except Exception as e: print(f"Error running command '{cmd}': {e}") return False +def backup_important_files(): + """Backup important configuration files.""" + important_files = [ + "config.py", + ".env", + "resources/session/ultroid.session", + "resources/session/ultroid.session-journal" + ] + + backed_up = [] + for file in important_files: + if os.path.exists(file): + backup_name = f"{file}.backup" + if run_command(f'copy "{file}" "{backup_name}"'): + backed_up.append((file, backup_name)) + print(f"โœ… Backed up {file}") + + return backed_up + +def restore_important_files(backed_up): + """Restore important configuration files.""" + for original, backup in backed_up: + if os.path.exists(backup): + if run_command(f'copy "{backup}" "{original}"'): + print(f"โœ… Restored {original}") + run_command(f'del "{backup}"') + +def clean_repository(): + """Clean the repository of cache files and reset to clean state.""" + print("๐Ÿงน Cleaning repository...") + + # Remove Python cache files + run_command('for /r . %i in (*.pyc) do @del "%i" >nul 2>&1') + run_command('for /d /r . %d in (__pycache__) do @if exist "%d" rd /s /q "%d" >nul 2>&1') + + # Reset all tracked files to their HEAD state + run_command("git reset --hard HEAD") + + # Clean untracked files (but preserve update script and important files) + run_command("git clean -fd -e update_script*.py -e config.py -e .env -e resources/session/") + def main(): """Main update function.""" print("๐Ÿ”„ Starting Ultroid update process...") @@ -36,78 +78,55 @@ def main(): # Check if we're in a git repository if not (script_dir / ".git").exists(): print("โŒ Not a git repository. Cannot update.") - return False + return False + # Get the repository URL from command line args or default to user's fork repo_url = sys.argv[1] if len(sys.argv) > 1 else "https://github.com/overspend1/Ultroid-fork.git" - # Fetch and pull updates - print("๐Ÿ“ฅ Fetching updates from repository...") + print(f"๐Ÿ”— Using repository: {repo_url}") - if repo_url: - print(f"๐Ÿ”— Using repository: {repo_url}") - # Set up remote if needed - if not run_command("git remote get-url origin"): - run_command(f"git remote add origin {repo_url}") - else: - run_command(f"git remote set-url origin {repo_url}") + # Backup important files + backed_up_files = backup_important_files() + + # Set up remote + if not run_command("git remote get-url origin"): + run_command(f"git remote add origin {repo_url}") + else: + run_command(f"git remote set-url origin {repo_url}") # Fetch latest changes + print("๐Ÿ“ฅ Fetching updates from repository...") if not run_command("git fetch origin"): print("โŒ Failed to fetch updates") return False - # Get current branch + + # Get current branch result = subprocess.run("git branch --show-current", shell=True, capture_output=True, text=True) current_branch = result.stdout.strip() or "main" print(f"๐ŸŒฟ Current branch: {current_branch}") - # Check for untracked files that might conflict - print("๐Ÿ” Checking for conflicting files...") - untracked_result = subprocess.run("git ls-files --others --exclude-standard", shell=True, capture_output=True, text=True) - untracked_files = untracked_result.stdout.strip().split('\n') if untracked_result.stdout.strip() else [] + # Clean repository + clean_repository() - # If update_script.py is untracked and would conflict, temporarily move it - script_moved = False - if "update_script.py" in untracked_files: - print("๐Ÿ“ฆ Temporarily moving update script to avoid conflicts...") - if run_command("move update_script.py update_script_temp.py"): - script_moved = True - - # Stash any local changes first - print("๐Ÿ’พ Stashing local changes...") - run_command("git stash push -m 'Auto-stash before update'") - # Pull updates - print("โฌ‡๏ธ Pulling updates...") + # Force pull updates (this will overwrite any local changes) + print("โฌ‡๏ธ Force pulling updates...") if not run_command(f"git pull origin {current_branch}"): - print("โŒ Failed to pull updates") - # Try to restore stashed changes - print("๐Ÿ”„ Attempting to restore stashed changes...") - run_command("git stash pop") - # Restore moved script if it was moved - if script_moved and os.path.exists("update_script_temp.py"): - print("๐Ÿ”„ Restoring update script...") - run_command("move update_script_temp.py update_script.py") - return False - # Restore stashed changes if any - print("๐Ÿ”„ Restoring local changes...") - stash_result = subprocess.run("git stash list", shell=True, capture_output=True, text=True) - if "Auto-stash before update" in stash_result.stdout: - if not run_command("git stash pop"): - print("โš ๏ธ Warning: Could not restore some local changes. Check git stash list.") - else: - print("โœ… Local changes restored successfully") + # If pull fails, try reset to remote + print("๐Ÿ”„ Trying hard reset to remote...") + if not run_command(f"git reset --hard origin/{current_branch}"): + print("โŒ Failed to update repository") + return False - # Restore moved script if it was moved - if script_moved and os.path.exists("update_script_temp.py"): - print("๐Ÿ”„ Restoring update script...") - run_command("move update_script_temp.py update_script.py") + # Restore important files + restore_important_files(backed_up_files) # Update dependencies print("๐Ÿ“ฆ Installing/updating dependencies...") if not run_command("pip3 install -r requirements.txt --upgrade"): print("โš ๏ธ Warning: Failed to update some dependencies") - # Try alternative pip command + # Try alternative pip command for systems that need it run_command("pip3 install -r requirements.txt --break-system-packages --upgrade") print("โœ… Update completed successfully!") @@ -139,15 +158,15 @@ def restart_bot(): os.execv(sys.executable, [sys.executable, "-m", "pyUltroid"]) if __name__ == "__main__": - print("๐Ÿš€ Ultroid Update Script") - print("=" * 40) + print("๐Ÿš€ Ultroid Update Script - Improved Version") + print("=" * 50) # Wait a moment for the bot to fully shutdown time.sleep(2) # Perform update if main(): - print("=" * 40) + print("=" * 50) restart_bot() else: print("โŒ Update failed. Please check the errors above.")