@sokeai/cli 1.0.116 → 1.0.118
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.
- package/README.md +2 -0
- package/docs/harmonyos.md +67 -0
- package/package.json +4 -1
- package/scripts/install.js +7 -21
- package/scripts/runtime-platform.js +32 -0
- package/skills/multi/demo-init/CHANGELOG.md +1241 -0
- package/skills/multi/demo-init/README.md +137 -0
- package/skills/multi/demo-init/SKILL.legacy.md +307 -0
- package/skills/multi/demo-init/assets/placeholder.pptx +0 -0
- package/skills/multi/demo-init/references/anti-patterns.md +83 -0
- package/skills/multi/demo-init/references/asset-design.md +230 -0
- package/skills/multi/demo-init/references/asset-spec.md +94 -0
- package/skills/multi/demo-init/references/blueprint-schema.md +174 -0
- package/skills/multi/demo-init/references/cli-onboarding.md +207 -0
- package/skills/multi/demo-init/references/cli-shared.md +183 -0
- package/skills/multi/demo-init/references/contracts.md +34 -0
- package/skills/multi/demo-init/references/course-setup.md +265 -0
- package/skills/multi/demo-init/references/demo-defaults.md +132 -0
- package/skills/multi/demo-init/references/demo-verify.md +355 -0
- package/skills/multi/demo-init/references/exam-setup.md +245 -0
- package/skills/multi/demo-init/references/gates-matrix.md +38 -0
- package/skills/multi/demo-init/references/map-setup.md +283 -0
- package/skills/multi/demo-init/references/plan-parse.md +185 -0
- package/skills/multi/demo-init/references/platform-access.md +55 -0
- package/skills/multi/demo-init/references/platform-commands.md +510 -0
- package/skills/multi/demo-init/references/point-setup.md +308 -0
- package/skills/multi/demo-init/references/portal-modules.md +130 -0
- package/skills/multi/demo-init/references/portal-setup.md +342 -0
- package/skills/multi/demo-init/references/project-setup.md +173 -0
- package/skills/multi/demo-init/references/run-protocol.md +208 -0
- package/skills/multi/demo-init/references/taxonomy-setup.md +101 -0
- package/skills/multi/demo-init/scripts/assign_backfill.py +281 -0
- package/skills/multi/demo-init/scripts/blueprint_lint.py +693 -0
- package/skills/multi/demo-init/scripts/blueprint_write.py +390 -0
- package/skills/multi/demo-init/scripts/category_bind.py +303 -0
- package/skills/multi/demo-init/scripts/category_create.py +748 -0
- package/skills/multi/demo-init/scripts/category_reassign.py +405 -0
- package/skills/multi/demo-init/scripts/category_verify.py +373 -0
- package/skills/multi/demo-init/scripts/cli_kit.py +597 -0
- package/skills/multi/demo-init/scripts/cli_probe.py +205 -0
- package/skills/multi/demo-init/scripts/course_build.py +857 -0
- package/skills/multi/demo-init/scripts/course_cover_repair.py +243 -0
- package/skills/multi/demo-init/scripts/doc_lint.py +619 -0
- package/skills/multi/demo-init/scripts/dup_cleanup.py +299 -0
- package/skills/multi/demo-init/scripts/dup_scan.py +368 -0
- package/skills/multi/demo-init/scripts/exam_build.py +551 -0
- package/skills/multi/demo-init/scripts/extract_docx.py +370 -0
- package/skills/multi/demo-init/scripts/gen_assets.py +1366 -0
- package/skills/multi/demo-init/scripts/guard.py +1272 -0
- package/skills/multi/demo-init/scripts/map_rebuild.py +391 -0
- package/skills/multi/demo-init/scripts/oss_image_upload.py +367 -0
- package/skills/multi/demo-init/scripts/portal_build.py +450 -0
- package/skills/multi/demo-init/scripts/portal_home_apply.py +255 -0
- package/skills/multi/demo-init/scripts/portal_home_layout.py +398 -0
- package/skills/multi/demo-init/scripts/project_build.py +309 -0
- package/skills/multi/demo-init/scripts/rebuild_taxonomy_map.py +100 -0
- package/skills/multi/demo-init/scripts/scope_plan.py +387 -0
- package/skills/multi/demo-init/scripts/selftest_all.py +211 -0
- package/skills/multi/demo-init/scripts/stock_probe.py +452 -0
- package/skills/multi/demo-init/scripts/taxonomy_build.py +512 -0
- package/skills/multi/demo-init-4.1.0.zip +0 -0
- package/skills/multi/demo-init-4.1.1.zip +0 -0
- package/skills/multi/demo-init-4.1.2.zip +0 -0
- package/skills/multi/demo-init-4.1.3.zip +0 -0
- package/skills/multi/ziQIJJpL +0 -0
- package/skills/multi/ziRsINis +0 -0
- package/skills/multi/ziUjKWRm +0 -0
- package/skills/multi/zipFWfuj +0 -0
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""portal_build.py — 门户幂等构建器(④ portal-setup 的脚本化部分)
|
|
4
|
+
|
|
5
|
+
首次直接创建一次,不按同名探测;创建前持久化尝试,续跑按本次 UUID 恢复。
|
|
6
|
+
外部编排层必须先取模块锁。配置失败保留已成功创建的门户,只修补配置。
|
|
7
|
+
|
|
8
|
+
覆盖范围(Step 1/2/3 + 发布 + 回读):
|
|
9
|
+
Step 0 读取本地创建尝试;首次零 list,失败恢复先回读本次 UUID
|
|
10
|
+
Step 1 `portal +create --title`(⛔ flag 是 --title 不是 --name)
|
|
11
|
+
Step 2 `portal +set-appearance --color`(logo 可选;⛔ --color 非 --theme-color)
|
|
12
|
+
Step 3 `portal +banner-add --image-file --weight 1|2`(⛔ --weight 必填且 ≥1)
|
|
13
|
+
Step 3b/3c 首页模块与金刚区 → **不重复实现**,委派 `portal_home_layout.py` / `portal_home_apply.py`
|
|
14
|
+
Step 4 `portal +publish`(create 后必须显式发布)
|
|
15
|
+
回读 `+get` 核 title;横幅按蓝图 banner_asset_keys 的实际数量核对
|
|
16
|
+
|
|
17
|
+
仅支持单门户蓝图;无门户跳过,多门户显式待明确,禁止静默只取首个。
|
|
18
|
+
|
|
19
|
+
不在本脚本内:Banner 图生成(③ asset-design)、首页 layout 计算(portal_home_layout.py)、
|
|
20
|
+
导航 apply(`portal +nav-apply`,三级来源判定见 portal-setup §Step 5,需人工定 A/B/C)。
|
|
21
|
+
|
|
22
|
+
用法:
|
|
23
|
+
# 1) 先干跑看命令(默认就是干跑,不写平台)
|
|
24
|
+
PY scripts/portal_build.py --demo-root D --blueprint-id ljj-20261022
|
|
25
|
+
# 2) 确认后真跑
|
|
26
|
+
PY scripts/portal_build.py --demo-root D --blueprint-id ljj-20261022 --apply
|
|
27
|
+
# 3) 只补装修(门户已存在,不碰 create)
|
|
28
|
+
PY scripts/portal_build.py --demo-root D --blueprint-id ljj-20261022 --apply --only appearance,banner,publish
|
|
29
|
+
PY scripts/portal_build.py --self-test
|
|
30
|
+
|
|
31
|
+
⚠️ 验证状态:**未在真租户验证**(v4.0.0 新增)。首次使用请按 README「脚本验证清单」逐项核对
|
|
32
|
+
portal-verify.json 与后台实际页面,确认无误后再纳入常规流程。
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
from __future__ import annotations
|
|
36
|
+
|
|
37
|
+
import argparse
|
|
38
|
+
import json
|
|
39
|
+
import os
|
|
40
|
+
import subprocess
|
|
41
|
+
import sys
|
|
42
|
+
|
|
43
|
+
from cli_kit import (Cli, CreateJournal, Report, arg_demo_root, bp_dir, ensure_utf8_stdio,
|
|
44
|
+
jload, load_json, pick_title, pick_uuid, rows_of, save_json, short)
|
|
45
|
+
|
|
46
|
+
MODULE = "portal-setup"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
# ---------------------------------------------------------------- 纯逻辑(可自测)
|
|
50
|
+
|
|
51
|
+
def portal_name(bp):
|
|
52
|
+
"""只使用单门户蓝图里的原文名称,不从客户名或 academy_name 推造门户。"""
|
|
53
|
+
portals = bp.get("portals") or []
|
|
54
|
+
if len(portals) != 1:
|
|
55
|
+
return ""
|
|
56
|
+
return (portals[0].get("name") or "").strip()
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def theme_color(bp):
|
|
60
|
+
color = (bp.get("theme_color") or "").strip()
|
|
61
|
+
if not color:
|
|
62
|
+
portals = bp.get("portals") or []
|
|
63
|
+
if portals:
|
|
64
|
+
color = (portals[0].get("theme_color") or "").strip()
|
|
65
|
+
if not color:
|
|
66
|
+
return ""
|
|
67
|
+
return color if color.startswith("#") else "#" + color
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def match_portal(rows, name):
|
|
71
|
+
"""失败恢复的精确同名诊断;绝不按名称挑选归属对象。"""
|
|
72
|
+
return [r for r in rows if (pick_title(r) or "").strip() == name]
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def plan_banners(manifest, keys):
|
|
76
|
+
"""仅挂载门户明确引用的横幅,顺序即 weight,不推造固定 a/b 两张。"""
|
|
77
|
+
items = {}
|
|
78
|
+
for it in (manifest.get("items") or []):
|
|
79
|
+
if isinstance(it, dict) and it.get("key"):
|
|
80
|
+
items[it["key"]] = it
|
|
81
|
+
out = []
|
|
82
|
+
for weight, key in enumerate(keys or [], start=1):
|
|
83
|
+
it = items.get(key) or {}
|
|
84
|
+
local = it.get("local_path") or ""
|
|
85
|
+
out.append({"key": key, "weight": weight, "file": local,
|
|
86
|
+
"ready": bool(local and os.path.isfile(local))})
|
|
87
|
+
return out
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def plan_logo(manifest):
|
|
91
|
+
for it in (manifest.get("items") or []):
|
|
92
|
+
if isinstance(it, dict) and it.get("key") == "logo.main":
|
|
93
|
+
local = it.get("local_path") or ""
|
|
94
|
+
return local if local and os.path.isfile(local) else ""
|
|
95
|
+
return ""
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def inspect_portal(cli, uuid):
|
|
99
|
+
ok, obj = cli.read(["portal", "+get", "--id", uuid, "--format", "json"])
|
|
100
|
+
if "PORTAL_NOT_FOUND" in json.dumps(obj, ensure_ascii=False):
|
|
101
|
+
return {"state": "missing", "response": obj}
|
|
102
|
+
data = obj.get("data") or {}
|
|
103
|
+
if isinstance(data, dict) and isinstance(data.get("info"), dict):
|
|
104
|
+
data = data["info"]
|
|
105
|
+
return {"state": "exists" if ok and isinstance(data, dict)
|
|
106
|
+
and str(data.get("uuid") or data.get("id") or "") == str(uuid) else "unknown",
|
|
107
|
+
"unpublished": isinstance(data, dict) and str(data.get("status")) == "0",
|
|
108
|
+
"response": obj}
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
# ---------------------------------------------------------------- 执行
|
|
112
|
+
|
|
113
|
+
def delete_failed_portal(cli, uuid):
|
|
114
|
+
# 仅由 journal 在本次归属、创建失败、明确未发布、备份完成后调用。
|
|
115
|
+
cli.call(["portal", "+close", "--id", uuid, "--format", "json"])
|
|
116
|
+
cli.call(["portal", "+delete", "--id", uuid, "--confirm", uuid, "--format", "json"])
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def run(args):
|
|
120
|
+
d = bp_dir(args.demo_root, args.blueprint_id)
|
|
121
|
+
bp_path = args.blueprint or os.path.join(d, "DEMO-BLUEPRINT.json")
|
|
122
|
+
bp = load_json(bp_path)
|
|
123
|
+
if not isinstance(bp, dict):
|
|
124
|
+
print("✗ 读不到 BLUEPRINT:%s" % bp_path)
|
|
125
|
+
return 2
|
|
126
|
+
|
|
127
|
+
manifest = load_json(os.path.join(d, "asset-manifest.json"), {}) or {}
|
|
128
|
+
log_path = os.path.join(d, "run.log")
|
|
129
|
+
cli = Cli(args.cli, log_path=log_path, dry=not args.apply)
|
|
130
|
+
rep = Report(MODULE, os.path.join(d, "portal-verify.json"))
|
|
131
|
+
|
|
132
|
+
if cli.dry:
|
|
133
|
+
print("[dry-run] 只打印命令,不写平台;确认后加 --apply")
|
|
134
|
+
|
|
135
|
+
portals = bp.get("portals") or []
|
|
136
|
+
if not portals:
|
|
137
|
+
rep.add("portals", "skipped", "文档未要求门户,跳过构建,不推造门户")
|
|
138
|
+
return rep.dump()
|
|
139
|
+
if len(portals) != 1:
|
|
140
|
+
rep.add("portals", "blocked", "待明确:脚本仅支持单门户;当前 %d 个门户全部未执行,需逐门户独立执行" % len(portals), pending=True)
|
|
141
|
+
rep.dump()
|
|
142
|
+
return 3
|
|
143
|
+
name = portal_name(bp)
|
|
144
|
+
if not name:
|
|
145
|
+
rep.add("portals[0].name", "blocked", "待明确:文档未指定门户名称,不自动命名", pending=True)
|
|
146
|
+
rep.dump()
|
|
147
|
+
return 3
|
|
148
|
+
portal = portals[0]
|
|
149
|
+
color = theme_color(bp)
|
|
150
|
+
banner_keys = portal.get("banner_asset_keys") or []
|
|
151
|
+
only = {s.strip() for s in (args.only or "").split(",") if s.strip()}
|
|
152
|
+
for field in ("modules", "nav"):
|
|
153
|
+
if not portal.get(field):
|
|
154
|
+
rep.add(field, "blocked", "待明确:未指定或空清单,保留原门户配置,不补默认入口", pending=True)
|
|
155
|
+
|
|
156
|
+
cli.log(MODULE, "START portal_build name=%s color=%s dry=%s" % (name, color, cli.dry))
|
|
157
|
+
|
|
158
|
+
creates = CreateJournal(os.path.join(d, "portal-create-attempts.json"), "portal")
|
|
159
|
+
|
|
160
|
+
def discover(title):
|
|
161
|
+
# 门户 CLI 无已验证的精确 keyword 参数:仅失败无 UUID 时读一次并精确过滤。
|
|
162
|
+
ok, obj = cli.read(["portal", "+list", "--format", "json"])
|
|
163
|
+
if not ok:
|
|
164
|
+
raise RuntimeError("失败诊断 list 不成功,禁止按空列表重建")
|
|
165
|
+
return match_portal(rows_of(obj), title)
|
|
166
|
+
|
|
167
|
+
action, uuid, reason = ("create", None, "首次直接创建,不做同名查询")
|
|
168
|
+
if not cli.dry:
|
|
169
|
+
legacy = load_json(os.path.join(d, "portal-ids.json"), {}) or {}
|
|
170
|
+
if legacy.get("portal_uuid") and not legacy.get("dry") and not creates.get(name):
|
|
171
|
+
rep.add(name, "blocked", "旧 ID 清单无本次创建归属记录;保留对象,禁止自动重建/删除", pending=True)
|
|
172
|
+
rep.dump()
|
|
173
|
+
return 3
|
|
174
|
+
action, uuid, reason = creates.prepare(name, lambda uid: inspect_portal(cli, uid), discover,
|
|
175
|
+
lambda uid: delete_failed_portal(cli, uid))
|
|
176
|
+
if action == "pending":
|
|
177
|
+
rep.add(name, "blocked", reason, uuid=uuid, pending=True)
|
|
178
|
+
rep.dump()
|
|
179
|
+
return 3
|
|
180
|
+
cli.log(MODULE, "Step0 %s:%s" % (action, reason))
|
|
181
|
+
|
|
182
|
+
if uuid:
|
|
183
|
+
rep.add(name, "planned" if cli.dry else "reused", reason, uuid=uuid)
|
|
184
|
+
elif not only or "create" in only:
|
|
185
|
+
if not cli.dry:
|
|
186
|
+
creates.begin(name, name)
|
|
187
|
+
try:
|
|
188
|
+
ok, obj, out, err = cli.call(["portal", "+create", "--title", name, "--format", "json"])
|
|
189
|
+
except (OSError, subprocess.TimeoutExpired) as exc:
|
|
190
|
+
if not cli.dry:
|
|
191
|
+
creates.failure(name, str(exc))
|
|
192
|
+
rep.add(name, "blocked", "创建请求异常,结果不明,保留尝试记录,待恢复核查", pending=True)
|
|
193
|
+
rep.dump()
|
|
194
|
+
return 3
|
|
195
|
+
uuid = "<dry-uuid>" if cli.dry else creates.returned(name, ok, obj, out or err)
|
|
196
|
+
if ok and uuid:
|
|
197
|
+
rep.add(name, "planned" if cli.dry else "created", "portal +create", uuid=uuid)
|
|
198
|
+
cli.log(MODULE, "created portal uuid=%s" % uuid)
|
|
199
|
+
if not cli.dry:
|
|
200
|
+
save_json(os.path.join(d, "portal-ids.json"),
|
|
201
|
+
{"portal_uuid": uuid, "name": name, "color": color, "dry": False})
|
|
202
|
+
else:
|
|
203
|
+
rep.add(name, "blocked", "创建失败或结果不明,尝试已保存;重跑先恢复,不重发 create:%s" % short(out or err), pending=True)
|
|
204
|
+
cli.log(MODULE, rep.summary_line())
|
|
205
|
+
rep.dump()
|
|
206
|
+
return 3
|
|
207
|
+
|
|
208
|
+
if not uuid and not cli.dry:
|
|
209
|
+
rep.add(name, "failed", "拿不到门户 uuid,后续步骤全部跳过")
|
|
210
|
+
cli.log(MODULE, rep.summary_line())
|
|
211
|
+
return rep.dump()
|
|
212
|
+
uuid = uuid or "<uuid>"
|
|
213
|
+
|
|
214
|
+
# ---- Step 1b:回读 title(名字错了不走 api 改名,直接报错)
|
|
215
|
+
if not cli.dry:
|
|
216
|
+
ok, obj = cli.read(["portal", "+get", "--id", uuid, "--format", "json"])
|
|
217
|
+
data = obj.get("data") or {}
|
|
218
|
+
if isinstance(data, dict) and isinstance(data.get("info"), dict):
|
|
219
|
+
data = data["info"]
|
|
220
|
+
got = pick_title(data)
|
|
221
|
+
returned_uuid = pick_uuid({"data": data})
|
|
222
|
+
if not ok or not got or got.strip() != name or returned_uuid != uuid:
|
|
223
|
+
rep.add("回读 title", "failed", "本次 UUID/名称未严格回读一致;保留对象,停止后续配置", uuid=uuid)
|
|
224
|
+
creates.failure(name, "创建后详情回读不一致,不删除成功对象")
|
|
225
|
+
return rep.dump()
|
|
226
|
+
rep.add("回读 title", "verified", "title 一致:%s" % name, uuid=uuid)
|
|
227
|
+
|
|
228
|
+
# ---- Step 2:主题色 + LOGO
|
|
229
|
+
if (not only or "appearance" in only) and not color:
|
|
230
|
+
rep.add("appearance", "blocked", "待明确:未指定主题色,保留原门户形象配置", pending=True)
|
|
231
|
+
elif not only or "appearance" in only:
|
|
232
|
+
argv = ["portal", "+set-appearance", "--id", uuid, "--color", color, "--format", "json"]
|
|
233
|
+
logo = plan_logo(manifest)
|
|
234
|
+
if logo:
|
|
235
|
+
argv += ["--logo-file", logo]
|
|
236
|
+
ok, obj, out, err = cli.call(argv)
|
|
237
|
+
if ok:
|
|
238
|
+
rep.add("appearance", "planned" if cli.dry else "created",
|
|
239
|
+
"color=%s logo=%s" % (color, os.path.basename(logo) or "-"), uuid=uuid)
|
|
240
|
+
else:
|
|
241
|
+
rep.add("appearance", "failed", short(out or err), uuid=uuid)
|
|
242
|
+
cli.log(MODULE, "FAIL appearance %s" % short(out or err))
|
|
243
|
+
|
|
244
|
+
# ---- Step 3:仅挂载蓝图明确引用的横幅
|
|
245
|
+
if not only or "banner" in only:
|
|
246
|
+
banners = plan_banners(manifest, banner_keys)
|
|
247
|
+
if not banners:
|
|
248
|
+
rep.add("banner", "skipped", "未指定横幅引用,保留原门户横幅")
|
|
249
|
+
for b in banners:
|
|
250
|
+
if not b["ready"]:
|
|
251
|
+
rep.add(b["key"], "blocked", "指定横幅资产无 local_path 或文件不存在,待补齐", pending=True)
|
|
252
|
+
cli.log(MODULE, "skip banner %s(缺文件)" % b["key"])
|
|
253
|
+
continue
|
|
254
|
+
step_key = "banner:" + b["key"]
|
|
255
|
+
if not cli.dry and creates.step(name, step_key):
|
|
256
|
+
state = creates.step(name, step_key)
|
|
257
|
+
rep.add(b["key"], "skipped" if state == "success" else "blocked",
|
|
258
|
+
"本次已挂载" if state == "success" else "上次横幅请求结果不明,保留并待回读核验,不重复挂载",
|
|
259
|
+
pending=state != "success", uuid=uuid)
|
|
260
|
+
continue
|
|
261
|
+
if not cli.dry:
|
|
262
|
+
creates.step(name, step_key, "requested")
|
|
263
|
+
ok, obj, out, err = cli.call(["portal", "+banner-add", "--id", uuid,
|
|
264
|
+
"--image-file", b["file"],
|
|
265
|
+
"--weight", str(b["weight"]), "--format", "json"])
|
|
266
|
+
if ok:
|
|
267
|
+
if not cli.dry:
|
|
268
|
+
creates.step(name, step_key, "success")
|
|
269
|
+
rep.add(b["key"], "planned" if cli.dry else "created",
|
|
270
|
+
"weight=%d" % b["weight"], uuid=uuid)
|
|
271
|
+
else:
|
|
272
|
+
rep.add(b["key"], "failed", short(out or err), uuid=uuid)
|
|
273
|
+
cli.log(MODULE, "FAIL banner %s %s" % (b["key"], short(out or err)))
|
|
274
|
+
|
|
275
|
+
# ---- Step 4:发布(create 后必须显式发布)
|
|
276
|
+
if (not only or "publish" in only) and not any(
|
|
277
|
+
row["status"] in ("failed", "blocked") for row in rep.rows):
|
|
278
|
+
ok, obj, out, err = cli.call(["portal", "+publish", "--id", uuid, "--format", "json"])
|
|
279
|
+
if ok:
|
|
280
|
+
rep.add("publish", "planned" if cli.dry else "published", "portal +publish", uuid=uuid)
|
|
281
|
+
else:
|
|
282
|
+
rep.add("publish", "failed", short(out or err), uuid=uuid)
|
|
283
|
+
cli.log(MODULE, "FAIL publish %s" % short(out or err))
|
|
284
|
+
|
|
285
|
+
# ---- 回读:数量按蓝图实际横幅引用检查;未要求横幅时不强制两张
|
|
286
|
+
if not cli.dry and banner_keys and (not only or "banner" in only):
|
|
287
|
+
ok, obj = cli.read(["portal", "+banner-list", "--id", uuid, "--format", "json"])
|
|
288
|
+
n = len(rows_of(obj)) if ok else 0
|
|
289
|
+
expected = len(banner_keys)
|
|
290
|
+
rep.add("banner-list", "verified" if ok and n >= expected else "failed",
|
|
291
|
+
"%d/%d 张已挂(按蓝图引用数量)" % (n, expected), uuid=uuid)
|
|
292
|
+
|
|
293
|
+
if not cli.dry:
|
|
294
|
+
save_json(os.path.join(d, "portal-ids.json"),
|
|
295
|
+
{"portal_uuid": uuid, "name": name, "color": color, "dry": False})
|
|
296
|
+
if any(row["status"] in ("failed", "blocked") for row in rep.rows):
|
|
297
|
+
creates.failure(name, "配置或回读未完成;保留已创建门户,仅修补")
|
|
298
|
+
else:
|
|
299
|
+
creates.complete(name)
|
|
300
|
+
cli.log(MODULE, rep.summary_line())
|
|
301
|
+
|
|
302
|
+
if not args.no_hint:
|
|
303
|
+
print("\n下一步(不在本脚本内):")
|
|
304
|
+
print(" ① 首页模块与金刚区:PY scripts/portal_home_layout.py --modules <BLUEPRINT> → "
|
|
305
|
+
"PY scripts/portal_home_apply.py --file <layout.json> --apply")
|
|
306
|
+
print(" ② 底部导航:按 portal-setup §Step 5 判 A/B/C 来源后用 portal +nav-apply")
|
|
307
|
+
print(" ③ 交付前:guard.py deliver-check + dup_scan.py")
|
|
308
|
+
return rep.dump()
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
# ---------------------------------------------------------------- 自测
|
|
312
|
+
|
|
313
|
+
def self_test():
|
|
314
|
+
cases = []
|
|
315
|
+
|
|
316
|
+
def ck(cond, label):
|
|
317
|
+
cases.append((bool(cond), label))
|
|
318
|
+
|
|
319
|
+
# 门户名严格按蓝图,不从客户名或学堂别名推造
|
|
320
|
+
ck(portal_name({"portals": [{"name": "客户培训中心"}], "academy_name": "不应覆盖"}) == "客户培训中心",
|
|
321
|
+
"门户名原样且优先于 academy_name")
|
|
322
|
+
ck(portal_name({"portals": [{"name": "总门户"}]}) == "总门户", "明确指定的名称不强制改成学堂")
|
|
323
|
+
ck(portal_name({"customer_name": "客户有限公司", "academy_name": "客户学堂"}) == "",
|
|
324
|
+
"无 portals 不从客户名或 academy_name 造门户")
|
|
325
|
+
ck(portal_name({"portals": [{"name": "甲"}, {"name": "乙"}]}) == "", "多门户不静默只取首个")
|
|
326
|
+
ck(portal_name({"portals": [{}]}) == "", "未指定名称不兜底")
|
|
327
|
+
|
|
328
|
+
# theme_color
|
|
329
|
+
ck(theme_color({"theme_color": "#247A3F"}) == "#247A3F", "主题色原样")
|
|
330
|
+
ck(theme_color({"theme_color": "247A3F"}) == "#247A3F", "主题色补 #")
|
|
331
|
+
ck(theme_color({}) == "", "未指定主题色不兜底")
|
|
332
|
+
|
|
333
|
+
# match_portal:精确等值
|
|
334
|
+
rows = [{"title": "老乡鸡学堂", "uuid": "u1", "create_time": "2026-10-01"},
|
|
335
|
+
{"title": "老乡鸡学堂2", "uuid": "u2", "create_time": "2026-10-02"}]
|
|
336
|
+
hits = match_portal(rows, "老乡鸡学堂")
|
|
337
|
+
ck(len(hits) == 1 and hits[0]["uuid"] == "u1", "同名诊断只认精确等值")
|
|
338
|
+
ck(match_portal(rows, "老乡鸡") == [], "前缀不匹配")
|
|
339
|
+
dup_rows = rows + [{"title": "老乡鸡学堂", "uuid": "u3", "create_time": "2026-10-09"}]
|
|
340
|
+
ck(len(match_portal(dup_rows, "老乡鸡学堂")) == 2, "多份同名全部仅作诊断,不挑最新收编")
|
|
341
|
+
|
|
342
|
+
# plan_banners:缺文件判未就绪
|
|
343
|
+
man = {"items": [{"key": "banner.main.a", "local_path": __file__},
|
|
344
|
+
{"key": "banner.main.b", "local_path": "/nope/b.png"}]}
|
|
345
|
+
bs = plan_banners(man, ["banner.main.a", "banner.main.b"])
|
|
346
|
+
ck(bs[0]["ready"] is True and bs[0]["weight"] == 1, "首张横幅就绪 weight=1")
|
|
347
|
+
ck(bs[1]["ready"] is False and bs[1]["weight"] == 2, "第二张横幅缺文件 weight=2")
|
|
348
|
+
ck(plan_banners({}, ["banner.custom.only"])[0]["ready"] is False, "自定义引用缺文件判未就绪")
|
|
349
|
+
ck(plan_banners(man, []) == [], "未指定横幅不补 a/b")
|
|
350
|
+
ck(len(plan_banners(man, ["banner.main.a"])) == 1, "单横幅蓝图不要求第二张")
|
|
351
|
+
|
|
352
|
+
# plan_logo:不存在就不传
|
|
353
|
+
ck(plan_logo({"items": [{"key": "logo.main", "local_path": "/nope/l.png"}]}) == "",
|
|
354
|
+
"缺 logo 文件不传参")
|
|
355
|
+
|
|
356
|
+
# 执行边界:没有门户、多门户或缺名称都不得请求平台
|
|
357
|
+
import tempfile
|
|
358
|
+
from unittest.mock import patch
|
|
359
|
+
with tempfile.TemporaryDirectory(prefix="portal-build-") as tmp:
|
|
360
|
+
d = bp_dir(tmp, "test")
|
|
361
|
+
bp_path = os.path.join(d, "DEMO-BLUEPRINT.json")
|
|
362
|
+
args = argparse.Namespace(demo_root=tmp, blueprint_id="test", blueprint=None,
|
|
363
|
+
cli="unused-cli", apply=True, only=None, no_hint=True)
|
|
364
|
+
for blueprint, expected_rc, label in [
|
|
365
|
+
({}, 0, "空蓝图不造门户"),
|
|
366
|
+
({"customer_name": "客户", "portals": []}, 0, "无门户直接跳过"),
|
|
367
|
+
({"portals": [{"name": "甲"}, {"name": "乙"}]}, 3, "多门户显式待明确"),
|
|
368
|
+
({"portals": [{}]}, 3, "名称缺失不自动命名"),
|
|
369
|
+
]:
|
|
370
|
+
save_json(bp_path, blueprint)
|
|
371
|
+
with patch(__name__ + ".Cli") as mock_cli:
|
|
372
|
+
mock_cli.return_value.dry = False
|
|
373
|
+
ck(run(args) == expected_rc, label)
|
|
374
|
+
ck(not mock_cli.return_value.call.called and not mock_cli.return_value.read.called,
|
|
375
|
+
label + "且不调用平台")
|
|
376
|
+
save_json(bp_path, {"portals": [{"name": "原文培训中心", "modules": [], "nav": []}]})
|
|
377
|
+
with patch(__name__ + ".Cli") as mock_cli:
|
|
378
|
+
mock_cli.return_value.dry = True
|
|
379
|
+
mock_cli.return_value.call.return_value = (True, {}, "", "")
|
|
380
|
+
ck(run(args) == 2, "单门户可独立规划但待明确不得误报完成")
|
|
381
|
+
report = load_json(os.path.join(d, "portal-verify.json"), {})
|
|
382
|
+
ck(all(any(row["target"] == field and row.get("pending") for row in report["rows"])
|
|
383
|
+
for field in ("modules", "nav")), "空模块与导航均显式待明确")
|
|
384
|
+
ck(not any("+banner-add" in call.args[0] or "+set-appearance" in call.args[0]
|
|
385
|
+
for call in mock_cli.return_value.call.call_args_list),
|
|
386
|
+
"未要求横幅和主题色不自动挂载或改形象")
|
|
387
|
+
|
|
388
|
+
save_json(bp_path, {"portals": [{"name": "首次直建门户", "modules": ["文档模块"],
|
|
389
|
+
"nav": ["文档导航"]}], "theme_color": "123456"})
|
|
390
|
+
def response(argv):
|
|
391
|
+
if "+create" in argv:
|
|
392
|
+
return True, {"code": 200, "data": {"uuid": "own-portal"}}, "", ""
|
|
393
|
+
return False, {"code": 500}, "配置失败", ""
|
|
394
|
+
with patch(__name__ + ".Cli") as mock_cli:
|
|
395
|
+
cli = mock_cli.return_value
|
|
396
|
+
cli.dry = False
|
|
397
|
+
cli.call.side_effect = response
|
|
398
|
+
cli.read.return_value = (True, {"code": 200, "data": {
|
|
399
|
+
"uuid": "own-portal", "title": "首次直建门户", "status": 0}})
|
|
400
|
+
ck(run(args) == 2, "首次门户配置失败保留对象")
|
|
401
|
+
ck(sum("+create" in c.args[0] for c in cli.call.call_args_list) == 1
|
|
402
|
+
and not any("+list" in c.args[0] for c in cli.read.call_args_list),
|
|
403
|
+
"首次门户零 list 且 create 只发一次")
|
|
404
|
+
cli.call.reset_mock()
|
|
405
|
+
cli.read.reset_mock()
|
|
406
|
+
run(args)
|
|
407
|
+
ck(not any("+create" in c.args[0] for c in cli.call.call_args_list)
|
|
408
|
+
and not any("+list" in c.args[0] for c in cli.read.call_args_list),
|
|
409
|
+
"门户配置失败续跑按本次 UUID 修补不重建不扫同名")
|
|
410
|
+
save_json(os.path.join(d, "portal-ids.json"), {})
|
|
411
|
+
save_json(bp_path, {"portals": [{"name": "无 UUID 失败门户"}]})
|
|
412
|
+
with patch(__name__ + ".Cli") as mock_cli:
|
|
413
|
+
cli = mock_cli.return_value
|
|
414
|
+
cli.dry = False
|
|
415
|
+
cli.call.return_value = (False, {}, "timeout", "")
|
|
416
|
+
cli.read.return_value = (True, {"code": 200, "data": {"list": [
|
|
417
|
+
{"uuid": "old", "title": "无 UUID 失败门户"}]}})
|
|
418
|
+
ck(run(args) == 3, "无 UUID 首次失败 pending")
|
|
419
|
+
cli.call.reset_mock()
|
|
420
|
+
cli.read.reset_mock()
|
|
421
|
+
ck(run(args) == 3 and not cli.call.called
|
|
422
|
+
and any("+list" in c.args[0] for c in cli.read.call_args_list),
|
|
423
|
+
"无 UUID 恢复同名仅诊断不收编不删不建")
|
|
424
|
+
|
|
425
|
+
failed = [label for ok, label in cases if not ok]
|
|
426
|
+
for ok, label in cases:
|
|
427
|
+
print(" %s %s" % ("PASS" if ok else "FAIL", label))
|
|
428
|
+
print("[self-test] %d/%d PASS" % (len(cases) - len(failed), len(cases)))
|
|
429
|
+
return 0 if not failed else 1
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
def main(argv=None):
|
|
433
|
+
ensure_utf8_stdio()
|
|
434
|
+
if argv is None:
|
|
435
|
+
argv = sys.argv[1:]
|
|
436
|
+
if "--self-test" in argv:
|
|
437
|
+
return self_test()
|
|
438
|
+
ap = argparse.ArgumentParser(description="门户幂等构建器(默认干跑)")
|
|
439
|
+
arg_demo_root(ap)
|
|
440
|
+
ap.add_argument("--blueprint", help="默认 <蓝图目录>/DEMO-BLUEPRINT.json")
|
|
441
|
+
ap.add_argument("--cli", default=os.environ.get("SOKE_CLI", "soke-cli"))
|
|
442
|
+
ap.add_argument("--apply", action="store_true", help="真写平台(缺省只干跑)")
|
|
443
|
+
ap.add_argument("--only", help="只跑某几步:create,appearance,banner,publish")
|
|
444
|
+
ap.add_argument("--no-hint", action="store_true", help="不打印后续步骤提示")
|
|
445
|
+
args = ap.parse_args(argv)
|
|
446
|
+
return run(args)
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
if __name__ == "__main__":
|
|
450
|
+
sys.exit(main())
|