|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Translation coverage report for PenguinLab. |
| 4 | +
|
| 5 | +Scans document/ and site/i18n/en/docusaurus-plugin-content-docs/current/ |
| 6 | +to calculate translation coverage per category and overall. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python3 scripts/coverage.py # Print report to stdout |
| 10 | + python3 scripts/coverage.py --json # Output JSON for shields.io badge |
| 11 | + python3 scripts/coverage.py --update # Update README.md badge section |
| 12 | +""" |
| 13 | + |
| 14 | +import json |
| 15 | +import os |
| 16 | +import re |
| 17 | +import sys |
| 18 | +from pathlib import Path |
| 19 | +from collections import defaultdict |
| 20 | + |
| 21 | +PROJECT_ROOT = Path(__file__).parent.parent |
| 22 | +DOCS_DIR = PROJECT_ROOT / 'document' |
| 23 | +I18N_DIR = (PROJECT_ROOT / 'site' / 'i18n' / 'en' / |
| 24 | + 'docusaurus-plugin-content-docs' / 'current') |
| 25 | + |
| 26 | +# Categories to track (subdirectory under document/) |
| 27 | +CATEGORIES = [ |
| 28 | + 'tutorials/foundations', |
| 29 | + 'tutorials/kernel', |
| 30 | + 'tutorials/drivers', |
| 31 | + 'tutorials/embedded', |
| 32 | + 'tutorials/debugging', |
| 33 | + 'tutorials/virtualization', |
| 34 | + 'notes', |
| 35 | +] |
| 36 | + |
| 37 | +# Top-level docs (not in a category) |
| 38 | +TOP_LEVEL_FILES = ['intro.md', 'booklist.md', 'qemu-reference.md'] |
| 39 | + |
| 40 | + |
| 41 | +def find_md_files(directory: Path) -> list[Path]: |
| 42 | + """Find all .md files excluding images dirs and _category_.json.""" |
| 43 | + files = [] |
| 44 | + for f in directory.rglob('*.md'): |
| 45 | + if any(part == 'images' for part in f.parts): |
| 46 | + continue |
| 47 | + if f.name == '_category_.json': |
| 48 | + continue |
| 49 | + files.append(f) |
| 50 | + return sorted(files) |
| 51 | + |
| 52 | + |
| 53 | +def get_rel_path(file_path: Path, base: Path) -> str: |
| 54 | + """Get relative path from base directory.""" |
| 55 | + try: |
| 56 | + return str(file_path.relative_to(base)) |
| 57 | + except ValueError: |
| 58 | + return str(file_path) |
| 59 | + |
| 60 | + |
| 61 | +def compute_coverage(): |
| 62 | + """Compute translation coverage stats.""" |
| 63 | + results = {} |
| 64 | + total_source = 0 |
| 65 | + total_translated = 0 |
| 66 | + |
| 67 | + # Per-category stats |
| 68 | + for cat in CATEGORIES: |
| 69 | + source_dir = DOCS_DIR / cat |
| 70 | + if not source_dir.exists(): |
| 71 | + results[cat] = {'source': 0, 'translated': 0, 'percentage': 0} |
| 72 | + continue |
| 73 | + |
| 74 | + source_files = find_md_files(source_dir) |
| 75 | + translated_count = 0 |
| 76 | + |
| 77 | + for sf in source_files: |
| 78 | + rel = get_rel_path(sf, DOCS_DIR) |
| 79 | + translated_path = I18N_DIR / rel |
| 80 | + if translated_path.exists(): |
| 81 | + translated_count += 1 |
| 82 | + |
| 83 | + count = len(source_files) |
| 84 | + pct = round(translated_count / count * 100) if count > 0 else 0 |
| 85 | + results[cat] = { |
| 86 | + 'source': count, |
| 87 | + 'translated': translated_count, |
| 88 | + 'percentage': pct, |
| 89 | + } |
| 90 | + total_source += count |
| 91 | + total_translated += translated_count |
| 92 | + |
| 93 | + # Top-level files |
| 94 | + tl_translated = 0 |
| 95 | + tl_count = 0 |
| 96 | + for fname in TOP_LEVEL_FILES: |
| 97 | + source = DOCS_DIR / fname |
| 98 | + if source.exists(): |
| 99 | + tl_count += 1 |
| 100 | + if (I18N_DIR / fname).exists(): |
| 101 | + tl_translated += 1 |
| 102 | + results['_root'] = { |
| 103 | + 'source': tl_count, |
| 104 | + 'translated': tl_translated, |
| 105 | + 'percentage': round(tl_translated / tl_count * 100) if tl_count > 0 else 0, |
| 106 | + } |
| 107 | + total_source += tl_count |
| 108 | + total_translated += tl_translated |
| 109 | + |
| 110 | + overall_pct = round(total_translated / total_source * 100) if total_source > 0 else 0 |
| 111 | + |
| 112 | + return { |
| 113 | + 'overall': { |
| 114 | + 'source': total_source, |
| 115 | + 'translated': total_translated, |
| 116 | + 'percentage': overall_pct, |
| 117 | + }, |
| 118 | + 'categories': results, |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | +def print_report(data): |
| 123 | + """Print a human-readable coverage report.""" |
| 124 | + print(f"Translation Coverage Report") |
| 125 | + print(f"{'=' * 50}") |
| 126 | + print(f"Overall: {data['overall']['translated']}/{data['overall']['source']} " |
| 127 | + f"({data['overall']['percentage']}%)") |
| 128 | + print(f"{'-' * 50}") |
| 129 | + |
| 130 | + labels = { |
| 131 | + 'tutorials/foundations': 'Foundations', |
| 132 | + 'tutorials/kernel': 'Kernel Subsystems', |
| 133 | + 'tutorials/drivers': 'Driver Development', |
| 134 | + 'tutorials/embedded': 'Embedded Full Stack', |
| 135 | + 'tutorials/debugging': 'Debugging & Perf', |
| 136 | + 'tutorials/virtualization': 'Virtualization', |
| 137 | + 'notes': 'Notes', |
| 138 | + '_root': 'Root Pages', |
| 139 | + } |
| 140 | + |
| 141 | + for cat, stats in data['categories'].items(): |
| 142 | + label = labels.get(cat, cat) |
| 143 | + pct = stats['percentage'] |
| 144 | + bar = '█' * (pct // 5) + '░' * (20 - pct // 5) |
| 145 | + print(f" {label:<25} {bar} {stats['translated']:>3}/{stats['source']:<3} ({pct}%)") |
| 146 | + |
| 147 | + print(f"{'=' * 50}") |
| 148 | + |
| 149 | + |
| 150 | +def output_json(data): |
| 151 | + """Output JSON for shields.io endpoint badge.""" |
| 152 | + badge = { |
| 153 | + 'schemaVersion': 1, |
| 154 | + 'label': 'en coverage', |
| 155 | + 'message': f"{data['overall']['percentage']}%", |
| 156 | + 'color': 'green' if data['overall']['percentage'] >= 80 else |
| 157 | + 'yellow' if data['overall']['percentage'] >= 50 else 'red', |
| 158 | + } |
| 159 | + print(json.dumps(badge, indent=2)) |
| 160 | + |
| 161 | + |
| 162 | +def update_readme(data): |
| 163 | + """Update README.md with coverage stats.""" |
| 164 | + readme_path = PROJECT_ROOT / 'README.md' |
| 165 | + if not readme_path.exists(): |
| 166 | + print("README.md not found", file=sys.stderr) |
| 167 | + return |
| 168 | + |
| 169 | + content = readme_path.read_text(encoding='utf-8') |
| 170 | + pct = data['overall']['percentage'] |
| 171 | + translated = data['overall']['translated'] |
| 172 | + total = data['overall']['source'] |
| 173 | + |
| 174 | + badge_url = f"https://img.shields.io/badge/en_coverage-{pct}%25-{'green' if pct >= 80 else 'yellow' if pct >= 50 else 'red'}.svg" |
| 175 | + |
| 176 | + # Look for existing coverage section and replace, or insert after title |
| 177 | + coverage_line = f" {translated}/{total} docs translated" |
| 178 | + |
| 179 | + marker_start = '<!-- COVERAGE_START -->' |
| 180 | + marker_end = '<!-- COVERAGE_END -->' |
| 181 | + |
| 182 | + if marker_start in content: |
| 183 | + # Replace existing section |
| 184 | + pattern = f"{marker_start}.*?{marker_end}" |
| 185 | + replacement = f"{marker_start}\n{coverage_line}\n{marker_end}" |
| 186 | + content = re.sub(pattern, replacement, content, flags=re.DOTALL) |
| 187 | + else: |
| 188 | + # Insert after first heading |
| 189 | + lines = content.split('\n') |
| 190 | + inserted = False |
| 191 | + new_lines = [] |
| 192 | + for line in lines: |
| 193 | + new_lines.append(line) |
| 194 | + if not inserted and line.startswith('# '): |
| 195 | + new_lines.append('') |
| 196 | + new_lines.append(f'{marker_start}') |
| 197 | + new_lines.append(coverage_line) |
| 198 | + new_lines.append(f'{marker_end}') |
| 199 | + inserted = True |
| 200 | + content = '\n'.join(new_lines) |
| 201 | + |
| 202 | + readme_path.write_text(content, encoding='utf-8') |
| 203 | + print(f"Updated README.md: {translated}/{total} ({pct}%)") |
| 204 | + |
| 205 | + |
| 206 | +def main(): |
| 207 | + if len(sys.argv) > 1: |
| 208 | + arg = sys.argv[1] |
| 209 | + if arg == '--json': |
| 210 | + data = compute_coverage() |
| 211 | + output_json(data) |
| 212 | + elif arg == '--update': |
| 213 | + data = compute_coverage() |
| 214 | + update_readme(data) |
| 215 | + else: |
| 216 | + print(f"Unknown argument: {arg}", file=sys.stderr) |
| 217 | + sys.exit(1) |
| 218 | + else: |
| 219 | + data = compute_coverage() |
| 220 | + print_report(data) |
| 221 | + |
| 222 | + |
| 223 | +if __name__ == '__main__': |
| 224 | + main() |
0 commit comments