|
9 | 9 | PACKAGE_NAME = "bugscan-x" |
10 | 10 | console = Console() |
11 | 11 |
|
12 | | -def check_and_update(): |
| 12 | +def check_for_updates(): |
13 | 13 | try: |
14 | | - with console.status("[yellow]Checking for updates...", spinner="dots") as status: |
| 14 | + with console.status("[yellow]Checking for updates...", spinner="dots"): |
15 | 15 | current_version = version(PACKAGE_NAME) |
16 | | - |
17 | 16 | try: |
18 | 17 | result = subprocess.run( |
19 | 18 | [sys.executable, '-m', 'pip', 'index', 'versions', PACKAGE_NAME], |
20 | 19 | capture_output=True, text=True, check=True, timeout=15 |
21 | 20 | ) |
22 | 21 | lines = result.stdout.splitlines() |
23 | 22 | latest_version = lines[-1].split()[-1] if lines else "0.0.0" |
| 23 | + |
| 24 | + if not latest_version or latest_version <= current_version: |
| 25 | + console.print(f"[green] You're up to date: {current_version}") |
| 26 | + return False, None, None |
| 27 | + |
| 28 | + return True, current_version, latest_version |
| 29 | + |
24 | 30 | except subprocess.TimeoutExpired: |
25 | 31 | console.print("[red] Update check timed out. Please check your internet connection.") |
26 | | - return |
27 | | - except subprocess.CalledProcessError as e: |
28 | | - console.print(f"[red] Failed to check updates: {e.stderr}") |
29 | | - return |
30 | | - |
31 | | - if not latest_version or latest_version <= current_version: |
32 | | - console.print(f"[green] You're up to date: {current_version}") |
33 | | - return |
34 | | - |
35 | | - console.print(f"[yellow] Update available: {current_version} → {latest_version}") |
36 | | - if not get_confirm(" Update now"): |
37 | | - return |
38 | | - |
39 | | - with console.status("[yellow] Installing update...", spinner="point") as status: |
| 32 | + return False, None, None |
| 33 | + except subprocess.CalledProcessError: |
| 34 | + console.print("[red] Failed to check updates") |
| 35 | + return False, None, None |
| 36 | + except Exception: |
| 37 | + console.print("[red] Error checking updates") |
| 38 | + return False, None, None |
| 39 | + |
| 40 | +def install_update(): |
| 41 | + try: |
| 42 | + with console.status("[yellow] Installing update...", spinner="point"): |
40 | 43 | try: |
41 | | - result = subprocess.run( |
| 44 | + subprocess.run( |
42 | 45 | [sys.executable, '-m', 'pip', 'install', '--upgrade', PACKAGE_NAME], |
43 | 46 | capture_output=True, text=True, check=True, timeout=60 |
44 | 47 | ) |
45 | 48 | console.print("[green] Update successful!") |
| 49 | + return True |
46 | 50 | except subprocess.TimeoutExpired: |
47 | 51 | console.print("[red] Installation timed out. Please try again.") |
48 | | - return |
49 | | - except subprocess.CalledProcessError as e: |
50 | | - console.print(f"[red] Installation failed: {e.stderr}") |
51 | | - return |
52 | | - |
53 | | - console.print("[yellow] Restarting application...") |
54 | | - time.sleep(1) |
55 | | - |
56 | | - os.execv(sys.executable, [sys.executable] + sys.argv) |
57 | | - |
| 52 | + return False |
| 53 | + except subprocess.CalledProcessError: |
| 54 | + console.print("[red] Installation failed") |
| 55 | + return False |
| 56 | + except Exception: |
| 57 | + console.print("[red] Error during installation") |
| 58 | + return False |
| 59 | + |
| 60 | +def restart_application(): |
| 61 | + console.print("[yellow] Restarting application...") |
| 62 | + time.sleep(1) |
| 63 | + os.execv(sys.executable, [sys.executable] + sys.argv) |
| 64 | + |
| 65 | +def check_and_update(): |
| 66 | + try: |
| 67 | + has_update, current_version, latest_version = check_for_updates() |
| 68 | + if not has_update: |
| 69 | + return |
| 70 | + |
| 71 | + console.print(f"[yellow] Update available: {current_version} → {latest_version}") |
| 72 | + if not get_confirm(" Update now"): |
| 73 | + return |
| 74 | + |
| 75 | + if install_update(): |
| 76 | + restart_application() |
| 77 | + |
58 | 78 | except KeyboardInterrupt: |
59 | | - console.print("\n[yellow] Update cancelled by user.") |
60 | | - except Exception as e: |
61 | | - console.print(f"[red] Error during update process: {str(e)}") |
| 79 | + console.print("[yellow] Update cancelled by user.") |
| 80 | + except Exception: |
| 81 | + console.print("[red] Error during update process") |
0 commit comments