1) 新增 cloudlab_core/(paths/policy/registry/integrity/doctor)与 cloudlab.py 统一 CLI 2) 13 个历史脚本硬编码路径收编到统一路径契约;归档打包器/校验器改读控制面 3) 门户回退护栏 + PORTAL_PIPELINE 四步幂等编排(--apply 实测 4→5→6→7) 4) tests/ 28 项 unittest + CI 门禁 + ARCHITECTURE/CONTRIBUTING/CHANGELOG 5) 索引卫生检查:重建后 n_files 3484→3434,剔除 50 个缓存/日志运行产物 6) 端到端验证归档重建:3699 文件 / 260.4 MB,控制面 11 个关键文件全部入包
144 lines
5.3 KiB
Python
144 lines
5.3 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""CloudLab 统一控制面入口(唯一推荐入口,纯标准库)。
|
||
|
||
python3 cloudlab.py doctor # 自检 + 回归门禁
|
||
python3 cloudlab.py test # 单元测试
|
||
python3 cloudlab.py verify # 归档完整性校验
|
||
python3 cloudlab.py snapshot # 自动登记本轮修订 / 新增
|
||
python3 cloudlab.py list # 查看资产与构建流水线
|
||
python3 cloudlab.py version # 版本
|
||
python3 cloudlab.py build portal # 门户编排(dry-run)
|
||
python3 cloudlab.py build portal --apply # 实际重建门户(含幂等校验)
|
||
python3 cloudlab.py build upload|deploy|archive
|
||
|
||
🔴 不要单独运行 build_portal.py:它只认 4 套资产,会把门户回退。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import argparse
|
||
import os
|
||
import re
|
||
import subprocess
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||
|
||
import cloudlab_core.registry as registry
|
||
import cloudlab_core.integrity as integrity
|
||
import cloudlab_core.doctor as doctor
|
||
from cloudlab_core import VERSION, WORKSPACE
|
||
|
||
|
||
def _run(script: str, extra_env: dict | None = None) -> int:
|
||
env = os.environ.copy()
|
||
env.update(extra_env or {})
|
||
print(f" ▶ {script}")
|
||
return subprocess.run([sys.executable, str(WORKSPACE / script)],
|
||
cwd=str(WORKSPACE), env=env).returncode
|
||
|
||
|
||
def _portal_libs() -> int:
|
||
p = WORKSPACE / "资料库总门户.html"
|
||
if not p.is_file():
|
||
return -1
|
||
m = re.search(r'"n_libs"\s*:\s*(\d+)',
|
||
p.read_text(encoding="utf-8", errors="replace"))
|
||
return int(m.group(1)) if m else -1
|
||
|
||
|
||
def cmd_version(_):
|
||
print(f"CloudLab {VERSION} (schema {registry.SCHEMA_VERSION})")
|
||
print(f"工作区:{WORKSPACE}")
|
||
return 0
|
||
|
||
|
||
def cmd_doctor(_):
|
||
return 0 if doctor.run() else 1
|
||
|
||
|
||
def cmd_test(_):
|
||
return subprocess.run(
|
||
[sys.executable, "-m", "unittest", "discover", "-s", "tests", "-v"],
|
||
cwd=str(WORKSPACE)).returncode
|
||
|
||
|
||
def cmd_verify(_):
|
||
return integrity.main([])
|
||
|
||
|
||
def cmd_snapshot(_):
|
||
return integrity.snapshot()
|
||
|
||
|
||
def cmd_list(_):
|
||
print(f"=== CloudLab {VERSION} 注册表 ===")
|
||
print(f"资料库资产({len(registry.LIBRARY_NAMES)} 套):")
|
||
for i, name in enumerate(registry.LIBRARY_NAMES, 1):
|
||
tag = "基础" if name in registry.BASE_LIBRARIES else "幂等登记"
|
||
print(f" {i}. {name} [{tag}]")
|
||
print(f"\n顶层目录({len(registry.TOP_DIRS)}):{', '.join(registry.TOP_DIRS)}")
|
||
print(f"顶层交付文件({len(registry.TOP_FILES)})")
|
||
print("\n门户构建流水线(顺序不可调换):")
|
||
for i, (s, label, expect) in enumerate(registry.PORTAL_PIPELINE, 1):
|
||
print(f" {i}. {label:<24} → n_libs={expect} {s}")
|
||
print(f"\n生成器:{registry.GENERATORS}")
|
||
print(f"当前门户 n_libs = {_portal_libs()}")
|
||
return 0
|
||
|
||
|
||
def cmd_build(a):
|
||
if a.target == "portal":
|
||
if not a.apply:
|
||
print("=== 门户构建流水线(dry-run,未改动任何文件)===")
|
||
for i, (s, label, expect) in enumerate(registry.PORTAL_PIPELINE, 1):
|
||
print(f" {i}. {label:<24} → n_libs={expect} {s}")
|
||
print(f"当前 n_libs = {_portal_libs()}")
|
||
print("\n加 --apply 才会实际重建;重建后自动登记修订快照。")
|
||
return 0
|
||
print("=== 门户重建(按注册表顺序,每步校验幂等)===")
|
||
env = {"CLOUDLAB_ALLOW_PORTAL_RESET": "1"}
|
||
for script, label, expect in registry.PORTAL_PIPELINE:
|
||
rc = _run(script, env)
|
||
if rc:
|
||
print(f"❌ 步骤失败:{label} (exit={rc})")
|
||
return rc
|
||
got = _portal_libs()
|
||
if got != expect:
|
||
print(f"❌ {label} 后 n_libs={got},期望 {expect};已中止")
|
||
return 1
|
||
print(f" ✅ {label} → n_libs={got}")
|
||
integrity.snapshot()
|
||
print(f"✅ 门户重建完成,n_libs={_portal_libs()},修订已登记")
|
||
return 0
|
||
if a.target in registry.GENERATORS:
|
||
return _run(registry.GENERATORS[a.target])
|
||
print(f"未知构建目标:{a.target}")
|
||
return 2
|
||
|
||
|
||
def main(argv=None) -> int:
|
||
ap = argparse.ArgumentParser(prog="cloudlab.py", description="CloudLab 工程控制面")
|
||
sub = ap.add_subparsers(dest="cmd", required=True)
|
||
|
||
sub.add_parser("doctor", help="自检 + 回归门禁").set_defaults(fn=cmd_doctor)
|
||
sub.add_parser("test", help="运行单元测试").set_defaults(fn=cmd_test)
|
||
sub.add_parser("verify", help="归档完整性校验").set_defaults(fn=cmd_verify)
|
||
sub.add_parser("snapshot", help="登记本轮修订 / 新增").set_defaults(fn=cmd_snapshot)
|
||
sub.add_parser("list", help="查看注册表").set_defaults(fn=cmd_list)
|
||
sub.add_parser("version", help="版本信息").set_defaults(fn=cmd_version)
|
||
|
||
p_build = sub.add_parser("build", help="构建门户 / 交付包")
|
||
p_build.add_argument("target", choices=["portal", *registry.GENERATORS.keys()])
|
||
p_build.add_argument("--apply", action="store_true",
|
||
help="portal 实际执行(默认 dry-run)")
|
||
p_build.set_defaults(fn=cmd_build)
|
||
|
||
a = ap.parse_args(argv)
|
||
return int(a.fn(a) or 0)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|