Signed-off-by: 南通一次元科技有限公司 <guwangtu@icimeta.com>
This commit is contained in:
commit
b91954df4a
106 files changed
+43359
No files matched your search
+257
@@ -0,0 +1,257 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""项目自检脚本:部署前/提交前统一检查,避免把低级错误带到线上。
|
||||
|
||||
检查项:
|
||||
1. 四个项目 + shared 的 Python 文件语法编译
|
||||
2. 公共模块(auth_guard.py)是否与 shared 一致
|
||||
3. 关键 HTML 页面是否包含统一嵌入样式、统一入口 shell.html 是否存在
|
||||
4. 前端内联 JS 语法检查(需要本机 node;没有则跳过并提示)
|
||||
5. 数据库文件是否存在、是否可打开
|
||||
6. 可选:--url 检查线上健康接口和统一入口
|
||||
|
||||
用法:
|
||||
python deploy/自检.py
|
||||
python deploy/自检.py --url http://your-server
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import sqlite3
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
WORKSPACE = Path(__file__).resolve().parent.parent
|
||||
PROJECTS = ["综合管理平台", "审计项目管理平台", "风险管理项目", "合同审核管理平台"]
|
||||
EMBED_PAGES = [
|
||||
"审计项目管理平台/templates/index.html",
|
||||
"风险管理项目/templates/index.html",
|
||||
"合同审核管理平台/index.html",
|
||||
"综合管理平台/knowledge.html",
|
||||
"综合管理平台/settings.html",
|
||||
"综合管理平台/digital_audit.html",
|
||||
]
|
||||
DATABASES = [
|
||||
"审计项目管理平台/audit_data.db",
|
||||
"风险管理项目/risk_data.db",
|
||||
"合同审核管理平台/contract.db",
|
||||
]
|
||||
SKIP_DIRS = {".venv", "__pycache__", "backups", "node_modules", ".git"}
|
||||
|
||||
failures = []
|
||||
warnings = []
|
||||
|
||||
|
||||
def fail(msg):
|
||||
failures.append(msg)
|
||||
print(f" [失败] {msg}")
|
||||
|
||||
|
||||
def warn(msg):
|
||||
warnings.append(msg)
|
||||
print(f" [提示] {msg}")
|
||||
|
||||
|
||||
def ok(msg):
|
||||
print(f" [通过] {msg}")
|
||||
|
||||
|
||||
def check_python_syntax():
|
||||
print("\n1. Python 语法检查")
|
||||
count = 0
|
||||
targets = list(WORKSPACE.glob("shared/*.py"))
|
||||
for project in PROJECTS:
|
||||
for path in (WORKSPACE / project).rglob("*.py"):
|
||||
if SKIP_DIRS.intersection(path.parts):
|
||||
continue
|
||||
targets.append(path)
|
||||
for path in targets:
|
||||
try:
|
||||
raw = path.read_bytes()
|
||||
source = None
|
||||
for enc in ("utf-8-sig", "utf-8", "gbk", "gb18030"):
|
||||
try:
|
||||
source = raw.decode(enc)
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
if source is None:
|
||||
warn(f"{path.relative_to(WORKSPACE)} 编码无法识别,跳过")
|
||||
continue
|
||||
compile(source, str(path), "exec")
|
||||
count += 1
|
||||
except SyntaxError as e:
|
||||
fail(f"{path.relative_to(WORKSPACE)} 语法错误:{e}")
|
||||
ok(f"{count} 个 Python 文件编译通过")
|
||||
|
||||
|
||||
def check_public_modules():
|
||||
print("\n2. 公共模块一致性")
|
||||
src = WORKSPACE / "shared" / "auth_guard.py"
|
||||
if not src.exists():
|
||||
fail("缺少 shared/auth_guard.py")
|
||||
return
|
||||
src_hash = hashlib.md5(src.read_bytes()).hexdigest()
|
||||
for project in PROJECTS:
|
||||
dst = WORKSPACE / project / "auth_guard.py"
|
||||
if not dst.exists():
|
||||
fail(f"{project}/auth_guard.py 缺失")
|
||||
elif hashlib.md5(dst.read_bytes()).hexdigest() != src_hash:
|
||||
fail(f"{project}/auth_guard.py 与 shared 不一致(执行 deploy/同步公共模块.py 修复)")
|
||||
else:
|
||||
ok(f"{project}/auth_guard.py 一致")
|
||||
|
||||
|
||||
def check_pages():
|
||||
print("\n3. 统一入口与嵌入页面")
|
||||
shell = WORKSPACE / "综合管理平台" / "shell.html"
|
||||
if shell.exists():
|
||||
ok("统一入口 shell.html 存在")
|
||||
else:
|
||||
fail("缺少综合管理平台/shell.html")
|
||||
for rel in EMBED_PAGES:
|
||||
path = WORKSPACE / rel
|
||||
if not path.exists():
|
||||
fail(f"缺少页面:{rel}")
|
||||
continue
|
||||
text = path.read_text(encoding="utf-8", errors="ignore")
|
||||
if "platform-embed-style" not in text:
|
||||
fail(f"{rel} 缺少统一嵌入样式(platform-embed-style)")
|
||||
elif "shared/embed_theme.css" not in text:
|
||||
fail(f"{rel} 嵌入样式不是统一主题来源(执行 deploy/同步统一主题.py)")
|
||||
elif "portalTopBack" not in text:
|
||||
warn(f"{rel} 未看到返回门户隐藏规则")
|
||||
else:
|
||||
ok(f"{rel} 统一嵌入主题正常")
|
||||
|
||||
|
||||
def node_path():
|
||||
for name in ("node", "node.exe"):
|
||||
path = shutil.which(name)
|
||||
if path:
|
||||
return path
|
||||
return None
|
||||
|
||||
|
||||
def check_js():
|
||||
print("\n4. 前端 JS 语法")
|
||||
node = node_path()
|
||||
if not node:
|
||||
warn("未找到 node,跳过 JS 语法检查")
|
||||
return
|
||||
temp = Path(tempfile.gettempdir()) / "_platform_js_check.js"
|
||||
checked = 0
|
||||
for rel in EMBED_PAGES + ["综合管理平台/shell.html"]:
|
||||
path = WORKSPACE / rel
|
||||
if not path.exists():
|
||||
continue
|
||||
html = path.read_text(encoding="utf-8", errors="ignore")
|
||||
for block in re.findall(r"<script[^>]*>(.*?)</script>", html, re.S):
|
||||
if not block.strip():
|
||||
continue
|
||||
temp.write_text(block, encoding="utf-8")
|
||||
r = subprocess.run([node, "--check", str(temp)], capture_output=True, text=True)
|
||||
if r.returncode != 0:
|
||||
fail(f"{rel} 内联 JS 语法错误:{r.stderr.strip()[:200]}")
|
||||
checked += 1
|
||||
ok(f"检查 {checked} 个内联脚本块")
|
||||
|
||||
|
||||
def check_static_assets():
|
||||
print("\n4.1 静态资源(数字化审计拆分后)")
|
||||
html_path = WORKSPACE / "综合管理平台" / "digital_audit.html"
|
||||
css_path = WORKSPACE / "综合管理平台" / "digital_audit.css"
|
||||
js_dir = WORKSPACE / "综合管理平台" / "js"
|
||||
for path in (css_path, html_path):
|
||||
if not path.exists():
|
||||
fail(f"缺少静态资源:{path.relative_to(WORKSPACE)}")
|
||||
else:
|
||||
ok(f"{path.relative_to(WORKSPACE)} 存在")
|
||||
if not js_dir.is_dir():
|
||||
fail("缺少静态资源目录:综合管理平台/js")
|
||||
return
|
||||
text = html_path.read_text(encoding="utf-8", errors="ignore")
|
||||
if "/static/digital_audit.css" not in text:
|
||||
fail("digital_audit.html 未引用 /static/digital_audit.css")
|
||||
modules = sorted(js_dir.glob("*.js"))
|
||||
if not modules:
|
||||
fail("综合管理平台/js 下没有前端模块")
|
||||
return
|
||||
for m in modules:
|
||||
if f"/static/js/{m.name}" not in text:
|
||||
fail(f"digital_audit.html 未引用前端模块 {m.name}")
|
||||
if "/static/digital_audit.js" in text:
|
||||
fail("digital_audit.html 仍引用已拆分旧文件 digital_audit.js")
|
||||
node = node_path()
|
||||
checked = 0
|
||||
if node:
|
||||
for m in modules:
|
||||
r = subprocess.run([node, "--check", str(m)], capture_output=True, text=True)
|
||||
if r.returncode != 0:
|
||||
fail(f"{m.name} 语法错误:{r.stderr.strip()[:200]}")
|
||||
else:
|
||||
checked += 1
|
||||
ok(f"{checked} 个前端模块语法通过")
|
||||
|
||||
|
||||
def check_databases():
|
||||
print("\n5. 数据库文件")
|
||||
for rel in DATABASES:
|
||||
path = WORKSPACE / rel
|
||||
if not path.exists():
|
||||
warn(f"{rel} 不存在(本地可能未下载数据,不影响代码自检)")
|
||||
continue
|
||||
try:
|
||||
conn = sqlite3.connect(f"file:{path.as_posix()}?mode=ro", uri=True)
|
||||
tables = conn.execute("SELECT COUNT(*) FROM sqlite_master WHERE type='table'").fetchone()[0]
|
||||
conn.close()
|
||||
ok(f"{rel} 可打开,{tables} 张表")
|
||||
except Exception as e:
|
||||
fail(f"{rel} 无法打开:{e}")
|
||||
|
||||
|
||||
def check_url(base):
|
||||
print(f"\n6. 线上检查:{base}")
|
||||
paths = ["/api/health", "/api/health/audit", "/api/health/risk", "/api/health/contract"]
|
||||
for p in paths:
|
||||
try:
|
||||
with urllib.request.urlopen(base.rstrip("/") + p, timeout=8) as resp:
|
||||
if resp.status == 200:
|
||||
ok(f"{p} 200")
|
||||
else:
|
||||
fail(f"{p} 返回 {resp.status}")
|
||||
except Exception as e:
|
||||
fail(f"{p} 无法访问:{e}")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--url", help="可选:线上服务根地址,如 http://your-server:8080")
|
||||
args = parser.parse_args()
|
||||
|
||||
print("=" * 60)
|
||||
print("审计管理平台 · 项目自检")
|
||||
print("=" * 60)
|
||||
check_python_syntax()
|
||||
check_public_modules()
|
||||
check_pages()
|
||||
check_js()
|
||||
check_static_assets()
|
||||
check_databases()
|
||||
if args.url:
|
||||
check_url(args.url)
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
if failures:
|
||||
print(f"自检失败:{len(failures)} 项错误,{len(warnings)} 项提示")
|
||||
return 1
|
||||
print(f"自检通过:0 项错误,{len(warnings)} 项提示")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user