Peach: Local-first video metadata scraper and media manager.
fix(packaging): 轻量安装工具优先使用可校验的 R2 制品
longmeidaoa74aca22f2fb
diff --git a/docs/OPERATIONS.md b/docs/OPERATIONS.mdindex c03f96e70..a9b6ae57a 100644--- a/docs/OPERATIONS.md+++ b/docs/OPERATIONS.md@@ -57,6 +57,8 @@ Windows 原生 Runner 在深目录中检出项目时,需为 Git 启用 `core.l可选的 `r2` 后端使用独立 Standard 桶与 HTTPS 自定义域名。`PEACH_R2_ACCESS_KEY_ID`、`PEACH_R2_SECRET_ACCESS_KEY`、`PEACH_R2_ENDPOINT` 放入受保护且隐藏的 CI 变量,环境作用域为 `release`;`PEACH_R2_BUCKET` 和 `PEACH_R2_PUBLIC_BASE` 指定发布目标。S3 传输复用 rclone,版本文件禁止覆盖;每份文件必须从公开入口完整下载并核对大小及 SHA-256 后,才更新公开版本索引和 Release 链接。+Windows 轻量安装器缺少本地工具包时,先从 R2 下载当前版本的工具;连接失败或内容校验失败时,尝试 GitHub 的同版本文件。两个来源均须匹配安装器内置清单的大小与 SHA-256,不能混用其他版本。发布完成后,用 `scripts/smoke_split_update.py` 的 `--prepare-with-app --online` 在空缓存中运行实际制品,核对联网取得的工具能否重建完整程序。GitLab Release 的下载表指向当前后端,文档链接指向对应版本标签。+R2 公网下载流量免费,存储与请求按账户共享额度计费。预算告警只通知,不会停止服务;R2 后端不提供零费用硬封顶。要求绝不产生按量费用的部署应使用不绑定按量计费的下载服务,不启用 R2 自动发布。上传前列举本桶现有文件并计算新增制品的总容量;默认超过 8 GB 时停止上传,保留已有文件。`PEACH_R2_MAX_BYTES` 可明确调整该桶预算;此检查不能限制同账户其他桶,也不代替请求量计费保护。版本文件设置长期缓存,公开版本索引只缓存一分钟。diff --git a/docs/REUSE.md b/docs/REUSE.mdindex da9641406..6da804950 100644--- a/docs/REUSE.md+++ b/docs/REUSE.md@@ -4,6 +4,8 @@GitLab API、CI 认证和 Release 创建复用官方 [glab 1.124.0](https://gitlab.com/gitlab-org/cli)(MIT,维护中);GitLab CI 的发布写入使用短期 `CI_JOB_TOKEN` 与官方 CI 自动登录。R2 的 S3 上传复用 [rclone 1.75.2](https://rclone.org/)(MIT,维护中),使用单桶对象读写凭据;版本文件不可覆盖,并由独立 HTTPS 下载校验 SHA-256。两者都是构建期 CLI,不进入应用运行时依赖。+轻量安装器的 R2 优先与 GitHub 同版本回退复用 `standalone_update.download`、既有 HTTPX 和内置工具清单;每个来源分别验证大小与 SHA-256。发布后空缓存验收复用 `smoke_split_update.py` 与实际冻结程序的 `--installer-tools` 入口,不新增下载器或依赖。+`fetch_release_tools.py` 从固定版本官方发行站读取 SHA-256 清单,仅提取指定二进制。Python 标准库负责制品清单、版本与提交判据,不另实现 GitLab 认证、S3 签名或多段上传协议。Windows 与 macOS 的 CI 入口复用既有测试、构建和独立制品验收脚本。下载后端及费用边界见[运维说明](OPERATIONS.md#gitlab-发布与下载后端)。细粒度凭据缺少公开 CI 元数据的读取权限时,发版查询可在官方 API 上进行一次匿名 GET,使用标准库 HTTPS 与项目共用 User-Agent;不扩大凭据权限,也不携带本机认证头。私有资源仍须通过服务端权限检查。diff --git a/scripts/package_windows_release.py b/scripts/package_windows_release.pyindex 602307eee..f7f77e08d 100644--- a/scripts/package_windows_release.py+++ b/scripts/package_windows_release.py@@ -25,14 +25,19 @@ def archive(destination: Path, root: Path, files: list[Path], manifest: dict | Npackage.writestr("Peach/" + MANIFEST, json.dumps(manifest, ensure_ascii=False, sort_keys=True))-def download_notes(version: str, *, lite_installer: bool = False) -> str:+def download_notes(version: str, *, lite_installer: bool = False,+ download_base: str | None = None, macos: bool = False) -> str:"""发布页的下载入口与包类型说明。"""- base = f"https://github.com/peach-mitao/peach/releases/download/v{version}/Peach-{version}-windows-x64"+ directory = (download_base or f"https://github.com/peach-mitao/peach/releases/download/v{version}").rstrip('/')+ base = f"{directory}/Peach-{version}-windows-x64"+ mac = (f"| [macOS Apple Silicon(DMG)]({directory}/Peach-{version}-macos-arm64.dmg) | Apple Silicon Mac |\n"+ f"| [macOS Intel(DMG)]({directory}/Peach-{version}-macos-x86_64.dmg) | Intel Mac |\n" if macos else "")lite = (f"| [轻量安装包(lite-setup.exe)]({base}-lite-setup.exe) | 不含工具,安装时复用已有工具或按需联网下载 |\n"if lite_installer else "")return ("\n## 下载选择\n\n""| 下载 | 用途 |\n| --- | --- |\n"+ + mac +f"| [安装包(setup.exe)]({base}-setup.exe) | 包含全部工具,首次安装推荐 |\n"+ lite +f"| [完整免安装包(ZIP)]({base}.zip) | 包含全部工具,解压后运行 `Peach.exe` |\n"diff --git a/scripts/publish_gitlab_release.py b/scripts/publish_gitlab_release.pyindex 4566bdaff..c474086e7 100644--- a/scripts/publish_gitlab_release.py+++ b/scripts/publish_gitlab_release.py@@ -17,6 +17,15 @@ if not __package__:from peach.user_agent import USER_AGENT+def r2_release_notes(body: str, version: str, base: str, project_url: str) -> str:+ from .package_windows_release import download_notes+ text = body.split('\n## 下载选择\n', 1)[0].rstrip() + '\n'+ text = text.replace('https://github.com/peach-mitao/peach/blob/master/',+ f'{project_url.rstrip("/")}/-/blob/v{version}/')+ return text + download_notes(version, lite_installer=True, download_base=f'{base.rstrip("/")}/v{version}',+ macos=True)++def publish() -> None:tag = os.environ["CI_COMMIT_TAG"]sha = os.environ["CI_COMMIT_SHA"]@@ -45,6 +54,8 @@ def publish() -> None:raise ValueError("R2 发布清单尚未就绪")links = [{"name": item["name"], "url": item["browser_download_url"], "link_type": "package","direct_asset_path": "/" + item["name"]} for item in release["assets"]]+ notes.write_text(r2_release_notes(notes.read_text(encoding='utf-8'), tag[1:], base,+ os.environ['CI_PROJECT_URL']), encoding='utf-8')command.extend(["--assets-links", json.dumps(links)])elif backend == "gitgud":from .release_r2 import inventorydiff --git a/scripts/smoke_split_update.py b/scripts/smoke_split_update.pyindex d73dfd86f..c2d533ff9 100644--- a/scripts/smoke_split_update.py+++ b/scripts/smoke_split_update.py@@ -25,10 +25,13 @@ def inventory(package: zipfile.ZipFile) -> dict[str, str]:return result-def verify(root: Path, version: str, destination: Path, *, prepare_with_app: bool = False) -> dict:+def verify(root: Path, version: str, destination: Path, *, prepare_with_app: bool = False,+ online: bool = False) -> dict:prefix = f"Peach-{version}-windows-x64"if destination.exists():raise ValueError("制品验收目录须不存在")+ if online and not prepare_with_app:+ raise ValueError("联网验收需要使用实际应用准备工具")with zipfile.ZipFile(root / f"{prefix}-update.zip") as thin, zipfile.ZipFile(root / f"{prefix}.zip") as full:expected = inventory(full)actual = inventory(thin)@@ -56,8 +59,9 @@ def verify(root: Path, version: str, destination: Path, *, prepare_with_app: boothin.extractall(destination)if prepare_with_app:error = destination / 'tools-error.txt'+ cache = destination / 'empty-cache' if online else rootprocess = subprocess.run([str(destination / 'Peach/Peach.exe'), '--data-root', str(destination / 'setup-data'), '--installer-tools',- str(destination / 'empty-install'), str(root), str(error)],+ str(destination / 'empty-install'), str(cache), str(error)],capture_output=True, timeout=120)if process.returncode:raise ValueError(error.read_text(encoding='utf-8') if error.exists() else '轻量安装工具准备失败')@@ -78,6 +82,7 @@ if __name__ == "__main__":parser.add_argument("--version", required=True)parser.add_argument("--destination", type=Path, required=True)parser.add_argument("--prepare-with-app", action="store_true", help="由实际应用准备轻量安装所需工具")+ parser.add_argument("--online", action="store_true", help="使用空缓存从公开下载入口验收工具准备")args = parser.parse_args()print(json.dumps(verify(args.root.resolve(), args.version, args.destination.resolve(),- prepare_with_app=args.prepare_with_app), ensure_ascii=False))+ prepare_with_app=args.prepare_with_app, online=args.online), ensure_ascii=False))diff --git a/src/peach/desktop_installer.py b/src/peach/desktop_installer.pyindex d362c5114..5902bb093 100644--- a/src/peach/desktop_installer.py+++ b/src/peach/desktop_installer.py@@ -142,8 +142,9 @@ def prepare(program: Path, owner: int, error_file: Path) -> int:def prepare_install_tools(program: Path, cache: Path, error_file: Path, *, stage: Path | None = None) -> int:"""在安装器临时目录备齐工具,不读取馆藏设置或修改现有安装。"""- from . import __version__, standalone_update, update_packages+ from . import __version__, release_updates, standalone_update, update_packagesfrom .fsutil import atomic_write_text+ import httpximport tempfilestage = (stage or Path(sys.executable).resolve().parent).resolve()try:@@ -155,10 +156,14 @@ def prepare_install_tools(program: Path, cache: Path, error_file: Path, *, stageshutil.copy2(cached, archive)progress(size)else:- standalone_update.download(url, archive, size, checksum, progress)+ try:+ standalone_update.download(url, archive, size, checksum, progress)+ except (httpx.HTTPError, ValueError):+ fallback = f'{release_updates.RELEASES_URL}/download/v{__version__}/{cached.name}'+ standalone_update.download(fallback, archive, size, checksum, progress)release = {'latest_version': __version__,- 'asset_url': f'https://github.com/peach-mitao/peach/releases/download/v{__version__}/package.zip'}+ 'asset_url': f'{release_updates.DOWNLOADS_BASE}/v{__version__}/package.zip'}with tempfile.TemporaryDirectory(prefix='peach-installer-tools-', dir=stage.parent) as folder:standalone_update.prepare_tools(stage, program, Path(folder).resolve(), release, {'total': 0, 'downloaded': 0},report=lambda data, **changes: data.update(changes), fetch=fetch)diff --git a/src/peach/standalone_update.py b/src/peach/standalone_update.pyindex 411f060cc..f16c063e0 100644--- a/src/peach/standalone_update.py+++ b/src/peach/standalone_update.py@@ -212,7 +212,7 @@ def start() -> dict:if public().get("state") in ACTIVE | {"ready"}:lock.release()return public()- data = write({"id": uuid.uuid4().hex}, state="downloading", progress=0, message="正在连接 GitHub")+ data = write({"id": uuid.uuid4().hex}, state="downloading", progress=0, message="正在连接发布源")threading.Thread(target=prepare, args=(data, lock), daemon=True, name="PeachPackageUpdate").start()return public()diff --git a/tests/test_scripts.py b/tests/test_scripts.pyindex 711af553c..503332b1a 100644--- a/tests/test_scripts.py+++ b/tests/test_scripts.py@@ -5,6 +5,7 @@ import importlib.utilimport ioimport jsonimport os+import reimport shutilimport sqlite3import subprocess@@ -4790,6 +4791,32 @@ class R2ReleaseTests(unittest.TestCase):module.rclone_environment()+class ReleaseDownloadNotesTests(unittest.TestCase):+ def test_default_download_notes_use_the_github_version_directory(self):+ from scripts.package_windows_release import download_notes+ links = re.findall(r'https://[^\s)]+', download_notes('1.0.0', lite_installer=True))+ self.assertEqual(len(links), 7)+ self.assertTrue(all(url.startswith('https://github.com/peach-mitao/peach/releases/download/v1.0.0/')+ for url in links))++ def test_r2_release_notes_link_both_native_platforms_and_tagged_docs(self):+ from scripts.package_windows_release import download_notes+ from scripts.publish_gitlab_release import r2_release_notes+ body = ('### 新增\n\n- **更新**:支持公开下载。\n\n'+ '[说明](https://github.com/peach-mitao/peach/blob/master/docs/OPERATIONS.md)\n'+ + download_notes('1.0.0', lite_installer=True))+ result = r2_release_notes(body, '1.0.0', 'https://downloads.peach.video',+ 'https://gitgud.io/peach-mitao/peach')+ links = re.findall(r'https://[^\s)]+', result)+ self.assertIn('### 新增\n\n- **更新**:支持公开下载。', result)+ self.assertEqual(result.count('## 下载选择'), 1)+ self.assertEqual(links[0], 'https://gitgud.io/peach-mitao/peach/-/blob/v1.0.0/docs/OPERATIONS.md')+ self.assertEqual(len(links[1:]), 9)+ self.assertTrue(all(url.startswith('https://downloads.peach.video/v1.0.0/') for url in links[1:]))+ self.assertIn('https://downloads.peach.video/v1.0.0/Peach-1.0.0-macos-arm64.dmg', links)+ self.assertIn('https://downloads.peach.video/v1.0.0/Peach-1.0.0-macos-x86_64.dmg', links)++class GitLabReleaseTests(unittest.TestCase):SHA = "1" * 40diff --git a/tests/test_standalone_update.py b/tests/test_standalone_update.pyindex 640733971..a6e5445c8 100644--- a/tests/test_standalone_update.py+++ b/tests/test_standalone_update.py@@ -282,6 +282,61 @@ class StandaloneUpdateTests(unittest.TestCase):for bundle in manifest['tools'].values():self.assertTrue(update_packages.matches(stage, bundle['files']))+ def prepare_online_lite(self, outcome='r2'):+ from peach.desktop_installer import prepare_install_tools+ output, manifest, _, stage, error = self.lite_fixture()+ target = self.root / 'fresh-online-install'+ requests = []+ original_client = httpx.Client++ def respond(request):+ requests.append(str(request.url))+ r2 = request.url.host == 'downloads.peach.video'+ if r2 and outcome == 'unavailable':+ return httpx.Response(503)+ if not r2 and outcome == 'r2':+ raise httpx.ConnectError('GitHub unavailable', request=request)+ name = request.url.path.rsplit('/', 1)[-1]+ bad = outcome == 'both-corrupt' or (r2 and outcome == 'mismatch')+ return httpx.Response(200, content=b'corrupt' if bad else (output / name).read_bytes())++ with patch('peach.__version__', '1.0.0'), patch.object(update.httpx, 'Client', side_effect=lambda **kwargs:+ original_client(**kwargs, transport=httpx.MockTransport(respond), trust_env=False)):+ result = prepare_install_tools(target, self.root / 'empty-cache', error, stage=stage)+ self.assertFalse(target.exists())+ return result, requests, manifest, stage, error++ def test_lite_installer_prepares_from_r2_with_no_github_or_adjacent_archives(self):+ result, requests, manifest, stage, error = self.prepare_online_lite()+ self.assertEqual(result, 0, error.read_text(encoding='utf-8') if error.exists() else '')+ self.assertEqual(len(requests), 3)+ self.assertTrue(all(url.startswith('https://downloads.peach.video/v1.0.0/') for url in requests))+ for bundle in manifest['tools'].values():+ self.assertTrue(update_packages.matches(stage, bundle['files']))++ def test_lite_installer_uses_same_version_github_when_r2_is_unavailable(self):+ result, requests, manifest, stage, _ = self.prepare_online_lite('unavailable')+ self.assertEqual(result, 0)+ self.assertEqual(len(requests), 6)+ self.assertTrue(all(url.startswith('https://github.com/peach-mitao/peach/releases/download/v1.0.0/')+ for url in requests[1::2]))+ for bundle in manifest['tools'].values():+ self.assertTrue(update_packages.matches(stage, bundle['files']))++ def test_lite_installer_accepts_only_the_embedded_manifest_digest_across_sources(self):+ result, requests, manifest, stage, _ = self.prepare_online_lite('mismatch')+ self.assertEqual(result, 0)+ self.assertEqual(len(requests), 6)+ for bundle in manifest['tools'].values():+ self.assertTrue(update_packages.matches(stage, bundle['files']))++ def test_lite_installer_rejects_both_sources_when_their_bytes_do_not_match(self):+ result, requests, manifest, stage, error = self.prepare_online_lite('both-corrupt')+ self.assertEqual(result, 1)+ self.assertEqual(len(requests), 2)+ self.assertIn('校验失败', error.read_text(encoding='utf-8'))+ self.assertFalse(update_packages.matches(stage, manifest['tools']['ffmpeg']['files']))+def test_lite_installer_rejects_a_corrupt_archive_and_can_retry_without_touching_the_install(self):from peach.desktop_installer import prepare_install_toolsoutput, manifest, _, stage, error = self.lite_fixture()