From 7f9f03449c7abefd1b1a4045f6de3dd78b157aa1 Mon Sep 17 00:00:00 2001 From: lzy <2041493578@qq.com> Date: Fri, 16 Oct 2026 20:27:31 +0800 Subject: [PATCH] Clarify update guidance for builds that cannot self-update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The update window always rendered a "下载并重启更新" button, disabled for source runs, non-Windows platforms and releases without a verifiable asset, and its hint told source users to download a Windows update package they cannot apply. Decide the reason in one pure helper, show the actionable step instead (git pull for a source checkout), and drop the dead button and progress bar from that window. A background startup check now only reports in the status bar when no automatic install is possible; the manual check still opens the full window so release notes and the release page stay reachable. --- CHANGELOG.md | 7 +++++++ README.zh-CN.md | 2 +- exmaterial/exmaterial_app.py | 2 +- exmaterial/update_ui.py | 27 ++++++++++++++++----------- exmaterial/updates.py | 20 ++++++++++++++++++-- tests/test_updates.py | 13 +++++++++++++ 6 files changed, 56 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a9e14b..d002365 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project are documented here. +## [Unreleased] + +### 改进 + +- 源码运行、非 Windows 平台或发布缺少可校验安装包时,更新窗口不再显示无法使用的“下载并重启更新”按钮,改为说明当前运行方式与对应的手动升级步骤。 +- 无法自动更新时,启动后台检查只在状态栏提示新版本,不再弹出更新窗口;“帮助 → 检查更新”仍提供完整说明和发布页入口。 + ## [1.0.3] - 2026-10-13 ### 新增 diff --git a/README.zh-CN.md b/README.zh-CN.md index 0653685..1712c95 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -58,7 +58,7 @@ python -m pip install ".[build]" 发现新版本后,点击“下载并重启更新”。下载完成后确认编辑内容已保存,程序会退出、替换自身并启动新版。更新只替换当前 EXE,`information/` 数据目录继续保留;新版启动失败时会尝试恢复旧程序。网络不可用时仍可正常使用软件,并可通过更新窗口打开发布页手动下载。 -此前没有更新功能的旧 EXE 需要先手动下载一次包含此功能的版本;此后可在软件内更新。源码运行方式仅提供检查和发布页入口。 +此前没有更新功能的旧 EXE 需要先手动下载一次包含此功能的版本;此后可在软件内更新。源码运行无法自动替换解释器,更新窗口会给出 `git pull` 等手动升级步骤,启动后台检查只在状态栏提示新版本。 发布维护要求:正式 Release 使用 `v主版本.次版本.补丁版本` 标签,附带 `ExMaterial-版本-windows-x64.exe`(或 `ExMaterial.exe`),并确保 GitHub 资产提供 SHA-256 `digest`。缺少可校验 EXE 时软件提供手动下载入口。源码版本和程序显示版本来自同一版本定义,发布时须同步更新 `exmaterial/__init__.py` 和 `pyproject.toml`。 diff --git a/exmaterial/exmaterial_app.py b/exmaterial/exmaterial_app.py index 62eddb3..7069db9 100644 --- a/exmaterial/exmaterial_app.py +++ b/exmaterial/exmaterial_app.py @@ -520,7 +520,7 @@ def __init__(self, root: Tk) -> None: self.plot_configs: dict[str, dict] = {} self.last_sample_density = "" self.status = StringVar() - self.updater = UpdateManager(root, APP_VERSION) + self.updater = UpdateManager(root, APP_VERSION, notify=self.set_status) self.build_ui() self.bind_xrd_file_drop() self.refresh_materials() diff --git a/exmaterial/update_ui.py b/exmaterial/update_ui.py index 5d9ca3a..7ee78f3 100644 --- a/exmaterial/update_ui.py +++ b/exmaterial/update_ui.py @@ -3,7 +3,6 @@ import queue import shutil -import sys import threading import webbrowser from tkinter import BOTH, END, LEFT, Button, Frame, Label, Text, Toplevel, messagebox, ttk @@ -15,9 +14,10 @@ class UpdateManager: - def __init__(self, root, current_version: str): + def __init__(self, root, current_version: str, notify=None): self.root = root self.current_version = current_version + self.notify = notify self.events = queue.Queue() self.busy = False self.window = None @@ -50,7 +50,11 @@ def poll(self) -> None: if kind == "checked": self.busy = False if value is not None: - self.show_release(value) + hint = updates.manual_update_hint(value) + if hint and not extra and self.notify: + self.notify(f"发现新版本 {value.version},{hint}") + else: + self.show_release(value) elif extra: messagebox.showinfo("检查更新", f"当前 {self.current_version} 已是最新正式版。", parent=self.root) elif kind == "check_error": @@ -92,19 +96,20 @@ def show_release(self, release: updates.Release) -> None: notes.pack(fill=BOTH, expand=True) notes.insert(END, release.notes) notes.configure(state="disabled") - can_install = release.can_install and sys.platform == "win32" and getattr(sys, "frozen", False) - hint = "更新会关闭并重启软件,请先保存编辑内容。实验数据目录会保留。" - if not can_install: - hint = "请从发布页下载 Windows 版更新包,或更新本地源码。" - self.status = Label(body, text=hint, wraplength=520, justify=LEFT) + hint = updates.manual_update_hint(release) + self.status = Label(body, wraplength=520, justify=LEFT, + text=hint or "更新会关闭并重启软件,请先保存编辑内容。实验数据目录会保留。") self.status.pack(anchor="w", pady=8) - self.progress = ttk.Progressbar(body, maximum=100) - self.progress.pack(fill="x", pady=(0, 8)) + if not hint: + self.progress = ttk.Progressbar(body, maximum=100) + self.progress.pack(fill="x", pady=(0, 8)) buttons = Frame(body) buttons.pack(fill="x") Button(buttons, text="查看发布页", command=lambda: webbrowser.open(release.page_url)).pack(side=LEFT) Button(buttons, text="稍后", command=self.close).pack(side=LEFT, padx=8) - self.install_button = Button(buttons, text="下载并重启更新", command=self.download, state="normal" if can_install else "disabled") + if hint: + return + self.install_button = Button(buttons, text="下载并重启更新", command=self.download) self.install_button.pack(side="right") def download(self) -> None: diff --git a/exmaterial/updates.py b/exmaterial/updates.py index 6c8f06c..e0585fc 100644 --- a/exmaterial/updates.py +++ b/exmaterial/updates.py @@ -41,6 +41,22 @@ def can_install(self) -> bool: return bool(self.download_url and self.sha256 and self.size) +SOURCE_UPDATE_HINT = "当前从源码运行,自动替换仅适用于 Windows EXE 版。请在源码目录执行 git pull 后重启软件,数据目录不受影响。" +PLATFORM_UPDATE_HINT = "自动替换仅适用于 Windows EXE 版,请从发布页下载对应版本。" +NO_PACKAGE_UPDATE_HINT = "此版本没有可校验的 Windows 安装包,请从发布页下载。" + + +def manual_update_hint(release: Release) -> str: + """Return why this running build cannot self-update, or an empty string when it can.""" + if not release.can_install: + return NO_PACKAGE_UPDATE_HINT + if sys.platform != "win32": + return PLATFORM_UPDATE_HINT + if not getattr(sys, "frozen", False): + return SOURCE_UPDATE_HINT + return "" + + def version_tuple(version: str) -> tuple[int, int, int]: match = re.fullmatch(r"[vV]?(\d+)\.(\d+)\.(\d+)(?:\+[\w.-]+)?", version) if not match: @@ -111,7 +127,7 @@ def download_release(release: Release, progress=None) -> Path: or parsed.query or parsed.fragment or not parsed.path.lower().endswith(".exe") or not re.fullmatch(r"[a-fA-F0-9]{64}", release.sha256) or not 2 <= release.size <= MAX_DOWNLOAD_SIZE): - raise UpdateError("此版本没有可校验的 Windows 安装包,请从发布页下载。") + raise UpdateError(NO_PACKAGE_UPDATE_HINT) stage = Path(tempfile.mkdtemp(prefix="exmaterial-update-")) payload = stage / "ExMaterial-update.exe" try: @@ -241,7 +257,7 @@ def installer_script(target: Path, payload: Path, pid: int, expected_sha256: str def launch_installer(payload: Path, expected_sha256: str) -> None: if sys.platform != "win32" or not getattr(sys, "frozen", False): - raise UpdateError("自动替换仅适用于 Windows EXE 版。源码版请从发布页更新。") + raise UpdateError(PLATFORM_UPDATE_HINT if sys.platform != "win32" else SOURCE_UPDATE_HINT) target = Path(sys.executable).resolve(strict=True) # Fail before closing the app when its install directory is not writable. try: diff --git a/tests/test_updates.py b/tests/test_updates.py index 6f5b823..8c97ce5 100644 --- a/tests/test_updates.py +++ b/tests/test_updates.py @@ -82,6 +82,19 @@ def test_source_edition_cannot_replace_python_executable(self): with self.assertRaises(updates.UpdateError): updates.launch_installer(Path("unused.exe"), "0" * 64) + def test_manual_update_hint_matches_the_running_build(self): + release = updates.release_from_json(release_data(), "1.0.0") + with patch.object(updates.sys, "platform", "win32"), patch.object(updates.sys, "frozen", True, create=True): + self.assertEqual(updates.manual_update_hint(release), "") + with patch.object(updates.sys, "platform", "win32"), patch.object(updates.sys, "frozen", False, create=True): + self.assertIn("git pull", updates.manual_update_hint(release)) + with patch.object(updates.sys, "platform", "linux"): + self.assertIn("发布页", updates.manual_update_hint(release)) + unverifiable = release_data() + unverifiable["assets"][0]["digest"] = None + with patch.object(updates.sys, "platform", "win32"), patch.object(updates.sys, "frozen", True, create=True): + self.assertIn("发布页", updates.manual_update_hint(updates.release_from_json(unverifiable, "1.0.0"))) + def test_rate_limit_has_actionable_message(self): error = HTTPError(updates.API_URL, 403, "rate limit exceeded", {}, None) with patch.object(updates, "urlopen", side_effect=error):