Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(config): 桌面图标的归属判据认包内入口

longmeidaoc448f5018045
diff --git a/src/peach/desktop_startup.py b/src/peach/desktop_startup.py
index 9927f9a56..f4e5290cc 100644
--- a/src/peach/desktop_startup.py
+++ b/src/peach/desktop_startup.py
@@ -142,6 +142,18 @@ def desktop_directory() -> Path:
return Path(os.environ.get("USERPROFILE", Path.home())) / "Desktop"
+def owned_target(target: Path, program: Path) -> bool:
+ """`.lnk` 指向的目标算不算这份安装的入口。
+
+ 源码安装的判据放宽到整个项目目录:`dist\\Peach\\Peach.exe` 和
+ `pythonw.exe -c "…runpy.run_module('peach.tray')"` 是同一份 Peach 的两个入口,而
+ `target()` 只会给出后者。按字面比就把自己先前放的图标判成「另一份安装」,配置页里
+ 连关掉它都不行——本机的桌面图标正是这么被锁住的。
+ """
+ return target == program.resolve() or (not distribution.standalone()
+ and target.is_relative_to(settings_file.PROJECT_ROOT.resolve()))
+
+
def desktop_entry(program: Path) -> tuple[Path, dict]:
"""桌面快捷方式的路径与当前状态;属于另一份安装时不认领。
@@ -151,7 +163,7 @@ def desktop_entry(program: Path) -> tuple[Path, dict]:
"""
path = desktop_directory() / "Peach.lnk"
current = shortcut("read", path)
- if current["enabled"] and Path(current["target"]).resolve() != program.resolve():
+ if current["enabled"] and not owned_target(Path(current["target"]).resolve(), program):
raise ValueError("桌面快捷方式属于另一份 Peach 安装")
return path, current
@@ -160,11 +172,8 @@ def windows_entry(config, program: Path) -> tuple[Path, dict]:
directory = startup_directory()
legacy = directory / "Peach.lnk"
current = shortcut("read", legacy)
- if current["enabled"]:
- path = Path(current["target"]).resolve()
- owned = path == program.resolve() or (not distribution.standalone() and path.is_relative_to(settings_file.PROJECT_ROOT.resolve()))
- if owned:
- return legacy, current
+ if current["enabled"] and owned_target(Path(current["target"]).resolve(), program):
+ return legacy, current
identity = hashlib.sha256(str(config.data_root.resolve()).casefold().encode()).hexdigest()[:12]
path = directory / f"Peach-{identity}.lnk"
current = shortcut("read", path)
diff --git a/tests/test_desktop_settings.py b/tests/test_desktop_settings.py
index 66b081979..b4a47b670 100644
--- a/tests/test_desktop_settings.py
+++ b/tests/test_desktop_settings.py
@@ -4,6 +4,7 @@ import base64
import ctypes
import os
import subprocess
+import sys
from pathlib import Path
import tempfile
from types import SimpleNamespace
@@ -135,6 +136,22 @@ class DesktopSettingsTests(unittest.TestCase):
{'enabled':False}, {'enabled':True,'target':str(self.root / 'Other.exe')}]), self.assertRaisesRegex(ValueError, '另一份'):
desktop_startup.windows_entry(self.config, self.program / 'Peach.exe')
+ def test_a_shortcut_aimed_at_the_packaged_entry_still_counts_as_this_installation(self):
+ """`dist/Peach/Peach.exe` 和 `pythonw.exe -c "…peach.tray…"` 是同一份 Peach 的两个入口。
+
+ 源码安装里 `target()` 只给出后者。按字面比目标路径,配置页会把自己先前放在桌面上
+ 的图标判成「另一份 Peach 安装」并锁住那颗开关,用户连关掉它都不行;本机的桌面图标
+ 就是这么被锁住的(0.29.0 上线后实测)。所以源码安装的归属判到整个项目目录。
+ """
+ program = Path(sys.executable).with_name('pythonw.exe')
+ packaged = (settings_file.PROJECT_ROOT / 'dist/Peach/Peach.exe').resolve()
+ with patch.object(desktop_startup.distribution, 'standalone', return_value=False):
+ self.assertTrue(desktop_startup.owned_target(packaged, program))
+ self.assertFalse(desktop_startup.owned_target((self.program / 'Peach.exe').resolve(), program))
+ # 独立发行版没有项目目录这层放宽:那份包只认自己那个 exe。
+ with patch.object(desktop_startup.distribution, 'standalone', return_value=True):
+ self.assertFalse(desktop_startup.owned_target(packaged, program))
+
@unittest.skipUnless(os.name == 'nt', 'Windows known folder')
def test_desktop_directory_comes_from_the_known_folder_not_the_home_path(self):
"""桌面目录问 shell 要,不拼 `%USERPROFILE%\\Desktop`。