mirror of
https://github.com/theniceboy/.config.git
synced 2026-07-16 22:01:21 +08:00
local bin
This commit is contained in:
parent
2129aa8144
commit
a74781d825
5 changed files with 900 additions and 0 deletions
127
bin/upgrade-all
Executable file
127
bin/upgrade-all
Executable file
|
|
@ -0,0 +1,127 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
import sys
|
||||
from queue import Queue
|
||||
|
||||
class Colors:
|
||||
RED = '\033[0;31m'
|
||||
GREEN = '\033[0;32m'
|
||||
YELLOW = '\033[1;33m'
|
||||
BLUE = '\033[0;34m'
|
||||
RESET = '\033[0m'
|
||||
|
||||
class ParallelTaskRunner:
|
||||
def __init__(self):
|
||||
self.tasks = []
|
||||
self.results = Queue()
|
||||
self.task_positions = {}
|
||||
self.lock = threading.Lock()
|
||||
|
||||
def add_task(self, name, command):
|
||||
"""Add a task to the execution queue."""
|
||||
self.tasks.append({'name': name, 'command': command})
|
||||
|
||||
def _update_task_line(self, task_id, text):
|
||||
"""Update a specific task's display line with thread-safe cursor movement."""
|
||||
with self.lock:
|
||||
if task_id in self.task_positions:
|
||||
lines_up = len(self.task_positions) - self.task_positions[task_id]
|
||||
print(f"\033[{lines_up}A\r{text}\033[K\033[{lines_up}B", end="", flush=True)
|
||||
else:
|
||||
self.task_positions[task_id] = len(self.task_positions)
|
||||
print(text, flush=True)
|
||||
|
||||
def _execute_task(self, task):
|
||||
"""Execute a single task with real-time status updates."""
|
||||
name, command = task['name'], task['command']
|
||||
task_id = threading.get_ident()
|
||||
|
||||
self._update_task_line(task_id, f"{Colors.YELLOW}[STARTING]{Colors.RESET} {name}")
|
||||
|
||||
try:
|
||||
process = subprocess.Popen(
|
||||
command,
|
||||
shell=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
text=True
|
||||
)
|
||||
|
||||
# Animated progress indicator
|
||||
spinner_chars = "|/-\\"
|
||||
spinner_idx = 0
|
||||
|
||||
while process.poll() is None:
|
||||
spinner = f"{Colors.BLUE}[RUNNING]{Colors.RESET} {name} [{spinner_chars[spinner_idx]}]"
|
||||
self._update_task_line(task_id, spinner)
|
||||
spinner_idx = (spinner_idx + 1) % len(spinner_chars)
|
||||
time.sleep(0.1)
|
||||
|
||||
stdout, _ = process.communicate()
|
||||
|
||||
if process.returncode == 0:
|
||||
self._update_task_line(task_id, f"{Colors.GREEN}[COMPLETED]{Colors.RESET} {name}")
|
||||
self.results.put({'name': name, 'success': True, 'output': stdout})
|
||||
else:
|
||||
self._update_task_line(task_id, f"{Colors.RED}[FAILED]{Colors.RESET} {name}")
|
||||
if stdout:
|
||||
with self.lock:
|
||||
print(f"\nError output for {name}:\n{stdout}")
|
||||
self.results.put({'name': name, 'success': False, 'output': stdout})
|
||||
|
||||
except Exception as e:
|
||||
self._update_task_line(task_id, f"{Colors.RED}[FAILED]{Colors.RESET} {name}")
|
||||
with self.lock:
|
||||
print(f"\nException for {name}: {e}")
|
||||
self.results.put({'name': name, 'success': False, 'output': str(e)})
|
||||
|
||||
def run_all(self):
|
||||
"""Execute all tasks in parallel and return overall success status."""
|
||||
print("Starting parallel upgrade tasks...\n")
|
||||
|
||||
# Start all tasks
|
||||
threads = [
|
||||
threading.Thread(target=self._execute_task, args=(task,))
|
||||
for task in self.tasks
|
||||
]
|
||||
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
|
||||
for thread in threads:
|
||||
thread.join()
|
||||
|
||||
# Display summary
|
||||
print(f"\n{'=' * 47}")
|
||||
|
||||
# Count results properly
|
||||
all_results = []
|
||||
while not self.results.empty():
|
||||
all_results.append(self.results.get())
|
||||
|
||||
failed_count = sum(1 for result in all_results if not result['success'])
|
||||
|
||||
if failed_count == 0:
|
||||
print(f"{Colors.GREEN}All upgrade tasks completed successfully!{Colors.RESET}")
|
||||
return True
|
||||
else:
|
||||
print(f"{Colors.RED}Some tasks failed. Check output above.{Colors.RESET}")
|
||||
return False
|
||||
|
||||
def main():
|
||||
runner = ParallelTaskRunner()
|
||||
|
||||
runner.add_task("Claude Code Update", "npm install -g @anthropic-ai/claude-code")
|
||||
runner.add_task("Homebrew Update", "brew update && brew upgrade --greedy")
|
||||
runner.add_task("Config Git Pull", "cd ~/.config && git pull")
|
||||
runner.add_task("Neovim Config Git Pull", "cd ~/.config/nvim && git pull")
|
||||
runner.add_task("SConfig Git Pull", "cd ~/.sconfig && git pull")
|
||||
|
||||
success = runner.run_all()
|
||||
sys.exit(0 if success else 1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue