Peach: Local-first video metadata scraper and media manager.
fix(config): 打包托盘从 EXE 所在目录向上找数据根
longmeidao19c2189a5ab8
diff --git a/docs/OPERATIONS.md b/docs/OPERATIONS.mdindex 4482391f7..a16e31aaa 100644--- a/docs/OPERATIONS.md+++ b/docs/OPERATIONS.md@@ -9,7 +9,7 @@- 配置完成之后,Windows 和有 TLS 的 macOS 托盘由 HTTP 导航进程与 HTTPS 业务进程组成;HTTP 的 `/healthz` 仅证明导航进程存活。业务入口 `/healthz?ready=1` 检查配置、页面、数据库查询和迁移校验和,不就绪返回 503;不带参数仍只探活。还没配置的机器上托盘只起一条引导服务,见下面的首次设置几条。- wheel 将 `web`、`migrations`、`resources` 装入 `peach/_resources`。`scripts/smoke_wheel.py` 在仓库外使用安装 wheel 的解释器运行;CI 消费任务只下载制品,不检出源码。测试只使用临时数据根。- 设置文件固定是 `<数据根>/config.toml`。数据根按三步找:环境变量 `PEACH_DATA_ROOT`、- 项目根同级(含上溯四层,覆盖主检出、`peach-worktrees/<任务>` 和打包后的 `dist/Peach/_internal`)+ 项目根同级(含上溯四层,覆盖主检出、`peach-worktrees/<任务>` 和打包后 EXE 所在的 `dist/Peach`)的 `peach-data/`、都没有就是「未配置」。优先级是环境变量 > 设置文件 > 内建默认。- 全新机器只跑 `peach init`。不带参数且 stdin 是终端时进问答(题目、默认值与落盘逻辑在`src/peach/onboarding.py`,托盘设置页复用同一组函数):问数据根、一个已存在的本地媒体diff --git a/src/peach/settings_file.py b/src/peach/settings_file.pyindex 3047e4a2a..403f9286b 100644--- a/src/peach/settings_file.py+++ b/src/peach/settings_file.py@@ -8,10 +8,10 @@数据根的发现刻意**不**依赖设置文件内容——设置文件本身就住在数据根里面:1. `PEACH_DATA_ROOT` 环境变量;-2. 从项目根逐级向上,第一个存在的 `<上级目录>/peach-data`。ADR-0017 的顶层布局把- `peach-app` 与 `peach-data` 并列,向上找因此同时覆盖三种检出形态:主检出、- `peach-worktrees/<task>` 里的隔离工作树,以及打包后落在 `dist/Peach/_internal`- 的 `_MEIPASS`;+2. 从起点逐级向上,第一个存在的 `<上级目录>/peach-data`。起点在源码树里是项目根,+ 打包后是 EXE 所在目录——单文件包的 `_MEIPASS` 是 `%TEMP%` 下的临时目录,它上面+ 没有任何东西。ADR-0017 的顶层布局把 `peach-app` 与 `peach-data` 并列,向上找因此+ 覆盖主检出、`peach-worktrees/<task>` 里的隔离工作树和 `dist/Peach` 里的托盘;3. 都没有 → 未配置。此时仍然给出一个落点(`peach init` 会去建它),但 `configured`为 False:服务照常启动,`/healthz` 报 `configured=false`,页面提示去跑 `peach init`。@@ -78,16 +78,36 @@ def _project_root(module_file: str = __file__, bundle_root: str | None = None) -PROJECT_ROOT = _project_root(bundle_root=getattr(sys, "_MEIPASS", None))-#: 向上找几层。四层刚好覆盖最深的那种形态(`peach-app/dist/Peach/_internal` 里的-#: `_MEIPASS` 要走四层才回到 `peach/`)。不做无界搜索:一路走到磁盘根会撞上碰巧同名的+def _search_anchor(+ project_root: Path = PROJECT_ROOT, *,+ frozen: bool = bool(getattr(sys, "frozen", False)), executable: str = sys.executable,+) -> Path:+ """向上找数据根的起点:源码树用项目根,打包产物用 EXE 所在目录。++ 资源根和发现起点是两回事。PyInstaller 的资源在 `_MEIPASS`,单文件包把它解到+ `%TEMP%/_MEIxxxx`,从那里向上只会走到用户目录,托盘于是把一台配置好的机器判成+ 首次运行并起 `serve --setup`。`sys.executable` 在两种打包形态里都是 EXE 本身。+ """+ if frozen:+ return Path(executable).resolve().parent+ return project_root+++SEARCH_ANCHOR = _search_anchor()++#: 向上找几层。四层刚好覆盖最深的那种形态(`peach-app/dist/Peach` 里的 EXE 走三层+#: 回到 `peach/`,再留一层给更深的检出)。不做无界搜索:一路走到磁盘根会撞上碰巧同名的#: 目录,而那种「找错数据根」的故障看起来完全像是数据丢了。_SEARCH_DEPTH = 4def discover_data_root(- project_root: Path = PROJECT_ROOT, environ: dict[str, str] | None = None,+ project_root: Path = SEARCH_ANCHOR, environ: dict[str, str] | None = None,) -> tuple[Path, bool]:- """返回 (数据根, 是否真的找到了)。找不到时第一项是 `peach init` 的默认落点。"""+ """返回 (数据根, 是否真的找到了)。找不到时第一项是 `peach init` 的默认落点。++ `project_root` 是向上找的起点,默认取 `SEARCH_ANCHOR`。+ """environ = os.environ if environ is None else environexplicit = (environ.get(DATA_ROOT_ENV) or "").strip()if explicit:@@ -350,7 +370,7 @@ def _apply_environment(config: PeachConfig, environ: dict[str, str]) -> PeachCondef load_config(- project_root: Path = PROJECT_ROOT, environ: dict[str, str] | None = None,+ project_root: Path = SEARCH_ANCHOR, environ: dict[str, str] | None = None,*, strict: bool = True,) -> PeachConfig:"""读一次设置文件并合并三层。diff --git a/tests/test_config.py b/tests/test_config.pyindex cd2b764a0..a9746f0ed 100644--- a/tests/test_config.py+++ b/tests/test_config.py@@ -80,6 +80,24 @@ class DataRootDiscoveryTests(unittest.TestCase):self.assertEqual(settings_file.discover_data_root(nested, {}), (tree.data_root, True))+ def test_frozen_build_searches_from_the_executable_directory(self):+ """单文件包的 `_MEIPASS` 是临时目录,起点必须是 EXE 所在目录才找得到数据根。"""+ with TempTree() as tree:+ executable = tree.project / "dist" / "Peach" / "Peach.exe"+ executable.parent.mkdir(parents=True)+ executable.write_bytes(b"")+ anchor = settings_file._search_anchor(+ tree.root / "temp" / "_MEI123", frozen=True, executable=str(executable))+ self.assertEqual(anchor, executable.parent)+ self.assertEqual(+ settings_file.discover_data_root(anchor, {}), (tree.data_root, True))++ def test_source_tree_searches_from_the_project_root(self):+ with TempTree() as tree:+ anchor = settings_file._search_anchor(+ tree.project, frozen=False, executable=str(tree.root / "python.exe"))+ self.assertEqual(anchor, tree.project)+def test_missing_data_root_is_an_explicit_unconfigured_state(self):with TempTree(with_data_root=False) as tree:path, found = settings_file.discover_data_root(tree.project, {})