Peach: Local-first video metadata scraper and media manager.
fix(desktop): 启动项失败的原因走 stdout 的 JSON
longmeidao553e91aeb629
diff --git a/src/peach/desktop_startup.py b/src/peach/desktop_startup.pyindex 8075da56d..003fd5a03 100644--- a/src/peach/desktop_startup.py+++ b/src/peach/desktop_startup.py@@ -14,38 +14,51 @@ from . import distribution, settings_filefrom .appid import MACOS_LAUNCH_AGENT_LABELfrom .fsutil import atomic_write_text+#: 结论只走 stdout 的一行 JSON,`ok` 决定成败。退出码和 stderr 都不作判据:+#: Windows PowerShell 5.1 把进度流序列化成 `#< CLIXML` 写到 stderr,成功那一次也写,+#: 于是「取 stderr 第一行当原因」拿到的是「正在准备首次使用模块。」而不是错误。_SHORTCUT_SCRIPT = r"""$ErrorActionPreference = 'Stop'+$ProgressPreference = 'SilentlyContinue'[Console]::OutputEncoding = [Text.UTF8Encoding]::new($false)-$peachInput = [Console]::In.ReadToEnd() | ConvertFrom-Json-$peachLinkPath = [IO.Path]::GetFullPath($peachInput.path)-if ([IO.Path]::GetExtension($peachLinkPath) -ne '.lnk') { throw 'Expected shortcut' }-$peachExists = Test-Path -LiteralPath $peachLinkPath -PathType Leaf-$peachShell = New-Object -ComObject WScript.Shell-if ($peachInput.action -eq 'read' -and -not $peachExists) {- @{enabled=$false; target=''; arguments=''} | ConvertTo-Json -Compress- exit 0+try {+ $peachInput = [Console]::In.ReadToEnd() | ConvertFrom-Json+ $peachLinkPath = [IO.Path]::GetFullPath($peachInput.path)+ if ([IO.Path]::GetExtension($peachLinkPath) -ne '.lnk') { throw 'Expected shortcut' }+ $peachExists = Test-Path -LiteralPath $peachLinkPath -PathType Leaf+ $peachShell = New-Object -ComObject WScript.Shell+ if ($peachInput.action -eq 'read' -and -not $peachExists) {+ @{ok=$true; enabled=$false; target=''; arguments=''} | ConvertTo-Json -Compress+ } else {+ $peachLink = $peachShell.CreateShortcut($peachLinkPath)+ if ($peachInput.action -ne 'read' -and $peachExists -and $peachLink.TargetPath -ne $peachInput.expected) {+ throw 'Startup shortcut belongs to another installation'+ }+ if ($peachInput.action -eq 'write') {+ $peachParent = Split-Path -Parent $peachLinkPath+ [IO.Directory]::CreateDirectory($peachParent) | Out-Null+ $peachLink.TargetPath = $peachInput.target+ $peachLink.Arguments = $peachInput.arguments+ $peachLink.WorkingDirectory = $peachInput.directory+ $peachLink.Description = 'Peach'+ $peachLink.WindowStyle = 7+ $peachLink.Save()+ if (-not (Test-Path -LiteralPath $peachLinkPath -PathType Leaf)) {+ throw "Save reported success but $peachLinkPath is absent"+ }+ $peachExists = $true+ }+ if ($peachInput.action -eq 'remove' -and $peachExists) {+ Remove-Item -LiteralPath $peachLinkPath+ $peachExists = $false+ }+ @{ok=$true; enabled=$peachExists; target=$peachLink.TargetPath; arguments=$peachLink.Arguments} |+ ConvertTo-Json -Compress+ }+} catch {+ @{ok=$false; error=$_.Exception.Message; kind=$_.Exception.GetType().FullName;+ line=$_.InvocationInfo.ScriptLineNumber} | ConvertTo-Json -Compress}-$peachLink = $peachShell.CreateShortcut($peachLinkPath)-if ($peachInput.action -ne 'read' -and $peachExists -and $peachLink.TargetPath -ne $peachInput.expected) {- throw 'Startup shortcut belongs to another installation'-}-if ($peachInput.action -eq 'write') {- $peachParent = Split-Path -Parent $peachLinkPath- [IO.Directory]::CreateDirectory($peachParent) | Out-Null- $peachLink.TargetPath = $peachInput.target- $peachLink.Arguments = $peachInput.arguments- $peachLink.WorkingDirectory = $peachInput.directory- $peachLink.Description = 'Peach'- $peachLink.WindowStyle = 7- $peachLink.Save()- $peachExists = $true-}-if ($peachInput.action -eq 'remove' -and $peachExists) {- Remove-Item -LiteralPath $peachLinkPath- $peachExists = $false-}-@{enabled=$peachExists; target=$peachLink.TargetPath; arguments=$peachLink.Arguments} | ConvertTo-Json -Compress"""@@ -58,16 +71,33 @@ def shortcut(action: str, path: Path, *, target: str = "", arguments: str = "",directory=directory, expected=expected)),capture_output=True, text=True, encoding="utf-8", timeout=15,creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0), check=False)- if result.returncode:+ try:+ payload = json.loads(result.stdout.lstrip(""))+ except ValueError:+ payload = None+ if not isinstance(payload, dict) or not payload.get("ok"):# PowerShell 说的原因不能吞掉。只留「请检查权限」这一句时,Windows runner 上# 这一步失败了三次,而权限、`WScript.Shell` COM 不可用、路径没落地和扩展名# 校验不通过在消息里长得一模一样,谁都没法往下查。- detail = next((line.strip() for line in- (result.stderr or result.stdout or "").splitlines()- if line.strip()), "")raise OSError("启动项未能保存,请检查当前用户的启动文件夹权限"- + (f"(PowerShell:{detail})" if detail else ""))- return json.loads(result.stdout.lstrip("\ufeff"))+ f"(PowerShell:{_shortcut_reason(payload, result)})")+ return payload+++def _shortcut_reason(payload, result) -> str:+ """脚本自报的原因优先;只有 PowerShell 连 JSON 都没吐出来才退回它的输出。++ 退回时逐行滤掉 CLIXML:5.1 把进度流序列化成 `#< CLIXML` 加一整段 XML 写进 stderr,+ 成功那一次也写,整段抄进消息只会把真正的原因顶掉。+ """+ if isinstance(payload, dict) and payload.get("error"):+ kind = str(payload.get("kind", "")).rsplit(".", 1)[-1]+ line = payload.get("line")+ located = f"{kind} 第 {line} 行" if kind and line else kind+ return f"{payload['error']}({located})" if located else str(payload["error"])+ noise = next((line.strip() for line in (result.stderr or "").splitlines()+ if line.strip() and not line.lstrip().startswith(("#<", "<"))), "")+ return noise or f"退出码 {result.returncode},输出为空"def target(config, *, executable: Path | None = None) -> tuple[Path, list[str], Path]:diff --git a/tests/test_desktop_settings.py b/tests/test_desktop_settings.pyindex 5eec0f2cd..1cb500c20 100644--- a/tests/test_desktop_settings.py+++ b/tests/test_desktop_settings.py@@ -134,7 +134,11 @@ class DesktopSettingsTests(unittest.TestCase):def test_native_shortcut_round_trip_in_temporary_directory(self):path = self.root / '启动 fixture.lnk'program = str(self.program / 'Peach.exe')- desktop_startup.shortcut('write', path, target=program, arguments='--silent', directory=str(self.program))+ written = desktop_startup.shortcut('write', path, target=program, arguments='--silent', directory=str(self.program))+ # 写完立刻核对文件就在这个路径上。只在 remove 之后断言不存在的话,`Save()` 把+ # 快捷方式落到别处或压根没落地都照样通过,runner 上红的正是这一段。+ self.assertTrue(written['enabled'])+ self.assertTrue(path.is_file(), sorted(item.name for item in self.root.iterdir()))result = desktop_startup.shortcut('read', path)self.assertEqual(Path(result['target']).resolve(), Path(program))self.assertEqual(result['arguments'], '--silent')@@ -143,17 +147,24 @@ class DesktopSettingsTests(unittest.TestCase):@unittest.skipUnless(os.name == 'nt', 'Windows 原生快捷方式')def test_a_failed_shortcut_call_reports_what_powershell_said(self):- """失败的消息带上 PowerShell 的第一行原因,不只给一句「请检查权限」。+ """失败的消息带上脚本自报的原因、异常类型和行号,不只给一句「请检查权限」。权限不足、`WScript.Shell` COM 起不来、路径没落地、扩展名不是 `.lnk`——这几种在只有一句提示的消息里长得一模一样。CI 的 Windows runner 上这一步失败过,日志里除了那句提示什么都没有,无从判断是哪一种。++ 判据落在脚本 `catch` 出来的那一行 JSON 上,不看退出码也不看 stderr:Windows+ PowerShell 5.1 成功那一次也往 stderr 写 `#< CLIXML` 的进度流,照它取原因只会拿到+ 「正在准备首次使用模块。」。"""with self.assertRaises(OSError) as raised:desktop_startup.shortcut('write', self.root / '不是快捷方式.txt',target=str(self.program / 'Peach.exe'))- self.assertIn('启动项未能保存', str(raised.exception))- self.assertIn('PowerShell', str(raised.exception))+ message = str(raised.exception)+ self.assertIn('启动项未能保存', message)+ self.assertIn('Expected shortcut', message)+ self.assertRegex(message, r'第 \d+ 行')+ self.assertNotIn('CLIXML', message)@unittest.skipUnless(os.name == 'nt', 'Windows 系统卸载助手')def test_native_uninstall_removes_only_temporary_owned_program_and_data(self):