Skip to content

Commit c75cd18

Browse files
committed
fix: restart desktop after automatic update
1 parent 526959c commit c75cd18

5 files changed

Lines changed: 65 additions & 36 deletions

File tree

desktop_qt_ui/services/update_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from dataclasses import dataclass
88
from pathlib import Path
99

10-
from git_update_helpers import (
10+
from desktop_qt_ui.core.git_update_helpers import (
1111
commits_behind,
1212
current_commit,
1313
fetch_origin,

desktop_qt_ui/ui/main_page/pages/about_page.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pathlib import Path
55
from typing import Callable
66

7-
from git_update_helpers import (
7+
from desktop_qt_ui.core.git_update_helpers import (
88
GIT_MIRRORS,
99
current_commit,
1010
git_executable,

packaging/launch.py

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@
2121
if str(PATH_ROOT) not in sys.path:
2222
sys.path.insert(0, str(PATH_ROOT))
2323

24-
from git_update_helpers import (
24+
from desktop_qt_ui.core.git_update_helpers import (
2525
GIT_MIRRORS as SHARED_GIT_MIRRORS,
2626
)
27-
from git_update_helpers import (
27+
from desktop_qt_ui.core.git_update_helpers import (
2828
SUPPORTED_BRANCHES as SHARED_SUPPORTED_BRANCHES,
2929
)
30-
from git_update_helpers import (
30+
from desktop_qt_ui.core.git_update_helpers import (
3131
current_branch as shared_current_branch,
3232
)
33-
from git_update_helpers import (
33+
from desktop_qt_ui.core.git_update_helpers import (
3434
fetch_origin,
3535
git_output,
3636
set_origin_url,
3737
)
38-
from git_update_helpers import (
38+
from desktop_qt_ui.core.git_update_helpers import (
3939
update_branch as shared_update_branch,
4040
)
4141

@@ -693,33 +693,20 @@ def restart_maintenance(action):
693693
raise SystemExit(1) from e
694694

695695
def restart_desktop_ui():
696-
"""Start a fresh desktop UI process after automatic maintenance finishes."""
697-
try:
698-
if sys.platform == "win32":
699-
start_script = PATH_ROOT / "Win-Start.bat"
700-
if start_script.exists():
701-
subprocess.Popen(
702-
["cmd.exe", "/d", "/c", str(start_script)],
703-
cwd=PATH_ROOT,
704-
creationflags=(
705-
subprocess.CREATE_NEW_PROCESS_GROUP
706-
| subprocess.DETACHED_PROCESS
707-
),
708-
)
709-
return True
710-
unix_start = PATH_ROOT / "Unix-Start.sh"
711-
if unix_start.exists():
712-
subprocess.Popen(
713-
[str(unix_start)],
714-
cwd=PATH_ROOT,
715-
start_new_session=True,
716-
)
717-
return True
718-
subprocess.Popen(
719-
[sys.executable, str(PATH_ROOT / "desktop_qt_ui" / "main.py")],
720-
cwd=PATH_ROOT,
721-
start_new_session=True,
696+
"""Restart the desktop UI with the interpreter that completed maintenance."""
697+
command = [sys.executable, str(PATH_ROOT / "desktop_qt_ui" / "main.py")]
698+
kwargs = {"cwd": PATH_ROOT}
699+
if sys.platform == "win32":
700+
kwargs["creationflags"] = (
701+
subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.DETACHED_PROCESS
722702
)
703+
else:
704+
kwargs["start_new_session"] = True
705+
706+
print(L("正在重新启动桌面端...", "Restarting the desktop UI..."))
707+
sys.stdout.flush()
708+
try:
709+
subprocess.Popen(command, **kwargs)
723710
return True
724711
except OSError as exc:
725712
print(
@@ -2941,13 +2928,16 @@ def __init__(self):
29412928
if not resume_updated_code(args, resume_action):
29422929
raise SystemExit(1)
29432930
if automatic:
2944-
restart_desktop_ui()
2931+
if not restart_desktop_ui():
2932+
raise SystemExit(1)
29452933
return
29462934
input(L("\n按回车键继续...", "\nPress Enter to continue..."))
29472935

29482936
if automatic:
2949-
run_full_update(args, automatic=True)
2950-
restart_desktop_ui()
2937+
if not run_full_update(args, automatic=True):
2938+
raise SystemExit(1)
2939+
if not restart_desktop_ui():
2940+
raise SystemExit(1)
29512941
return
29522942

29532943
# 首次显示版本信息

test/test_update_service.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,42 @@ def test_automatic_full_update_skips_confirmation(monkeypatch):
125125
args = type("Args", (), {"requirements": "auto"})()
126126
assert maintenance_launch.run_full_update(args, automatic=True)
127127
assert dependency_updates == [["example-package"]]
128+
129+
130+
def test_restart_desktop_ui_uses_current_maintenance_interpreter(monkeypatch):
131+
captured = {}
132+
133+
def fake_popen(args, **kwargs):
134+
captured["args"] = args
135+
captured["kwargs"] = kwargs
136+
return object()
137+
138+
monkeypatch.setattr(maintenance_launch.subprocess, "Popen", fake_popen)
139+
monkeypatch.setattr(maintenance_launch.sys, "platform", "win32")
140+
141+
assert maintenance_launch.restart_desktop_ui()
142+
assert captured["args"] == [
143+
maintenance_launch.sys.executable,
144+
str(maintenance_launch.PATH_ROOT / "desktop_qt_ui" / "main.py"),
145+
]
146+
assert captured["kwargs"]["cwd"] == maintenance_launch.PATH_ROOT
147+
assert captured["kwargs"]["creationflags"] & maintenance_launch.subprocess.DETACHED_PROCESS
148+
149+
150+
def test_automatic_maintenance_restarts_after_update(monkeypatch):
151+
events = []
152+
monkeypatch.setattr(maintenance_launch, "init_language", lambda: None)
153+
monkeypatch.setattr(
154+
maintenance_launch,
155+
"run_full_update",
156+
lambda _args, automatic=False: events.append(("update", automatic)) or True,
157+
)
158+
monkeypatch.setattr(
159+
maintenance_launch,
160+
"restart_desktop_ui",
161+
lambda: events.append(("restart", True)) or True,
162+
)
163+
164+
maintenance_launch.maintenance_menu(automatic=True)
165+
166+
assert events == [("update", True), ("restart", True)]

0 commit comments

Comments
 (0)