repo_stats.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding: utf-8 -*-
  2. """统计交付物的真实规模(行数/文件数),供文档引用。"""
  3. import os
  4. ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  5. SKIP_DIRS = {"build", "build-cmake", "runs", "__pycache__", ".git", "--no-plc"}
  6. def walk(sub, exts):
  7. files = 0
  8. lines = 0
  9. base = os.path.join(ROOT, sub)
  10. for dirpath, dirnames, filenames in os.walk(base):
  11. dirnames[:] = [d for d in dirnames if d not in SKIP_DIRS]
  12. for fn in filenames:
  13. if not fn.endswith(exts):
  14. continue
  15. files += 1
  16. try:
  17. with open(os.path.join(dirpath, fn), "r", encoding="utf-8",
  18. errors="ignore") as fh:
  19. lines += sum(1 for _ in fh)
  20. except OSError:
  21. pass
  22. return files, lines
  23. GROUPS = [
  24. ("portcv (py)", "portcv", (".py",)),
  25. ("scripts (py)", "scripts", (".py",)),
  26. ("tests (py)", "tests", (".py",)),
  27. ("cpp (cpp)", "cpp", (".cpp",)),
  28. ("cpp (hpp/h)", "cpp", (".hpp", ".h")),
  29. ("docs (md)", "docs", (".md",)),
  30. ("deploy", "deploy", (".service", ".conf", ".md", ".yaml", ".txt")),
  31. ("configs (json)", "configs", (".json",)),
  32. ]
  33. if __name__ == "__main__":
  34. total = 0
  35. for name, sub, exts in GROUPS:
  36. f, ln = walk(sub, exts)
  37. total += ln
  38. print("%-14s %4d files %7d lines" % (name, f, ln))
  39. top_md = [fn for fn in os.listdir(ROOT) if fn.endswith(".md")]
  40. print("root md:", top_md)
  41. print("total lines:", total)