Skip to content

Commit adf6c05

Browse files
committed
refactor: Enhance update checking and installation logic
1 parent 9449c31 commit adf6c05

1 file changed

Lines changed: 51 additions & 31 deletions

File tree

bugscanx/modules/others/script_updater.py

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,73 @@
99
PACKAGE_NAME = "bugscan-x"
1010
console = Console()
1111

12-
def check_and_update():
12+
def check_for_updates():
1313
try:
14-
with console.status("[yellow]Checking for updates...", spinner="dots") as status:
14+
with console.status("[yellow]Checking for updates...", spinner="dots"):
1515
current_version = version(PACKAGE_NAME)
16-
1716
try:
1817
result = subprocess.run(
1918
[sys.executable, '-m', 'pip', 'index', 'versions', PACKAGE_NAME],
2019
capture_output=True, text=True, check=True, timeout=15
2120
)
2221
lines = result.stdout.splitlines()
2322
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+
2430
except subprocess.TimeoutExpired:
2531
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"):
4043
try:
41-
result = subprocess.run(
44+
subprocess.run(
4245
[sys.executable, '-m', 'pip', 'install', '--upgrade', PACKAGE_NAME],
4346
capture_output=True, text=True, check=True, timeout=60
4447
)
4548
console.print("[green] Update successful!")
49+
return True
4650
except subprocess.TimeoutExpired:
4751
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+
5878
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

Comments
 (0)