Files
CloudLab/build_site.py
hmandWorkBuddy b393b3a4a3
CloudLab CI/CD / 门禁 doctor (push) Failing after 34s
CloudLab CI/CD / 单元测试 (push) Skipped
CloudLab CI/CD / 归档完整性 (push) Skipped
CloudLab CI/CD / 构建站点与归档 (push) Skipped
CloudLab CI/CD / 部署到 lab.hdtime.space(rsync) (push) Skipped
CloudLab CI/CD / 构建镜像并推送 Gitea 容器仓库 (push) Skipped
CloudLab CI/CD / 容器部署到 lab.hdtime.space (push) Skipped
CloudLab CI/CD / 发布版本(Tag) (push) Skipped
feat(library): 大规模馆藏建设 catalog 3984→6177
用户反馈数量太少、达不到图书馆程度,并要求纳入医学/化学等基本教科书。
本轮三管齐下(tools/collect_mass.py + split_book_catalog.py):

1. NCBI Bookshelf OA 书库:从 file_list.csv(9771 本)筛出 1995 本主题相关书,
   按 13 学科生成书目索引,并**逐本拆成独立 JSON 上架**(关键改进:之前 1995 本
   塞在 13 个文件里目录只显示 13 条,拆分后每本都可独立检索)
2. 精选教材全文:并发分段下载 10 本 OA 书 tar.gz,抽 33 个章节 PDF/NXML
   (2 本解包失败已用短路径重试恢复)
3. 教材书目 35 学科(Crossref)+ EPMC 60 主题 × 500 条 + NCBI OA 章节 50 篇

诚实边界:NCBI OA 子集里真正的「经典教科书」有限(多为专业专著/报告),
多数经典教材有版权不能下载——以 1995 本书目索引覆盖各分支,全文按需取。

catalog 6177:F 医学 105→2066(1995 本书上架)、K 教材 394→461、
A 总论 56→131;search 5136 文档;站点 6165 文件 / 477.7MB。
科学基线精确更新(含参数敏感性表格,未用全局替换避免误伤常量)。

UI 216/0 全绿。

Co-Authored-By: WorkBuddy <noreply@workbuddy.ai>
2026-11-23 02:26:33 +08:00

160 lines
6.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""CloudLab 静态站点生成器(纯标准库、零联网、零凭据、零删除)。
python3 cloudlab.py build site
CLOUDLAB_ROOT=/ws CLOUDLAB_SITE_DIR=/out python3 build_site.py
产出(默认 $WORKSPACE/../CloudLab-Site,可被 CLOUDLAB_SITE_DIR 覆盖):
- 7 套资料库资产(按 policy.exclusion_reason 过滤,与归档同源规则)
- 资料库总门户.html(必须已是 7 套;由 doctor 门禁保证,本脚本不修改仓库)
- index.html 落地页,链接到门户与各库
- 站点清单.csv 逐文件 sha256,供部署后校验
设计要点:
- 本脚本**只读仓库**,不调用 build portal --apply、不改动任何受控文件,便于 CI 安全复用。
- 站点范围 = 「门户 + 7 套资产」(不含云端实验室 Flask 应用),与发布范围一致。
- 由 cloudlab.py 以 subprocess 调用,因此无需 import 防护。
"""
from __future__ import annotations
import csv
import io
import os
import shutil
import sys
from datetime import datetime
from pathlib import Path
# 允许从任意 cwd 运行,也允许被完整性校验器动态加载。
sys.path.insert(0, str(Path(__file__).resolve().parent))
from cloudlab_core.paths import WORKSPACE as WS
from cloudlab_core.registry import LIBRARY_NAMES, ASSET_DIRS
from cloudlab_core.policy import exclusion_reason
from cloudlab_core.integrity import sha256_file
def _collect() -> list[tuple[str, Path]]:
"""返回 (相对仓库根的路径, 源文件) 列表,已按路径排序。"""
# 打包范围 = 全部登记资产目录(含新采集的开放教材/前沿技术/中文资料);
# 云端实验室等 Flask 应用由 ASSET_DIRS 中非资料目录控制,不在此处。
asset_libs = [d for d in ASSET_DIRS if d in LIBRARY_NAMES or
d in ("开放教材库", "前沿技术库", "中文开放资料", "扩展资料库", "全域主题库", "结构化资源库", "馆藏建设")]
files: list[tuple[str, Path]] = []
for lib in asset_libs:
root = WS / lib
if not root.is_dir():
print(f" ⚠️ 资产目录缺失,跳过:{lib}")
continue
for p in sorted(root.rglob("*")):
if not p.is_file():
continue
rel = p.relative_to(WS).as_posix()
if exclusion_reason(rel, p):
continue
files.append((rel, p))
portal = WS / "资料库总门户.html"
if portal.is_file():
files.append(("资料库总门户.html", portal))
files.sort(key=lambda x: x[0])
return files
def _render_index(asset_libs: list[str], has_portal: bool) -> str:
stamp = datetime.now().strftime("%Y-%m-%d %H:%M")
cards = "\n".join(
f' <li><a href="{lib}/">{lib}</a></li>' for lib in asset_libs
)
portal_link = (
'<p class="lead"><a class="btn" href="资料库总门户.html">进入资料库总门户 →</a></p>'
if has_portal else '<p class="lead warn">⚠️ 门户文件缺失(doctor 门禁未通过)</p>'
)
return f"""<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CloudLab 生命科学资料库</title>
<style>
:root {{ color-scheme: light; }}
body {{ font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
margin: 0; padding: 0; background: #f6f8fa; color: #1f2328; }}
header {{ background: #0d4f8b; color: #fff; padding: 40px 24px; }}
header h1 {{ margin: 0 0 8px; font-size: 28px; }}
main {{ max-width: 880px; margin: 0 auto; padding: 32px 24px; }}
.lead {{ font-size: 17px; }}
.btn {{ display: inline-block; margin-top: 8px; padding: 10px 18px; border-radius: 8px;
background: #0d4f8b; color: #fff; text-decoration: none; font-weight: 600; }}
.warn {{ color: #b35900; font-weight: 600; }}
ul.libs {{ list-style: none; padding: 0; display: grid;
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }}
ul.libs li {{ background: #fff; border: 1px solid #d0d7de; border-radius: 8px; padding: 14px; }}
ul.libs a {{ color: #0d4f8b; text-decoration: none; font-weight: 600; }}
footer {{ max-width: 880px; margin: 0 auto; padding: 16px 24px 48px; color: #57606a; font-size: 13px; }}
</style>
</head>
<body>
<header>
<h1>CloudLab 生命科学资料库</h1>
<p>7 套资料库资产 · 统一门户导航 · 静态只读站点</p>
</header>
<main>
{portal_link}
<h2>资料库清单</h2>
<ul class="libs">
{cards}
</ul>
</main>
<footer>
由 <code>cloudlab.py build site</code> 生成 · {stamp} · 规则来自 cloudlab_core 单一事实来源
</footer>
</body>
</html>
"""
def main() -> int:
site_dir_env = os.environ.get("CLOUDLAB_SITE_DIR")
out = Path(site_dir_env).resolve() if site_dir_env else (WS.parent / "CloudLab-Site")
out = out.resolve()
# 安全防护:默认输出目录必须是约定的 CloudLab-Site,避免误清空任意目录;
# 显式设置 CLOUDLAB_SITE_DIR 时由调用方对目标负责。
if out.exists():
if out.name != "CloudLab-Site" and not site_dir_env:
print("❌ 输出目录非约定名且无 CLOUDLAB_SITE_DIR,拒绝清空以防误删")
return 1
shutil.rmtree(out)
out.mkdir(parents=True, exist_ok=True)
asset_libs = [d for d in ASSET_DIRS if d in LIBRARY_NAMES]
files = _collect()
has_portal = (WS / "资料库总门户.html").is_file()
if not has_portal:
print("⚠️ 门户文件缺失,仍继续生成(落地页会标注告警)")
(out / "index.html").write_text(
_render_index(asset_libs, has_portal), encoding="utf-8")
for rel, src in files:
dst = out / rel
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src, dst)
manifest = io.StringIO(newline="")
w = csv.writer(manifest)
w.writerow(["path", "size_bytes", "sha256"])
for rel, src in files:
w.writerow([rel, src.stat().st_size, sha256_file(src)])
(out / "站点清单.csv").write_text(manifest.getvalue(), encoding="utf-8")
total = sum(p.stat().st_size for _, p in files)
print(f"✅ 站点已生成:{out}")
print(f" 文件 {len(files)} / {total / 1048576:.1f} MB;"
f"落地页 + 站点清单已写入;范围=门户+{len(asset_libs)}套资产")
return 0
if __name__ == "__main__":
sys.exit(main())