Files
cobol-java-v3/hina/gcov_collector.py
hangshuo652 bc1d56d1a4 feat: Phase 2 complete — 13 Phases of COBOL type classification and test benchmark
P0.6: gcov infrastructure
P1: extract_structure output expansion (11 new feature fields)
P2: Confusion group rule engine (8 pairs + contradiction + backtrack)
P3: 4-factor confidence calculation + quality gate update
P4: 33+2 COBOL program type test samples (22 files, 7 categories)
P5: parametrized/ test data generation engine
P6: japanese_data.py lookup tables
P7-10: Type-specific test suites (~159 parametrized tests)
P11: Full classification pipeline (classify_program) + orchestrator integration
P12: Documentation (module-interfaces, test-plan v3.0, coverage-matrix)

Architecture decisions:
- classification_pipeline/ merged to hina/pipeline/
- parametrized/ as independent module
- japanese_data.py as root-level file
- hina/__all__ only exports classify_program()

Co-Authored-By: Claude <noreply@anthropic.com>
2026-06-19 23:51:55 +08:00

59 lines
2.0 KiB
Python

import subprocess
import logging
from pathlib import Path
logger = logging.getLogger(__name__)
def collect_gcov(cobol_src: Path, work_dir: Path) -> dict:
try:
cd = str(work_dir)
gcda_files = list(Path(cd).glob("*.gcda"))
if not gcda_files:
logger.warning("[gcov] 未找到 .gcda 文件,可能未启用插桩编译")
return {"available": False, "reason": "no_gcda_files"}
result = subprocess.run(
["gcov", cobol_src.name],
capture_output=True, text=True, timeout=30,
cwd=cd,
)
if result.returncode != 0:
logger.warning(f"[gcov] gcov 执行失败: {result.stderr[:200]}")
return {"available": False, "reason": "gcov_failed"}
gcov_file = Path(cd) / f"{cobol_src.stem}.cbl.gcov"
if not gcov_file.exists():
gcov_file = Path(cd) / f"{cobol_src.stem}.gcov"
if not gcov_file.exists():
logger.warning("[gcov] .gcov 文件未生成")
return {"available": False, "reason": "no_gcov_output"}
total_lines = 0
executed_lines = 0
with open(str(gcov_file), encoding="utf-8", errors="replace") as f:
for line in f:
stripped = line.strip()
if stripped and not stripped.startswith("-"):
total_lines += 1
if not stripped.startswith("#"):
executed_lines += 1
line_rate = executed_lines / max(total_lines, 1)
return {
"available": True,
"line_rate": round(line_rate, 4),
"total_lines": total_lines,
"executed_lines": executed_lines,
}
except FileNotFoundError:
logger.warning("[gcov] gcov 命令未找到,降级为仅静态分析")
return {"available": False, "reason": "gcov_not_installed"}
except Exception as e:
logger.warning(f"[gcov] 采集异常: {e}")
return {"available": False, "reason": str(e)[:100]}