|
|
@@ -0,0 +1,50 @@
|
|
|
+"""跨层导入检查脚本。用法: python scripts/check_imports.py"""
|
|
|
+import sys
|
|
|
+import io
|
|
|
+from pathlib import Path
|
|
|
+
|
|
|
+# Windows 控制台编码兼容
|
|
|
+sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
|
|
|
+
|
|
|
+ROOT = Path(__file__).parent.parent
|
|
|
+violations = 0
|
|
|
+
|
|
|
+RULES = [
|
|
|
+ {
|
|
|
+ "name": "views/ 禁止直接导入 foundation/database/",
|
|
|
+ "check_dirs": ["views"],
|
|
|
+ "forbidden": ["foundation.database"],
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "name": "reviewers/ 禁止直接导入 foundation/database/",
|
|
|
+ "check_dirs": [
|
|
|
+ "core/construction_review/component/reviewers",
|
|
|
+ "core/construction_review/component/doc_worker",
|
|
|
+ ],
|
|
|
+ "forbidden": ["foundation.database"],
|
|
|
+ },
|
|
|
+]
|
|
|
+
|
|
|
+for rule in RULES:
|
|
|
+ print(f"\n检查: {rule['name']}")
|
|
|
+ for check_dir in rule["check_dirs"]:
|
|
|
+ full_dir = ROOT / check_dir
|
|
|
+ if not full_dir.exists():
|
|
|
+ print(f" 跳过(目录不存在): {check_dir}")
|
|
|
+ continue
|
|
|
+ for py_file in full_dir.rglob("*.py"):
|
|
|
+ try:
|
|
|
+ content = py_file.read_text(encoding="utf-8")
|
|
|
+ except Exception:
|
|
|
+ continue
|
|
|
+ for pattern in rule["forbidden"]:
|
|
|
+ import_line = f"from {pattern}"
|
|
|
+ if import_line in content:
|
|
|
+ print(f" ❌ {py_file.relative_to(ROOT)}")
|
|
|
+ violations += 1
|
|
|
+
|
|
|
+if violations:
|
|
|
+ print(f"\n共发现 {violations} 处跨层导入违规")
|
|
|
+ sys.exit(1)
|
|
|
+else:
|
|
|
+ print("\n跨层导入检查通过")
|