#!/usr/bin/env python3 """ Script to update README.md with coverage badges from CI artifacts. This script should be run after CI completes to update the badges in your README. """ import os import re import sys def update_readme_badges(): """Update README.md with coverage badges.""" readme_path = "README.md" if not os.path.exists(readme_path): print("README.md not found!") return False # Read current README with open(readme_path, 'r') as f: content = f.read() # Coverage badges to add/update test_coverage_badge = "![Test Coverage](./coverage.svg)" docs_coverage_badge = "![Documentation Coverage](./coverage-docs.svg)" # Check if badges already exist and update them, otherwise add them at the top if "![Test Coverage]" in content: content = re.sub(r'!\[Test Coverage\]\([^)]+\)', test_coverage_badge, content) else: # Add after the first line (title) lines = content.split('\n') if len(lines) > 0: lines.insert(1, f"\n{test_coverage_badge}") content = '\n'.join(lines) if "![Documentation Coverage]" in content: content = re.sub(r'!\[Documentation Coverage\]\([^)]+\)', docs_coverage_badge, content) else: # Add after test coverage badge lines = content.split('\n') for i, line in enumerate(lines): if "![Test Coverage]" in line: lines.insert(i + 1, docs_coverage_badge) break content = '\n'.join(lines) # Write updated README with open(readme_path, 'w') as f: f.write(content) print("README.md updated with coverage badges!") return True def show_coverage_summary(): """Display coverage summary if available.""" if os.path.exists("coverage-summary.txt"): with open("coverage-summary.txt", 'r') as f: test_coverage = f.read().strip() print(f"Current Test Coverage: {test_coverage}") # Try to get documentation coverage from interrogate output if os.path.exists("coverage.json"): import json try: with open("coverage.json", 'r') as f: coverage_data = json.load(f) print(f"Detailed Coverage: {coverage_data['totals']['percent_covered']:.1f}%") covered = coverage_data['totals']['covered_lines'] total = coverage_data['totals']['num_statements'] print(f"Lines Covered: {covered}/{total}") except (KeyError, json.JSONDecodeError): print("Could not parse coverage data") if __name__ == "__main__": if len(sys.argv) > 1 and sys.argv[1] == "--summary": show_coverage_summary() else: update_readme_badges() show_coverage_summary()