ohos-devloop 0.1.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ohos_devloop/__init__.py +2 -0
- ohos_devloop/_version.py +24 -0
- ohos_devloop/agents/skills/collab-delegate/SKILL.md +79 -0
- ohos_devloop/agents/skills/loop-iterate/SKILL.md +54 -0
- ohos_devloop/agents/skills/openharmony-ci/SKILL.md +71 -0
- ohos_devloop/agents/skills/openharmony-ci/agents/openai.yaml +4 -0
- ohos_devloop/agents/skills/openharmony-ci/scripts/openharmony_ci.py +520 -0
- ohos_devloop/agents/skills/pool-status/SKILL.md +47 -0
- ohos_devloop/config/e2e-cases.yaml +31 -0
- ohos_devloop/config/e2e-hap.yaml +54 -0
- ohos_devloop/config/inventory-template.yaml +35 -0
- ohos_devloop/config/inventory.yaml +68 -0
- ohos_devloop/config/report-templates/analysis-report.md +48 -0
- ohos_devloop/config/report-templates/impl-report.md +47 -0
- ohos_devloop/config/task-template.md +21 -0
- ohos_devloop/config/tdd-board.yaml +31 -0
- ohos_devloop/config/tdd-coverage.yaml +47 -0
- ohos_devloop/config/tdd-emulator.yaml +36 -0
- ohos_devloop/config/tdd-host.yaml +26 -0
- ohos_devloop/config/tdd-suites.yaml +34 -0
- ohos_devloop/config/templates/agents-user.md +11 -0
- ohos_devloop/config/templates/quickstart.md +82 -0
- ohos_devloop/config/verify.yaml +45 -0
- ohos_devloop/examples/anchor-e2e.yaml +55 -0
- ohos_devloop/examples/assert-smoke.yaml +33 -0
- ohos_devloop/examples/diffcov-fixture/cov.json +6688 -0
- ohos_devloop/examples/diffcov-fixture/diff.txt +24 -0
- ohos_devloop/examples/diffcov-smoke.yaml +32 -0
- ohos_devloop/examples/e2e-emu-smoke.yaml +45 -0
- ohos_devloop/examples/layout-fixture.norm.json +1 -0
- ohos_devloop/examples/smoke.yaml +27 -0
- ohos_devloop/ohos_cli.py +131 -0
- ohos_devloop/scripts/agent-run-v3.sh +149 -0
- ohos_devloop/scripts/apply.py +1448 -0
- ohos_devloop/scripts/check_env.py +284 -0
- ohos_devloop/scripts/collab.py +303 -0
- ohos_devloop/scripts/diffcov.py +211 -0
- ohos_devloop/scripts/flash.py +511 -0
- ohos_devloop/scripts/mount_ws.py +192 -0
- ohos_devloop/scripts/release.py +2645 -0
- ohos_devloop/scripts/release_audit.py +260 -0
- ohos_devloop/scripts/release_metadata.py +397 -0
- ohos_devloop/scripts/release_smoke.py +287 -0
- ohos_devloop/scripts/report_draft.py +234 -0
- ohos_devloop/scripts/server_init.py +310 -0
- ohos_devloop/scripts/sgrep.py +82 -0
- ohos_devloop/scripts/sign_hap.py +256 -0
- ohos_devloop/scripts/telemetry.py +260 -0
- ohos_devloop/scripts/ui_probe.py +396 -0
- ohos_devloop/scripts/verify_frame_diff.py +138 -0
- ohos_devloop/scripts/ws_init.py +131 -0
- ohos_devloop/scripts/wsroot.py +192 -0
- ohos_devloop-0.1.2.dist-info/METADATA +177 -0
- ohos_devloop-0.1.2.dist-info/RECORD +58 -0
- ohos_devloop-0.1.2.dist-info/WHEEL +5 -0
- ohos_devloop-0.1.2.dist-info/entry_points.txt +2 -0
- ohos_devloop-0.1.2.dist-info/licenses/LICENSE +202 -0
- ohos_devloop-0.1.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,2645 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""release.py —— 裸 release 预检 + TestPyPI 演练(Phase 5 D-01/D-02/D-05..D-08/D-10,库 + CLI)。
|
|
4
|
+
|
|
5
|
+
裸模式(无参)完成:精确 tag/干净树检查 → clean dist → python -m build --wheel → 首次构建
|
|
6
|
+
归档(runs/release-<version>/artifact/,SHA256 为正本)→ release_audit 四步审计(复用
|
|
7
|
+
既有正本,不复制)→ 给定归档 wheel 的真实 venv/smoke(release_smoke.run_smoke)→
|
|
8
|
+
只读双面预检(凭据存在性 / 远端 tag 对账 / PyPI 占用 / GitCode 认证+可见性+Release 占用)
|
|
9
|
+
→ 打印版本、tag、SHA256 与目标后停止。
|
|
10
|
+
|
|
11
|
+
test 模式(05-03):TestPyPI 演练——A1=create-then-verify 写入门(05-VALIDATION.md
|
|
12
|
+
Decision Outcomes,pending/strict-literal 均 fail-closed 且 strict-literal 连 TestPyPI
|
|
13
|
+
也阻塞)→ 精确 tag/干净树实时复检 → TestPyPI 占用(同版本即使同 SHA256 也冲突,重新
|
|
14
|
+
演练必须 bump tag,D-06)→ twine 凭据前门 → 唯一归档 wheel 上传(显式 repository-url,
|
|
15
|
+
无 skip-existing)→ JSON API 有界轮询验证(PollPolicy)→ 原子写脱敏 test receipt
|
|
16
|
+
(version/tag/HEAD/wheel SHA256 + TestPyPI api/file URL/upload_time/verified_at)。
|
|
17
|
+
已有同版本归档时 test/all 零重建(归档直取,只重跑归档 audit/smoke)。
|
|
18
|
+
|
|
19
|
+
铁的边界(must_haves):
|
|
20
|
+
- 裸模式零 twine upload、零 GitCode POST/PUT;test 只写 TestPyPI 面。
|
|
21
|
+
- 不用 reset --hard / git clean 制造干净树;clean 只清理路径守卫后的仓根 dist 构建区。
|
|
22
|
+
- 归档字节才是审计、smoke 和发布的正本;dist 只是候选构建区(裸模式为履行 D-02 的
|
|
23
|
+
候选构建只写 preflight.candidate 观察值,不覆盖归档、不改 receipt、不改 target)。
|
|
24
|
+
- 状态语义唯一:target 只表示当前归档已获授权的上传轨道(test/all),last_command 另记
|
|
25
|
+
bare/test/all/resume;裸预检只更新 last_command/preflight 并写独立 preflight.json,
|
|
26
|
+
绝不改 target/surfaces、不重写 receipt.json——TestPyPI verified evidence 全程保留。
|
|
27
|
+
- 凭据只从环境变量读取(TWINE_USERNAME/TWINE_PASSWORD/GITCODE_TOKEN),不打印、不落盘、
|
|
28
|
+
不读隐式凭据文件;预检只证明存在性与 GitCode 只读认证,写权限由真实写操作证明(A5)。
|
|
29
|
+
- GitCode access_token 只在内存组 URL;异常统一重写为脱敏 ReleaseError(host + 异常类型),
|
|
30
|
+
日志/状态只留白名单字段;operations.jsonl 只记白名单事件(区分本地上传前门与服务器拒绝)。
|
|
31
|
+
|
|
32
|
+
CLI:
|
|
33
|
+
python ohos_devloop/scripts/release.py (或 ohos-devloop release)
|
|
34
|
+
python ohos_devloop/scripts/release.py test (或 ohos-devloop release test)
|
|
35
|
+
退出码:0=全绿 1=预检/审计/smoke/上传门失败(fail-closed) 2=用法错误
|
|
36
|
+
"""
|
|
37
|
+
from __future__ import annotations
|
|
38
|
+
|
|
39
|
+
import dataclasses
|
|
40
|
+
import hashlib
|
|
41
|
+
import io
|
|
42
|
+
import json
|
|
43
|
+
import os
|
|
44
|
+
import re
|
|
45
|
+
import shutil
|
|
46
|
+
import subprocess
|
|
47
|
+
import sys
|
|
48
|
+
import time
|
|
49
|
+
import urllib.error
|
|
50
|
+
import urllib.parse
|
|
51
|
+
import urllib.request
|
|
52
|
+
import zipfile
|
|
53
|
+
from datetime import datetime
|
|
54
|
+
from pathlib import Path
|
|
55
|
+
|
|
56
|
+
REPO = Path(__file__).resolve().parents[2] # 实际执行脚本所在仓根(身份断言的锚)
|
|
57
|
+
|
|
58
|
+
# 扁平兄弟模块(apply.py 机制 (a) 同款):裸跑/被 importlib 加载时 scripts 目录可能不在 sys.path
|
|
59
|
+
_HERE = Path(__file__).resolve().parent
|
|
60
|
+
if str(_HERE) not in sys.path:
|
|
61
|
+
sys.path.insert(0, str(_HERE))
|
|
62
|
+
import release_audit # noqa: E402
|
|
63
|
+
import release_metadata # noqa: E402
|
|
64
|
+
import release_smoke # noqa: E402
|
|
65
|
+
|
|
66
|
+
STRICT_TAG_RE = re.compile(r"^v\d+\.\d+\.\d+$") # 严格 vX.Y.Z(发布 tag 语法)
|
|
67
|
+
BUILD_TIMEOUT = 900
|
|
68
|
+
|
|
69
|
+
# 有界外部读取(05-05 V12/DoS 防线,T-5-28):所有 PyPI/TestPyPI/GitCode/preflight 外部
|
|
70
|
+
# 响应统一走 BoundedTransport/read_bounded/stream_expected_sha256——单请求固定 30s 超时;
|
|
71
|
+
# JSON/其他元数据 4MiB、source/blob/Release HTML 8MiB、raw=对应本地资产 size+1、
|
|
72
|
+
# wheel=expected_size+1 流式 hash;未分类响应默认 JSON 上限。
|
|
73
|
+
HTTP_TIMEOUT_S = 30
|
|
74
|
+
JSON_MAX_BYTES = 4 << 20
|
|
75
|
+
HTML_MAX_BYTES = 8 << 20
|
|
76
|
+
READ_CHUNK_BYTES = 64 * 1024 # 有界读取分块尺寸(≤64KiB,不一次性整读)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class ReleaseError(Exception):
|
|
80
|
+
"""可预期发布失败。消息构造时即已脱敏(不含凭据/完整带 token URL)。"""
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class ReleasePollTimeout(ReleaseError):
|
|
84
|
+
"""轮询预算耗尽:上传结果未知(uploaded_unverified/failed 方向,绝不判成功)。"""
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
# ---------------------------------------------------------------------------
|
|
88
|
+
# PyPI/TestPyPI 发布面 registry 与轮询策略(05-03 Task 1,D-06/D-07/D-10)
|
|
89
|
+
# ---------------------------------------------------------------------------
|
|
90
|
+
|
|
91
|
+
# realm → 显式 URL(上传绝不读 .pypirc/靠 twine 默认仓配置漂移;与 Twine 7 utils 常量一致)
|
|
92
|
+
PYPI_REALMS = {
|
|
93
|
+
"testpypi": {"json": "https://test.pypi.org",
|
|
94
|
+
"legacy": "https://test.pypi.org/legacy/", "label": "TestPyPI"},
|
|
95
|
+
"pypi": {"json": "https://pypi.org",
|
|
96
|
+
"legacy": "https://upload.pypi.org/legacy/", "label": "公开 PyPI"},
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
TWINE_UPLOAD_TIMEOUT = 600
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@dataclasses.dataclass
|
|
103
|
+
class PollPolicy:
|
|
104
|
+
"""有界轮询策略(A4 操作默认值,非官方 SLA):2s 起步指数退避至 10s,总预算 180s。"""
|
|
105
|
+
|
|
106
|
+
timeout_s: float = 180.0
|
|
107
|
+
initial_interval_s: float = 2.0
|
|
108
|
+
max_interval_s: float = 10.0
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _retry_after(headers: dict) -> float | None:
|
|
112
|
+
"""合法 Retry-After(纯数字、0-120s)→ 等待秒数;非法/缺失 → None(回退默认退避)。"""
|
|
113
|
+
for k in ("Retry-After", "retry-after", "Retry-after", "RETRY-AFTER"):
|
|
114
|
+
v = headers.get(k)
|
|
115
|
+
if v is None:
|
|
116
|
+
continue
|
|
117
|
+
s = str(v).strip()
|
|
118
|
+
if s.isdigit() and float(s) <= 120:
|
|
119
|
+
return float(s)
|
|
120
|
+
return None
|
|
121
|
+
return None
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _pypi_match_entry(data: dict, version: str, wheel_name: str,
|
|
125
|
+
wheel_sha256: str) -> tuple[dict | None, str | None]:
|
|
126
|
+
"""校验 PyPI 版本 JSON 内容。
|
|
127
|
+
|
|
128
|
+
返回 (entry, conflict):
|
|
129
|
+
- entry 非空:全部匹配(file_url/sha256/upload_time 证据字段);
|
|
130
|
+
- conflict 非空:远端已有内容与归档不符(立即冲突,不轮询等待);
|
|
131
|
+
- 双 None:版本可见但 urls 为空(传播中 pending——空 urls 不能反证版本不存在)。
|
|
132
|
+
"""
|
|
133
|
+
info = data.get("info") or {}
|
|
134
|
+
if str(info.get("version") or "") != version:
|
|
135
|
+
return None, f"info.version 与请求不符(冲突): {info.get('version')!r}"
|
|
136
|
+
urls = data.get("urls") or []
|
|
137
|
+
if not urls:
|
|
138
|
+
return None, None
|
|
139
|
+
if len(urls) != 1:
|
|
140
|
+
return None, f"版本文件数 {len(urls)} != 1(冲突:期望唯一 wheel)"
|
|
141
|
+
u = urls[0]
|
|
142
|
+
if u.get("filename") != wheel_name:
|
|
143
|
+
return None, f"远端文件名 {u.get('filename')!r} 与归档不符(冲突)"
|
|
144
|
+
if u.get("packagetype") != "bdist_wheel":
|
|
145
|
+
return None, f"packagetype {u.get('packagetype')!r} 非 bdist_wheel(冲突:禁 sdist/其他制品)"
|
|
146
|
+
sha = str((u.get("digests") or {}).get("sha256") or "")
|
|
147
|
+
if sha != wheel_sha256:
|
|
148
|
+
return None, "远端 sha256 与归档不符(冲突——版本不可覆盖,需人工核对后 bump tag)"
|
|
149
|
+
if u.get("yanked"):
|
|
150
|
+
return None, "远端 wheel 已 yanked(冲突)"
|
|
151
|
+
if not u.get("url"):
|
|
152
|
+
return None, "远端条目缺文件 URL(冲突)"
|
|
153
|
+
entry = {"file_url": u["url"], "sha256": sha,
|
|
154
|
+
"upload_time": u.get("upload_time_iso_8601") or u.get("upload_time")}
|
|
155
|
+
return entry, None
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
# ---------------------------------------------------------------------------
|
|
159
|
+
# 可注入 seam:子进程 / HTTP(测试注入 runner/transport,生产默认真实)
|
|
160
|
+
# ---------------------------------------------------------------------------
|
|
161
|
+
|
|
162
|
+
def sh(argv: list[str], timeout: int | None = None, cwd: str | Path | None = None,
|
|
163
|
+
env: dict | None = None) -> tuple[int, str, str]:
|
|
164
|
+
"""list 参数起子进程(apply.sh 同款:无 shell 拼接,天然免疫引号/路径转换问题)。"""
|
|
165
|
+
try:
|
|
166
|
+
p = subprocess.run([str(a) for a in argv], capture_output=True, text=True,
|
|
167
|
+
encoding="utf-8", errors="replace", timeout=timeout, cwd=cwd, env=env)
|
|
168
|
+
return p.returncode, p.stdout or "", p.stderr or ""
|
|
169
|
+
except subprocess.TimeoutExpired:
|
|
170
|
+
return 124, "", f"timeout after {timeout}s"
|
|
171
|
+
except FileNotFoundError as e:
|
|
172
|
+
return 127, "", str(e)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
class _NoRedirect(urllib.request.HTTPRedirectHandler):
|
|
176
|
+
"""禁用自动重定向:query token 不得跨宿主跟随(RESEARCH Pattern 3)。"""
|
|
177
|
+
|
|
178
|
+
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|
179
|
+
return None
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
# ---------------------------------------------------------------------------
|
|
183
|
+
# 有界外部传输(05-05 Task 1,V12/DoS:统一 30s 超时 + 分类字节上限)
|
|
184
|
+
# ---------------------------------------------------------------------------
|
|
185
|
+
|
|
186
|
+
@dataclasses.dataclass
|
|
187
|
+
class BoundedResponse:
|
|
188
|
+
"""有界响应:status/final_url/body(headers 可选,仅供 Retry-After 等白名单读取)。"""
|
|
189
|
+
|
|
190
|
+
status: int
|
|
191
|
+
final_url: str
|
|
192
|
+
body: bytes
|
|
193
|
+
headers: dict | None = None
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _declared_length(response) -> int | None:
|
|
197
|
+
"""响应声明 Content-Length(非法/缺失 → None)。"""
|
|
198
|
+
headers = getattr(response, "headers", None)
|
|
199
|
+
if headers is None:
|
|
200
|
+
return None
|
|
201
|
+
try:
|
|
202
|
+
return int(headers.get("Content-Length"))
|
|
203
|
+
except (TypeError, ValueError):
|
|
204
|
+
return None
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def read_bounded(response, max_bytes: int) -> bytes:
|
|
208
|
+
"""有界响应体读取:先拒绝声明超限,再按 ≤64KiB 分块、至多多探 1 字节判 EOF。
|
|
209
|
+
|
|
210
|
+
实际字节超过上限立即 fail-closed(发生在任何 JSON 解析/HTML 解码/内容比对之前);
|
|
211
|
+
边界内恰好 size 成功。调用方按响应类别传上限:JSON/其他=JSON_MAX_BYTES、
|
|
212
|
+
HTML=HTML_MAX_BYTES、raw=对应本地资产 size+1。
|
|
213
|
+
"""
|
|
214
|
+
declared = _declared_length(response)
|
|
215
|
+
if declared is not None and declared > max_bytes:
|
|
216
|
+
raise ReleaseError(f"响应声明 Content-Length {declared} 超过上限 {max_bytes}(fail-closed)")
|
|
217
|
+
chunks: list[bytes] = []
|
|
218
|
+
total = 0
|
|
219
|
+
while True:
|
|
220
|
+
chunk = response.read(min(READ_CHUNK_BYTES, max_bytes + 1 - total))
|
|
221
|
+
if not chunk:
|
|
222
|
+
break
|
|
223
|
+
total += len(chunk)
|
|
224
|
+
if total > max_bytes:
|
|
225
|
+
raise ReleaseError(f"响应实际字节 {total} 超过上限 {max_bytes}(fail-closed)")
|
|
226
|
+
chunks.append(chunk)
|
|
227
|
+
return b"".join(chunks)
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
class BoundedTransport:
|
|
231
|
+
"""统一有界传输:__call__(request, max_bytes=JSON_MAX_BYTES) -> BoundedResponse。
|
|
232
|
+
|
|
233
|
+
固定 HTTP_TIMEOUT_S 单请求超时打开响应(不接受调用方放宽);响应体经 read_bounded
|
|
234
|
+
分类上限读取;4xx/5xx 以 BoundedResponse(status) 返回(不抛 HTTPError);网络层
|
|
235
|
+
异常(含超时)重写为脱敏 ReleaseError(只留 host 与异常类型名,不带路径/query)。
|
|
236
|
+
API 请求默认 no-redirect opener(token 不跨宿主跟随)。
|
|
237
|
+
"""
|
|
238
|
+
|
|
239
|
+
def __init__(self, opener=None):
|
|
240
|
+
self.opener = opener if opener is not None else urllib.request.build_opener(_NoRedirect)
|
|
241
|
+
|
|
242
|
+
def __call__(self, request, max_bytes: int = JSON_MAX_BYTES) -> BoundedResponse:
|
|
243
|
+
try:
|
|
244
|
+
with self.opener.open(request, timeout=HTTP_TIMEOUT_S) as r:
|
|
245
|
+
return BoundedResponse(int(r.status),
|
|
246
|
+
getattr(r, "url", None) or request.full_url,
|
|
247
|
+
read_bounded(r, max_bytes),
|
|
248
|
+
dict(r.headers))
|
|
249
|
+
except urllib.error.HTTPError as e:
|
|
250
|
+
try:
|
|
251
|
+
body = read_bounded(e, max_bytes)
|
|
252
|
+
except Exception:
|
|
253
|
+
body = b""
|
|
254
|
+
return BoundedResponse(int(e.code), request.full_url, body,
|
|
255
|
+
dict(e.headers or {}))
|
|
256
|
+
except ReleaseError:
|
|
257
|
+
raise
|
|
258
|
+
except Exception as e:
|
|
259
|
+
raise ReleaseError(
|
|
260
|
+
f"HTTP 请求失败(网络层/超时,host {_safe_host(request.full_url)})"
|
|
261
|
+
f": {type(e).__name__}")
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def stream_expected_sha256(request, expected_size: int, opener=None) -> tuple[str, int]:
|
|
265
|
+
"""流式下载并计算 SHA256:单请求 30s;最多读取 expected_size+1 字节并边读边 hash,
|
|
266
|
+
累计超过 expected_size 立即失败,结束时要求 size 精确相等(不为计算 hash 把整个
|
|
267
|
+
wheel 读入内存——分块更新后即弃)。下载经稳定 URL 可跟随重定向,final URL 不记录。
|
|
268
|
+
"""
|
|
269
|
+
if expected_size is None or int(expected_size) <= 0:
|
|
270
|
+
raise ReleaseError(f"expected_size 非法: {expected_size!r}(流式 hash 需要精确期望尺寸)")
|
|
271
|
+
expected_size = int(expected_size)
|
|
272
|
+
opener = opener if opener is not None else urllib.request.build_opener()
|
|
273
|
+
h = hashlib.sha256()
|
|
274
|
+
total = 0
|
|
275
|
+
try:
|
|
276
|
+
with opener.open(request, timeout=HTTP_TIMEOUT_S) as r:
|
|
277
|
+
declared = _declared_length(r)
|
|
278
|
+
if declared is not None and declared > expected_size:
|
|
279
|
+
raise ReleaseError(
|
|
280
|
+
f"附件声明 Content-Length {declared} > expected_size {expected_size}(fail-closed)")
|
|
281
|
+
while True:
|
|
282
|
+
chunk = r.read(min(READ_CHUNK_BYTES, expected_size + 1 - total))
|
|
283
|
+
if not chunk:
|
|
284
|
+
break
|
|
285
|
+
total += len(chunk)
|
|
286
|
+
if total > expected_size:
|
|
287
|
+
raise ReleaseError(
|
|
288
|
+
f"附件下载字节 {total} 超过 expected_size {expected_size}(fail-closed)")
|
|
289
|
+
h.update(chunk)
|
|
290
|
+
except ReleaseError:
|
|
291
|
+
raise
|
|
292
|
+
except urllib.error.HTTPError as e:
|
|
293
|
+
try:
|
|
294
|
+
e.close()
|
|
295
|
+
except Exception:
|
|
296
|
+
pass
|
|
297
|
+
raise ReleaseError(
|
|
298
|
+
f"附件下载失败: HTTP {e.code}(host {_safe_host(request.full_url)})") from None
|
|
299
|
+
except Exception as e:
|
|
300
|
+
raise ReleaseError(
|
|
301
|
+
f"附件下载失败(网络层/超时,host {_safe_host(request.full_url)})"
|
|
302
|
+
f": {type(e).__name__}") from None
|
|
303
|
+
if total != expected_size:
|
|
304
|
+
raise ReleaseError(
|
|
305
|
+
f"附件下载字节 {total} != expected_size {expected_size}(fail-closed)")
|
|
306
|
+
return h.hexdigest(), total
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def download_sha256(url: str, expected_size: int) -> tuple[str, int]:
|
|
310
|
+
"""稳定附件 URL → (SHA256, size):stream_expected_sha256 的 URL 便捷入口。"""
|
|
311
|
+
return stream_expected_sha256(urllib.request.Request(url, method="GET"), expected_size)
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
def http_get(url: str, timeout: int = HTTP_TIMEOUT_S,
|
|
315
|
+
max_bytes: int = JSON_MAX_BYTES) -> tuple[int, dict, bytes]:
|
|
316
|
+
"""只读 HTTP GET(生产 transport,有界)。4xx/5xx 以 (status, headers, body) 返回;
|
|
317
|
+
网络层异常(含超时)经 BoundedTransport 重写为脱敏 ReleaseError。timeout 形参保留
|
|
318
|
+
向后兼容,值固定 HTTP_TIMEOUT_S(单请求超时是有界传输的固定属性,不接受放宽)。"""
|
|
319
|
+
req = urllib.request.Request(url, method="GET")
|
|
320
|
+
r = BoundedTransport()(req, max_bytes=max_bytes)
|
|
321
|
+
return r.status, dict(r.headers or {}), r.body
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def http_write(method: str, url: str, data: bytes, headers: dict | None = None,
|
|
325
|
+
max_bytes: int = JSON_MAX_BYTES) -> tuple[int, dict, bytes]:
|
|
326
|
+
"""有界写请求(POST/PUT 生产 transport):30s + 响应体分类上限;4xx/5xx 以
|
|
327
|
+
(status, headers, body) 返回(调用方决定失败语义并脱敏消息)。"""
|
|
328
|
+
req = urllib.request.Request(url, data=data, method=method, headers=dict(headers or {}))
|
|
329
|
+
r = BoundedTransport()(req, max_bytes=max_bytes)
|
|
330
|
+
return r.status, dict(r.headers or {}), r.body
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
def _wrapped_write(writer, method: str, url: str, data: bytes, headers: dict):
|
|
334
|
+
"""写调用 + 网络层异常脱敏(只留 host 与异常类型名;预签名 URL 的 host 由调用方单独处理)。"""
|
|
335
|
+
try:
|
|
336
|
+
return writer(method, url, data, headers)
|
|
337
|
+
except ReleaseError:
|
|
338
|
+
raise
|
|
339
|
+
except Exception as e:
|
|
340
|
+
raise ReleaseError(
|
|
341
|
+
f"HTTP 写请求失败(网络层,host {_safe_host(url)}): {type(e).__name__}")
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def _safe_host(url: str) -> str:
|
|
345
|
+
try:
|
|
346
|
+
return urllib.parse.urlsplit(url).netloc or "unknown"
|
|
347
|
+
except Exception:
|
|
348
|
+
return "unknown"
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def _wrapped_get(getter, url: str) -> tuple[int, dict, bytes]:
|
|
352
|
+
"""transport 调用 + 网络层异常脱敏:只保留 host 与异常类型名(不带 URL/query/消息体)。"""
|
|
353
|
+
try:
|
|
354
|
+
return getter(url)
|
|
355
|
+
except ReleaseError:
|
|
356
|
+
raise
|
|
357
|
+
except Exception as e:
|
|
358
|
+
raise ReleaseError(f"HTTP 请求失败(网络层,host {_safe_host(url)}): {type(e).__name__}")
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
# ---------------------------------------------------------------------------
|
|
362
|
+
# 只读 API 客户端(occupancy / 可见性 / 认证;无任何写方法)
|
|
363
|
+
# ---------------------------------------------------------------------------
|
|
364
|
+
|
|
365
|
+
class PyPIClient:
|
|
366
|
+
"""PyPI/TestPyPI JSON API 客户端(占用/验证只读 + realm 显式 Twine 上传)。
|
|
367
|
+
|
|
368
|
+
可注入 transport(GET)/runner(twine 子进程)/clock+sleeper(轮询)——测试零真实网络。
|
|
369
|
+
"""
|
|
370
|
+
|
|
371
|
+
def __init__(self, index_url: str = "https://pypi.org", transport=None,
|
|
372
|
+
runner=None, clock=None, sleeper=None):
|
|
373
|
+
self.index_url = index_url.rstrip("/")
|
|
374
|
+
self._get = transport or http_get
|
|
375
|
+
self._run = runner or sh
|
|
376
|
+
self._clock = clock or time.monotonic
|
|
377
|
+
self._sleep = sleeper or time.sleep
|
|
378
|
+
|
|
379
|
+
def _label(self) -> str:
|
|
380
|
+
for cfg in PYPI_REALMS.values():
|
|
381
|
+
if self.index_url == cfg["json"].rstrip("/"):
|
|
382
|
+
return cfg["label"]
|
|
383
|
+
return self.index_url
|
|
384
|
+
|
|
385
|
+
def version_occupied(self, name: str, version: str) -> tuple[bool, str]:
|
|
386
|
+
label = self._label()
|
|
387
|
+
status, _headers, _body = _wrapped_get(
|
|
388
|
+
self._get, f"{self.index_url}/pypi/{name}/{version}/json")
|
|
389
|
+
if status == 200:
|
|
390
|
+
# D-07:版本端点 200 即占用——空 urls 不能反证版本不存在
|
|
391
|
+
return True, f"{label} 已有 {name} {version}(HTTP 200,占用冲突)"
|
|
392
|
+
if status == 404:
|
|
393
|
+
return False, f"{label} 无 {name} {version}(HTTP 404,可首发)"
|
|
394
|
+
raise ReleaseError(f"PyPI 占用查询失败: HTTP {status}(host {_safe_host(self.index_url)})")
|
|
395
|
+
|
|
396
|
+
def upload(self, state, realm: str, env: dict | None = None) -> dict:
|
|
397
|
+
"""Twine 上传唯一归档 wheel 到指定 realm(D-10:显式 repository-url + 标准 env)。
|
|
398
|
+
|
|
399
|
+
argv 恒为 upload --non-interactive --repository-url <realm legacy> + 归档 wheel:
|
|
400
|
+
无 skip-existing、无 verbose、无 dist glob;凭据只经环境继承,绝不进 argv/日志。
|
|
401
|
+
凭据缺失或归档缺失在 subprocess 之前失败(fail-closed)。
|
|
402
|
+
"""
|
|
403
|
+
cfg = PYPI_REALMS.get(realm)
|
|
404
|
+
if cfg is None:
|
|
405
|
+
raise ReleaseError(f"未知 PyPI realm: {realm}(可选: {', '.join(sorted(PYPI_REALMS))})")
|
|
406
|
+
e = os.environ if env is None else env
|
|
407
|
+
if str(e.get("TWINE_USERNAME") or "") != "__token__" \
|
|
408
|
+
or not str(e.get("TWINE_PASSWORD") or "").strip():
|
|
409
|
+
raise ReleaseError(
|
|
410
|
+
"twine 凭据缺失或非法:需要 TWINE_USERNAME=__token__ 与非空 TWINE_PASSWORD"
|
|
411
|
+
"(env-only,不读 .pypirc/隐式凭据文件;凭据值不打印、不落盘,D-10)")
|
|
412
|
+
archive = Path(state.run_dir) / "artifact" / state.wheel_name
|
|
413
|
+
if not archive.is_file():
|
|
414
|
+
raise ReleaseError(f"归档 wheel 缺失: {archive}(上传只消费归档正本,不读 dist)")
|
|
415
|
+
argv = [sys.executable, "-m", "twine", "upload", "--non-interactive",
|
|
416
|
+
"--repository-url", cfg["legacy"], str(archive)]
|
|
417
|
+
rc, _out, _err = self._run(argv, timeout=TWINE_UPLOAD_TIMEOUT) # 原始输出不落档
|
|
418
|
+
if rc != 0:
|
|
419
|
+
raise ReleaseError(
|
|
420
|
+
f"twine upload 失败(realm {realm},rc={rc})——请求结果可能未知,"
|
|
421
|
+
"不得盲目重试:用 release resume 先远端对账,404 才允许重传")
|
|
422
|
+
return {"realm": realm, "wheel": archive.name}
|
|
423
|
+
|
|
424
|
+
def verify(self, name: str, version: str, wheel_name: str, wheel_sha256: str,
|
|
425
|
+
policy: PollPolicy | None = None) -> dict:
|
|
426
|
+
"""有界轮询版本 JSON 直到 info.version/唯一 bdist_wheel/精确 filename/sha256/非 yanked
|
|
427
|
+
全部匹配(D-07)。
|
|
428
|
+
|
|
429
|
+
404/空 urls 视为传播中继续轮询;429(合法 Retry-After)/5xx 在总预算内重试;
|
|
430
|
+
内容不符立即冲突;超时抛 ReleasePollTimeout(结果未知方向,绝不判成功);
|
|
431
|
+
非 200/404/429/5xx(含重定向)立即失败且不跟随。
|
|
432
|
+
"""
|
|
433
|
+
policy = policy or PollPolicy()
|
|
434
|
+
api_url = f"{self.index_url}/pypi/{name}/{version}/json"
|
|
435
|
+
host = _safe_host(api_url)
|
|
436
|
+
interval = policy.initial_interval_s
|
|
437
|
+
start = self._clock()
|
|
438
|
+
last = "尚未请求"
|
|
439
|
+
while True:
|
|
440
|
+
status, headers, body = _wrapped_get(self._get, api_url)
|
|
441
|
+
if status == 200:
|
|
442
|
+
try:
|
|
443
|
+
data = json.loads(body.decode("utf-8", errors="replace"))
|
|
444
|
+
except json.JSONDecodeError:
|
|
445
|
+
raise ReleaseError(f"PyPI JSON API 返回非 JSON(fail-closed,host {host})") from None
|
|
446
|
+
entry, conflict = _pypi_match_entry(data, version, wheel_name, wheel_sha256)
|
|
447
|
+
if conflict:
|
|
448
|
+
raise ReleaseError(f"PyPI 验证冲突: {conflict}")
|
|
449
|
+
if entry is not None:
|
|
450
|
+
return {"api_url": api_url, "file_url": entry["file_url"],
|
|
451
|
+
"sha256": entry["sha256"], "upload_time": entry["upload_time"],
|
|
452
|
+
"verified_at": _now_iso()}
|
|
453
|
+
last = "版本可见但文件列表为空(传播中)"
|
|
454
|
+
elif status == 404:
|
|
455
|
+
last = "HTTP 404(版本未出现)"
|
|
456
|
+
elif status == 429 or status >= 500:
|
|
457
|
+
last = f"HTTP {status}(预算内重试)"
|
|
458
|
+
else:
|
|
459
|
+
raise ReleaseError(
|
|
460
|
+
f"PyPI 验证查询失败: HTTP {status}(host {host},不跟随重定向)")
|
|
461
|
+
elapsed = self._clock() - start
|
|
462
|
+
remain = policy.timeout_s - elapsed
|
|
463
|
+
if remain <= 0:
|
|
464
|
+
raise ReleasePollTimeout(
|
|
465
|
+
f"PyPI 验证轮询超时(预算 {policy.timeout_s}s 耗尽;最后状态: {last})"
|
|
466
|
+
"——上传结果未知,按 uploaded_unverified 处理,可用 release resume 先对账")
|
|
467
|
+
ra = _retry_after(headers)
|
|
468
|
+
self._sleep(min(ra if ra is not None else interval, remain))
|
|
469
|
+
if ra is None:
|
|
470
|
+
interval = min(interval * 2, policy.max_interval_s)
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
def reconcile_pypi(state, realm: str, meta: dict, client: PyPIClient | None = None) -> tuple[str, str]:
|
|
474
|
+
"""远端对账(D-04):单次 GET + 内容判定,返回明确 (category, detail)。
|
|
475
|
+
|
|
476
|
+
category ∈ missing / matching / conflict / pending——不凭进程退出码或上传回显猜成功。
|
|
477
|
+
missing 才允许重传;matching 只对账不重传;conflict 即中止;pending 不可反证不存在。
|
|
478
|
+
"""
|
|
479
|
+
cfg = PYPI_REALMS.get(realm) or {}
|
|
480
|
+
client = client or PyPIClient(index_url=cfg.get("json", "https://pypi.org"))
|
|
481
|
+
api_url = f"{client.index_url}/pypi/{meta['name']}/{state.version}/json"
|
|
482
|
+
status, _h, body = _wrapped_get(client._get, api_url)
|
|
483
|
+
if status == 404:
|
|
484
|
+
return "missing", f"{realm} 无 {meta['name']} {state.version}(HTTP 404,才可重试上传)"
|
|
485
|
+
if status != 200:
|
|
486
|
+
raise ReleaseError(f"PyPI 对账查询失败: HTTP {status}(host {_safe_host(api_url)})")
|
|
487
|
+
try:
|
|
488
|
+
data = json.loads(body.decode("utf-8", errors="replace"))
|
|
489
|
+
except json.JSONDecodeError:
|
|
490
|
+
raise ReleaseError("PyPI JSON API 返回非 JSON(fail-closed)") from None
|
|
491
|
+
entry, conflict = _pypi_match_entry(data, state.version, state.wheel_name, state.wheel_sha256)
|
|
492
|
+
if conflict:
|
|
493
|
+
return "conflict", f"{realm} 已有 {meta['name']} {state.version} 且内容不符: {conflict}"
|
|
494
|
+
if entry is None:
|
|
495
|
+
return ("pending",
|
|
496
|
+
f"{realm} 版本已占用(HTTP 200)但文件列表为空/未可验证——"
|
|
497
|
+
"不可反证不存在,也不可重传(占用即冲突)")
|
|
498
|
+
return "matching", f"{realm} 远端内容与归档一致(version/filename/sha256 全匹配,只对账不重传)"
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
def reconcile_gitcode(state, notes: str, client: GitCodeClient) -> tuple[str, str]:
|
|
502
|
+
"""GitCode 面远端对账(D-04/T-5-14):单次 GET by tag + 元数据/资产内容判定。
|
|
503
|
+
|
|
504
|
+
返回 (category, detail)——不凭进程退出码或 create/PUT 回显猜成功:
|
|
505
|
+
- missing:404——Release 不存在,才可创建;
|
|
506
|
+
- no-asset:Release 存在且 tag/target/body/正式状态全匹配,但无 attach 上传附件
|
|
507
|
+
(仅 source 自动归档)——才可取 upload_url 并 PUT;
|
|
508
|
+
- matching:Release 匹配且 attach 附件下载 SHA256 与归档一致——只验证不重传;
|
|
509
|
+
- pending:Release 存在但 assets 为空(传播中)——只读轮询验证方向,不可反证;
|
|
510
|
+
- conflict:任何不匹配(元数据/额外附件/不同 hash/未知 type/额外 wheel)。
|
|
511
|
+
"""
|
|
512
|
+
rel = client.release_for_tag(state.tag)
|
|
513
|
+
if rel is None:
|
|
514
|
+
return "missing", f"GitCode 无 tag {state.tag} 的 Release(HTTP 404,才可创建)"
|
|
515
|
+
try:
|
|
516
|
+
_gc_check_release_formal(rel, state, notes)
|
|
517
|
+
attach, source = _gc_classify_assets(rel)
|
|
518
|
+
if not attach and not source:
|
|
519
|
+
return "pending", "Release 无任何 assets(传播中,不可反证不存在)"
|
|
520
|
+
if not attach:
|
|
521
|
+
return ("no-asset",
|
|
522
|
+
f"Release 匹配且尚无上传附件(仅 {len(source)} 个平台 source 自动归档)"
|
|
523
|
+
"——才可取 upload_url 并 PUT")
|
|
524
|
+
if len(attach) > 1:
|
|
525
|
+
raise ReleaseError(
|
|
526
|
+
f"GitCode 上传资产全集必须恰一个 wheel,实际 {len(attach)} 个(冲突)")
|
|
527
|
+
asset = attach[0]
|
|
528
|
+
name = asset.get("name") or asset.get("file_name") or ""
|
|
529
|
+
if name != state.wheel_name:
|
|
530
|
+
raise ReleaseError(f"GitCode 上传资产名不符: {name!r}(冲突)")
|
|
531
|
+
digest = asset.get("digest")
|
|
532
|
+
if digest is not None and str(digest) != state.wheel_sha256:
|
|
533
|
+
raise ReleaseError("GitCode 附件 digest 与归档 SHA256 不符(冲突)")
|
|
534
|
+
url = asset.get("browser_download_url") or client._asset_url(state)
|
|
535
|
+
try:
|
|
536
|
+
sha, _size = client._download(url, state.wheel_size)
|
|
537
|
+
except ReleaseError:
|
|
538
|
+
raise
|
|
539
|
+
except Exception as e:
|
|
540
|
+
raise ReleaseError(
|
|
541
|
+
f"GitCode 附件下载失败(网络层): {type(e).__name__}") from None
|
|
542
|
+
if sha != state.wheel_sha256:
|
|
543
|
+
raise ReleaseError("GitCode 附件下载字节 SHA256 与归档不符(冲突——不同 hash 停止)")
|
|
544
|
+
return "matching", "GitCode Release 匹配且附件字节与归档一致(只验证不重传)"
|
|
545
|
+
except ReleaseError as ex:
|
|
546
|
+
return "conflict", str(ex)
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
class GitCodeClient:
|
|
550
|
+
"""GitCode /api/v5 客户端(只读预检 + 正式 Release 创建/上传/验证,D-11/A7)。
|
|
551
|
+
|
|
552
|
+
access_token 只在内存组 URL,绝不进日志/状态/异常;upload_url 返回的预签名
|
|
553
|
+
URL/headers 是短期内存对象(upload_info),绝不进入 ReleaseState/receipt/日志。
|
|
554
|
+
可注入 transport(GET)/poster(POST/PUT)/downloader(附件下载)/clock+sleeper
|
|
555
|
+
(轮询)——测试零真实网络。
|
|
556
|
+
"""
|
|
557
|
+
|
|
558
|
+
API = "https://api.gitcode.com/api/v5"
|
|
559
|
+
|
|
560
|
+
def __init__(self, owner: str, repo: str, token: str, transport=None, poster=None,
|
|
561
|
+
downloader=None, clock=None, sleeper=None):
|
|
562
|
+
self.owner, self.repo_name, self.token = owner, repo, token
|
|
563
|
+
self._get = transport or http_get
|
|
564
|
+
self._write = poster or http_write
|
|
565
|
+
self._download = downloader or download_sha256
|
|
566
|
+
self._clock = clock or time.monotonic
|
|
567
|
+
self._sleep = sleeper or time.sleep
|
|
568
|
+
|
|
569
|
+
def _url(self, path: str) -> str:
|
|
570
|
+
return f"{self.API}/{path}?access_token={self.token}"
|
|
571
|
+
|
|
572
|
+
def _release_url(self, state) -> str:
|
|
573
|
+
"""稳定 Release 页 URL(模板正本,绝不记录 API 返回的临时/签名字段)。"""
|
|
574
|
+
return f"https://gitcode.com/{self.owner}/{self.repo_name}/releases/{state.tag}"
|
|
575
|
+
|
|
576
|
+
def _asset_url(self, state) -> str:
|
|
577
|
+
"""稳定附件下载 URL(官方 download 路由模板)。"""
|
|
578
|
+
return (f"https://gitcode.com/{self.owner}/{self.repo_name}/releases/download/"
|
|
579
|
+
f"{state.tag}/{state.wheel_name}")
|
|
580
|
+
|
|
581
|
+
def repo_info(self) -> dict:
|
|
582
|
+
status, _h, body = _wrapped_get(
|
|
583
|
+
self._get, self._url(f"repos/{self.owner}/{self.repo_name}"))
|
|
584
|
+
if status == 200:
|
|
585
|
+
try:
|
|
586
|
+
return json.loads(body.decode("utf-8", errors="replace"))
|
|
587
|
+
except json.JSONDecodeError:
|
|
588
|
+
raise ReleaseError("GitCode 仓库 API 返回非 JSON(fail-closed)") from None
|
|
589
|
+
if status in (401, 403):
|
|
590
|
+
raise ReleaseError(
|
|
591
|
+
f"GitCode 只读认证失败: HTTP {status}(GITCODE_TOKEN 错误或权限不足;"
|
|
592
|
+
"预检只验证只读认证,写权限由真实写操作证明)")
|
|
593
|
+
raise ReleaseError(f"GitCode 仓库 API 查询失败: HTTP {status}(host api.gitcode.com)")
|
|
594
|
+
|
|
595
|
+
def release_for_tag(self, tag: str) -> dict | None:
|
|
596
|
+
status, _h, body = _wrapped_get(
|
|
597
|
+
self._get, self._url(f"repos/{self.owner}/{self.repo_name}/releases/tags/{tag}"))
|
|
598
|
+
if status == 200:
|
|
599
|
+
try:
|
|
600
|
+
return json.loads(body.decode("utf-8", errors="replace"))
|
|
601
|
+
except json.JSONDecodeError:
|
|
602
|
+
raise ReleaseError("GitCode Release API 返回非 JSON(fail-closed)") from None
|
|
603
|
+
if status == 404:
|
|
604
|
+
return None
|
|
605
|
+
raise ReleaseError(f"GitCode Release 占用查询失败: HTTP {status}(host api.gitcode.com)")
|
|
606
|
+
|
|
607
|
+
# -- 正式 Release 写链(D-11:官方五键 create → upload_url → raw PUT → verify) --
|
|
608
|
+
|
|
609
|
+
def create(self, state, notes: str) -> dict:
|
|
610
|
+
"""创建正式 Release:POST /repos/{owner}/{repo}/releases。
|
|
611
|
+
|
|
612
|
+
body 恰五个官方键(tag_name/name/body/target_commitish/release_status=latest);
|
|
613
|
+
target_commitish=HEAD full SHA、body=当前 CHANGELOG section 逐字原文;
|
|
614
|
+
不发送猜测的 draft 参数(A7:非 draft 由返回字段验收,不由请求参数承诺)。
|
|
615
|
+
"""
|
|
616
|
+
body = {"tag_name": state.tag, "name": state.tag, "body": notes,
|
|
617
|
+
"target_commitish": state.head, "release_status": "latest"}
|
|
618
|
+
status, _h, resp = _wrapped_write(
|
|
619
|
+
self._write, "POST", self._url(f"repos/{self.owner}/{self.repo_name}/releases"),
|
|
620
|
+
json.dumps(body, ensure_ascii=False).encode("utf-8"),
|
|
621
|
+
{"Content-Type": "application/json"})
|
|
622
|
+
if status in (200, 201):
|
|
623
|
+
try:
|
|
624
|
+
return json.loads(resp.decode("utf-8", errors="replace"))
|
|
625
|
+
except json.JSONDecodeError:
|
|
626
|
+
raise ReleaseError("GitCode create 返回非 JSON(fail-closed,host api.gitcode.com)") from None
|
|
627
|
+
if status in (401, 403):
|
|
628
|
+
raise ReleaseError(
|
|
629
|
+
f"GitCode Release 创建被拒绝: HTTP {status}(GITCODE_TOKEN 写权限不足或占用;"
|
|
630
|
+
"host api.gitcode.com)")
|
|
631
|
+
raise ReleaseError(f"GitCode Release 创建失败: HTTP {status}(host api.gitcode.com)")
|
|
632
|
+
|
|
633
|
+
def upload_url(self, state) -> dict:
|
|
634
|
+
"""获取附件上传 URL/一次性 headers:GET releases/{tag}/upload_url?file_name=<精确
|
|
635
|
+
wheel 名>。返回 {url, headers} 短期内存对象(upload_info)——绝不进入状态/receipt/
|
|
636
|
+
日志;本方法与 put_wheel 之外的任何路径不得触碰它。"""
|
|
637
|
+
url = (self._url(f"repos/{self.owner}/{self.repo_name}/releases/{state.tag}"
|
|
638
|
+
"/upload_url")
|
|
639
|
+
+ f"&file_name={urllib.parse.quote(state.wheel_name)}")
|
|
640
|
+
status, _h, body = _wrapped_get(self._get, url)
|
|
641
|
+
if status != 200:
|
|
642
|
+
raise ReleaseError(f"GitCode upload_url 获取失败: HTTP {status}(host api.gitcode.com)")
|
|
643
|
+
try:
|
|
644
|
+
data = json.loads(body.decode("utf-8", errors="replace"))
|
|
645
|
+
except json.JSONDecodeError:
|
|
646
|
+
raise ReleaseError("GitCode upload_url 返回非 JSON(fail-closed)") from None
|
|
647
|
+
if not isinstance(data, dict) or not data.get("url") \
|
|
648
|
+
or not isinstance(data.get("headers"), dict):
|
|
649
|
+
raise ReleaseError("GitCode upload_url 响应缺 url/headers(fail-closed)")
|
|
650
|
+
return {"url": str(data["url"]), "headers": dict(data["headers"])}
|
|
651
|
+
|
|
652
|
+
def put_wheel(self, upload_info: dict, wheel: Path) -> dict:
|
|
653
|
+
"""对预签名 URL PUT 归档 wheel 原始字节(非 multipart;headers 原样使用返回值)。
|
|
654
|
+
|
|
655
|
+
异常只保留 status/类型名——预签名 URL、query、x-obs-* 头永不记录。
|
|
656
|
+
"""
|
|
657
|
+
data = Path(wheel).read_bytes()
|
|
658
|
+
try:
|
|
659
|
+
status, _h, _b = self._write("PUT", upload_info["url"], data,
|
|
660
|
+
dict(upload_info.get("headers") or {}))
|
|
661
|
+
except ReleaseError:
|
|
662
|
+
raise
|
|
663
|
+
except Exception as e:
|
|
664
|
+
raise ReleaseError(f"GitCode 附件 PUT 网络层失败: {type(e).__name__}(预签名端点不记录)")
|
|
665
|
+
if not (200 <= status < 300):
|
|
666
|
+
raise ReleaseError(
|
|
667
|
+
f"GitCode 附件 PUT 失败: HTTP {status}(对象存储端点不记录;结果未知时用 "
|
|
668
|
+
"release resume 先查附件再决定,禁止盲重 PUT)")
|
|
669
|
+
return {"status": status}
|
|
670
|
+
|
|
671
|
+
def verify(self, state, notes: str, policy: PollPolicy | None = None) -> dict:
|
|
672
|
+
"""有界轮询 Release(by tag)直到正式元数据 + 唯一 wheel 附件 + 下载 SHA256 达标。
|
|
673
|
+
|
|
674
|
+
404/assets 空/仅 source 归档视为传播中继续轮询;429(合法 Retry-After)/5xx 在
|
|
675
|
+
总预算内重试;tag/target/body 不符、prerelease=true、release_status=pre/缺失、
|
|
676
|
+
任何返回 draft 字段 true、未知/缺失 asset type、额外附件、digest 或下载 SHA256
|
|
677
|
+
不符均立即冲突;超时 ReleasePollTimeout(结果未知方向,绝不判成功)。
|
|
678
|
+
assets 全集按实际 type=attach/source 分类:attach 上传全集必须恰一个且名精确等于
|
|
679
|
+
归档 wheel;type=source 是平台自动源码归档(豁免、保留、不删除);下载字节是
|
|
680
|
+
地面真值,digest 存在才交叉校验、缺失不等待。
|
|
681
|
+
"""
|
|
682
|
+
policy = policy or PollPolicy(timeout_s=300.0)
|
|
683
|
+
api_url = self._url(f"repos/{self.owner}/{self.repo_name}/releases/tags/{state.tag}")
|
|
684
|
+
interval = policy.initial_interval_s
|
|
685
|
+
start = self._clock()
|
|
686
|
+
last = "尚未请求"
|
|
687
|
+
while True:
|
|
688
|
+
status, headers, body = _wrapped_get(self._get, api_url)
|
|
689
|
+
if status == 200:
|
|
690
|
+
try:
|
|
691
|
+
rel = json.loads(body.decode("utf-8", errors="replace"))
|
|
692
|
+
except json.JSONDecodeError:
|
|
693
|
+
raise ReleaseError(
|
|
694
|
+
"GitCode Release API 返回非 JSON(fail-closed,host api.gitcode.com)") from None
|
|
695
|
+
_gc_check_release_formal(rel, state, notes)
|
|
696
|
+
attach, source = _gc_classify_assets(rel)
|
|
697
|
+
if len(attach) == 1:
|
|
698
|
+
asset = attach[0]
|
|
699
|
+
name = asset.get("name") or asset.get("file_name") or ""
|
|
700
|
+
if name != state.wheel_name:
|
|
701
|
+
raise ReleaseError(
|
|
702
|
+
f"GitCode 上传资产名不符: {name!r} != 归档 wheel {state.wheel_name!r}(冲突)")
|
|
703
|
+
return self._verify_asset(rel, asset, state)
|
|
704
|
+
if len(attach) > 1:
|
|
705
|
+
raise ReleaseError(
|
|
706
|
+
f"GitCode 上传资产全集必须恰一个 wheel,实际 {len(attach)} 个(额外 "
|
|
707
|
+
"wheel/sdist/附件均 fail-closed,T-5-15)")
|
|
708
|
+
last = ("Release 无任何 assets(传播中)" if not source
|
|
709
|
+
else "上传附件尚未可见(仅平台 source 自动归档,传播中)")
|
|
710
|
+
elif status == 404:
|
|
711
|
+
last = "HTTP 404(Release 未出现)"
|
|
712
|
+
elif status == 429 or status >= 500:
|
|
713
|
+
last = f"HTTP {status}(预算内重试)"
|
|
714
|
+
else:
|
|
715
|
+
raise ReleaseError(
|
|
716
|
+
f"GitCode Release 验证查询失败: HTTP {status}(host api.gitcode.com,不跟随重定向)")
|
|
717
|
+
elapsed = self._clock() - start
|
|
718
|
+
remain = policy.timeout_s - elapsed
|
|
719
|
+
if remain <= 0:
|
|
720
|
+
raise ReleasePollTimeout(
|
|
721
|
+
f"GitCode Release 验证轮询超时(预算 {policy.timeout_s}s 耗尽;最后状态: {last})"
|
|
722
|
+
"——结果未知,按 uploaded_unverified 处理,可用 release resume 先远端对账")
|
|
723
|
+
ra = _retry_after(headers)
|
|
724
|
+
self._sleep(min(ra if ra is not None else interval, remain))
|
|
725
|
+
if ra is None:
|
|
726
|
+
interval = min(interval * 2, policy.max_interval_s)
|
|
727
|
+
|
|
728
|
+
def _verify_asset(self, rel: dict, asset: dict, state) -> dict:
|
|
729
|
+
"""附件验收:digest 存在才交叉校验;稳定 URL 下载字节流式 SHA256 是地面真值。"""
|
|
730
|
+
digest = asset.get("digest")
|
|
731
|
+
if digest is not None and str(digest) != state.wheel_sha256:
|
|
732
|
+
raise ReleaseError(
|
|
733
|
+
f"GitCode 附件 digest 与归档 SHA256 不符(冲突): {str(digest)[:12]}…")
|
|
734
|
+
url = asset.get("browser_download_url") or self._asset_url(state)
|
|
735
|
+
try:
|
|
736
|
+
sha, _size = self._download(url, state.wheel_size)
|
|
737
|
+
except ReleaseError:
|
|
738
|
+
raise
|
|
739
|
+
except Exception as e:
|
|
740
|
+
raise ReleaseError(
|
|
741
|
+
f"GitCode 附件下载失败(网络层): {type(e).__name__}(稳定 URL 的 query 不记录)")
|
|
742
|
+
if sha != state.wheel_sha256:
|
|
743
|
+
raise ReleaseError("GitCode 附件下载字节 SHA256 与归档不符(冲突——下载字节是地面真值)")
|
|
744
|
+
return {"release_url": self._release_url(state), "asset_url": url,
|
|
745
|
+
"sha256": sha, "digest": digest if digest is not None else None,
|
|
746
|
+
"created_at": rel.get("created_at"),
|
|
747
|
+
"target_commitish": rel.get("target_commitish"),
|
|
748
|
+
"prerelease": bool(rel.get("prerelease", False)),
|
|
749
|
+
"release_status": rel.get("release_status"),
|
|
750
|
+
"verified_at": _now_iso()}
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
def _gc_check_release_formal(rel: dict, state, notes: str) -> None:
|
|
754
|
+
"""正式 Release 元数据逐字段比对(T-5-12):tag/target/body 逐字一致、prerelease=false、
|
|
755
|
+
release_status 存在且非 pre、任何返回 draft 字段必须 false。不符立即冲突。"""
|
|
756
|
+
if rel.get("tag_name") != state.tag:
|
|
757
|
+
raise ReleaseError(
|
|
758
|
+
f"GitCode Release tag_name 不符: {rel.get('tag_name')!r} != {state.tag!r}(冲突)")
|
|
759
|
+
if rel.get("target_commitish") != state.head:
|
|
760
|
+
raise ReleaseError("GitCode Release target_commitish 与归档 HEAD 不符(冲突)")
|
|
761
|
+
if rel.get("body") != notes:
|
|
762
|
+
raise ReleaseError("GitCode Release body 与当前 CHANGELOG section 不逐字一致(冲突)")
|
|
763
|
+
if rel.get("prerelease"):
|
|
764
|
+
raise ReleaseError("GitCode Release prerelease=true(须为正式非 prerelease 发布,A7)")
|
|
765
|
+
if "release_status" not in rel or rel.get("release_status") is None:
|
|
766
|
+
raise ReleaseError("GitCode Release 元数据缺 release_status(fail-closed,无法证明非 pre)")
|
|
767
|
+
if str(rel.get("release_status")) == "pre":
|
|
768
|
+
raise ReleaseError("GitCode Release release_status=pre(须为正式非 prerelease 发布,A7)")
|
|
769
|
+
if rel.get("draft"):
|
|
770
|
+
raise ReleaseError("GitCode Release draft=true(须为正式非 draft 发布,A7)")
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
def _gc_classify_assets(rel: dict) -> tuple[list[dict], list[dict]]:
|
|
774
|
+
"""assets 全集按实际 type 分类:返回 (attach 上传资产全集, source 平台自动归档)。
|
|
775
|
+
|
|
776
|
+
未知/缺失 type 一律 fail-closed——不得按预期 sdist 文件名或 URL 命名空间猜测分类
|
|
777
|
+
(T-5-15);type=source 豁免、保留、不删除。
|
|
778
|
+
"""
|
|
779
|
+
attach: list[dict] = []
|
|
780
|
+
source: list[dict] = []
|
|
781
|
+
for a in (rel.get("assets") or []):
|
|
782
|
+
t = a.get("type") if isinstance(a, dict) else None
|
|
783
|
+
if t == "attach":
|
|
784
|
+
attach.append(a)
|
|
785
|
+
elif t == "source":
|
|
786
|
+
source.append(a)
|
|
787
|
+
else:
|
|
788
|
+
raise ReleaseError(
|
|
789
|
+
f"GitCode asset type 未知/缺失: {t!r}(fail-closed,不按文件名猜测分类)")
|
|
790
|
+
return attach, source
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
# ---------------------------------------------------------------------------
|
|
794
|
+
# 状态正本(原子写)
|
|
795
|
+
# ---------------------------------------------------------------------------
|
|
796
|
+
|
|
797
|
+
SURFACES = ("testpypi", "pypi", "gitcode")
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
def _fresh_surfaces() -> dict:
|
|
801
|
+
"""每面独立状态字典:{status: pending} 起步,verified 后携带 api_url/file_url/sha256/
|
|
802
|
+
upload_time(ISO-8601)/verified_at/error 证据字段(接口契约:surfaces.<面>.*)。"""
|
|
803
|
+
return {s: {"status": "pending"} for s in SURFACES}
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
@dataclasses.dataclass
|
|
807
|
+
class ReleaseState:
|
|
808
|
+
"""发布状态:身份五元组 + 上传授权轨道 + 各面状态 + 裸预检证据。
|
|
809
|
+
|
|
810
|
+
target 只表示上传授权轨道(test|all,由上传模式设置;裸预检不改);
|
|
811
|
+
last_command 另记 bare/test/all/resume(裸预检只更新 last_command/preflight);
|
|
812
|
+
surfaces 每面状态机 pending/uploading/uploaded_unverified/verified/failed。
|
|
813
|
+
"""
|
|
814
|
+
|
|
815
|
+
version: str
|
|
816
|
+
tag: str
|
|
817
|
+
head: str
|
|
818
|
+
wheel_name: str
|
|
819
|
+
wheel_size: int
|
|
820
|
+
wheel_sha256: str
|
|
821
|
+
run_dir: Path
|
|
822
|
+
target: str | None = None
|
|
823
|
+
last_command: str | None = None
|
|
824
|
+
surfaces: dict = dataclasses.field(default_factory=_fresh_surfaces)
|
|
825
|
+
preflight: dict = dataclasses.field(default_factory=dict)
|
|
826
|
+
confirmation: dict = dataclasses.field(default_factory=dict) # D-13 确认绑定(mode/confirmed_at)
|
|
827
|
+
approval: dict = dataclasses.field(default_factory=dict) # 已消费审批绑定(脱敏白名单)
|
|
828
|
+
approval_stale: bool = False
|
|
829
|
+
|
|
830
|
+
def identity(self) -> dict:
|
|
831
|
+
return {"version": self.version, "tag": self.tag, "head": self.head,
|
|
832
|
+
"wheel": {"name": self.wheel_name, "size": self.wheel_size,
|
|
833
|
+
"sha256": self.wheel_sha256}}
|
|
834
|
+
|
|
835
|
+
def surface(self, realm: str) -> dict:
|
|
836
|
+
surf = self.surfaces.get(realm)
|
|
837
|
+
if not isinstance(surf, dict):
|
|
838
|
+
surf = {"status": "pending"}
|
|
839
|
+
self.surfaces[realm] = surf
|
|
840
|
+
return surf
|
|
841
|
+
|
|
842
|
+
def set_surface(self, realm: str, **fields) -> None:
|
|
843
|
+
self.surface(realm).update(fields)
|
|
844
|
+
|
|
845
|
+
def to_payload(self) -> dict:
|
|
846
|
+
return {**self.identity(), "target": self.target, "last_command": self.last_command,
|
|
847
|
+
"surfaces": {k: dict(v) for k, v in self.surfaces.items()},
|
|
848
|
+
"preflight": self.preflight,
|
|
849
|
+
"confirmation": self.confirmation or None,
|
|
850
|
+
"approval": self.approval or None,
|
|
851
|
+
"approval_stale": self.approval_stale or None,
|
|
852
|
+
"generated": _now_iso()}
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
def _now_iso() -> str:
|
|
856
|
+
return datetime.now().astimezone().isoformat(timespec="seconds")
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
def write_state(run_dir: Path, payload: dict, filename: str = "state.json") -> Path:
|
|
860
|
+
"""原子状态写:临时文件 + flush/fsync + os.replace(崩溃不留半份 JSON)。"""
|
|
861
|
+
run_dir.mkdir(parents=True, exist_ok=True)
|
|
862
|
+
tmp = run_dir / f".{filename}.tmp"
|
|
863
|
+
with open(tmp, "w", encoding="utf-8") as f:
|
|
864
|
+
json.dump(payload, f, ensure_ascii=False, indent=2)
|
|
865
|
+
f.flush()
|
|
866
|
+
os.fsync(f.fileno())
|
|
867
|
+
os.replace(tmp, run_dir / filename)
|
|
868
|
+
return run_dir / filename
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
def _merge_existing_state(state: ReleaseState) -> dict:
|
|
872
|
+
"""裸模式与既有 state.json 合并:身份一致才保留 target/surfaces(不改授权轨道)。"""
|
|
873
|
+
payload = state.to_payload()
|
|
874
|
+
prev = state.run_dir / "state.json"
|
|
875
|
+
if not prev.is_file():
|
|
876
|
+
return payload
|
|
877
|
+
try:
|
|
878
|
+
old = json.loads(prev.read_text(encoding="utf-8"))
|
|
879
|
+
except (OSError, json.JSONDecodeError) as e:
|
|
880
|
+
raise ReleaseError(f"既有 state.json 不可读(fail-closed): {e}") from None
|
|
881
|
+
same = (old.get("version"), old.get("tag"), (old.get("wheel") or {}).get("sha256")) == \
|
|
882
|
+
(state.version, state.tag, state.wheel_sha256)
|
|
883
|
+
if not same:
|
|
884
|
+
raise ReleaseError("既有 state.json 与当前发布身份不符(version/tag/SHA256 不一致)——"
|
|
885
|
+
"归档与状态错位,需人工核对后处理,不自动覆盖")
|
|
886
|
+
payload["target"] = old.get("target")
|
|
887
|
+
payload["surfaces"] = old.get("surfaces") or payload["surfaces"]
|
|
888
|
+
return payload
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
# ---------------------------------------------------------------------------
|
|
892
|
+
# 步骤 1:执行身份 + checkout 检查
|
|
893
|
+
# ---------------------------------------------------------------------------
|
|
894
|
+
|
|
895
|
+
def assert_execution_identity(expected_root: Path) -> dict:
|
|
896
|
+
"""任何 build/状态写/网络调用之前:cwd、本脚本、审计模块及其 REPO 必属同一仓根。
|
|
897
|
+
|
|
898
|
+
PATH 中指向主仓的 console 在获批 worktree 内启动会在此 fail-closed——获批 worktree
|
|
899
|
+
必须改用其内部 scripts/CLI 入口(D-02 身份边界)。
|
|
900
|
+
"""
|
|
901
|
+
root = Path(expected_root).resolve()
|
|
902
|
+
cwd = Path.cwd().resolve()
|
|
903
|
+
if cwd != root:
|
|
904
|
+
raise ReleaseError(
|
|
905
|
+
f"执行身份不符: cwd {cwd} 不等于仓根 {root}——release 必须从仓根运行;"
|
|
906
|
+
"获批 worktree 须用其内部脚本/CLI 入口,主仓 console 不得跨仓启动")
|
|
907
|
+
script = Path(__file__).resolve()
|
|
908
|
+
if root not in script.parents:
|
|
909
|
+
raise ReleaseError(f"执行身份不符: release.py 不在仓根 {root} 之下")
|
|
910
|
+
audit_script = Path(release_audit.__file__).resolve()
|
|
911
|
+
if root not in audit_script.parents:
|
|
912
|
+
raise ReleaseError("执行身份不符: release_audit.py 不在仓根之下——审计模块被重定向到另一仓")
|
|
913
|
+
audit_repo = Path(release_audit.REPO).resolve()
|
|
914
|
+
if audit_repo != root:
|
|
915
|
+
raise ReleaseError("执行身份不符: release_audit.REPO 不等于仓根——审计模块被重定向到另一仓")
|
|
916
|
+
return {"script_path": str(script), "repo": str(root),
|
|
917
|
+
"audit_script_path": str(audit_script), "audit_repo": str(audit_repo)}
|
|
918
|
+
|
|
919
|
+
|
|
920
|
+
def _ver_tuple(v: str) -> tuple[int, ...] | None:
|
|
921
|
+
"""'v0.1.2' → (0,1,2);解析不出 → None(apply.py _ver_tuple 同款,不引 packaging)。"""
|
|
922
|
+
m = re.match(r"v?(\d+(?:\.\d+)*)", str(v).strip())
|
|
923
|
+
return tuple(int(x) for x in m.group(1).split(".")) if m else None
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
def inspect_checkout(repo: Path) -> dict:
|
|
927
|
+
"""D-05:HEAD 恰一个严格 vX.Y.Z tag + porcelain(含 untracked)干净 + 单调性。
|
|
928
|
+
|
|
929
|
+
单调性历史基线明确排除 HEAD 当前发布 tag:候选须高于其余全部本地 v* 版本——
|
|
930
|
+
当前最高合法 HEAD tag 正向可通过;另一本地 tag 更高即失败。
|
|
931
|
+
"""
|
|
932
|
+
root = Path(repo).resolve()
|
|
933
|
+
rc, out, _ = sh(["git", "rev-parse", "HEAD"], cwd=root, timeout=60)
|
|
934
|
+
if rc != 0:
|
|
935
|
+
raise ReleaseError("无法读取 HEAD(不是 git 仓?)——release 需源码仓检出态")
|
|
936
|
+
head = out.strip()
|
|
937
|
+
rc, out, _ = sh(["git", "tag", "--points-at", "HEAD"], cwd=root, timeout=60)
|
|
938
|
+
if rc != 0:
|
|
939
|
+
raise ReleaseError("git tag --points-at 失败(fail-closed)")
|
|
940
|
+
at_head = [t.strip() for t in out.splitlines() if t.strip()]
|
|
941
|
+
if not at_head:
|
|
942
|
+
raise ReleaseError("HEAD 不在任何精确 v* tag 上(D-05)——对发布提交打严格 vX.Y.Z tag 后重跑")
|
|
943
|
+
strict = [t for t in at_head if STRICT_TAG_RE.match(t)]
|
|
944
|
+
if len(strict) != 1:
|
|
945
|
+
raise ReleaseError(
|
|
946
|
+
f"HEAD 上严格 vX.Y.Z tag 必须恰一个,实际 {len(strict)} 个"
|
|
947
|
+
f"(HEAD 上: {', '.join(sorted(at_head))})")
|
|
948
|
+
tag = strict[0]
|
|
949
|
+
version = tag[1:]
|
|
950
|
+
rc, out, _ = sh(["git", "status", "--porcelain"], cwd=root, timeout=120)
|
|
951
|
+
if rc != 0:
|
|
952
|
+
raise ReleaseError("git status --porcelain 失败(fail-closed)")
|
|
953
|
+
if out.strip():
|
|
954
|
+
n = len(out.strip().splitlines())
|
|
955
|
+
raise ReleaseError(
|
|
956
|
+
f"工作树不干净: {n} 处 dirty/untracked(porcelain 含 untracked)——"
|
|
957
|
+
"禁止用 reset --hard / git clean 制造干净状态;请自行提交或整理后重跑(D-05)")
|
|
958
|
+
# 单调性:基线 = 全部本地 v* tag − HEAD 当前发布 tag
|
|
959
|
+
rc, out, _ = sh(["git", "tag", "--list", "v*"], cwd=root, timeout=60)
|
|
960
|
+
if rc != 0:
|
|
961
|
+
raise ReleaseError("git tag --list 失败(fail-closed)")
|
|
962
|
+
cand = _ver_tuple(tag)
|
|
963
|
+
others = []
|
|
964
|
+
for t in (x.strip() for x in out.splitlines() if x.strip()):
|
|
965
|
+
if t == tag:
|
|
966
|
+
continue # 基线明确排除 HEAD 当前发布 tag
|
|
967
|
+
tv = _ver_tuple(t)
|
|
968
|
+
if tv is not None:
|
|
969
|
+
others.append((tv, t))
|
|
970
|
+
higher = sorted([(v, t) for v, t in others if v >= cand])
|
|
971
|
+
if higher:
|
|
972
|
+
raise ReleaseError(
|
|
973
|
+
f"候选版本 {version} 不高于其余本地发布历史(存在更高 tag {higher[0][1]})——"
|
|
974
|
+
"按 D-15 bump 下一个干净 v* tag")
|
|
975
|
+
return {"head": head, "tag": tag, "version": version}
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
# ---------------------------------------------------------------------------
|
|
979
|
+
# 步骤 2:制品归档(构建一次,全程引用同一字节)
|
|
980
|
+
# ---------------------------------------------------------------------------
|
|
981
|
+
|
|
982
|
+
def project_meta(repo: Path) -> dict:
|
|
983
|
+
"""pyproject 解析:PyPI 项目名 + GitCode owner/repo(占用/可见性检查的公开面对账源)。"""
|
|
984
|
+
text = (Path(repo) / "pyproject.toml").read_text(encoding="utf-8")
|
|
985
|
+
nm = re.search(r'^name\s*=\s*"([^"]+)"', text, re.M)
|
|
986
|
+
if not nm:
|
|
987
|
+
raise ReleaseError("pyproject.toml 缺 name(发布预检的 PyPI 占用检查需要项目名)")
|
|
988
|
+
url = re.search(r'^Repository\s*=\s*"https://gitcode\.com/([^/"\s]+)/([^/"\s]+)"',
|
|
989
|
+
text, re.M)
|
|
990
|
+
if not url:
|
|
991
|
+
raise ReleaseError("pyproject.toml [project.urls] Repository 须为 gitcode.com 地址"
|
|
992
|
+
"(GitCode 占用/可见性检查的对账源)")
|
|
993
|
+
return {"name": nm.group(1), "gitcode_owner": url.group(1), "gitcode_repo": url.group(2)}
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
def sha256_file(p: Path) -> str:
|
|
997
|
+
h = hashlib.sha256()
|
|
998
|
+
with p.open("rb") as f:
|
|
999
|
+
for chunk in iter(lambda: f.read(1 << 16), b""):
|
|
1000
|
+
h.update(chunk)
|
|
1001
|
+
return h.hexdigest()
|
|
1002
|
+
|
|
1003
|
+
|
|
1004
|
+
def wheel_metadata_version(wheel: Path) -> str:
|
|
1005
|
+
with zipfile.ZipFile(wheel) as z:
|
|
1006
|
+
meta = next((n for n in z.namelist() if n.endswith(".dist-info/METADATA")), None)
|
|
1007
|
+
if meta is None:
|
|
1008
|
+
raise ReleaseError(f"wheel 缺 .dist-info/METADATA 条目: {wheel.name}")
|
|
1009
|
+
text = z.read(meta).decode("utf-8", errors="replace")
|
|
1010
|
+
for line in text.splitlines():
|
|
1011
|
+
if line.lower().startswith("version:"):
|
|
1012
|
+
return line.split(":", 1)[1].strip()
|
|
1013
|
+
raise ReleaseError(f"METADATA 缺 Version 行: {wheel.name}")
|
|
1014
|
+
|
|
1015
|
+
|
|
1016
|
+
def _clean_dist(repo: Path) -> Path:
|
|
1017
|
+
"""路径守卫后清空仓根 dist(只清构建输出区,绝不触碰用户工作树其他内容)。"""
|
|
1018
|
+
root = Path(repo).resolve()
|
|
1019
|
+
dist = (root / "dist").resolve()
|
|
1020
|
+
if dist.parent != root or dist.name != "dist":
|
|
1021
|
+
raise ReleaseError(f"拒绝清理仓根 dist 之外的路径: {dist}")
|
|
1022
|
+
if dist.exists():
|
|
1023
|
+
shutil.rmtree(dist)
|
|
1024
|
+
dist.mkdir(parents=True)
|
|
1025
|
+
return dist
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
def _check_wheel_identity(wheel: Path, version: str) -> None:
|
|
1029
|
+
m = re.match(r"^ohos_devloop-([^-]+)-\w+-\w+-\w+\.whl$", wheel.name)
|
|
1030
|
+
if not m or m.group(1) != version:
|
|
1031
|
+
raise ReleaseError(f"wheel 文件名与 tag 版本不一致: {wheel.name}(期望版本 {version})")
|
|
1032
|
+
mv = wheel_metadata_version(wheel)
|
|
1033
|
+
if mv != version:
|
|
1034
|
+
raise ReleaseError(f"wheel METADATA Version {mv} != tag 版本 {version}(fail-closed)")
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
def _tail(text: str, n: int = 2000) -> str:
|
|
1038
|
+
return text.strip()[-n:]
|
|
1039
|
+
|
|
1040
|
+
|
|
1041
|
+
def _find_archive(run_dir: Path) -> Path | None:
|
|
1042
|
+
"""归档目录唯一 wheel 查找(无 → None;多个 → fail-closed)。"""
|
|
1043
|
+
artifact_dir = run_dir / "artifact"
|
|
1044
|
+
if not artifact_dir.is_dir():
|
|
1045
|
+
return None
|
|
1046
|
+
wheels = sorted(artifact_dir.glob("*.whl"))
|
|
1047
|
+
if len(wheels) > 1:
|
|
1048
|
+
raise ReleaseError(f"归档目录已有 {len(wheels)} 个 wheel(必须唯一): {artifact_dir}")
|
|
1049
|
+
return wheels[0] if wheels else None
|
|
1050
|
+
|
|
1051
|
+
|
|
1052
|
+
def _run_archive_gates(state: ReleaseState, archive: Path) -> None:
|
|
1053
|
+
"""归档硬门:审计四步 + 给定归档 wheel 的 strict 安装态 smoke(上传身份绑定归档字节)。"""
|
|
1054
|
+
print(f"[3/5] release_audit 四步审计(复用既有正本,绑定归档字节)")
|
|
1055
|
+
if release_audit.main([str(archive)]) != 0:
|
|
1056
|
+
raise ReleaseError("release_audit 四步审计未通过(fail-closed,无跳过通道;见上方输出)")
|
|
1057
|
+
state.preflight["audit"] = {"status": "pass", "wheel": archive.name}
|
|
1058
|
+
print(f"[4/5] 真实安装态 smoke(干净 venv + 真实 run;strict——环境缺失即 FAIL)")
|
|
1059
|
+
evidence = state.run_dir / "logs" / "smoke"
|
|
1060
|
+
smoke = release_smoke.run_smoke(archive, evidence, expected_sha256=state.wheel_sha256,
|
|
1061
|
+
strict=True)
|
|
1062
|
+
if not smoke.get("ok"):
|
|
1063
|
+
fails = "; ".join(str(x) for x in smoke.get("failures") or []) or "无详情"
|
|
1064
|
+
raise ReleaseError(f"安装态 smoke 未通过(strict——环境缺失/未执行步骤均 FAIL): {fails[:500]}")
|
|
1065
|
+
state.preflight["smoke"] = {"status": "pass", "evidence": str(evidence)}
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
def _state_from_archive(run_dir: Path, archive: Path, version: str, tag: str,
|
|
1069
|
+
head: str) -> ReleaseState:
|
|
1070
|
+
arch_sha = sha256_file(archive)
|
|
1071
|
+
if wheel_metadata_version(archive) != version:
|
|
1072
|
+
raise ReleaseError(f"归档 wheel METADATA 版本 != {version}: {archive.name}")
|
|
1073
|
+
return ReleaseState(version=version, tag=tag, head=head,
|
|
1074
|
+
wheel_name=archive.name, wheel_size=archive.stat().st_size,
|
|
1075
|
+
wheel_sha256=arch_sha, run_dir=run_dir)
|
|
1076
|
+
|
|
1077
|
+
|
|
1078
|
+
def load_release_artifact(run_dir: Path, version: str, tag: str, head: str) -> ReleaseState:
|
|
1079
|
+
"""已有归档直取(test/all/resume 专用):零构建、不清理归档、不做候选 hash 对账。
|
|
1080
|
+
|
|
1081
|
+
只对归档重跑 audit/strict smoke 与实时硬门——上传/恢复身份始终绑定归档字节
|
|
1082
|
+
(Pitfall 3:同 tag 不当必然同 hash,归档才是唯一正本)。
|
|
1083
|
+
"""
|
|
1084
|
+
archive = _find_archive(Path(run_dir))
|
|
1085
|
+
if archive is None:
|
|
1086
|
+
raise ReleaseError(
|
|
1087
|
+
f"归档缺失: {Path(run_dir) / 'artifact'}——已有归档模式只消费归档正本,"
|
|
1088
|
+
"不重建、不读 dist(D-08/D-16)")
|
|
1089
|
+
state = _state_from_archive(Path(run_dir), archive, version, tag, head)
|
|
1090
|
+
_run_archive_gates(state, archive)
|
|
1091
|
+
return state
|
|
1092
|
+
|
|
1093
|
+
|
|
1094
|
+
def prepare_artifact(repo: Path, checkout: dict, mode: str = "bare") -> ReleaseState:
|
|
1095
|
+
"""制品准备(mode 语义分叉):
|
|
1096
|
+
|
|
1097
|
+
- bare:恒做 D-02 候选构建(clean dist → wheel-only build),候选只写独立
|
|
1098
|
+
preflight.candidate/reproducibility 观察值(不覆盖归档、不作可复现性硬门);
|
|
1099
|
+
无归档时首次构建即归档。
|
|
1100
|
+
- test/all:无归档时 build_and_archive(首次构建即归档);已有同版本归档时
|
|
1101
|
+
load_release_artifact——零构建、不清理归档、不注入候选 hash 对账。
|
|
1102
|
+
- resume:归档必须已存在(缺归档 fail-closed),同样零构建。
|
|
1103
|
+
"""
|
|
1104
|
+
root = Path(repo).resolve()
|
|
1105
|
+
version, tag, head = checkout["version"], checkout["tag"], checkout["head"]
|
|
1106
|
+
run_dir = root / "runs" / f"release-{version}"
|
|
1107
|
+
artifact_dir = run_dir / "artifact"
|
|
1108
|
+
archive = _find_archive(run_dir)
|
|
1109
|
+
if mode in ("test", "all", "resume"):
|
|
1110
|
+
if archive is not None:
|
|
1111
|
+
return load_release_artifact(run_dir, version, tag, head)
|
|
1112
|
+
if mode == "resume":
|
|
1113
|
+
raise ReleaseError(
|
|
1114
|
+
f"归档缺失: {artifact_dir}——resume 只从归档恢复,绝不重建(D-04/D-08)")
|
|
1115
|
+
print(f"[2/5] clean dist → python -m build --wheel(wheel-only,REL-05)")
|
|
1116
|
+
dist = _clean_dist(root)
|
|
1117
|
+
rc, out, err = sh([sys.executable, "-m", "build", "--wheel", "--outdir", str(dist)],
|
|
1118
|
+
cwd=root, timeout=BUILD_TIMEOUT)
|
|
1119
|
+
if rc != 0:
|
|
1120
|
+
raise ReleaseError(f"python -m build --wheel 失败(rc={rc}):\n{_tail(out + err)}")
|
|
1121
|
+
wheels = sorted(dist.glob("*.whl"))
|
|
1122
|
+
extra = [p.name for p in dist.iterdir()
|
|
1123
|
+
if p.is_file() and p.suffix != ".whl"] if dist.exists() else []
|
|
1124
|
+
if len(wheels) != 1 or extra:
|
|
1125
|
+
raise ReleaseError(
|
|
1126
|
+
f"构建输出必须恰一个 wheel、零 sdist: wheels={len(wheels)},其他制品={extra[:5]}"
|
|
1127
|
+
"(禁止裸 python -m build / dist 旧物混入,Pitfall 2)")
|
|
1128
|
+
candidate = wheels[0]
|
|
1129
|
+
_check_wheel_identity(candidate, version)
|
|
1130
|
+
cand_sha = sha256_file(candidate)
|
|
1131
|
+
if archive is None:
|
|
1132
|
+
artifact_dir.mkdir(parents=True, exist_ok=True)
|
|
1133
|
+
archive = artifact_dir / candidate.name
|
|
1134
|
+
shutil.copyfile(candidate, archive) # 首次构建即归档:归档字节是唯一正本
|
|
1135
|
+
state = _state_from_archive(run_dir, archive, version, tag, head)
|
|
1136
|
+
state.preflight["candidate"] = {"filename": candidate.name, "sha256": cand_sha}
|
|
1137
|
+
state.preflight["reproducibility"] = { # 观察值:候选与归档 hash 不同不覆盖、不阻断
|
|
1138
|
+
"archived_sha256": state.wheel_sha256, "candidate_sha256": cand_sha,
|
|
1139
|
+
"match": cand_sha == state.wheel_sha256}
|
|
1140
|
+
_run_archive_gates(state, archive)
|
|
1141
|
+
return state
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
# ---------------------------------------------------------------------------
|
|
1145
|
+
# 步骤 3:只读远端预检(占用/凭据/公开前置;全部先于任何上传)
|
|
1146
|
+
# ---------------------------------------------------------------------------
|
|
1147
|
+
|
|
1148
|
+
def preflight_remote(repo: Path, state: ReleaseState, meta: dict,
|
|
1149
|
+
env: dict | None = None) -> dict:
|
|
1150
|
+
"""只读预检:凭据存在性 → 远端 tag/HEAD 对账 → PyPI 占用 → GitCode 认证/可见性/Release 占用。
|
|
1151
|
+
|
|
1152
|
+
每项独立记录(全部跑完再汇总失败);零写操作(GET-only)。
|
|
1153
|
+
"""
|
|
1154
|
+
e = os.environ if env is None else env
|
|
1155
|
+
checks: list[dict] = []
|
|
1156
|
+
|
|
1157
|
+
def record(name: str, ok: bool, detail: str):
|
|
1158
|
+
checks.append({"name": name, "status": "pass" if ok else "fail", "detail": detail})
|
|
1159
|
+
|
|
1160
|
+
missing = [k for k in ("TWINE_USERNAME", "TWINE_PASSWORD", "GITCODE_TOKEN")
|
|
1161
|
+
if not str(e.get(k) or "").strip()]
|
|
1162
|
+
record("credentials-present", not missing,
|
|
1163
|
+
"TWINE_USERNAME/TWINE_PASSWORD/GITCODE_TOKEN 均非空(env-only;只验存在性,"
|
|
1164
|
+
"写权限由真实写操作证明)" if not missing
|
|
1165
|
+
else f"缺失环境变量: {', '.join(missing)}(凭据只经环境变量注入,D-10)")
|
|
1166
|
+
|
|
1167
|
+
rc, out, _ = sh(["git", "ls-remote", "--tags", "origin"], cwd=repo, timeout=120)
|
|
1168
|
+
if rc != 0:
|
|
1169
|
+
record("remote-tag-head", False, "git ls-remote --tags origin 失败(无 origin 远端或不可达)")
|
|
1170
|
+
else:
|
|
1171
|
+
remote: dict[str, str] = {}
|
|
1172
|
+
for line in out.strip().splitlines():
|
|
1173
|
+
sha, _, ref = line.partition("\t")
|
|
1174
|
+
ref = ref.strip()
|
|
1175
|
+
if ref.startswith("refs/tags/"):
|
|
1176
|
+
remote[ref[len("refs/tags/"):]] = sha.strip()
|
|
1177
|
+
peeled = remote.get(f"{state.tag}^{{}}") or remote.get(state.tag)
|
|
1178
|
+
record("remote-tag-head", peeled == state.head,
|
|
1179
|
+
f"远端 tag {state.tag}(peeled)== 本地 HEAD" if peeled == state.head
|
|
1180
|
+
else f"远端 tag {state.tag} 与本地 HEAD 不一致(远端 "
|
|
1181
|
+
f"{'缺失' if peeled is None else peeled[:12]} vs 本地 {state.head[:12]})")
|
|
1182
|
+
|
|
1183
|
+
pypi = PyPIClient()
|
|
1184
|
+
try:
|
|
1185
|
+
occupied, detail = pypi.version_occupied(meta["name"], state.version)
|
|
1186
|
+
record("pypi-occupancy", not occupied, detail)
|
|
1187
|
+
except ReleaseError as ex:
|
|
1188
|
+
record("pypi-occupancy", False, str(ex))
|
|
1189
|
+
|
|
1190
|
+
token = str(e.get("GITCODE_TOKEN") or "").strip()
|
|
1191
|
+
if token:
|
|
1192
|
+
gc = GitCodeClient(meta["gitcode_owner"], meta["gitcode_repo"], token)
|
|
1193
|
+
try:
|
|
1194
|
+
info = gc.repo_info()
|
|
1195
|
+
private = bool(info.get("private", True)) # 缺键按私有算(fail-closed)
|
|
1196
|
+
record("gitcode-repo-public", not private,
|
|
1197
|
+
"GitCode 仓库为公开态(只读认证通过)" if not private
|
|
1198
|
+
else "GitCode 仓库仍为私有(D-20 公开前置未满足——不得上传公开 PyPI)")
|
|
1199
|
+
except ReleaseError as ex:
|
|
1200
|
+
record("gitcode-repo-public", False, str(ex))
|
|
1201
|
+
try:
|
|
1202
|
+
rel = gc.release_for_tag(state.tag)
|
|
1203
|
+
record("gitcode-release-occupancy", rel is None,
|
|
1204
|
+
f"GitCode 无 tag {state.tag} 的 Release(可创建)" if rel is None
|
|
1205
|
+
else f"GitCode 已有 tag {state.tag} 的 Release(占用冲突)")
|
|
1206
|
+
except ReleaseError as ex:
|
|
1207
|
+
record("gitcode-release-occupancy", False, str(ex))
|
|
1208
|
+
else:
|
|
1209
|
+
record("gitcode-repo-public", False, "未检查(GITCODE_TOKEN 缺失)")
|
|
1210
|
+
record("gitcode-release-occupancy", False, "未检查(GITCODE_TOKEN 缺失)")
|
|
1211
|
+
# D-20 公开历史门(05-06):attestation 按 tracked schema 消费 + 实时重扫对账——
|
|
1212
|
+
# 缺 attestation / 任何新命中 / 人工复核缺失 / 证据过期 / 公开面不符均阻止上传
|
|
1213
|
+
# (wheel 审计通过不能推导整仓或历史可公开)。
|
|
1214
|
+
attestation = Path(state.run_dir) / "public-readiness.json"
|
|
1215
|
+
try:
|
|
1216
|
+
verify_public_readiness(repo, state.tag, attestation)
|
|
1217
|
+
record("public-readiness", True,
|
|
1218
|
+
"公开历史证明实时校验通过(重扫 digest/命中处置/二进制复核/匿名公开面一致)")
|
|
1219
|
+
except ReleaseError as ex:
|
|
1220
|
+
record("public-readiness", False, str(ex))
|
|
1221
|
+
return {"checks": checks}
|
|
1222
|
+
|
|
1223
|
+
|
|
1224
|
+
# ---------------------------------------------------------------------------
|
|
1225
|
+
# A1 写入门与决策读取(05-VALIDATION.md Decision Outcomes;05-04 blocking-human 填写)
|
|
1226
|
+
# ---------------------------------------------------------------------------
|
|
1227
|
+
|
|
1228
|
+
VALIDATION_REL = Path(".planning") / "phases" / "05-release-pipeline-dual-source-launch" \
|
|
1229
|
+
/ "05-VALIDATION.md"
|
|
1230
|
+
|
|
1231
|
+
|
|
1232
|
+
def load_decision_outcomes(repo: Path | None = None) -> dict:
|
|
1233
|
+
"""解析 05-VALIDATION.md Decision Outcomes 的 A1/A2 选择。
|
|
1234
|
+
|
|
1235
|
+
Selected 列含 create-then-verify / strict-literal / required-receipt /
|
|
1236
|
+
recommended-receipt / pending;文件缺失或未选 → None(fail-closed 方向,绝不默认放行)。
|
|
1237
|
+
"""
|
|
1238
|
+
root = Path(repo) if repo is not None else REPO
|
|
1239
|
+
outcomes = {"A1": None, "A2": None}
|
|
1240
|
+
path = root / VALIDATION_REL
|
|
1241
|
+
if not path.is_file():
|
|
1242
|
+
return outcomes
|
|
1243
|
+
for line in path.read_text(encoding="utf-8", errors="replace").splitlines():
|
|
1244
|
+
if not line.lstrip().startswith("|"):
|
|
1245
|
+
continue
|
|
1246
|
+
cells = [c.strip() for c in line.strip().strip("|").split("|")]
|
|
1247
|
+
if len(cells) < 3:
|
|
1248
|
+
continue
|
|
1249
|
+
for key in ("A1", "A2"):
|
|
1250
|
+
if not cells[0].startswith(key):
|
|
1251
|
+
continue
|
|
1252
|
+
sel = cells[2] # 第 3 列 Selected(第 2 列是 allowed values,不读取)
|
|
1253
|
+
for literal in ("create-then-verify", "strict-literal",
|
|
1254
|
+
"required-receipt", "recommended-receipt", "pending"):
|
|
1255
|
+
if literal in sel:
|
|
1256
|
+
outcomes[key] = literal
|
|
1257
|
+
break
|
|
1258
|
+
return outcomes
|
|
1259
|
+
|
|
1260
|
+
|
|
1261
|
+
def assert_publication_permitted(outcomes: dict, surface: str = "testpypi") -> None:
|
|
1262
|
+
"""A1 写入门:只有 create-then-verify 允许任何远端写(含 TestPyPI)。
|
|
1263
|
+
|
|
1264
|
+
pending/缺失/strict-literal 均 fail-closed——strict-literal 下包括 {surface} 在内的
|
|
1265
|
+
所有真实公开写入阻塞(D-20 严格字面与 D-03 冲突,需重新决策),绝不默认放行。
|
|
1266
|
+
"""
|
|
1267
|
+
a1 = (outcomes or {}).get("A1")
|
|
1268
|
+
if a1 == "create-then-verify":
|
|
1269
|
+
return
|
|
1270
|
+
if a1 == "strict-literal":
|
|
1271
|
+
raise ReleaseError(
|
|
1272
|
+
f"A1=strict-literal:Release URL 严格字面解释与 D-03 上传顺序冲突,"
|
|
1273
|
+
f"包括 {surface} 在内的所有真实公开写入均阻塞——请先重新决策 D-03/D-20"
|
|
1274
|
+
"(05-04 Decision Outcomes),不得默认放行或以占位证据代替")
|
|
1275
|
+
raise ReleaseError(
|
|
1276
|
+
f"A1 决策未选(pending/缺失,当前 {a1!r}):不允许对 {surface} 的任何远端写入——"
|
|
1277
|
+
"05-04 blocking-human 选择 create-then-verify 后才开放上传")
|
|
1278
|
+
|
|
1279
|
+
|
|
1280
|
+
# ---------------------------------------------------------------------------
|
|
1281
|
+
# D-20 公开历史门(05-06 Task 1):当前树 + 全 reachable history 值驱动扫描与实时对账
|
|
1282
|
+
# ---------------------------------------------------------------------------
|
|
1283
|
+
|
|
1284
|
+
PUBLIC_READINESS_SCHEMA = "packaging/public-readiness-schema.json"
|
|
1285
|
+
OFFICE_ZIP_SUFFIXES = frozenset({".docx", ".docm", ".xlsx", ".xlsm", ".pptx", ".pptm",
|
|
1286
|
+
".odt", ".ods", ".odp", ".epub"})
|
|
1287
|
+
IMAGE_SUFFIXES = frozenset({".png", ".jpg", ".jpeg", ".gif", ".bmp", ".ico",
|
|
1288
|
+
".webp", ".tif", ".tiff"})
|
|
1289
|
+
GIT_BATCH_BLOBS = 500 # cat-file --batch 分批上限(stdin/stdout 管道与内存有界)
|
|
1290
|
+
|
|
1291
|
+
|
|
1292
|
+
def _path_suffix(path: str) -> str:
|
|
1293
|
+
name = path.rsplit("/", 1)[-1]
|
|
1294
|
+
return "." + name.rsplit(".", 1)[-1].lower() if "." in name else ""
|
|
1295
|
+
|
|
1296
|
+
|
|
1297
|
+
def _scan_rules(blacklist: dict) -> tuple[list, list]:
|
|
1298
|
+
"""黑名单 → (编译规则, 例外表):单一数据源 release_audit.load_blacklist,不复制清单。"""
|
|
1299
|
+
rules = [(name, re.compile(str(pat)))
|
|
1300
|
+
for name, pat in {**blacklist.get("patterns", {}),
|
|
1301
|
+
**blacklist.get("generic", {})}.items()]
|
|
1302
|
+
return rules, blacklist.get("documented_exceptions") or []
|
|
1303
|
+
|
|
1304
|
+
|
|
1305
|
+
def _scan_text(path: str, text: str, rules, exceptions) -> set[str]:
|
|
1306
|
+
"""逐行值驱动匹配(窄域例外沿用 release_audit._is_excepted 语义)。
|
|
1307
|
+
|
|
1308
|
+
只返回命中 pattern 名集合——值、行文本、行号绝不离开本层(脱敏契约)。
|
|
1309
|
+
"""
|
|
1310
|
+
names: set[str] = set()
|
|
1311
|
+
for line in text.splitlines():
|
|
1312
|
+
for name, rx in rules:
|
|
1313
|
+
if name in names:
|
|
1314
|
+
continue # 该 pattern 在本对象已记录;例外行不记录、后续行仍会被检查
|
|
1315
|
+
if rx.search(line) and not release_audit._is_excepted(path, line, exceptions):
|
|
1316
|
+
names.add(name)
|
|
1317
|
+
return names
|
|
1318
|
+
|
|
1319
|
+
|
|
1320
|
+
def _scan_object(path: str, data: bytes, rules, exceptions) -> tuple[set[str], dict | None]:
|
|
1321
|
+
"""单对象分类扫描:文本逐行 / zip 型 Office 文档只读 XML 条目(格式感知)/ 其余人工复核。
|
|
1322
|
+
|
|
1323
|
+
返回 (命中 pattern 名集合, 人工复核项或 None)。zip 只读(zipfile + BytesIO),
|
|
1324
|
+
绝不 import/exec 被扫内容(制品/对象是数据不是代码)。
|
|
1325
|
+
"""
|
|
1326
|
+
suffix = _path_suffix(path)
|
|
1327
|
+
if suffix in IMAGE_SUFFIXES:
|
|
1328
|
+
return set(), {"reason": "image"}
|
|
1329
|
+
if suffix in OFFICE_ZIP_SUFFIXES:
|
|
1330
|
+
try:
|
|
1331
|
+
with zipfile.ZipFile(io.BytesIO(data)) as z:
|
|
1332
|
+
names: set[str] = set()
|
|
1333
|
+
for n in z.namelist():
|
|
1334
|
+
if n.endswith((".xml", ".rels")):
|
|
1335
|
+
names |= _scan_text(path, z.read(n).decode("utf-8", errors="replace"),
|
|
1336
|
+
rules, exceptions)
|
|
1337
|
+
return names, None
|
|
1338
|
+
except (zipfile.BadZipFile, OSError, RuntimeError, ValueError):
|
|
1339
|
+
return set(), {"reason": "zip-undecodable"}
|
|
1340
|
+
if b"\x00" in data[:8192]:
|
|
1341
|
+
return set(), {"reason": "binary"}
|
|
1342
|
+
try:
|
|
1343
|
+
text = data.decode("utf-8")
|
|
1344
|
+
except UnicodeDecodeError:
|
|
1345
|
+
return set(), {"reason": "undecodable"}
|
|
1346
|
+
return _scan_text(path, text, rules, exceptions), None
|
|
1347
|
+
|
|
1348
|
+
|
|
1349
|
+
def _iter_git_blobs(repo: Path, shas: list[str]):
|
|
1350
|
+
"""git cat-file --batch 分批流式读取;只产出 (blob sha, bytes)——tree/tag/commit 跳过。"""
|
|
1351
|
+
for start in range(0, len(shas), GIT_BATCH_BLOBS):
|
|
1352
|
+
batch = shas[start:start + GIT_BATCH_BLOBS]
|
|
1353
|
+
proc = subprocess.Popen(["git", "cat-file", "--batch"], cwd=str(repo),
|
|
1354
|
+
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
|
1355
|
+
try:
|
|
1356
|
+
out, _err = proc.communicate(("\n".join(batch) + "\n").encode("ascii"),
|
|
1357
|
+
timeout=600)
|
|
1358
|
+
finally:
|
|
1359
|
+
if proc.poll() is None:
|
|
1360
|
+
proc.kill()
|
|
1361
|
+
pos = 0
|
|
1362
|
+
for _sha in batch:
|
|
1363
|
+
nl = out.find(b"\n", pos)
|
|
1364
|
+
if nl < 0:
|
|
1365
|
+
break
|
|
1366
|
+
header = out[pos:nl].decode("ascii", errors="replace").split()
|
|
1367
|
+
pos = nl + 1
|
|
1368
|
+
if len(header) == 2 and header[1] == "missing":
|
|
1369
|
+
continue
|
|
1370
|
+
if len(header) != 3:
|
|
1371
|
+
continue
|
|
1372
|
+
size = int(header[2])
|
|
1373
|
+
data = out[pos:pos + size]
|
|
1374
|
+
pos += size + 1 # 对象内容后的换行
|
|
1375
|
+
if header[1] == "blob":
|
|
1376
|
+
yield header[0], data
|
|
1377
|
+
|
|
1378
|
+
|
|
1379
|
+
def _scan_core(repo: Path) -> dict:
|
|
1380
|
+
"""扫描核心(零副作用、不落盘):当前面 git ls-files 工作树内容 + 历史面全 refs
|
|
1381
|
+
reachable blob。输出只含 pattern 名/object/path/复核状态/计数与确定性 digest。"""
|
|
1382
|
+
root = Path(repo).resolve()
|
|
1383
|
+
rules, exceptions = _scan_rules(release_audit.load_blacklist(release_audit.BLACKLIST))
|
|
1384
|
+
rc, out, _ = sh(["git", "rev-parse", "HEAD"], cwd=root, timeout=60)
|
|
1385
|
+
if rc != 0:
|
|
1386
|
+
raise ReleaseError("无法读取 HEAD(不是 git 仓?)——公开历史扫描需源码仓检出态")
|
|
1387
|
+
head = out.strip()
|
|
1388
|
+
# 当前面:git ls-files 对应工作树内容
|
|
1389
|
+
rc, out, _ = sh(["git", "ls-files", "-z"], cwd=root, timeout=300)
|
|
1390
|
+
if rc != 0:
|
|
1391
|
+
raise ReleaseError("git ls-files 失败(fail-closed)")
|
|
1392
|
+
current_hits: list[dict] = []
|
|
1393
|
+
current_manual: list[dict] = []
|
|
1394
|
+
current_fp: list[tuple[str, str]] = [] # (path, 内容 sha256)——digest 的内容指纹面
|
|
1395
|
+
text_n = zip_n = 0
|
|
1396
|
+
files = [p for p in out.split("\x00") if p]
|
|
1397
|
+
for rel in files:
|
|
1398
|
+
try:
|
|
1399
|
+
data = (root / rel).read_bytes()
|
|
1400
|
+
except OSError:
|
|
1401
|
+
current_manual.append({"object": "worktree", "path": rel, "reason": "unreadable"})
|
|
1402
|
+
current_fp.append((rel, "unreadable"))
|
|
1403
|
+
continue
|
|
1404
|
+
current_fp.append((rel, hashlib.sha256(data).hexdigest()))
|
|
1405
|
+
names, manual = _scan_object(rel, data, rules, exceptions)
|
|
1406
|
+
if manual is not None:
|
|
1407
|
+
current_manual.append({"object": "worktree", "path": rel, **manual})
|
|
1408
|
+
elif _path_suffix(rel) in OFFICE_ZIP_SUFFIXES:
|
|
1409
|
+
zip_n += 1
|
|
1410
|
+
else:
|
|
1411
|
+
text_n += 1
|
|
1412
|
+
for name in sorted(names):
|
|
1413
|
+
current_hits.append({"object": "worktree", "path": rel, "pattern": name})
|
|
1414
|
+
# 历史面:全待公开 refs 的 reachable objects(当前树干净 ≠ 历史干净,D-20)
|
|
1415
|
+
rc, out, _ = sh(["git", "rev-list", "--objects", "--all"], cwd=root, timeout=600)
|
|
1416
|
+
if rc != 0:
|
|
1417
|
+
raise ReleaseError("git rev-list --objects --all 失败(fail-closed)")
|
|
1418
|
+
blob_path: dict[str, str] = {}
|
|
1419
|
+
for line in out.splitlines():
|
|
1420
|
+
line = line.strip()
|
|
1421
|
+
if not line:
|
|
1422
|
+
continue
|
|
1423
|
+
sha, _, path = line.partition(" ")
|
|
1424
|
+
if not path:
|
|
1425
|
+
continue # commit/tag 无路径;tree 由 cat-file 类型过滤兜底
|
|
1426
|
+
blob_path.setdefault(sha, path) # 同 blob 多路径取首个(rev-list 顺序确定性)
|
|
1427
|
+
hist_hits: list[dict] = []
|
|
1428
|
+
hist_manual: list[dict] = []
|
|
1429
|
+
hist_fp: list[tuple[str, str]] = [] # (blob sha, path)——git 内容指纹面
|
|
1430
|
+
h_text = h_zip = 0
|
|
1431
|
+
seen: set[str] = set()
|
|
1432
|
+
for sha, data in _iter_git_blobs(root, sorted(blob_path)):
|
|
1433
|
+
if sha in seen:
|
|
1434
|
+
continue
|
|
1435
|
+
seen.add(sha)
|
|
1436
|
+
rel = blob_path[sha]
|
|
1437
|
+
hist_fp.append((sha, rel))
|
|
1438
|
+
names, manual = _scan_object(rel, data, rules, exceptions)
|
|
1439
|
+
if manual is not None:
|
|
1440
|
+
hist_manual.append({"object": sha, "path": rel, **manual})
|
|
1441
|
+
elif _path_suffix(rel) in OFFICE_ZIP_SUFFIXES:
|
|
1442
|
+
h_zip += 1
|
|
1443
|
+
else:
|
|
1444
|
+
h_text += 1
|
|
1445
|
+
for name in sorted(names):
|
|
1446
|
+
hist_hits.append({"object": sha, "path": rel, "pattern": name})
|
|
1447
|
+
# digest = HEAD + 双面对象内容指纹 + 命中/人工清单的确定性指纹:任何内容漂移(含
|
|
1448
|
+
# 未提交工作树改动、无新增命中的内容变化)都改变 digest——过期证明可判。
|
|
1449
|
+
digest_payload = {
|
|
1450
|
+
"head": head,
|
|
1451
|
+
"current": {"objects": sorted(current_fp),
|
|
1452
|
+
"hits": sorted((h["pattern"], h["object"], h["path"])
|
|
1453
|
+
for h in current_hits),
|
|
1454
|
+
"manual": sorted((m["object"], m["path"], m["reason"])
|
|
1455
|
+
for m in current_manual)},
|
|
1456
|
+
"history": {"objects": sorted(hist_fp),
|
|
1457
|
+
"hits": sorted((h["pattern"], h["object"], h["path"])
|
|
1458
|
+
for h in hist_hits),
|
|
1459
|
+
"manual": sorted((m["object"], m["path"], m["reason"])
|
|
1460
|
+
for m in hist_manual)},
|
|
1461
|
+
}
|
|
1462
|
+
digest = hashlib.sha256(json.dumps(digest_payload, sort_keys=True, ensure_ascii=False,
|
|
1463
|
+
separators=(",", ":")).encode("utf-8")).hexdigest()
|
|
1464
|
+
return {
|
|
1465
|
+
"schema": "public-readiness-scan/1",
|
|
1466
|
+
"generated": _now_iso(),
|
|
1467
|
+
"head": head,
|
|
1468
|
+
"blacklist_source": "packaging/audit-blacklist.yaml",
|
|
1469
|
+
"current": {"objects": len(files), "text_scanned": text_n, "zip_xml_scanned": zip_n,
|
|
1470
|
+
"hits": current_hits, "manual_review_required": current_manual},
|
|
1471
|
+
"history": {"objects": len(seen), "text_scanned": h_text, "zip_xml_scanned": h_zip,
|
|
1472
|
+
"hits": hist_hits, "manual_review_required": hist_manual},
|
|
1473
|
+
"counts": {"current_hits": len(current_hits), "history_hits": len(hist_hits),
|
|
1474
|
+
"manual_review_required": len(current_manual) + len(hist_manual)},
|
|
1475
|
+
"digest": digest,
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
|
|
1479
|
+
def scan_public_history(repo: Path, evidence: Path) -> dict:
|
|
1480
|
+
"""当前树 + 全待公开 refs reachable objects 值驱动扫描(D-20,只读零副作用)。
|
|
1481
|
+
|
|
1482
|
+
复用 release_audit.load_blacklist 的 patterns/generic/documented_exceptions 单一
|
|
1483
|
+
数据源;命中只记 pattern/object/path,人工复核逐对象列 reason;证据原子写
|
|
1484
|
+
<evidence>/scan.json(runs/ 运行证据,非 tracked——证明本体只写 runs,避免 tag 后脏树)。
|
|
1485
|
+
"""
|
|
1486
|
+
result = _scan_core(repo)
|
|
1487
|
+
write_state(Path(evidence), result, "scan.json")
|
|
1488
|
+
return result
|
|
1489
|
+
|
|
1490
|
+
|
|
1491
|
+
_SCHEMA_TYPES = {"object": dict, "array": list, "string": str, "boolean": bool, "integer": int}
|
|
1492
|
+
|
|
1493
|
+
|
|
1494
|
+
def _schema_fail(path: str, why: str) -> None:
|
|
1495
|
+
raise ReleaseError(f"public-readiness 证明不符合 schema({path}: {why})——"
|
|
1496
|
+
"fail-closed,不消费不自符 schema 的证明")
|
|
1497
|
+
|
|
1498
|
+
|
|
1499
|
+
def _validate_json_schema(data, schema: dict, path: str = "$") -> None:
|
|
1500
|
+
"""受限 JSON Schema 子集校验(type/required/properties/additionalProperties/items/
|
|
1501
|
+
enum/const/pattern/minLength/minimum/maximum)——stdlib 实现,零新依赖。"""
|
|
1502
|
+
if "const" in schema:
|
|
1503
|
+
if data != schema["const"]:
|
|
1504
|
+
_schema_fail(path, "const 不符")
|
|
1505
|
+
return
|
|
1506
|
+
if "enum" in schema and data not in schema["enum"]:
|
|
1507
|
+
_schema_fail(path, "enum 外取值")
|
|
1508
|
+
t = schema.get("type")
|
|
1509
|
+
if t is not None:
|
|
1510
|
+
py = _SCHEMA_TYPES.get(t)
|
|
1511
|
+
# bool 是 int 子类:integer 不收 bool;boolean 要求精确 bool
|
|
1512
|
+
if py is None or not isinstance(data, py) \
|
|
1513
|
+
or (t == "integer" and isinstance(data, bool)) \
|
|
1514
|
+
or (t == "boolean" and not isinstance(data, bool)):
|
|
1515
|
+
_schema_fail(path, f"类型应为 {t}")
|
|
1516
|
+
if isinstance(data, str):
|
|
1517
|
+
if "pattern" in schema and re.search(schema["pattern"], data) is None:
|
|
1518
|
+
_schema_fail(path, "pattern 不符")
|
|
1519
|
+
if "minLength" in schema and len(data) < schema["minLength"]:
|
|
1520
|
+
_schema_fail(path, "minLength 不足")
|
|
1521
|
+
if isinstance(data, int) and not isinstance(data, bool):
|
|
1522
|
+
if "minimum" in schema and data < schema["minimum"]:
|
|
1523
|
+
_schema_fail(path, "minimum 不足")
|
|
1524
|
+
if "maximum" in schema and data > schema["maximum"]:
|
|
1525
|
+
_schema_fail(path, "maximum 超出")
|
|
1526
|
+
if isinstance(data, dict):
|
|
1527
|
+
for key in schema.get("required", []):
|
|
1528
|
+
if key not in data:
|
|
1529
|
+
_schema_fail(path, f"缺必填键 {key}")
|
|
1530
|
+
props = schema.get("properties", {})
|
|
1531
|
+
if schema.get("additionalProperties") is False:
|
|
1532
|
+
extra = sorted(set(data) - set(props))
|
|
1533
|
+
if extra:
|
|
1534
|
+
_schema_fail(path, f"白名单外键: {extra}")
|
|
1535
|
+
for key, sub in props.items():
|
|
1536
|
+
if key in data:
|
|
1537
|
+
_validate_json_schema(data[key], sub, f"{path}.{key}")
|
|
1538
|
+
if isinstance(data, list) and schema.get("items"):
|
|
1539
|
+
for i, el in enumerate(data):
|
|
1540
|
+
_validate_json_schema(el, schema["items"], f"{path}[{i}]")
|
|
1541
|
+
|
|
1542
|
+
|
|
1543
|
+
def verify_public_readiness(repo: Path, tag: str, attestation: Path,
|
|
1544
|
+
transport=None) -> dict:
|
|
1545
|
+
"""D-20 公开历史证明实时对账(重新执行扫描,不信任文档自报):
|
|
1546
|
+
|
|
1547
|
+
tracked schema 校验 → public_ready/inventory 供给 → tag/version/head 绑定 → 实时
|
|
1548
|
+
重扫并对账 digest / 命中处置 / 二进制人工复核 → 匿名公开面(匿名 git smart-HTTP
|
|
1549
|
+
advertisement 含目标 tag 精确 SHA、远端精确 tag peeled SHA=HEAD、source/raw 目标
|
|
1550
|
+
内容——严格区分目标内容与通用 SPA/登录页,A1:Release 创建前只查占用与 URL 模板,
|
|
1551
|
+
不 GET Release 页)。匿名 transport 只记录布尔结果、稳定 URL(去 query)和状态码。
|
|
1552
|
+
任一不符 fail-closed;完全匹配才放行。
|
|
1553
|
+
"""
|
|
1554
|
+
root = Path(repo).resolve()
|
|
1555
|
+
p = Path(attestation)
|
|
1556
|
+
if not p.is_file():
|
|
1557
|
+
raise ReleaseError(
|
|
1558
|
+
"缺 public-readiness.json(D-20 公开历史门)——公开仓当前树/历史敏感值清查与"
|
|
1559
|
+
"公开化完成前,不得上传公开 PyPI(由 05-06/05-07 签发后逐字节供给本 runs 根)")
|
|
1560
|
+
try:
|
|
1561
|
+
data = json.loads(p.read_text(encoding="utf-8"))
|
|
1562
|
+
except (OSError, json.JSONDecodeError) as e:
|
|
1563
|
+
raise ReleaseError(
|
|
1564
|
+
f"public-readiness.json 不可读(fail-closed): {type(e).__name__}") from None
|
|
1565
|
+
try:
|
|
1566
|
+
schema = json.loads((root / PUBLIC_READINESS_SCHEMA).read_text(encoding="utf-8"))
|
|
1567
|
+
except (OSError, json.JSONDecodeError) as e:
|
|
1568
|
+
raise ReleaseError(
|
|
1569
|
+
f"public-readiness schema 不可用(fail-closed): {type(e).__name__}") from None
|
|
1570
|
+
_validate_json_schema(data, schema, "$")
|
|
1571
|
+
if data["public_ready"] is not True:
|
|
1572
|
+
raise ReleaseError("public-readiness.json 未声明 public_ready=true(D-20 公开历史门未满足)")
|
|
1573
|
+
if data["release_inventory_supplied"] is not True:
|
|
1574
|
+
raise ReleaseError("public-readiness.json 未声明 release_inventory_supplied=true——"
|
|
1575
|
+
".release-worktree 的 gitignored inventory 保密供给未完成(05-07 交接门)")
|
|
1576
|
+
if data["tag"] != tag:
|
|
1577
|
+
raise ReleaseError(f"public-readiness 证明 tag 过期/不符: {data['tag']!r} != {tag!r}")
|
|
1578
|
+
if data["version"] != tag[1:]:
|
|
1579
|
+
raise ReleaseError(f"public-readiness 证明 version 与 tag 不一致: {data['version']!r}")
|
|
1580
|
+
rc, out, _ = sh(["git", "rev-parse", "HEAD"], cwd=root, timeout=60)
|
|
1581
|
+
if rc != 0:
|
|
1582
|
+
raise ReleaseError("无法读取 HEAD(fail-closed)")
|
|
1583
|
+
head = out.strip()
|
|
1584
|
+
if data["head"] != head:
|
|
1585
|
+
raise ReleaseError("public-readiness 证明 head 过期(证明签发后 HEAD 漂移)——重新签发")
|
|
1586
|
+
# 实时重扫优先于自报:digest / 命中处置 / 二进制复核逐项对账
|
|
1587
|
+
rescan = _scan_core(root)
|
|
1588
|
+
if rescan["digest"] != data["scan_digest"]:
|
|
1589
|
+
raise ReleaseError("公开历史实时重扫 digest 与证明不符(树/历史漂移或伪造证明)——"
|
|
1590
|
+
"重新扫描处置并签发")
|
|
1591
|
+
covered = {(d.get("pattern"), d.get("path")) for d in data["hit_disposition"]}
|
|
1592
|
+
for scope in ("current", "history"):
|
|
1593
|
+
uncovered = [h for h in rescan[scope]["hits"]
|
|
1594
|
+
if (h["pattern"], h["path"]) not in covered]
|
|
1595
|
+
if uncovered:
|
|
1596
|
+
sample = sorted({h["path"] for h in uncovered})[:3]
|
|
1597
|
+
raise ReleaseError(
|
|
1598
|
+
f"公开历史扫描 {scope} 面有 {len(uncovered)} 处命中未在证明中处置"
|
|
1599
|
+
f"(如 {sample};pattern 名见 scan 证据)——处置或重写后重新签发(D-20)")
|
|
1600
|
+
reviewed = {(m.get("object"), m.get("path")) for m in data["binary_review"]["objects"]}
|
|
1601
|
+
for scope in ("current", "history"):
|
|
1602
|
+
unreviewed = [m for m in rescan[scope]["manual_review_required"]
|
|
1603
|
+
if (m["object"], m["path"]) not in reviewed]
|
|
1604
|
+
if unreviewed:
|
|
1605
|
+
sample = sorted({m["path"] for m in unreviewed})[:3]
|
|
1606
|
+
raise ReleaseError(
|
|
1607
|
+
f"公开历史 {scope} 面有 {len(unreviewed)} 个二进制/不可判对象缺人工复核"
|
|
1608
|
+
f"(如 {sample})——缺证据即 FAIL,不能静默豁免(A6/T-5-17)")
|
|
1609
|
+
if data["binary_review"]["complete"] is not True:
|
|
1610
|
+
raise ReleaseError("binary_review.complete 非 true(人工复核未声明完成,A6)")
|
|
1611
|
+
if data["repo_public"] is not True:
|
|
1612
|
+
raise ReleaseError("证明 repo_public 非 true(签发时仓未公开)")
|
|
1613
|
+
if data["remote_tag_sha"] != head:
|
|
1614
|
+
raise ReleaseError("证明 remote_tag_sha 与当前 HEAD 不符(远端 tag 绑定过期)")
|
|
1615
|
+
if data["source_check"]["target_content"] is not True:
|
|
1616
|
+
raise ReleaseError("证明 source_check.target_content 非 true(签发时未证目标内容)")
|
|
1617
|
+
if data["raw_check"]["bytes_match"] is not True:
|
|
1618
|
+
raise ReleaseError("证明 raw_check.bytes_match 非 true(签发时未证字节一致)")
|
|
1619
|
+
|
|
1620
|
+
def _fetch(url: str, max_bytes: int = HTML_MAX_BYTES) -> tuple[int, dict, bytes]:
|
|
1621
|
+
try:
|
|
1622
|
+
if isinstance(transport, BoundedTransport):
|
|
1623
|
+
# 真实有界传输(2026-11-19 worktree 探针首次实跑暴露):__call__ 收
|
|
1624
|
+
# Request、返 BoundedResponse——统一解包 (status, headers, body),
|
|
1625
|
+
# 4xx/5xx 经 BoundedResponse(status) 返回(不抛 HTTPError)。
|
|
1626
|
+
r = transport(urllib.request.Request(url, method="GET"),
|
|
1627
|
+
max_bytes=max_bytes)
|
|
1628
|
+
return r.status, dict(r.headers or {}), r.body
|
|
1629
|
+
fn = transport if transport is not None else http_get
|
|
1630
|
+
return fn(url, max_bytes=max_bytes)
|
|
1631
|
+
except ReleaseError:
|
|
1632
|
+
raise
|
|
1633
|
+
except Exception as e:
|
|
1634
|
+
raise ReleaseError(
|
|
1635
|
+
f"公开面核验请求失败(host {_safe_host(url)}): {type(e).__name__}") from None
|
|
1636
|
+
|
|
1637
|
+
meta = project_meta(root)
|
|
1638
|
+
owner, repo_name = meta["gitcode_owner"], meta["gitcode_repo"]
|
|
1639
|
+
# 远端精确 tag peeled SHA 对账(公开源码 tag 与本地 HEAD 同一提交)
|
|
1640
|
+
rc, out, _ = sh(["git", "ls-remote", "--tags", "origin"], cwd=root, timeout=120)
|
|
1641
|
+
if rc != 0:
|
|
1642
|
+
raise ReleaseError("git ls-remote --tags origin 失败——公开历史门无法对账远端 tag")
|
|
1643
|
+
remote: dict[str, str] = {}
|
|
1644
|
+
for line in out.strip().splitlines():
|
|
1645
|
+
sha, _, ref = line.partition("\t")
|
|
1646
|
+
ref = ref.strip()
|
|
1647
|
+
if ref.startswith("refs/tags/"):
|
|
1648
|
+
remote[ref[len("refs/tags/"):]] = sha.strip()
|
|
1649
|
+
peeled = remote.get(f"{tag}^{{}}") or remote.get(tag)
|
|
1650
|
+
if peeled != head:
|
|
1651
|
+
raise ReleaseError(f"远端 tag {tag}(peeled)与本地 HEAD 不一致——公开源码 tag 须先推送对齐")
|
|
1652
|
+
# 匿名 git smart-HTTP 公开面:advertisement 可达且 refs 含目标 tag 的精确 SHA。
|
|
1653
|
+
# GitCode v5 repo 元数据端点对匿名一律 401(2026-11-19 实测,公开对照仓同拒),
|
|
1654
|
+
# 以等强证据链替代元数据旗标:私有仓 401、登录页/通用 SPA 均无 pkt-line 主体
|
|
1655
|
+
# (A1:空 200 不是证据);仓可见性旗标(认证可见 ≠ 公开)由 preflight_remote
|
|
1656
|
+
# 的 tokened repo_info private=false 覆盖。
|
|
1657
|
+
anon_url = (f"https://gitcode.com/{owner}/{repo_name}.git/info/refs"
|
|
1658
|
+
"?service=git-upload-pack")
|
|
1659
|
+
a_status, _ah, a_body = _fetch(anon_url, max_bytes=JSON_MAX_BYTES)
|
|
1660
|
+
if a_status != 200 or b"# service=git-upload-pack" not in a_body[:64]:
|
|
1661
|
+
raise ReleaseError(f"匿名 git smart-HTTP 公开面不可达: HTTP {a_status}"
|
|
1662
|
+
"(host gitcode.com,登录页/私有仓均拒绝)——D-20 公开前置未满足")
|
|
1663
|
+
peeled_line = f"{head} refs/tags/{tag}^{{}}\n".encode("ascii")
|
|
1664
|
+
plain_line = f"{head} refs/tags/{tag}\n".encode("ascii")
|
|
1665
|
+
if peeled_line not in a_body and plain_line not in a_body:
|
|
1666
|
+
raise ReleaseError(f"匿名 smart-HTTP refs 未含 {tag} 的精确 SHA {head[:12]}——"
|
|
1667
|
+
"公开 tag 未对齐(D-20 公开前置未满足)")
|
|
1668
|
+
# source 页匿名目标内容(A1:严格区分目标内容与通用 SPA/登录页,空 200 不是证据)
|
|
1669
|
+
source_url = f"https://gitcode.com/{owner}/{repo_name}/tree/{tag}"
|
|
1670
|
+
try:
|
|
1671
|
+
s_status, _h, s_body = _fetch(source_url)
|
|
1672
|
+
except ReleaseError as ex:
|
|
1673
|
+
raise ReleaseError(f"source 页匿名读取失败: {ex}") from None
|
|
1674
|
+
if s_status != 200 or tag not in s_body.decode("utf-8", errors="replace"):
|
|
1675
|
+
raise ReleaseError(
|
|
1676
|
+
f"source 页匿名目标内容不可证: HTTP {s_status} 且页面不含 tag"
|
|
1677
|
+
"(疑似登录页/通用 SPA——A1 严格区分目标内容)")
|
|
1678
|
+
# raw 匿名字节 == 本地跟踪内容(登录页/SPA/漂移字节均拒绝)
|
|
1679
|
+
raw_rel = "pyproject.toml"
|
|
1680
|
+
raw_url = f"https://gitcode.com/api/v5/repos/{owner}/{repo_name}/raw/{raw_rel}?ref={tag}"
|
|
1681
|
+
local = root / raw_rel
|
|
1682
|
+
try:
|
|
1683
|
+
r_status, _h, r_body = _fetch(raw_url, max_bytes=local.stat().st_size + 1)
|
|
1684
|
+
except ReleaseError as ex:
|
|
1685
|
+
raise ReleaseError(f"raw 匿名读取失败(超限/登录页均拒绝): {ex}") from None
|
|
1686
|
+
if r_status != 200 or r_body != local.read_bytes():
|
|
1687
|
+
raise ReleaseError(f"raw 匿名字节与本地跟踪内容不符: HTTP {r_status}(目标内容不可证)")
|
|
1688
|
+
# 全部通过:落实时重扫证据(runs/ 运行证据;失败路径不写半份),返回脱敏布尔/URL/状态码
|
|
1689
|
+
ev = root / "runs" / ("public-readiness-" + datetime.now().strftime("%Y%m%dT%H%M%S"))
|
|
1690
|
+
try:
|
|
1691
|
+
write_state(ev, rescan, "scan.json")
|
|
1692
|
+
except OSError:
|
|
1693
|
+
pass # 证据落盘失败不推翻已通过对账(正文已在证明对账中消费)
|
|
1694
|
+
return {"public_ready": True, "attestation": str(p), "checks": {
|
|
1695
|
+
"schema": True,
|
|
1696
|
+
"head_tag_binding": True,
|
|
1697
|
+
"scan_digest": True,
|
|
1698
|
+
"hit_disposition": {"current_covered": len(rescan["current"]["hits"]),
|
|
1699
|
+
"history_covered": len(rescan["history"]["hits"])},
|
|
1700
|
+
"binary_review": {"covered": rescan["counts"]["manual_review_required"]},
|
|
1701
|
+
"repo_public": {"public": True, "status_code": a_status,
|
|
1702
|
+
"url": anon_url.split("?")[0], "tag_sha_matches": True},
|
|
1703
|
+
"remote_tag": {"peeled_sha_matches": True},
|
|
1704
|
+
"source_check": {"status_code": s_status, "url": source_url,
|
|
1705
|
+
"target_content": True},
|
|
1706
|
+
"raw_check": {"status_code": r_status, "url": raw_url.split("?")[0],
|
|
1707
|
+
"bytes_match": True},
|
|
1708
|
+
}}
|
|
1709
|
+
|
|
1710
|
+
|
|
1711
|
+
# ---------------------------------------------------------------------------
|
|
1712
|
+
# 操作证据(logs/operations.jsonl 白名单事件)与 test receipt
|
|
1713
|
+
# ---------------------------------------------------------------------------
|
|
1714
|
+
|
|
1715
|
+
OPERATION_EVENTS = ("preflight_occupied", "twine_upload_started", "upload_verified",
|
|
1716
|
+
"gitcode_release_started")
|
|
1717
|
+
|
|
1718
|
+
|
|
1719
|
+
def log_operation(run_dir: Path, event: str, **fields) -> None:
|
|
1720
|
+
"""追加脱敏白名单事件(区分本地上传前门与服务器拒绝;不含 token/完整带凭据 URL/env/
|
|
1721
|
+
原始 Twine 输出——字段白名单由调用方约束,事件名由本函数 fail-closed 校验)。"""
|
|
1722
|
+
if event not in OPERATION_EVENTS:
|
|
1723
|
+
raise ReleaseError(f"非白名单操作事件: {event}(允许: {', '.join(OPERATION_EVENTS)})")
|
|
1724
|
+
run_dir = Path(run_dir)
|
|
1725
|
+
(run_dir / "logs").mkdir(parents=True, exist_ok=True)
|
|
1726
|
+
rec = {"event": event, "ts": _now_iso(), **fields}
|
|
1727
|
+
with open(run_dir / "logs" / "operations.jsonl", "a", encoding="utf-8") as f:
|
|
1728
|
+
f.write(json.dumps(rec, ensure_ascii=False) + "\n")
|
|
1729
|
+
|
|
1730
|
+
|
|
1731
|
+
def write_test_receipt(run_dir: Path, state: ReleaseState) -> Path:
|
|
1732
|
+
"""原子写 test receipt(脱敏):身份五元组 + TestPyPI verified 证据。
|
|
1733
|
+
|
|
1734
|
+
pypi/gitcode 只记 status=pending(不被写成已发布);不含 token/签名 URL(D-08)。
|
|
1735
|
+
"""
|
|
1736
|
+
payload = {
|
|
1737
|
+
**state.identity(),
|
|
1738
|
+
"target": state.target,
|
|
1739
|
+
"surfaces": {
|
|
1740
|
+
"testpypi": dict(state.surface("testpypi")),
|
|
1741
|
+
"pypi": {"status": state.surface("pypi").get("status", "pending")},
|
|
1742
|
+
"gitcode": {"status": state.surface("gitcode").get("status", "pending")},
|
|
1743
|
+
},
|
|
1744
|
+
"generated": _now_iso(),
|
|
1745
|
+
}
|
|
1746
|
+
return write_state(run_dir, payload, "receipt.json")
|
|
1747
|
+
|
|
1748
|
+
|
|
1749
|
+
# ---------------------------------------------------------------------------
|
|
1750
|
+
# release test:TestPyPI 演练(D-01/D-06/D-07/D-16 + A1 写入门)
|
|
1751
|
+
# ---------------------------------------------------------------------------
|
|
1752
|
+
|
|
1753
|
+
def publish_test(repo: Path, state: ReleaseState, meta: dict, env: dict | None = None,
|
|
1754
|
+
client: PyPIClient | None = None, outcomes: dict | None = None) -> Path:
|
|
1755
|
+
"""TestPyPI 演练:全部上传前硬门 → twine 单归档 wheel 上传 → 有界 JSON 验证 → test receipt。
|
|
1756
|
+
|
|
1757
|
+
硬门顺序(任一失败都在 twine 子进程之前,零上传副作用):
|
|
1758
|
+
A1=create-then-verify → 精确 tag/干净树实时复检(与归档身份一致)→ TestPyPI 占用
|
|
1759
|
+
(404 才可首发;同版本即使同 SHA256 也冲突,重新演练必须 bump tag)→ twine 凭据。
|
|
1760
|
+
每次远端写之前原子持久化 uploading;结果未知写 failed/uploaded_unverified。
|
|
1761
|
+
"""
|
|
1762
|
+
realm = "testpypi"
|
|
1763
|
+
root = Path(repo).resolve()
|
|
1764
|
+
e = os.environ if env is None else env
|
|
1765
|
+
run_dir = Path(state.run_dir)
|
|
1766
|
+
if outcomes is None:
|
|
1767
|
+
outcomes = load_decision_outcomes(root)
|
|
1768
|
+
assert_publication_permitted(outcomes, surface=realm)
|
|
1769
|
+
checkout = inspect_checkout(root) # 实时硬门:精确 tag/干净树
|
|
1770
|
+
if (checkout["version"], checkout["tag"], checkout["head"]) != \
|
|
1771
|
+
(state.version, state.tag, state.head):
|
|
1772
|
+
raise ReleaseError("checkout 漂移:当前 HEAD/tag/版本与归档身份不符——上传身份必须绑定归档记录")
|
|
1773
|
+
client = client or PyPIClient(index_url=PYPI_REALMS[realm]["json"])
|
|
1774
|
+
occupied, detail = client.version_occupied(meta["name"], state.version)
|
|
1775
|
+
if occupied:
|
|
1776
|
+
log_operation(run_dir, "preflight_occupied", surface=realm, version=state.version)
|
|
1777
|
+
raise ReleaseError(f"{detail}——重新演练必须 bump tag(D-06),"
|
|
1778
|
+
"禁止覆盖/--skip-existing/静默跳过")
|
|
1779
|
+
if str(e.get("TWINE_USERNAME") or "") != "__token__" \
|
|
1780
|
+
or not str(e.get("TWINE_PASSWORD") or "").strip():
|
|
1781
|
+
raise ReleaseError("缺少 twine 凭据(TWINE_USERNAME=__token__ + 非空 TWINE_PASSWORD,"
|
|
1782
|
+
"env-only)——上传前失败,零远端写")
|
|
1783
|
+
state.target = "test"
|
|
1784
|
+
state.last_command = "test"
|
|
1785
|
+
state.set_surface(realm, status="uploading")
|
|
1786
|
+
write_state(run_dir, state.to_payload()) # 写前持久化(崩溃可恢复锚点)
|
|
1787
|
+
log_operation(run_dir, "twine_upload_started", surface=realm, wheel=state.wheel_name)
|
|
1788
|
+
try:
|
|
1789
|
+
client.upload(state, realm, env=e)
|
|
1790
|
+
except ReleaseError as ex:
|
|
1791
|
+
state.set_surface(realm, status="failed", error=str(ex))
|
|
1792
|
+
write_state(run_dir, state.to_payload())
|
|
1793
|
+
raise
|
|
1794
|
+
state.set_surface(realm, status="uploaded_unverified")
|
|
1795
|
+
write_state(run_dir, state.to_payload())
|
|
1796
|
+
try:
|
|
1797
|
+
ev = client.verify(meta["name"], state.version, state.wheel_name, state.wheel_sha256)
|
|
1798
|
+
except ReleasePollTimeout as ex: # 结果未知:保留 uploaded_unverified(可 resume 对账)
|
|
1799
|
+
state.set_surface(realm, status="uploaded_unverified", error=str(ex))
|
|
1800
|
+
write_state(run_dir, state.to_payload())
|
|
1801
|
+
raise
|
|
1802
|
+
except ReleaseError as ex: # 冲突/协议失败:failed(resume 远端对账后分类)
|
|
1803
|
+
state.set_surface(realm, status="failed", error=str(ex))
|
|
1804
|
+
write_state(run_dir, state.to_payload())
|
|
1805
|
+
raise
|
|
1806
|
+
state.set_surface(realm, status="verified", api_url=ev["api_url"],
|
|
1807
|
+
file_url=ev["file_url"], sha256=ev["sha256"],
|
|
1808
|
+
upload_time=ev["upload_time"], verified_at=ev["verified_at"])
|
|
1809
|
+
log_operation(run_dir, "upload_verified", surface=realm, sha256=ev["sha256"])
|
|
1810
|
+
write_state(run_dir, state.to_payload())
|
|
1811
|
+
return write_test_receipt(run_dir, state)
|
|
1812
|
+
|
|
1813
|
+
|
|
1814
|
+
# ---------------------------------------------------------------------------
|
|
1815
|
+
# release all:A2 receipt 门 / 公开门 / 审批消费 / D-13 确认 / PyPI→GitCode 顺序(05-05 Task 2)
|
|
1816
|
+
# ---------------------------------------------------------------------------
|
|
1817
|
+
|
|
1818
|
+
def _load_test_receipt(run_dir: Path) -> dict | None:
|
|
1819
|
+
"""读取既有 receipt.json 作为 A2 门输入;文件不存在 → None;损坏 → fail-closed。"""
|
|
1820
|
+
p = Path(run_dir) / "receipt.json"
|
|
1821
|
+
if not p.is_file():
|
|
1822
|
+
return None
|
|
1823
|
+
try:
|
|
1824
|
+
data = json.loads(p.read_text(encoding="utf-8"))
|
|
1825
|
+
except (OSError, json.JSONDecodeError) as e:
|
|
1826
|
+
raise ReleaseError(f"receipt.json 不可读(fail-closed): {type(e).__name__}") from None
|
|
1827
|
+
if not isinstance(data, dict):
|
|
1828
|
+
raise ReleaseError("receipt.json 非 JSON 对象(fail-closed)")
|
|
1829
|
+
return data
|
|
1830
|
+
|
|
1831
|
+
|
|
1832
|
+
def _assert_receipt_matches(state: ReleaseState, receipt: dict) -> None:
|
|
1833
|
+
"""TestPyPI 回执身份比对:version/tag/wheel SHA256 逐项一致且 testpypi 面 verified。"""
|
|
1834
|
+
wheel = receipt.get("wheel") or {}
|
|
1835
|
+
if receipt.get("version") != state.version or receipt.get("tag") != state.tag \
|
|
1836
|
+
or wheel.get("sha256") != state.wheel_sha256:
|
|
1837
|
+
raise ReleaseError(
|
|
1838
|
+
"TestPyPI 回执与当前归档不符(version/tag/SHA256 不一致)——fail-closed,"
|
|
1839
|
+
"不得沿用:重新演练必须同 tag 同 SHA256(release test,D-16)")
|
|
1840
|
+
tp = (receipt.get("surfaces") or {}).get("testpypi")
|
|
1841
|
+
if not isinstance(tp, dict) or tp.get("status") != "verified" \
|
|
1842
|
+
or tp.get("sha256") != state.wheel_sha256:
|
|
1843
|
+
raise ReleaseError("TestPyPI 回执未 verified 或 SHA256 不符——不得进入正式发布(D-16)")
|
|
1844
|
+
|
|
1845
|
+
|
|
1846
|
+
def assert_testpypi_receipt_gate(state: ReleaseState, outcomes: dict,
|
|
1847
|
+
test_receipt: dict | None) -> dict | None:
|
|
1848
|
+
"""A2 receipt 门(05-04 Decision Outcomes):
|
|
1849
|
+
|
|
1850
|
+
- required-receipt:缺回执/身份不符/未 verified 均在上传前失败(零上传),不能从
|
|
1851
|
+
归档直接 all;
|
|
1852
|
+
- recommended-receipt:无回执可继续(最终回执 testpypi=null),但任何已有记录不匹配
|
|
1853
|
+
即失败;
|
|
1854
|
+
- pending/缺失:无法判定,fail-closed。
|
|
1855
|
+
"""
|
|
1856
|
+
a2 = (outcomes or {}).get("A2")
|
|
1857
|
+
if a2 == "required-receipt":
|
|
1858
|
+
if test_receipt is None:
|
|
1859
|
+
raise ReleaseError(
|
|
1860
|
+
"A2=required-receipt:缺 TestPyPI verified 回执(receipt.json)——不能从归档"
|
|
1861
|
+
"直接 all;先 release test 完成同 tag 同 SHA256 演练(05-08 真实执行)")
|
|
1862
|
+
_assert_receipt_matches(state, test_receipt)
|
|
1863
|
+
return test_receipt
|
|
1864
|
+
if a2 == "recommended-receipt":
|
|
1865
|
+
if test_receipt is not None:
|
|
1866
|
+
_assert_receipt_matches(state, test_receipt)
|
|
1867
|
+
return test_receipt
|
|
1868
|
+
raise ReleaseError(
|
|
1869
|
+
f"A2 决策未选(pending/缺失,当前 {a2!r}):release all 的 TestPyPI receipt 门无法"
|
|
1870
|
+
"判定——05-04 Decision Outcomes 必须选择 required/recommended-receipt")
|
|
1871
|
+
|
|
1872
|
+
|
|
1873
|
+
def _load_public_readiness(run_dir: Path) -> dict:
|
|
1874
|
+
"""D-20 公开历史门:public-readiness.json(05-06/05-07 签发的公开化证明)。
|
|
1875
|
+
|
|
1876
|
+
缺失/不可读/public_ready 非 true → 上传前失败(零上传)。返回 {sha256, public_ready}。
|
|
1877
|
+
"""
|
|
1878
|
+
p = Path(run_dir) / "public-readiness.json"
|
|
1879
|
+
if not p.is_file():
|
|
1880
|
+
raise ReleaseError(
|
|
1881
|
+
"缺 public-readiness.json(D-20 公开历史门)——公开仓当前树/历史敏感值清查与"
|
|
1882
|
+
"公开化完成前,不得上传公开 PyPI(由 05-06/05-07 签发后逐字节供给本 runs 根)")
|
|
1883
|
+
try:
|
|
1884
|
+
data = json.loads(p.read_text(encoding="utf-8"))
|
|
1885
|
+
except (OSError, json.JSONDecodeError) as e:
|
|
1886
|
+
raise ReleaseError(f"public-readiness.json 不可读(fail-closed): {type(e).__name__}") from None
|
|
1887
|
+
if not isinstance(data, dict) or data.get("public_ready") is not True:
|
|
1888
|
+
raise ReleaseError("public-readiness.json 未声明 public_ready=true(D-20 公开历史门未满足)")
|
|
1889
|
+
return {"sha256": sha256_file(p), "public_ready": True}
|
|
1890
|
+
|
|
1891
|
+
|
|
1892
|
+
def _public_link_transport(root: Path, meta: dict):
|
|
1893
|
+
"""release_metadata 匿名 GET 适配层:全部经有界 http_get(30s;raw=对应本地资产
|
|
1894
|
+
size+1、blob/source HTML=8MiB);网络层/超限异常按不可达处理(上层判失败)。"""
|
|
1895
|
+
root = Path(root)
|
|
1896
|
+
raw_re = re.compile(
|
|
1897
|
+
rf"^https://gitcode\.com/api/v5/repos/{re.escape(meta['gitcode_owner'])}/"
|
|
1898
|
+
rf"{re.escape(meta['gitcode_repo'])}/raw/([^?]+)\?ref=([^&\s]+)$")
|
|
1899
|
+
|
|
1900
|
+
def transport(url: str):
|
|
1901
|
+
m = raw_re.match(url)
|
|
1902
|
+
if m is not None:
|
|
1903
|
+
local = root / urllib.parse.unquote(m.group(1))
|
|
1904
|
+
max_bytes = local.stat().st_size + 1 if local.is_file() else HTML_MAX_BYTES
|
|
1905
|
+
else:
|
|
1906
|
+
max_bytes = HTML_MAX_BYTES
|
|
1907
|
+
status, headers, body = http_get(url, max_bytes=max_bytes)
|
|
1908
|
+
return status, (headers or {}).get("Content-Type", ""), body
|
|
1909
|
+
|
|
1910
|
+
return transport
|
|
1911
|
+
|
|
1912
|
+
|
|
1913
|
+
def _assert_public_metadata(root: Path, state: ReleaseState, meta: dict) -> None:
|
|
1914
|
+
"""公开元数据/版本化链接门(D-18/D-19/T-5-05):CHANGELOG/pyproject 契约 + 匿名资产
|
|
1915
|
+
链接校验(A1:Release 页创建前只验模板不 GET)。"""
|
|
1916
|
+
summary = release_metadata.validate_release_metadata(root, state.tag)
|
|
1917
|
+
if summary.get("version") != state.version:
|
|
1918
|
+
raise ReleaseError("validate_release_metadata 版本与归档不符(fail-closed)")
|
|
1919
|
+
entries = release_metadata.validate_public_asset_links(
|
|
1920
|
+
root, state.tag, transport=_public_link_transport(root, meta))
|
|
1921
|
+
failed = [e for e in entries if not e.get("ok")]
|
|
1922
|
+
if failed:
|
|
1923
|
+
detail = "; ".join(f"{e.get('url')}: {e.get('reason')}" for e in failed[:5])
|
|
1924
|
+
raise ReleaseError(
|
|
1925
|
+
f"公开资产链接校验失败 {len(failed)} 项(fail-closed): {detail[:500]}")
|
|
1926
|
+
|
|
1927
|
+
|
|
1928
|
+
APPROVAL_TOP_KEYS = frozenset(
|
|
1929
|
+
{"approved", "stale", "approver", "approved_at", "version", "tag", "head",
|
|
1930
|
+
"wheel_sha256", "targets", "a1", "a2", "public_readiness_sha256",
|
|
1931
|
+
"testpypi_receipt"})
|
|
1932
|
+
|
|
1933
|
+
|
|
1934
|
+
def _approval_stale(state: ReleaseState, reason: str):
|
|
1935
|
+
"""原子标记 approval_stale=true 并在上传前抛错(--yes 不能绕过;05-09 需重新审批)。"""
|
|
1936
|
+
state.approval_stale = True
|
|
1937
|
+
write_state(state.run_dir, state.to_payload())
|
|
1938
|
+
raise ReleaseError(
|
|
1939
|
+
f"approval_stale: {reason}——审批绑定与当前发布记录不一致,零上传;"
|
|
1940
|
+
"--yes 不能绕过 malformed/stale 审批,需重新审批(05-09)")
|
|
1941
|
+
|
|
1942
|
+
|
|
1943
|
+
def validate_approval(approval_path, state: ReleaseState, decisions: dict,
|
|
1944
|
+
readiness: dict | None, test_receipt: dict | None) -> dict:
|
|
1945
|
+
"""严格消费 runs/release-<version>/approval.json(白名单 schema + 逐字段绑定)。
|
|
1946
|
+
|
|
1947
|
+
校验布尔批准/失效字段、批准人/时间、version/tag/head/wheel_sha256、两个 targets、
|
|
1948
|
+
a1/a2、public_readiness_sha256、testpypi_receipt(recommended 无演练可为 null)。
|
|
1949
|
+
occupancy 不参与判定:本记录已上传/verified 的同 hash 面是合法恢复进度,不判 stale;
|
|
1950
|
+
只有本记录之外的内容(不同 version/tag/head/hash/决策/readiness/receipt)才失效。
|
|
1951
|
+
返回脱敏确认绑定;任何缺字段/类型错/drift → _approval_stale(fail-closed)。
|
|
1952
|
+
"""
|
|
1953
|
+
try:
|
|
1954
|
+
data = json.loads(Path(approval_path).read_text(encoding="utf-8"))
|
|
1955
|
+
except (OSError, json.JSONDecodeError) as e:
|
|
1956
|
+
_approval_stale(state, f"approval.json 不可读/非 JSON: {type(e).__name__}")
|
|
1957
|
+
if not isinstance(data, dict):
|
|
1958
|
+
_approval_stale(state, f"approval.json 非 JSON 对象: {type(data).__name__}")
|
|
1959
|
+
if set(data) != APPROVAL_TOP_KEYS:
|
|
1960
|
+
extra_missing = sorted(set(data) ^ APPROVAL_TOP_KEYS) if isinstance(data, dict) else []
|
|
1961
|
+
_approval_stale(state, f"顶层键必须恰为白名单(多余/缺失均 stale): {extra_missing}")
|
|
1962
|
+
if data.get("approved") is not True:
|
|
1963
|
+
_approval_stale(state, f"approved 必须为 true: {data.get('approved')!r}")
|
|
1964
|
+
if data.get("stale") is not False:
|
|
1965
|
+
_approval_stale(state, f"stale 必须为 false: {data.get('stale')!r}")
|
|
1966
|
+
for key in ("approver", "approved_at", "version", "tag", "head", "wheel_sha256",
|
|
1967
|
+
"public_readiness_sha256"):
|
|
1968
|
+
if not isinstance(data.get(key), str) or not data.get(key):
|
|
1969
|
+
_approval_stale(state, f"{key} 必须为非空字符串: {data.get(key)!r}")
|
|
1970
|
+
if data["version"] != state.version:
|
|
1971
|
+
_approval_stale(state, f"version 绑定不符: {data['version']!r} != {state.version!r}")
|
|
1972
|
+
if data["tag"] != state.tag:
|
|
1973
|
+
_approval_stale(state, f"tag 绑定不符: {data['tag']!r} != {state.tag!r}")
|
|
1974
|
+
if data["head"] != state.head:
|
|
1975
|
+
_approval_stale(state, "head 绑定不符(审批后 HEAD 漂移)")
|
|
1976
|
+
if data["wheel_sha256"] != state.wheel_sha256:
|
|
1977
|
+
_approval_stale(state, "wheel_sha256 绑定不符(归档字节与审批时不一致)")
|
|
1978
|
+
targets = data.get("targets")
|
|
1979
|
+
if not isinstance(targets, list) or sorted(str(t) for t in targets) != ["gitcode", "pypi"]:
|
|
1980
|
+
_approval_stale(state, f"targets 必须为 ['pypi','gitcode']: {targets!r}")
|
|
1981
|
+
a1, a2 = (decisions or {}).get("A1"), (decisions or {}).get("A2")
|
|
1982
|
+
if data.get("a1") != a1:
|
|
1983
|
+
_approval_stale(state, f"a1 绑定不符: {data.get('a1')!r} != {a1!r}")
|
|
1984
|
+
if data.get("a2") != a2:
|
|
1985
|
+
_approval_stale(state, f"a2 绑定不符: {data.get('a2')!r} != {a2!r}")
|
|
1986
|
+
ready_sha = (readiness or {}).get("sha256")
|
|
1987
|
+
if data["public_readiness_sha256"] != ready_sha:
|
|
1988
|
+
_approval_stale(state, "public_readiness_sha256 与当前 public-readiness.json 字节不符")
|
|
1989
|
+
tp = data.get("testpypi_receipt")
|
|
1990
|
+
if test_receipt is None:
|
|
1991
|
+
if tp is not None:
|
|
1992
|
+
_approval_stale(state, f"testpypi_receipt 应为 null(当前无 TestPyPI 演练回执): {tp!r}")
|
|
1993
|
+
elif not isinstance(tp, dict) or tp.get("version") != state.version \
|
|
1994
|
+
or tp.get("sha256") != state.wheel_sha256:
|
|
1995
|
+
_approval_stale(state, "testpypi_receipt 与当前回执不符(version/SHA256 漂移)")
|
|
1996
|
+
return {"approver": data["approver"], "approved_at": data["approved_at"],
|
|
1997
|
+
"targets": ["pypi", "gitcode"], "a1": data["a1"], "a2": data["a2"]}
|
|
1998
|
+
|
|
1999
|
+
|
|
2000
|
+
def confirm_release(state: ReleaseState, *, yes: bool = False, isatty=None,
|
|
2001
|
+
prompt=None) -> dict:
|
|
2002
|
+
"""D-13 确认门(全部预检/审批门之后、任何远端写之前)。
|
|
2003
|
+
|
|
2004
|
+
交互终端 → 显式输入 yes 确认;非交互环境必须显式 --yes,否则停在上传前(零上传)。
|
|
2005
|
+
返回确认绑定 {mode: interactive|flag, confirmed_at}(不含任何凭据)。
|
|
2006
|
+
"""
|
|
2007
|
+
if yes:
|
|
2008
|
+
return {"mode": "flag", "confirmed_at": _now_iso()}
|
|
2009
|
+
_isatty = sys.stdin.isatty if isatty is None else isatty
|
|
2010
|
+
if not _isatty():
|
|
2011
|
+
raise ReleaseError(
|
|
2012
|
+
"非交互环境且未显式 --yes:release all 停在上传前(D-13)——核对上方目标后"
|
|
2013
|
+
"加 --yes 重跑(交互终端则直接确认)")
|
|
2014
|
+
ask = input if prompt is None else prompt
|
|
2015
|
+
answer = ask(f"确认将 {state.wheel_name}(SHA256 {state.wheel_sha256})发布到"
|
|
2016
|
+
f" 公开 PyPI 与 GitCode Release(tag {state.tag})?输入 yes 继续: ")
|
|
2017
|
+
if str(answer).strip().lower() != "yes":
|
|
2018
|
+
raise ReleaseError(f"确认被拒绝({answer!r})——零上传,发布中止")
|
|
2019
|
+
return {"mode": "interactive", "confirmed_at": _now_iso()}
|
|
2020
|
+
|
|
2021
|
+
|
|
2022
|
+
def _merge_prior_surfaces(state: ReleaseState) -> None:
|
|
2023
|
+
"""累计证据合并:同身份既有 state.json 的非 pending 面并入当前 state(test→all/resume
|
|
2024
|
+
不抹 TestPyPI verified evidence);不同身份记录不动(普通 all 的占用冲突另行处理)。"""
|
|
2025
|
+
prev = Path(state.run_dir) / "state.json"
|
|
2026
|
+
if not prev.is_file():
|
|
2027
|
+
return
|
|
2028
|
+
try:
|
|
2029
|
+
old = json.loads(prev.read_text(encoding="utf-8"))
|
|
2030
|
+
except (OSError, json.JSONDecodeError):
|
|
2031
|
+
return # 既有 state 损坏由 resume 身份门兜底;all 只做证据合并
|
|
2032
|
+
old_wheel = old.get("wheel") or {}
|
|
2033
|
+
if (old.get("version"), old.get("tag"), old_wheel.get("sha256")) != \
|
|
2034
|
+
(state.version, state.tag, state.wheel_sha256):
|
|
2035
|
+
return
|
|
2036
|
+
for realm in SURFACES:
|
|
2037
|
+
old_surf = (old.get("surfaces") or {}).get(realm)
|
|
2038
|
+
if isinstance(old_surf, dict) and old_surf.get("status") not in (None, "pending") \
|
|
2039
|
+
and state.surface(realm).get("status") == "pending":
|
|
2040
|
+
state.surfaces[realm] = dict(old_surf)
|
|
2041
|
+
|
|
2042
|
+
|
|
2043
|
+
def _print_release_targets(state: ReleaseState, meta: dict) -> None:
|
|
2044
|
+
print()
|
|
2045
|
+
print("全部预检通过——待确认的发布目标(D-13):")
|
|
2046
|
+
print(f" version: {state.version}")
|
|
2047
|
+
print(f" tag: {state.tag}(HEAD {state.head[:12]})")
|
|
2048
|
+
print(f" wheel: {state.wheel_name}({state.wheel_size} bytes)")
|
|
2049
|
+
print(f" sha256: {state.wheel_sha256}")
|
|
2050
|
+
print(f" 目标 1: 公开 PyPI {meta['name']} {state.version}"
|
|
2051
|
+
f"({PYPI_REALMS['pypi']['legacy']},名称注册随首次上传完成,PUB-03)")
|
|
2052
|
+
print(f" 目标 2: GitCode 正式 Release {meta['gitcode_owner']}/{meta['gitcode_repo']}"
|
|
2053
|
+
f" tag {state.tag}(body=当前 CHANGELOG section,非 draft/非 prerelease)")
|
|
2054
|
+
|
|
2055
|
+
|
|
2056
|
+
def _receipt_surface(state: ReleaseState, realm: str) -> dict:
|
|
2057
|
+
"""回执面证据:剥离 error 后的稳定字段白名单(无 token/签名 URL)。"""
|
|
2058
|
+
surf = dict(state.surface(realm))
|
|
2059
|
+
surf.pop("error", None)
|
|
2060
|
+
return surf
|
|
2061
|
+
|
|
2062
|
+
|
|
2063
|
+
def write_receipt(run_dir: Path, state: ReleaseState, decisions: dict,
|
|
2064
|
+
test_receipt: dict | None = None) -> Path:
|
|
2065
|
+
"""最终脱敏回执(原子替换、累计合并):顶层 version/tag/head/wheel/target/last_command/
|
|
2066
|
+
decisions/surfaces/created_at。
|
|
2067
|
+
|
|
2068
|
+
pypi/gitcode 必须已 verified(含稳定 URL/hash/时间戳/正式字段);testpypi 仅在有
|
|
2069
|
+
真实 verified 记录时并入,A2=recommended 无演练时明确为 null(绝不虚构三面同 hash)。
|
|
2070
|
+
不写 token、Authorization、完整 query、预签名 URL、x-obs-* 或环境 dump。
|
|
2071
|
+
"""
|
|
2072
|
+
for realm in ("pypi", "gitcode"):
|
|
2073
|
+
surf = state.surface(realm)
|
|
2074
|
+
if surf.get("status") != "verified":
|
|
2075
|
+
raise ReleaseError(
|
|
2076
|
+
f"{realm} 面未 verified(当前 {surf.get('status')!r})——不得写最终双源回执")
|
|
2077
|
+
tp = state.surface("testpypi")
|
|
2078
|
+
surfaces = {
|
|
2079
|
+
"pypi": _receipt_surface(state, "pypi"),
|
|
2080
|
+
"gitcode": _receipt_surface(state, "gitcode"),
|
|
2081
|
+
"testpypi": _receipt_surface(state, "testpypi")
|
|
2082
|
+
if tp.get("status") == "verified" else None,
|
|
2083
|
+
}
|
|
2084
|
+
payload = {
|
|
2085
|
+
**state.identity(),
|
|
2086
|
+
"target": state.target,
|
|
2087
|
+
"last_command": state.last_command,
|
|
2088
|
+
"decisions": {"a1": (decisions or {}).get("A1"), "a2": (decisions or {}).get("A2")},
|
|
2089
|
+
"surfaces": surfaces,
|
|
2090
|
+
"created_at": _now_iso(),
|
|
2091
|
+
}
|
|
2092
|
+
return write_state(Path(run_dir), payload, "receipt.json")
|
|
2093
|
+
|
|
2094
|
+
|
|
2095
|
+
def publish_all(repo: Path, state: ReleaseState, meta: dict, env: dict | None = None,
|
|
2096
|
+
*, outcomes: dict | None = None, yes: bool = False, isatty=None,
|
|
2097
|
+
prompt=None) -> Path:
|
|
2098
|
+
"""正式双源发布(D-03/D-13/D-14/D-15):全部硬门 → 确认 → 公开 PyPI verified →
|
|
2099
|
+
GitCode 正式 Release → 最终脱敏回执。
|
|
2100
|
+
|
|
2101
|
+
副作用顺序由单一状态机控制:A1 写入门 → checkout 实时复检 → A2 receipt 门 → 既有
|
|
2102
|
+
证据合并 → 公开预检(occupancy/凭据/公开仓/Release 未占用/远端 tag/元数据/版本化
|
|
2103
|
+
链接/公开历史门)→ approval.json 消费(存在即必须通过,--yes 不绕过)→ D-13 确认 →
|
|
2104
|
+
PyPI upload/verify(verified 才继续)→ GitCode create/upload_url/raw PUT/verify →
|
|
2105
|
+
最终回执。每次远端写之前原子持久化状态(崩溃可恢复锚点)。
|
|
2106
|
+
"""
|
|
2107
|
+
root = Path(repo).resolve()
|
|
2108
|
+
e = os.environ if env is None else env
|
|
2109
|
+
run_dir = Path(state.run_dir)
|
|
2110
|
+
if outcomes is None:
|
|
2111
|
+
outcomes = load_decision_outcomes(root)
|
|
2112
|
+
# 1) A1 写入门(pending/缺失/strict-literal:零远端写,含 TestPyPI 面)
|
|
2113
|
+
assert_publication_permitted(outcomes, surface="all")
|
|
2114
|
+
# 2) 实时硬门:checkout 与归档身份一致
|
|
2115
|
+
checkout = inspect_checkout(root)
|
|
2116
|
+
if (checkout["version"], checkout["tag"], checkout["head"]) != \
|
|
2117
|
+
(state.version, state.tag, state.head):
|
|
2118
|
+
raise ReleaseError("checkout 漂移:当前 HEAD/tag/版本与归档身份不符——上传身份必须绑定归档记录")
|
|
2119
|
+
# 3) A2 receipt 门(required:缺/错即预检失败;recommended:无 receipt 可行)
|
|
2120
|
+
test_receipt = _load_test_receipt(run_dir)
|
|
2121
|
+
assert_testpypi_receipt_gate(state, outcomes, test_receipt)
|
|
2122
|
+
# 4) 累计证据合并(test→all 保留 TestPyPI verified surface)
|
|
2123
|
+
_merge_prior_surfaces(state)
|
|
2124
|
+
# 5) 公开预检(只读 GET;任一失败零上传)
|
|
2125
|
+
pre = preflight_remote(root, state, meta, env=e)
|
|
2126
|
+
state.preflight.update(pre)
|
|
2127
|
+
approval_path = run_dir / "approval.json"
|
|
2128
|
+
ready_check = next((c for c in pre["checks"] if c["name"] == "public-readiness"), None)
|
|
2129
|
+
if ready_check is not None and ready_check["status"] != "pass" \
|
|
2130
|
+
and approval_path.is_file():
|
|
2131
|
+
# 审批绑定依赖 public-readiness 字节:实时校验失败 → 绑定不可复现 → stale(仍零上传)
|
|
2132
|
+
_approval_stale(state, "public-readiness 实时校验未通过(D-20 公开历史门)——"
|
|
2133
|
+
f"审批的 public_readiness_sha256 绑定不可复现: "
|
|
2134
|
+
f"{ready_check['detail'][:200]}")
|
|
2135
|
+
failed = [c for c in pre["checks"] if c["status"] != "pass"]
|
|
2136
|
+
if failed:
|
|
2137
|
+
detail = "; ".join(f"[{c['name']}] {c['detail']}" for c in failed[:5])
|
|
2138
|
+
raise ReleaseError(f"公开预检失败 {len(failed)} 项(零上传): {detail[:500]}")
|
|
2139
|
+
_assert_public_metadata(root, state, meta)
|
|
2140
|
+
try:
|
|
2141
|
+
readiness = _load_public_readiness(run_dir)
|
|
2142
|
+
except ReleaseError:
|
|
2143
|
+
if approval_path.is_file():
|
|
2144
|
+
# 审批绑定依赖 public-readiness 字节:证明缺失/未达标时绑定不可复现 → stale(仍零上传)
|
|
2145
|
+
_approval_stale(state, "public-readiness.json 缺失/未达标(D-20 公开历史门)——"
|
|
2146
|
+
"审批的 public_readiness_sha256 绑定不可复现")
|
|
2147
|
+
raise
|
|
2148
|
+
# 6) 审批消费(approval.json 存在 → 必须通过;--yes 不得绕过)
|
|
2149
|
+
if approval_path.is_file():
|
|
2150
|
+
state.approval = validate_approval(approval_path, state, outcomes, readiness,
|
|
2151
|
+
test_receipt)
|
|
2152
|
+
# 7) D-13 确认(展示目标 → 交互确认或显式 --yes)
|
|
2153
|
+
_print_release_targets(state, meta)
|
|
2154
|
+
state.confirmation = confirm_release(state, yes=yes, isatty=isatty, prompt=prompt)
|
|
2155
|
+
state.target = "all"
|
|
2156
|
+
state.last_command = "all"
|
|
2157
|
+
write_state(run_dir, state.to_payload())
|
|
2158
|
+
# 8) 公开 PyPI(verified 才进入 GitCode)
|
|
2159
|
+
pypi = PyPIClient(index_url=PYPI_REALMS["pypi"]["json"])
|
|
2160
|
+
state.set_surface("pypi", status="uploading")
|
|
2161
|
+
write_state(run_dir, state.to_payload()) # 写前持久化(崩溃锚点)
|
|
2162
|
+
log_operation(run_dir, "twine_upload_started", surface="pypi", wheel=state.wheel_name)
|
|
2163
|
+
try:
|
|
2164
|
+
pypi.upload(state, "pypi", env=e)
|
|
2165
|
+
except ReleaseError as ex:
|
|
2166
|
+
state.set_surface("pypi", status="failed", error=str(ex))
|
|
2167
|
+
write_state(run_dir, state.to_payload())
|
|
2168
|
+
raise
|
|
2169
|
+
state.set_surface("pypi", status="uploaded_unverified")
|
|
2170
|
+
write_state(run_dir, state.to_payload())
|
|
2171
|
+
try:
|
|
2172
|
+
ev = pypi.verify(meta["name"], state.version, state.wheel_name, state.wheel_sha256)
|
|
2173
|
+
except ReleasePollTimeout as ex:
|
|
2174
|
+
state.set_surface("pypi", status="uploaded_unverified", error=str(ex))
|
|
2175
|
+
write_state(run_dir, state.to_payload())
|
|
2176
|
+
raise
|
|
2177
|
+
except ReleaseError as ex:
|
|
2178
|
+
state.set_surface("pypi", status="failed", error=str(ex))
|
|
2179
|
+
write_state(run_dir, state.to_payload())
|
|
2180
|
+
raise
|
|
2181
|
+
state.set_surface("pypi", status="verified", api_url=ev["api_url"],
|
|
2182
|
+
file_url=ev["file_url"], sha256=ev["sha256"],
|
|
2183
|
+
upload_time=ev["upload_time"], verified_at=ev["verified_at"])
|
|
2184
|
+
state.surface("pypi").pop("error", None)
|
|
2185
|
+
log_operation(run_dir, "upload_verified", surface="pypi", sha256=ev["sha256"])
|
|
2186
|
+
write_state(run_dir, state.to_payload())
|
|
2187
|
+
# 9) GitCode 正式 Release(pypi=verified 后才 create)
|
|
2188
|
+
notes = release_metadata.extract_changelog_section(
|
|
2189
|
+
(root / "CHANGELOG.md").read_text(encoding="utf-8"), state.version)
|
|
2190
|
+
token = str(e.get("GITCODE_TOKEN") or "").strip()
|
|
2191
|
+
gc = GitCodeClient(meta["gitcode_owner"], meta["gitcode_repo"], token)
|
|
2192
|
+
state.set_surface("gitcode", status="uploading")
|
|
2193
|
+
write_state(run_dir, state.to_payload()) # 写前持久化(崩溃锚点)
|
|
2194
|
+
log_operation(run_dir, "gitcode_release_started", surface="gitcode",
|
|
2195
|
+
tag=state.tag, wheel=state.wheel_name)
|
|
2196
|
+
try:
|
|
2197
|
+
gc.create(state, notes)
|
|
2198
|
+
upload_info = gc.upload_url(state) # 短期内存对象(绝不进 state/receipt/日志)
|
|
2199
|
+
gc.put_wheel(upload_info, run_dir / "artifact" / state.wheel_name)
|
|
2200
|
+
gev = gc.verify(state, notes)
|
|
2201
|
+
except ReleasePollTimeout as ex:
|
|
2202
|
+
state.set_surface("gitcode", status="uploaded_unverified", error=str(ex))
|
|
2203
|
+
write_state(run_dir, state.to_payload())
|
|
2204
|
+
raise
|
|
2205
|
+
except ReleaseError as ex:
|
|
2206
|
+
state.set_surface("gitcode", status="failed", error=str(ex))
|
|
2207
|
+
write_state(run_dir, state.to_payload())
|
|
2208
|
+
raise
|
|
2209
|
+
state.set_surface("gitcode", status="verified", release_url=gev["release_url"],
|
|
2210
|
+
asset_url=gev["asset_url"], sha256=gev["sha256"],
|
|
2211
|
+
created_at=gev["created_at"],
|
|
2212
|
+
target_commitish=gev["target_commitish"],
|
|
2213
|
+
prerelease=gev["prerelease"],
|
|
2214
|
+
release_status=gev["release_status"],
|
|
2215
|
+
verified_at=gev["verified_at"])
|
|
2216
|
+
state.surface("gitcode").pop("error", None)
|
|
2217
|
+
log_operation(run_dir, "upload_verified", surface="gitcode", sha256=gev["sha256"])
|
|
2218
|
+
write_state(run_dir, state.to_payload())
|
|
2219
|
+
# 10) 最终脱敏回执(累计合并 TestPyPI 证据)
|
|
2220
|
+
return write_receipt(run_dir, state, outcomes, test_receipt)
|
|
2221
|
+
|
|
2222
|
+
|
|
2223
|
+
# ---------------------------------------------------------------------------
|
|
2224
|
+
# resume:TestPyPI 面崩溃恢复(D-04;公开双源恢复由 05-05 复用同一状态机)
|
|
2225
|
+
# ---------------------------------------------------------------------------
|
|
2226
|
+
|
|
2227
|
+
def _state_from_payload(payload: dict, run_dir: Path) -> ReleaseState:
|
|
2228
|
+
"""从 state.json 重建 ReleaseState(向后兼容旧字符串面状态)。"""
|
|
2229
|
+
wheel = payload.get("wheel") or {}
|
|
2230
|
+
state = ReleaseState(
|
|
2231
|
+
version=str(payload.get("version") or ""), tag=str(payload.get("tag") or ""),
|
|
2232
|
+
head=str(payload.get("head") or ""), wheel_name=str(wheel.get("name") or ""),
|
|
2233
|
+
wheel_size=int(wheel.get("size") or 0),
|
|
2234
|
+
wheel_sha256=str(wheel.get("sha256") or ""), run_dir=Path(run_dir))
|
|
2235
|
+
state.target = payload.get("target")
|
|
2236
|
+
state.last_command = payload.get("last_command")
|
|
2237
|
+
for k, v in (payload.get("surfaces") or {}).items():
|
|
2238
|
+
state.surfaces[k] = dict(v) if isinstance(v, dict) else {"status": str(v)}
|
|
2239
|
+
state.preflight = dict(payload.get("preflight") or {})
|
|
2240
|
+
state.confirmation = dict(payload.get("confirmation") or {})
|
|
2241
|
+
state.approval = dict(payload.get("approval") or {})
|
|
2242
|
+
state.approval_stale = bool(payload.get("approval_stale"))
|
|
2243
|
+
return state
|
|
2244
|
+
|
|
2245
|
+
|
|
2246
|
+
def _verify_and_finish(state: ReleaseState, realm: str, meta: dict, client: PyPIClient,
|
|
2247
|
+
run_dir: Path, receipt_writer=None) -> None:
|
|
2248
|
+
"""只读验证收口:verified 落面 + upload_verified 事件 + state/receipt 原子写。
|
|
2249
|
+
|
|
2250
|
+
receipt_writer 默认写 test receipt(testpypi 轨道);公开面恢复由调用方在双面
|
|
2251
|
+
齐备后写最终回执(write_receipt),此处传 no-op。
|
|
2252
|
+
"""
|
|
2253
|
+
ev = client.verify(meta["name"], state.version, state.wheel_name, state.wheel_sha256)
|
|
2254
|
+
state.set_surface(realm, status="verified", api_url=ev["api_url"],
|
|
2255
|
+
file_url=ev["file_url"], sha256=ev["sha256"],
|
|
2256
|
+
upload_time=ev["upload_time"], verified_at=ev["verified_at"])
|
|
2257
|
+
state.surface(realm).pop("error", None)
|
|
2258
|
+
log_operation(run_dir, "upload_verified", surface=realm, sha256=ev["sha256"])
|
|
2259
|
+
write_state(run_dir, state.to_payload())
|
|
2260
|
+
if receipt_writer is None:
|
|
2261
|
+
write_test_receipt(run_dir, state)
|
|
2262
|
+
|
|
2263
|
+
|
|
2264
|
+
def _finish_gitcode_surface(state: ReleaseState, run_dir: Path, gev: dict) -> None:
|
|
2265
|
+
"""GitCode 面 verified 落证(publish_all/resume 共用;稳定 URL/正式字段白名单)。"""
|
|
2266
|
+
state.set_surface("gitcode", status="verified", release_url=gev["release_url"],
|
|
2267
|
+
asset_url=gev["asset_url"], sha256=gev["sha256"],
|
|
2268
|
+
created_at=gev["created_at"],
|
|
2269
|
+
target_commitish=gev["target_commitish"],
|
|
2270
|
+
prerelease=gev["prerelease"],
|
|
2271
|
+
release_status=gev["release_status"],
|
|
2272
|
+
verified_at=gev["verified_at"])
|
|
2273
|
+
state.surface("gitcode").pop("error", None)
|
|
2274
|
+
log_operation(run_dir, "upload_verified", surface="gitcode", sha256=gev["sha256"])
|
|
2275
|
+
write_state(run_dir, state.to_payload())
|
|
2276
|
+
|
|
2277
|
+
|
|
2278
|
+
def _resume_all(root: Path, meta: dict, state: ReleaseState, run_dir: Path,
|
|
2279
|
+
e: dict, outcomes: dict, yes: bool) -> int:
|
|
2280
|
+
"""target=all 双源恢复(D-04/D-07/D-08):
|
|
2281
|
+
|
|
2282
|
+
- 输入只有归档 + state.json;不读 dist、不重建、不重跑 audit/smoke。
|
|
2283
|
+
- 全部面 verified:只读幂等输出 DUAL-SOURCE-OK(receipt 缺失才按既有证据补写),零远端写。
|
|
2284
|
+
- 有未完成面 → 每个远端写之前重新过 A1 门 + A2 receipt 门 + approval/confirmation
|
|
2285
|
+
绑定(approval.json 存在则按白名单 schema 重算 approval_stale;本记录已上传/verified
|
|
2286
|
+
的同 hash 面是合法恢复进度,不判 stale)。
|
|
2287
|
+
- 每面先远端对账:missing(404/无附件)才重试;matching 只对账/复验;不同内容冲突中止。
|
|
2288
|
+
- PyPI 未 verified 时绝不进入 GitCode 面(D-03 顺序)。
|
|
2289
|
+
"""
|
|
2290
|
+
pypi_surf = state.surface("pypi")
|
|
2291
|
+
gc_surf = state.surface("gitcode")
|
|
2292
|
+
if pypi_surf.get("status") == "verified" and gc_surf.get("status") == "verified":
|
|
2293
|
+
receipt = run_dir / "receipt.json"
|
|
2294
|
+
final_ok = False
|
|
2295
|
+
if receipt.is_file():
|
|
2296
|
+
try:
|
|
2297
|
+
data = json.loads(receipt.read_text(encoding="utf-8"))
|
|
2298
|
+
final_ok = (data.get("target") == "all"
|
|
2299
|
+
and (data.get("surfaces") or {}).get("pypi", {}).get("status") == "verified"
|
|
2300
|
+
and (data.get("surfaces") or {}).get("gitcode", {}).get("status") == "verified")
|
|
2301
|
+
except (OSError, json.JSONDecodeError):
|
|
2302
|
+
final_ok = False
|
|
2303
|
+
if not final_ok:
|
|
2304
|
+
write_receipt(run_dir, state, outcomes, _load_test_receipt(run_dir))
|
|
2305
|
+
print("resume: 最终回执缺失,已按既有双面 verified 证据补写")
|
|
2306
|
+
state.last_command = "resume"
|
|
2307
|
+
write_state(run_dir, state.to_payload())
|
|
2308
|
+
print(f"resume: 公开双源均已 verified(sha256 {state.wheel_sha256[:12]}…),幂等无动作")
|
|
2309
|
+
print(f"resume: 回执 {receipt}")
|
|
2310
|
+
print("DUAL-SOURCE-OK")
|
|
2311
|
+
return 0
|
|
2312
|
+
# 有未完成的远端写面——写前重新过全部门(A1 → A2 → approval/confirmation)
|
|
2313
|
+
assert_publication_permitted(outcomes, surface="all")
|
|
2314
|
+
test_receipt = _load_test_receipt(run_dir)
|
|
2315
|
+
assert_testpypi_receipt_gate(state, outcomes, test_receipt)
|
|
2316
|
+
approval_path = run_dir / "approval.json"
|
|
2317
|
+
if approval_path.is_file():
|
|
2318
|
+
readiness = _load_public_readiness(run_dir)
|
|
2319
|
+
state.approval = validate_approval(approval_path, state, outcomes, readiness,
|
|
2320
|
+
test_receipt)
|
|
2321
|
+
else:
|
|
2322
|
+
_print_release_targets(state, meta)
|
|
2323
|
+
state.confirmation = confirm_release(state, yes=yes)
|
|
2324
|
+
state.last_command = "resume"
|
|
2325
|
+
write_state(run_dir, state.to_payload())
|
|
2326
|
+
# -- 公开 PyPI 面 -------------------------------------------------------
|
|
2327
|
+
if pypi_surf.get("status") != "verified":
|
|
2328
|
+
pypi = PyPIClient(index_url=PYPI_REALMS["pypi"]["json"])
|
|
2329
|
+
category, detail = reconcile_pypi(state, "pypi", meta, client=pypi)
|
|
2330
|
+
print(f"resume: 公开 PyPI 对账 → {category}({detail})")
|
|
2331
|
+
if category == "conflict":
|
|
2332
|
+
state.set_surface("pypi", status="failed", error=detail)
|
|
2333
|
+
write_state(run_dir, state.to_payload())
|
|
2334
|
+
raise ReleaseError(f"resume 对账冲突: {detail}——不覆盖不重传(本记录外的内容冲突)")
|
|
2335
|
+
if category == "missing":
|
|
2336
|
+
if str(e.get("TWINE_USERNAME") or "") != "__token__" \
|
|
2337
|
+
or not str(e.get("TWINE_PASSWORD") or "").strip():
|
|
2338
|
+
raise ReleaseError("缺少 twine 凭据(TWINE_USERNAME=__token__ + 非空 "
|
|
2339
|
+
"TWINE_PASSWORD,env-only)——重试上传前失败,零远端写")
|
|
2340
|
+
state.set_surface("pypi", status="uploading")
|
|
2341
|
+
write_state(run_dir, state.to_payload())
|
|
2342
|
+
log_operation(run_dir, "twine_upload_started", surface="pypi",
|
|
2343
|
+
wheel=state.wheel_name)
|
|
2344
|
+
try:
|
|
2345
|
+
pypi.upload(state, "pypi", env=e)
|
|
2346
|
+
except ReleaseError as ex:
|
|
2347
|
+
state.set_surface("pypi", status="failed", error=str(ex))
|
|
2348
|
+
write_state(run_dir, state.to_payload())
|
|
2349
|
+
raise
|
|
2350
|
+
state.set_surface("pypi", status="uploaded_unverified")
|
|
2351
|
+
write_state(run_dir, state.to_payload())
|
|
2352
|
+
# matching / pending / 重传后:只读轮询验证(结果未知保持 uploaded_unverified)
|
|
2353
|
+
try:
|
|
2354
|
+
_verify_and_finish(state, "pypi", meta, pypi, run_dir, receipt_writer=lambda *_: None)
|
|
2355
|
+
except ReleasePollTimeout as ex:
|
|
2356
|
+
state.set_surface("pypi", status="uploaded_unverified", error=str(ex))
|
|
2357
|
+
write_state(run_dir, state.to_payload())
|
|
2358
|
+
raise
|
|
2359
|
+
except ReleaseError as ex:
|
|
2360
|
+
state.set_surface("pypi", status="failed", error=str(ex))
|
|
2361
|
+
write_state(run_dir, state.to_payload())
|
|
2362
|
+
raise
|
|
2363
|
+
if state.surface("pypi").get("status") != "verified":
|
|
2364
|
+
raise ReleaseError("公开 PyPI 未 verified——不得进入 GitCode 恢复(D-03 顺序)")
|
|
2365
|
+
# -- GitCode 面 ---------------------------------------------------------
|
|
2366
|
+
if gc_surf.get("status") != "verified":
|
|
2367
|
+
notes = release_metadata.extract_changelog_section(
|
|
2368
|
+
(root / "CHANGELOG.md").read_text(encoding="utf-8"), state.version)
|
|
2369
|
+
gc = GitCodeClient(meta["gitcode_owner"], meta["gitcode_repo"],
|
|
2370
|
+
str(e.get("GITCODE_TOKEN") or "").strip())
|
|
2371
|
+
category, detail = reconcile_gitcode(state, notes, gc)
|
|
2372
|
+
print(f"resume: GitCode 对账 → {category}({detail})")
|
|
2373
|
+
if category == "conflict":
|
|
2374
|
+
state.set_surface("gitcode", status="failed", error=detail)
|
|
2375
|
+
write_state(run_dir, state.to_payload())
|
|
2376
|
+
raise ReleaseError(f"resume 对账冲突: {detail}——存在但不属于本记录或元数据不同,"
|
|
2377
|
+
"停止(普通 all 也不享受此对账例外)")
|
|
2378
|
+
if category == "missing":
|
|
2379
|
+
state.set_surface("gitcode", status="uploading")
|
|
2380
|
+
write_state(run_dir, state.to_payload())
|
|
2381
|
+
log_operation(run_dir, "gitcode_release_started", surface="gitcode",
|
|
2382
|
+
tag=state.tag, wheel=state.wheel_name)
|
|
2383
|
+
try:
|
|
2384
|
+
gc.create(state, notes)
|
|
2385
|
+
except ReleaseError as ex:
|
|
2386
|
+
state.set_surface("gitcode", status="failed", error=str(ex))
|
|
2387
|
+
write_state(run_dir, state.to_payload())
|
|
2388
|
+
raise
|
|
2389
|
+
category = "no-asset" # create 成功但附件未知 → 按无附件续走
|
|
2390
|
+
if category in ("no-asset",):
|
|
2391
|
+
upload_info = gc.upload_url(state) # 短期内存对象
|
|
2392
|
+
state.set_surface("gitcode", status="uploading")
|
|
2393
|
+
write_state(run_dir, state.to_payload())
|
|
2394
|
+
try:
|
|
2395
|
+
gc.put_wheel(upload_info, run_dir / "artifact" / state.wheel_name)
|
|
2396
|
+
except ReleaseError as ex:
|
|
2397
|
+
state.set_surface("gitcode", status="failed", error=str(ex))
|
|
2398
|
+
write_state(run_dir, state.to_payload())
|
|
2399
|
+
raise
|
|
2400
|
+
# matching / pending / PUT 后:有界轮询验证收口
|
|
2401
|
+
try:
|
|
2402
|
+
gev = gc.verify(state, notes)
|
|
2403
|
+
except ReleasePollTimeout as ex:
|
|
2404
|
+
state.set_surface("gitcode", status="uploaded_unverified", error=str(ex))
|
|
2405
|
+
write_state(run_dir, state.to_payload())
|
|
2406
|
+
raise
|
|
2407
|
+
except ReleaseError as ex:
|
|
2408
|
+
state.set_surface("gitcode", status="failed", error=str(ex))
|
|
2409
|
+
write_state(run_dir, state.to_payload())
|
|
2410
|
+
raise
|
|
2411
|
+
_finish_gitcode_surface(state, run_dir, gev)
|
|
2412
|
+
# -- 最终回执(累计合并) ------------------------------------------------
|
|
2413
|
+
write_receipt(run_dir, state, outcomes, test_receipt)
|
|
2414
|
+
print(f"resume: 双源恢复完成(公开 PyPI 与 GitCode 均 verified,同 sha256 "
|
|
2415
|
+
f"{state.wheel_sha256[:12]}…)")
|
|
2416
|
+
print(f"resume: 回执 {run_dir / 'receipt.json'}")
|
|
2417
|
+
print("DUAL-SOURCE-OK")
|
|
2418
|
+
return 0
|
|
2419
|
+
|
|
2420
|
+
|
|
2421
|
+
def resume(repo: Path, meta: dict, env: dict | None = None,
|
|
2422
|
+
client: PyPIClient | None = None, outcomes: dict | None = None,
|
|
2423
|
+
yes: bool = False) -> int:
|
|
2424
|
+
"""崩溃恢复(D-04;target 分轨:testpypi / 公开双源):
|
|
2425
|
+
|
|
2426
|
+
- 输入只有归档 + state.json + 本地精确 tag/HEAD:不读 dist、不重新 build、不重跑
|
|
2427
|
+
audit/smoke(它们在原流程已绑定归档字节)。
|
|
2428
|
+
- 身份硬门:checkout(tag/HEAD/version)与 state 记录全等;归档在盘且文件名/SHA256
|
|
2429
|
+
与记录一致(缺失/篡改 fail-closed)。
|
|
2430
|
+
- 裸预检不是授权:target 未设置(last_command=bare)只显示状态,绝不上传(--yes 也不行)。
|
|
2431
|
+
- target=test:TestPyPI 面(先对账 404 才重试,重试前再过 A1 门)。
|
|
2432
|
+
- target=all:公开双源面(_resume_all——先对账后动作,写前重过 A1/A2/approval/confirmation)。
|
|
2433
|
+
"""
|
|
2434
|
+
root = Path(repo).resolve()
|
|
2435
|
+
realm = "testpypi"
|
|
2436
|
+
e = os.environ if env is None else env
|
|
2437
|
+
if outcomes is None:
|
|
2438
|
+
outcomes = load_decision_outcomes(root)
|
|
2439
|
+
checkout = inspect_checkout(root) # 实时硬门:精确 tag/干净树(先于任何读取/网络)
|
|
2440
|
+
run_dir = root / "runs" / f"release-{checkout['version']}"
|
|
2441
|
+
state_path = run_dir / "state.json"
|
|
2442
|
+
if not state_path.is_file():
|
|
2443
|
+
raise ReleaseError(f"无可恢复状态: {state_path} 缺失——resume 只恢复本工具此前记录的发布")
|
|
2444
|
+
state = _state_from_payload(json.loads(state_path.read_text(encoding="utf-8")), run_dir)
|
|
2445
|
+
if (state.version, state.tag, state.head) != \
|
|
2446
|
+
(checkout["version"], checkout["tag"], checkout["head"]):
|
|
2447
|
+
raise ReleaseError("resume 身份不符:state 记录与当前 checkout(version/tag/HEAD)"
|
|
2448
|
+
"不一致——恢复只针对同一发布记录,不自动跨版本迁移")
|
|
2449
|
+
archive = _find_archive(run_dir)
|
|
2450
|
+
if archive is None:
|
|
2451
|
+
raise ReleaseError(f"归档缺失: {run_dir / 'artifact'}——resume 只从归档恢复,绝不重建")
|
|
2452
|
+
if archive.name != state.wheel_name or sha256_file(archive) != state.wheel_sha256:
|
|
2453
|
+
raise ReleaseError("归档与 state 记录不符(文件名/SHA256)——归档缺失、被篡改或错位,"
|
|
2454
|
+
"fail-closed;需人工核对后处理")
|
|
2455
|
+
if state.target is None:
|
|
2456
|
+
# 裸预检(last_command=bare)不是上传授权轨道:只显示,不触发任何远端写
|
|
2457
|
+
status = state.surface(realm).get("status", "pending")
|
|
2458
|
+
print(f"resume: 无上传授权轨道(target 未设置,last_command={state.last_command!r}"
|
|
2459
|
+
" 不是授权;--yes 也不构成授权)——如需演练请先 release test;如需正式发布等 release all")
|
|
2460
|
+
print(f"resume: 本地状态: testpypi={status},preflight 记录 "
|
|
2461
|
+
f"{'有' if state.preflight.get('checks') else '无'}")
|
|
2462
|
+
return 0
|
|
2463
|
+
if state.target == "all":
|
|
2464
|
+
return _resume_all(root, meta, state, run_dir, e, outcomes, yes)
|
|
2465
|
+
if state.target != "test":
|
|
2466
|
+
raise ReleaseError(f"state.target 非法: {state.target!r}(合法: test|all)——状态文件损坏")
|
|
2467
|
+
status = state.surface(realm).get("status", "pending")
|
|
2468
|
+
if status == "verified":
|
|
2469
|
+
receipt = run_dir / "receipt.json"
|
|
2470
|
+
if not receipt.is_file():
|
|
2471
|
+
write_test_receipt(run_dir, state)
|
|
2472
|
+
print("resume: receipt 缺失,已按既有 verified 证据补写")
|
|
2473
|
+
print(f"resume: testpypi 已 verified(sha256 {state.wheel_sha256[:12]}…),幂等无动作")
|
|
2474
|
+
state.last_command = "resume"
|
|
2475
|
+
write_state(run_dir, state.to_payload())
|
|
2476
|
+
return 0
|
|
2477
|
+
client = client or PyPIClient(index_url=PYPI_REALMS[realm]["json"])
|
|
2478
|
+
category, detail = reconcile_pypi(state, realm, meta, client=client)
|
|
2479
|
+
print(f"resume: 远端对账 → {category}({detail})")
|
|
2480
|
+
if category == "matching":
|
|
2481
|
+
_verify_and_finish(state, realm, meta, client, run_dir) # 只对账不重传
|
|
2482
|
+
state.last_command = "resume"
|
|
2483
|
+
write_state(run_dir, state.to_payload())
|
|
2484
|
+
print(f"resume: testpypi 对账一致,已转 verified(sha256 {state.wheel_sha256[:12]}…)")
|
|
2485
|
+
return 0
|
|
2486
|
+
if category == "conflict":
|
|
2487
|
+
state.set_surface(realm, status="failed", error=detail)
|
|
2488
|
+
state.last_command = "resume"
|
|
2489
|
+
write_state(run_dir, state.to_payload())
|
|
2490
|
+
raise ReleaseError(f"resume 对账冲突: {detail}——不覆盖不重传,人工核对后 bump tag(D-06)")
|
|
2491
|
+
if category == "pending":
|
|
2492
|
+
# 版本已占用但未可验证:轮询只读验证(不可重传——占用即冲突)
|
|
2493
|
+
try:
|
|
2494
|
+
_verify_and_finish(state, realm, meta, client, run_dir)
|
|
2495
|
+
except ReleasePollTimeout as ex:
|
|
2496
|
+
state.set_surface(realm, status="uploaded_unverified", error=str(ex))
|
|
2497
|
+
state.last_command = "resume"
|
|
2498
|
+
write_state(run_dir, state.to_payload())
|
|
2499
|
+
raise
|
|
2500
|
+
state.last_command = "resume"
|
|
2501
|
+
write_state(run_dir, state.to_payload())
|
|
2502
|
+
print("resume: 传播完成,testpypi 已 verified")
|
|
2503
|
+
return 0
|
|
2504
|
+
# missing:远端明确 404 才可重试——重试即远端写,副作用前再过 A1 门(strict-literal 零上传)
|
|
2505
|
+
assert_publication_permitted(outcomes, surface=realm)
|
|
2506
|
+
if str(e.get("TWINE_USERNAME") or "") != "__token__" \
|
|
2507
|
+
or not str(e.get("TWINE_PASSWORD") or "").strip():
|
|
2508
|
+
raise ReleaseError("缺少 twine 凭据(TWINE_USERNAME=__token__ + 非空 TWINE_PASSWORD,"
|
|
2509
|
+
"env-only)——重试上传前失败,零远端写")
|
|
2510
|
+
state.set_surface(realm, status="uploading")
|
|
2511
|
+
state.last_command = "resume"
|
|
2512
|
+
write_state(run_dir, state.to_payload()) # 写前持久化(重试的崩溃锚点)
|
|
2513
|
+
log_operation(run_dir, "twine_upload_started", surface=realm, wheel=state.wheel_name)
|
|
2514
|
+
try:
|
|
2515
|
+
client.upload(state, realm, env=e)
|
|
2516
|
+
except ReleaseError as ex:
|
|
2517
|
+
state.set_surface(realm, status="failed", error=str(ex))
|
|
2518
|
+
write_state(run_dir, state.to_payload())
|
|
2519
|
+
raise
|
|
2520
|
+
state.set_surface(realm, status="uploaded_unverified")
|
|
2521
|
+
write_state(run_dir, state.to_payload())
|
|
2522
|
+
try:
|
|
2523
|
+
_verify_and_finish(state, realm, meta, client, run_dir)
|
|
2524
|
+
except ReleasePollTimeout as ex:
|
|
2525
|
+
state.set_surface(realm, status="uploaded_unverified", error=str(ex))
|
|
2526
|
+
write_state(run_dir, state.to_payload())
|
|
2527
|
+
raise
|
|
2528
|
+
except ReleaseError as ex:
|
|
2529
|
+
state.set_surface(realm, status="failed", error=str(ex))
|
|
2530
|
+
write_state(run_dir, state.to_payload())
|
|
2531
|
+
raise
|
|
2532
|
+
print(f"resume: 重试上传并验证完成(testpypi verified,sha256 "
|
|
2533
|
+
f"{state.wheel_sha256[:12]}…)")
|
|
2534
|
+
return 0
|
|
2535
|
+
|
|
2536
|
+
|
|
2537
|
+
# ---------------------------------------------------------------------------
|
|
2538
|
+
# main(裸模式 / test / all / resume 模式)
|
|
2539
|
+
# ---------------------------------------------------------------------------
|
|
2540
|
+
|
|
2541
|
+
USAGE_TEXT = (
|
|
2542
|
+
"用法: python ohos_devloop/scripts/release.py [test|all|resume] [--yes] (或 ohos-devloop release …)\n"
|
|
2543
|
+
" (无参)裸模式只预检不上传:精确 tag/干净树 → wheel 构建/归档 → 审计 → 安装态 smoke\n"
|
|
2544
|
+
" → 只读双面预检,打印后停止(零上传)。\n"
|
|
2545
|
+
" test TestPyPI 演练(D-01/D-06/D-07/D-16):A1=create-then-verify 写入门 → TestPyPI\n"
|
|
2546
|
+
" 占用/凭据前门 → 唯一归档 wheel twine 上传 → JSON 有界验证 → 脱敏 test receipt。\n"
|
|
2547
|
+
" 同版本占用即失败(重新演练 bump tag);已有归档零重建。\n"
|
|
2548
|
+
" all 正式双源发布(D-03/D-13,建议先 release test 演练):A1/A2 门(05-VALIDATION\n"
|
|
2549
|
+
" Decision Outcomes)→ TestPyPI receipt 门(required 缺/错即失败;recommended\n"
|
|
2550
|
+
" 无回执可继续)→ 公开预检(占用/凭据/公开仓/Release 未占用/版本化链接/公开历史门)\n"
|
|
2551
|
+
" → approval.json 消费(存在即必须通过,--yes 不绕过 stale/损坏审批)→ 展示目标后\n"
|
|
2552
|
+
" 交互确认或显式 --yes → 公开 PyPI 上传并 JSON verified → GitCode 正式 Release\n"
|
|
2553
|
+
" (五键 create → upload_url → 归档字节 raw PUT → 元数据/附件 SHA256 验证)→\n"
|
|
2554
|
+
" 最终脱敏回执。公开 PyPI verified 前绝不创建 GitCode Release;已有归档零重建。\n"
|
|
2555
|
+
" resume 双源崩溃恢复(D-04/D-07/D-08):只从归档+state 恢复(不读 dist、不重建);先远端\n"
|
|
2556
|
+
" 对账——404/明确缺失才重试(重试前再过 A1 门 + 确认/approval 绑定)、同记录同\n"
|
|
2557
|
+
" hash 只对账不重传、不同内容冲突中止;裸预检不是上传授权;target=all 的待写\n"
|
|
2558
|
+
" 恢复须重新确认(--yes 或交互);全部面 verified 时幂等打印回执(DUAL-SOURCE-OK)。")
|
|
2559
|
+
|
|
2560
|
+
|
|
2561
|
+
def main(argv: list[str] | None = None) -> int:
|
|
2562
|
+
try:
|
|
2563
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
2564
|
+
sys.stderr.reconfigure(encoding="utf-8", errors="replace")
|
|
2565
|
+
except Exception:
|
|
2566
|
+
pass
|
|
2567
|
+
args = list(sys.argv[1:] if argv is None else argv)
|
|
2568
|
+
modes = [a for a in args if not a.startswith("--")]
|
|
2569
|
+
flags = [a for a in args if a.startswith("--")]
|
|
2570
|
+
if len(modes) > 1 or (modes and modes[0] not in ("test", "all", "resume")) \
|
|
2571
|
+
or any(f != "--yes" for f in flags):
|
|
2572
|
+
print(USAGE_TEXT, file=sys.stderr)
|
|
2573
|
+
return 2
|
|
2574
|
+
yes = "--yes" in flags
|
|
2575
|
+
mode = modes[0] if modes else None
|
|
2576
|
+
try:
|
|
2577
|
+
print("[1/5] 执行身份断言(cwd/脚本/审计模块同仓根,先于任何构建与网络调用)")
|
|
2578
|
+
assert_execution_identity(REPO)
|
|
2579
|
+
checkout = inspect_checkout(REPO)
|
|
2580
|
+
meta = project_meta(REPO)
|
|
2581
|
+
if mode == "resume":
|
|
2582
|
+
print("[2/5] resume:身份对账(归档+state,不读 dist、不重建)")
|
|
2583
|
+
return resume(REPO, meta, yes=yes)
|
|
2584
|
+
state = prepare_artifact(REPO, checkout,
|
|
2585
|
+
mode=mode if mode in ("test", "all") else "bare")
|
|
2586
|
+
if mode == "all":
|
|
2587
|
+
print("[5/5] release all(A1/A2 门 → 公开预检/审批 → 确认 → 公开 PyPI → GitCode → 回执)")
|
|
2588
|
+
receipt = publish_all(REPO, state, meta, yes=yes)
|
|
2589
|
+
print()
|
|
2590
|
+
print("正式双源发布完成(公开 PyPI 与 GitCode 均 verified,同 SHA256)")
|
|
2591
|
+
print(f" version: {state.version}")
|
|
2592
|
+
print(f" tag: {state.tag}(HEAD {state.head[:12]})")
|
|
2593
|
+
print(f" wheel: {state.wheel_name}({state.wheel_size} bytes)")
|
|
2594
|
+
print(f" sha256: {state.wheel_sha256}")
|
|
2595
|
+
print(f" receipt: {receipt}")
|
|
2596
|
+
return 0
|
|
2597
|
+
if mode == "test":
|
|
2598
|
+
print("[5/5] TestPyPI 演练(A1 写入门 → 占用/凭据前门 → 单归档 wheel 上传 → 有界验证)")
|
|
2599
|
+
receipt = publish_test(REPO, state, meta)
|
|
2600
|
+
print()
|
|
2601
|
+
print("TestPyPI 演练完成(verified;receipt 已写,公开双源仍未发布)")
|
|
2602
|
+
print(f" version: {state.version}")
|
|
2603
|
+
print(f" tag: {state.tag}(HEAD {state.head[:12]})")
|
|
2604
|
+
print(f" wheel: {state.wheel_name}({state.wheel_size} bytes)")
|
|
2605
|
+
print(f" sha256: {state.wheel_sha256}")
|
|
2606
|
+
print(f" receipt: {receipt}")
|
|
2607
|
+
return 0
|
|
2608
|
+
print("[5/5] 只读双面预检(凭据存在性/远端对账/占用/公开前置)")
|
|
2609
|
+
pre = preflight_remote(REPO, state, meta)
|
|
2610
|
+
state.preflight.update(pre)
|
|
2611
|
+
state.preflight["generated"] = _now_iso()
|
|
2612
|
+
state.last_command = "bare"
|
|
2613
|
+
payload = _merge_existing_state(state)
|
|
2614
|
+
write_state(state.run_dir, payload, "state.json")
|
|
2615
|
+
write_state(state.run_dir,
|
|
2616
|
+
{"version": state.version, "tag": state.tag,
|
|
2617
|
+
"wheel_sha256": state.wheel_sha256,
|
|
2618
|
+
"checks": state.preflight.get("checks", []),
|
|
2619
|
+
"candidate": state.preflight.get("candidate"),
|
|
2620
|
+
"generated": state.preflight["generated"]},
|
|
2621
|
+
"preflight.json")
|
|
2622
|
+
failed = [c for c in pre["checks"] if c["status"] != "pass"]
|
|
2623
|
+
print()
|
|
2624
|
+
print(f"预检完成(裸模式——未上传,零 twine upload / 零 GitCode POST/PUT)")
|
|
2625
|
+
print(f" version: {state.version}")
|
|
2626
|
+
print(f" tag: {state.tag}(HEAD {state.head[:12]})")
|
|
2627
|
+
print(f" wheel: {state.wheel_name}({state.wheel_size} bytes)")
|
|
2628
|
+
print(f" sha256: {state.wheel_sha256}")
|
|
2629
|
+
print(f" 归档: {state.run_dir / 'artifact' / state.wheel_name}"
|
|
2630
|
+
"(审计/smoke/后续上传均绑定此字节)")
|
|
2631
|
+
print(f" 状态: {state.run_dir / 'state.json'}(+ preflight.json)")
|
|
2632
|
+
print(f" 目标: 公开 PyPI + GitCode Release(上传模式:release test / 后续 all)")
|
|
2633
|
+
if failed:
|
|
2634
|
+
print(f"\n预检存在失败项 {len(failed)} 个:", file=sys.stderr)
|
|
2635
|
+
for c in failed:
|
|
2636
|
+
print(f" - [{c['name']}] {c['detail']}", file=sys.stderr)
|
|
2637
|
+
return 1
|
|
2638
|
+
return 0
|
|
2639
|
+
except ReleaseError as e:
|
|
2640
|
+
print(f"ERROR: {e}", file=sys.stderr)
|
|
2641
|
+
return 1
|
|
2642
|
+
|
|
2643
|
+
|
|
2644
|
+
if __name__ == "__main__":
|
|
2645
|
+
sys.exit(main())
|