Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix(packaging): macOS 安装包带上 Sentry DSN,发行构建缺 DSN 时失败

longmeidao9a26ed8aef81
diff --git a/docs/DIAGNOSTICS.md b/docs/DIAGNOSTICS.md
index 4e83182d1..8a716aee7 100644
--- a/docs/DIAGNOSTICS.md
+++ b/docs/DIAGNOSTICS.md
@@ -57,7 +57,7 @@
两项均通过公开 DSN 发送到 Sentry 托管服务,接收服务会接触连接 IP。客户端不发送 IP 字段、用户身份、设备标识、观看记录、媒体信息、凭据、完整日志或诊断 ZIP。SDK 发送队列不落盘,本机聚合文件仅存技术投影和次数;后台发送前复查本机同意版本。关闭或重新选择会清除未提交聚合,并让排队旧事件失效;已在途或已接收的请求无法撤回。本机 ZIP 导出独立可用。
-接收端由维护者配置:源码与 Docker 使用 `PEACH_SENTRY_DSN`;Windows 发行构建从同名环境变量写入包内 `sentry.json`,GitHub Release 使用仓库变量 `PEACH_SENTRY_DSN`。只接受 Sentry 的公开 HTTPS DSN(含 US / DE 区域),不需要 API token。没有 DSN 时不会创建 SDK 客户端。SDK 自动框架集成、日志、性能追踪和指标均关闭;版本统计作为普通事件计入 Sentry 错误事件额度。账号套餐和总量以 [Sentry 订阅页](https://sentry.io/settings/) 为准。
+接收端由维护者配置:源码与 Docker 使用 `PEACH_SENTRY_DSN`;Windows 与 macOS 打包由 `scripts/write_reporting_config.py` 从同名环境变量写入包根 `sentry.json`。发行流水线从 GitGud 项目 CI/CD 变量 `PEACH_SENTRY_DSN` 与 `PEACH_LIBRARY_SENTRY_DSN` 读取,两者为受保护变量、环境作用域 `release`;缺失或不合法时发行构建失败,本地构建写空值,安装后显示上传服务未配置。只接受 Sentry 的公开 HTTPS DSN(含 US / DE 区域),不需要 API token。没有 DSN 时不会创建 SDK 客户端。SDK 自动框架集成、日志、性能追踪和指标均关闭;版本统计作为普通事件计入 Sentry 错误事件额度。账号套餐和总量以 [Sentry 订阅页](https://sentry.io/settings/) 为准。
回归验收使用临时 Peach 数据目录和 SDK 内存接收端。实际接收与发行制品的验收记录保存在仓库外。技术诊断选择见 [ADR-0110](adr/0110-recommended-diagnostics-choice.md)。安装辅助进程只在本机取证,不初始化远程客户端或发送事件;应用进程按用户选择发送。
@@ -110,7 +110,7 @@
原始 JSON 上限 100 MiB,gzip 附件上限 18 MiB;任一超限均整份拒绝,不发送截断数据。gzip 头不含源文件名和生成时间。传输只接受固定用途事件、固定文件名附件与字段白名单,后台真正发送前复查同意版本。关闭让排队记录失效,已发送数据无法经本机开关撤回。
-维护者另设 `PEACH_LIBRARY_SENTRY_DSN`,Windows Release 读取同名仓库变量并写入包内 `library_dsn`。该 DSN 的项目必须与错误诊断项目不同,同项目配置视为未配置。研究快照作为 Sentry 附件接收,每次也产生一条事件;组织内的事件和附件额度仍共享。见 [Sentry 附件说明](https://docs.sentry.io/platforms/python/enriching-events/attachments/)。
+维护者另设 `PEACH_LIBRARY_SENTRY_DSN`,两端打包按上文写入包内 `library_dsn`。该 DSN 的项目必须与错误诊断项目不同,同项目配置视为未配置。研究快照作为 Sentry 附件接收,每次也产生一条事件;组织内的事件和附件额度仍共享。见 [Sentry 附件说明](https://docs.sentry.io/platforms/python/enriching-events/attachments/)。
群体分析按 `period` 分开汇总作品持有率、作品评分分布、人物及系列偏好和手动标签;每份快照每部作品只贡献一次持有,实体的 `favorite` 表示主动收藏,`collected` 表示持有相关作品,不能混用。评分使用有效评分人数或数量作分母,不能把未评分当作 0 分,也不能把文件数量当用户数量。每周快照是累计状态,不能跨周累加成新增收藏或播放。没有跨周期标识,但不能据此宣称明细无法关联;每机每周一次也不能识别同一人多安装。结果只代表自愿参与样本,须说明安装覆盖与选择偏差。接收端分析看板不属于客户端上传功能。
diff --git a/scripts/build_macos_standalone.py b/scripts/build_macos_standalone.py
index a43f410e3..7ae90a42d 100644
--- a/scripts/build_macos_standalone.py
+++ b/scripts/build_macos_standalone.py
@@ -70,7 +70,8 @@ def package_dmg(app: Path, destination: Path) -> Path:
return destination
-def build(destination: Path, *, dmg: bool = True, target_arch: str | None = None) -> dict[str, str]:
+def build(destination: Path, *, dmg: bool = True, target_arch: str | None = None,
+ require_reporting: bool = False) -> dict[str, str]:
if sys.platform != "darwin":
raise SystemExit("macOS 应用必须在 macOS 上构建")
if (ROOT / "resources" / "seed").exists():
@@ -78,6 +79,8 @@ def build(destination: Path, *, dmg: bool = True, target_arch: str | None = None
architecture = target_arch or platform.machine()
if architecture not in {"arm64", "x86_64"} or architecture != platform.machine():
raise SystemExit("目标架构必须与当前 Python 一致;请使用 arm64 或 x86_64 Python 及对应依赖构建")
+ from write_reporting_config import reporting_config, write as write_reporting_config
+ reporting_config(require=require_reporting)
from build_macos_app import build_frontend_bundles
from media_tool_bundle import validate
from build_macos_media import audit_binary
@@ -107,11 +110,13 @@ def build(destination: Path, *, dmg: bool = True, target_arch: str | None = None
capture_output=True, text=True, encoding="utf-8", check=True).stdout.strip()),
}
(work / "build-info.json").write_text(json.dumps(identity), encoding="utf-8")
+ reporting = write_reporting_config(work / "sentry.json", require=require_reporting)
subprocess.run([
sys.executable, "-m", "PyInstaller", "--noconfirm", "--clean",
"--distpath", str(destination), "--workpath", str(work / "app"),
str(ROOT / "scripts" / "peach_macos.spec"),
], check=True, cwd=ROOT, env={**os.environ, "PEACH_MACOS_BUILD_INFO": str(work / "build-info.json"),
+ "PEACH_MACOS_REPORTING_CONFIG": str(reporting),
"PEACH_MACOS_TARGET_ARCH": architecture})
app = destination / "Peach.app"
tools = app / "Contents/Resources/tools"
@@ -147,9 +152,11 @@ def main() -> int:
parser.add_argument("--destination", type=Path)
parser.add_argument("--target-arch", choices=("arm64", "x86_64"), default=platform.machine())
parser.add_argument("--no-dmg", action="store_true", help="只生成独立应用")
+ parser.add_argument("--require-reporting", action="store_true", help="发行构建:缺 Sentry DSN 时失败")
args = parser.parse_args()
destination = args.destination or ROOT / "dist" / "macos.noindex" / args.target_arch
- print(json.dumps(build(destination, dmg=not args.no_dmg, target_arch=args.target_arch), ensure_ascii=False))
+ print(json.dumps(build(destination, dmg=not args.no_dmg, target_arch=args.target_arch,
+ require_reporting=args.require_reporting), ensure_ascii=False))
return 0
diff --git a/scripts/build_windows.ps1 b/scripts/build_windows.ps1
index 7a08200a7..dde0a10a5 100644
--- a/scripts/build_windows.ps1
+++ b/scripts/build_windows.ps1
@@ -5,7 +5,9 @@
[switch]$Installer,
[string]$CloudflaredPath = '',
[string]$FFmpegPath = '',
- [string]$UntruncPath = ''
+ [string]$UntruncPath = '',
+ # 发行构建:两项 Sentry DSN 必须齐全且合法,否则不产出安装包。
+ [switch]$RequireReporting
)
$ErrorActionPreference = 'Stop'
@@ -83,9 +85,10 @@ $BuildInfo = [ordered]@{
}
Set-Content -LiteralPath $BuildInfoPath -Value (ConvertTo-Json $BuildInfo) -Encoding utf8
$SentryConfigPath = Join-Path $BuildPath 'sentry.json'
-Set-Content -LiteralPath $SentryConfigPath -Value (ConvertTo-Json @{
- dsn = "$env:PEACH_SENTRY_DSN"; library_dsn = "$env:PEACH_LIBRARY_SENTRY_DSN"
-}) -Encoding utf8
+$ReportingArguments = @('--output', $SentryConfigPath)
+if ($RequireReporting) { $ReportingArguments += '--require' }
+& $Python -X utf8 (Join-Path $PSScriptRoot 'write_reporting_config.py') @ReportingArguments
+if ($LASTEXITCODE -ne 0) { throw 'Sentry reporting config is missing or invalid.' }
$BuildMode = @('--onefile')
$BuildDestination = $OutputPath
diff --git a/scripts/ci_macos.sh b/scripts/ci_macos.sh
index 592bb6b04..ec08494fd 100644
--- a/scripts/ci_macos.sh
+++ b/scripts/ci_macos.sh
@@ -84,6 +84,8 @@ uv sync --locked --all-extras --python "$python" --no-cache \
--reinstall-package zeroconf --no-binary-package zeroconf
uv pip check --python "$python"
output="${PEACH_MACOS_OUTPUT_DIR:-$root/dist/macos.noindex/$architecture}"
+# 媒体工具编译耗时长;缺 Sentry DSN 的发行构建在此之前失败。
+"$python" scripts/write_reporting_config.py --require --output "$root/build/macos-standalone/$architecture/sentry.json"
"$python" scripts/setup_macos_media_tools.py --root "$root/build/macos-media-toolchain"
export PATH="$root/build/macos-media-toolchain/env/bin:$PATH"
for tool in ffmpeg untrunc; do
@@ -92,7 +94,7 @@ for tool in ffmpeg untrunc; do
done
"$python" scripts/fetch_cloudflared_macos.py --arch "$architecture" \
--destination "$root/build/media-macos/$architecture/cloudflared"
-"$python" scripts/build_macos_standalone.py --target-arch "$architecture" --destination "$output"
+"$python" scripts/build_macos_standalone.py --target-arch "$architecture" --destination "$output" --require-reporting
"$python" -c 'import json,subprocess,sys; from pathlib import Path; info=json.loads(Path("build/macos-standalone",sys.argv[1],"build-info.json").read_text()); head=subprocess.check_output(["git","rev-parse","HEAD"],text=True).strip(); assert info["commit"]==head and not info["dirty"], info' "$architecture"
cp "build/macos-standalone/$architecture/build-info.json" "$output/build-info.json"
version="$("$python" -c 'from peach import __version__; print(__version__)')"
@@ -115,6 +117,8 @@ hdiutil attach "$image" -readonly -nobrowse -mountpoint "$mountpoint"
mounted=1
test "$(readlink "$mountpoint/Applications")" = /Applications
test -s "$mountpoint/Peach.app/Contents/Resources/LICENSE"
+"$python" -c 'import json,sys; config=json.load(open(sys.argv[1],encoding="utf-8")); assert config["dsn"] and config["library_dsn"], "包内 sentry.json 缺 DSN"' \
+ "$mountpoint/Peach.app/Contents/Resources/sentry.json"
codesign --verify --deep --strict "$mountpoint/Peach.app"
lipo -verify_arch "$architecture" "$mountpoint/Peach.app/Contents/MacOS/Peach"
"$python" scripts/smoke_macos.py "$mountpoint/Peach.app"
diff --git a/scripts/ci_windows.ps1 b/scripts/ci_windows.ps1
index a7dbbb5dd..c574f2b2c 100644
--- a/scripts/ci_windows.ps1
+++ b/scripts/ci_windows.ps1
@@ -36,7 +36,7 @@ if ($Mode -eq 'build') {
Assert-Exit 'cloudflared 校验失败'
& ./scripts/test.ps1 -Scope packaging -Focus test_desktop_installer
Assert-Exit '安装目录保护回归失败'
- & ./scripts/build_windows.ps1 -Installer -CloudflaredPath build/cloudflared.exe -FFmpegPath build/ffmpeg-windows/ffmpeg -UntruncPath build/untrunc-windows/untrunc
+ & ./scripts/build_windows.ps1 -Installer -RequireReporting -CloudflaredPath build/cloudflared.exe -FFmpegPath build/ffmpeg-windows/ffmpeg -UntruncPath build/untrunc-windows/untrunc
Assert-Exit 'Windows 构建失败'
New-Item -ItemType Directory -Path release -Force | Out-Null
& .venv/Scripts/python.exe scripts/package_windows_release.py --source dist/Peach --output release --version $Version --lite-installer
diff --git a/scripts/peach_macos.spec b/scripts/peach_macos.spec
index c65ee7808..d1beda197 100644
--- a/scripts/peach_macos.spec
+++ b/scripts/peach_macos.spec
@@ -17,6 +17,7 @@ version = json.loads(identity.read_text(encoding="utf-8"))["version"]
resource_manifest = runpy.run_path(str(root / 'src/peach/bundle_resources.py'))
datas = [(str(source), target) for source, target in resource_manifest['data_files'](root)]
datas += [(str(identity), "."), (str(root / "scripts" / "standalone.txt"), ".")]
+datas += [(os.environ["PEACH_MACOS_REPORTING_CONFIG"], ".")]
datas += [(str(root / "LICENSE"), ".")]
datas += copy_metadata("sentry-sdk")
binaries = []
diff --git a/scripts/write_reporting_config.py b/scripts/write_reporting_config.py
new file mode 100644
index 000000000..8967a0d84
--- /dev/null
+++ b/scripts/write_reporting_config.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python3
+"""把构建环境里的公开 Sentry DSN 写成包根 `sentry.json`,Windows 与 macOS 打包共用。
+
+只读环境变量 `PEACH_SENTRY_DSN` 与 `PEACH_LIBRARY_SENTRY_DSN`,不读检出里的 `sentry.json`。
+本地构建缺 DSN 时写空值,安装后显示上传服务未配置;发行构建带 `--require`,
+缺失、格式不合法或两项指向同一 Sentry 项目都直接失败,不产出静默的安装包。
+"""
+from __future__ import annotations
+
+import argparse
+import json
+import os
+from pathlib import Path
+import sys
+
+ROOT = Path(__file__).resolve().parents[1]
+sys.path.insert(0, str(ROOT / "src"))
+
+from peach import library_sharing, remote_reporting
+
+TARGETS = {"dsn": "PEACH_SENTRY_DSN", "library_dsn": "PEACH_LIBRARY_SENTRY_DSN"}
+
+
+def reporting_config(*, require: bool = False) -> dict[str, str]:
+ config = {key: os.environ.get(name, "").strip() for key, name in TARGETS.items()}
+ if not require:
+ return config
+ missing = [name for key, name in TARGETS.items() if not config[key]]
+ if missing:
+ raise SystemExit(f"发行构建缺少 CI 变量:{'、'.join(missing)}")
+ if remote_reporting.dsn() != config["dsn"]:
+ raise SystemExit("PEACH_SENTRY_DSN 不是 Sentry 托管服务的公开 DSN")
+ if library_sharing.target() != config["library_dsn"]:
+ raise SystemExit("PEACH_LIBRARY_SENTRY_DSN 不是公开 DSN,或与错误诊断指向同一 Sentry 项目")
+ return config
+
+
+def write(output: Path, *, require: bool = False) -> Path:
+ output.parent.mkdir(parents=True, exist_ok=True)
+ output.write_text(json.dumps(reporting_config(require=require)), encoding="utf-8")
+ return output
+
+
+def main() -> int:
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("--output", type=Path, required=True)
+ parser.add_argument("--require", action="store_true", help="发行构建:两项 DSN 必须齐全且合法")
+ args = parser.parse_args()
+ print(write(args.output, require=args.require))
+ return 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/tests/test_remote_reporting.py b/tests/test_remote_reporting.py
index 0908251c9..9e5e03ec3 100644
--- a/tests/test_remote_reporting.py
+++ b/tests/test_remote_reporting.py
@@ -537,6 +537,53 @@ class RemoteReportingTests(unittest.TestCase):
client.assert_not_called()
+class PackagedReportingConfigTests(unittest.TestCase):
+ """两端打包共用的 `sentry.json` 写入;发行构建缺 DSN 必须失败,而不是产出静默的安装包。"""
+
+ LIBRARY_DSN = "https://[email protected]/789"
+
+ def setUp(self):
+ folder = tempfile.TemporaryDirectory()
+ self.addCleanup(folder.cleanup)
+ self.root = Path(folder.name).resolve()
+ self.output = self.root / "bundle" / "sentry.json"
+
+ def write(self, *flags, **targets):
+ env = {key: value for key, value in os.environ.items()
+ if key not in {"PEACH_SENTRY_DSN", "PEACH_LIBRARY_SENTRY_DSN"}}
+ env.update(targets, PYTHONIOENCODING="utf-8")
+ script = Path(__file__).resolve().parents[1] / "scripts/write_reporting_config.py"
+ return subprocess.run([sys.executable, "-X", "utf8", str(script), "--output", str(self.output), *flags],
+ capture_output=True, text=True, encoding="utf-8", env=env, timeout=60)
+
+ def test_release_config_is_read_back_by_the_packaged_runtime(self):
+ result = self.write("--require", PEACH_SENTRY_DSN=DSN, PEACH_LIBRARY_SENTRY_DSN=self.LIBRARY_DSN)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ from peach import library_sharing
+ with mock.patch.dict(os.environ, {"PEACH_SENTRY_DSN": "", "PEACH_LIBRARY_SENTRY_DSN": ""}), \
+ mock.patch("peach.settings_file.PROJECT_ROOT", self.output.parent):
+ self.assertEqual(reporting.dsn(), DSN)
+ self.assertEqual(library_sharing.target(), self.LIBRARY_DSN)
+
+ def test_local_build_without_targets_ships_an_unconfigured_bundle(self):
+ result = self.write()
+ self.assertEqual(result.returncode, 0, result.stderr)
+ self.assertEqual(json.loads(self.output.read_text(encoding="utf-8")), {"dsn": "", "library_dsn": ""})
+
+ def test_release_build_rejects_missing_invalid_or_shared_targets(self):
+ cases = {
+ "缺共享馆藏": {"PEACH_SENTRY_DSN": DSN},
+ "非 Sentry 托管": {"PEACH_SENTRY_DSN": "https://[email protected]/456",
+ "PEACH_LIBRARY_SENTRY_DSN": self.LIBRARY_DSN},
+ "同一项目": {"PEACH_SENTRY_DSN": DSN, "PEACH_LIBRARY_SENTRY_DSN": DSN},
+ }
+ for name, targets in cases.items():
+ with self.subTest(name):
+ result = self.write("--require", **targets)
+ self.assertNotEqual(result.returncode, 0)
+ self.assertFalse(self.output.exists())
+
+
class RemoteReportingApiTests(unittest.TestCase):
setUp = RemoteReportingTests.setUp