module_boundary_audit.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. r"""模块边界审计(用户令 2026-09-22:按算法/数据接入管理/前端/后端等模块重构,组件化、高内聚低耦合、可插拔)。
  4. ## 它查什么(每条都由 `configs/modules.yaml` 驱动)
  5. | 规则 | 内容 | 退出码 |
  6. |---|---|---|
  7. | R1 结构 | 每个登记模块目录含 `common/<pkg>/__init__.py`、`common/<pkg>/api.py`、`README.md` | 5 |
  8. | R2 边界 | 模块之间**只许经对方 `api.py`**:`app_x/**/*.py` 里 import 另一个 `app_y` 的实现包(非 `api`)即违规 | 6 |
  9. | R3 依赖方向 | 只许 import `allow` 里列出的模块;反向依赖/成环即违规 | 7 |
  10. | R4 公共层纯净 | `app_common/**` 不得 import 任何其它业务模块 | 7 |
  11. | R5 接口可用 | 每个 `api.py` 的 `_TARGETS` 转发目标(既有实现的文件路径)必须真实存在 | 8 |
  12. 另外**统计迁移进度**(不算失败):`src/**`、`scripts/**` 里还有多少件尚未迁进模块目录(P1–P7 的度量)。
  13. ## 用法
  14. python scripts/module_boundary_audit.py # 审计(rc=0 通过)
  15. python scripts/module_boundary_audit.py --brief # 只打结论与进度
  16. 退出码: 0 通过 · 5 结构缺失 · 6 边界违规 · 7 依赖违规 · 8 接口目标缺失
  17. """
  18. from __future__ import annotations
  19. import ast
  20. import pathlib
  21. import sys
  22. ROOT = pathlib.Path(__file__).resolve().parents[1]
  23. sys.path.insert(0, str(ROOT))
  24. from src import paths as P # noqa: E402
  25. def load_registry() -> dict:
  26. import yaml
  27. return yaml.safe_load(P.config('modules.yaml').read_text(encoding='utf-8'))
  28. def module_pkg(name: str) -> str:
  29. return f'{name}_guanlan'
  30. def _imports_of(path: pathlib.Path) -> list[str]:
  31. """→ 该文件里所有 import 的模块名(`import a.b` 与 `from a.b import c` 都算 a.b)。"""
  32. try:
  33. tree = ast.parse(path.read_text(encoding='utf-8'))
  34. except SyntaxError:
  35. return []
  36. out = []
  37. for n in ast.walk(tree):
  38. if isinstance(n, ast.Import):
  39. out += [a.name for a in n.names]
  40. elif isinstance(n, ast.ImportFrom) and n.module:
  41. out.append(n.module)
  42. return out
  43. def audit(verbose: bool = True):
  44. reg = load_registry()
  45. mods = {m['name']: m for m in reg['modules']}
  46. problems: list[tuple[str, str]] = [] # (规则, 说明)
  47. notes: list[str] = []
  48. # R1 结构
  49. for name, m in mods.items():
  50. base = ROOT / name
  51. for rel in (f'common/__init__.py', f'common/{module_pkg(name)}/__init__.py',
  52. f'common/{module_pkg(name)}/api.py', 'README.md'):
  53. if not (base / rel).is_file():
  54. problems.append(('R1', f'{name}/ 缺 {rel}'))
  55. # R5 接口目标存在
  56. for name in mods:
  57. api = ROOT / name / 'common' / module_pkg(name) / 'api.py'
  58. if not api.is_file():
  59. continue
  60. try:
  61. tree = ast.parse(api.read_text(encoding='utf-8'))
  62. except SyntaxError as e:
  63. problems.append(('R1', f'{name}/api.py 语法错误: {e}'))
  64. continue
  65. tg = {}
  66. for n in tree.body:
  67. if isinstance(n, ast.Assign) and getattr(n.targets[0], 'id', '') == '_TARGETS':
  68. try:
  69. tg = ast.literal_eval(n.value)
  70. except Exception: # noqa: BLE001
  71. tg = {}
  72. for pub, target in tg.items():
  73. rel = pathlib.Path(*target.split('.'))
  74. if not ((ROOT / rel).with_suffix('.py').is_file() or (ROOT / rel / '__init__.py').is_file()):
  75. problems.append(('R5', f'{name}.api.{pub} → {target} 的转发目标不存在'))
  76. # R2/R3/R4 边界与依赖
  77. for name, m in mods.items():
  78. allow = set(m.get('allow') or [])
  79. for f in (ROOT / name).rglob('*.py'):
  80. for imp in _imports_of(f):
  81. parts = imp.split('.')
  82. if parts[0] not in mods or parts[0] == name:
  83. continue
  84. other = parts[0]
  85. # 只许 app_<other>.common.app_<other>_guanlan.api
  86. ok_face = (len(parts) >= 4 and parts[1] == 'common'
  87. and parts[3] == 'api' and parts[2] == module_pkg(other))
  88. if not ok_face:
  89. problems.append(('R2', f'{f.relative_to(ROOT).as_posix()} 直接 import {imp}'
  90. f'(应走 {other}.common.{module_pkg(other)}.api)'))
  91. if other not in allow:
  92. problems.append(('R3', f'{f.relative_to(ROOT).as_posix()} import {other}'
  93. f',但 {name} 的 allow 未包含它'))
  94. if name == 'app_common':
  95. problems.append(('R4', f'{f.relative_to(ROOT).as_posix()} 公共层依赖了业务模块 {other}'))
  96. # 迁移进度(不算失败)
  97. legacy = [p for p in list((ROOT / 'src').rglob('*.py')) + list((ROOT / 'scripts').rglob('*.py'))
  98. if p.is_file()]
  99. notes.append(f'尚未迁入模块目录的既有实现件: src/** {len(list((ROOT / "src").rglob("*.py")))} 件 · '
  100. f'scripts/** {len(list((ROOT / "scripts").rglob("*.py")))} 件(迁移进度按 docs/重构方案_模块化_v0.1.md 的 P1–P7 推进)')
  101. rc = 0
  102. if any(p[0] in ('R1',) for p in problems):
  103. rc = 5
  104. if any(p[0] == 'R2' for p in problems):
  105. rc = rc or 6
  106. if any(p[0] in ('R3', 'R4') for p in problems):
  107. rc = rc or 7
  108. if any(p[0] == 'R5' for p in problems):
  109. rc = rc or 8
  110. if verbose:
  111. print('模块边界审计 · 登记模块 %d 个(%s)' % (len(mods), '、'.join(mods)))
  112. for n in notes:
  113. print(' [i]', n)
  114. if problems:
  115. for rule, msg in problems:
  116. print(' [X] %s %s' % (rule, msg))
  117. else:
  118. print(' [OK] 结构齐全 · 模块间只经 api 调用 · 依赖方向合规 · 公共层纯净 · 接口目标全在位')
  119. return rc, problems, notes, mods
  120. def main() -> int:
  121. brief = '--brief' in sys.argv
  122. rc, problems, notes, mods = audit(verbose=not brief)
  123. if brief:
  124. print('[OK] module_boundary_audit rc=0(登记 %d 模块)' % len(mods) if rc == 0
  125. else '[X] module_boundary_audit rc=%d(%d 条违规)' % (rc, len(problems)))
  126. return rc
  127. if __name__ == '__main__':
  128. raise SystemExit(main())