|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Validate Python/pandas examples from playground HTML pages. |
| 4 | +
|
| 5 | +Extracts all <textarea class="playground-python"> blocks from playground HTML |
| 6 | +files, runs each one with Python/pandas, and reports any failures. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python scripts/validate-python-examples.py [playground_dir] |
| 10 | +
|
| 11 | +Exit code 0 if all examples pass, 1 if any fail. |
| 12 | +""" |
| 13 | + |
| 14 | +import html |
| 15 | +import os |
| 16 | +import re |
| 17 | +import subprocess |
| 18 | +import sys |
| 19 | +import tempfile |
| 20 | +import time |
| 21 | + |
| 22 | + |
| 23 | +def extract_python_blocks(html_file: str) -> list[tuple[str, str]]: |
| 24 | + """Extract Python code blocks from a playground HTML file. |
| 25 | +
|
| 26 | + Returns a list of (section_label, code) tuples. |
| 27 | + """ |
| 28 | + with open(html_file, "r", encoding="utf-8") as f: |
| 29 | + content = f.read() |
| 30 | + |
| 31 | + blocks: list[tuple[str, str]] = [] |
| 32 | + |
| 33 | + # Find all playground-python textareas |
| 34 | + pattern = re.compile( |
| 35 | + r'<textarea\s+class="playground-python"[^>]*>(.*?)</textarea>', |
| 36 | + re.DOTALL, |
| 37 | + ) |
| 38 | + |
| 39 | + # Also find section headings to label blocks |
| 40 | + section_pattern = re.compile(r"<h2>(.*?)</h2>", re.DOTALL) |
| 41 | + sections = section_pattern.findall(content) |
| 42 | + |
| 43 | + for i, match in enumerate(pattern.finditer(content)): |
| 44 | + code = html.unescape(match.group(1)) |
| 45 | + # Try to find the closest preceding section heading |
| 46 | + label = sections[i] if i < len(sections) else f"block_{i}" |
| 47 | + # Strip HTML tags from label |
| 48 | + label = re.sub(r"<[^>]+>", "", label).strip() |
| 49 | + blocks.append((label, code)) |
| 50 | + |
| 51 | + return blocks |
| 52 | + |
| 53 | + |
| 54 | +def run_python_block(code: str, label: str, html_file: str) -> tuple[bool, str, float]: |
| 55 | + """Run a Python code block and return (success, output, elapsed_ms).""" |
| 56 | + with tempfile.NamedTemporaryFile( |
| 57 | + mode="w", suffix=".py", delete=False, encoding="utf-8" |
| 58 | + ) as f: |
| 59 | + f.write(code) |
| 60 | + tmp_path = f.name |
| 61 | + |
| 62 | + try: |
| 63 | + start = time.perf_counter() |
| 64 | + result = subprocess.run( |
| 65 | + [sys.executable, tmp_path], |
| 66 | + capture_output=True, |
| 67 | + text=True, |
| 68 | + timeout=30, |
| 69 | + ) |
| 70 | + elapsed_ms = (time.perf_counter() - start) * 1000 |
| 71 | + |
| 72 | + if result.returncode != 0: |
| 73 | + return ( |
| 74 | + False, |
| 75 | + f"STDERR:\n{result.stderr}\nSTDOUT:\n{result.stdout}", |
| 76 | + elapsed_ms, |
| 77 | + ) |
| 78 | + return True, result.stdout, elapsed_ms |
| 79 | + except subprocess.TimeoutExpired: |
| 80 | + return False, "TIMEOUT (30s)", 0.0 |
| 81 | + finally: |
| 82 | + os.unlink(tmp_path) |
| 83 | + |
| 84 | + |
| 85 | +def main() -> int: |
| 86 | + playground_dir = sys.argv[1] if len(sys.argv) > 1 else "playground" |
| 87 | + |
| 88 | + if not os.path.isdir(playground_dir): |
| 89 | + print(f"Error: directory '{playground_dir}' not found", file=sys.stderr) |
| 90 | + return 1 |
| 91 | + |
| 92 | + html_files = sorted( |
| 93 | + f |
| 94 | + for f in os.listdir(playground_dir) |
| 95 | + if f.endswith(".html") and f != "index.html" |
| 96 | + ) |
| 97 | + |
| 98 | + total = 0 |
| 99 | + passed = 0 |
| 100 | + failed = 0 |
| 101 | + failures: list[str] = [] |
| 102 | + |
| 103 | + for html_file in html_files: |
| 104 | + filepath = os.path.join(playground_dir, html_file) |
| 105 | + blocks = extract_python_blocks(filepath) |
| 106 | + |
| 107 | + if not blocks: |
| 108 | + continue |
| 109 | + |
| 110 | + print(f"\n{'='*60}") |
| 111 | + print(f" {html_file} ({len(blocks)} Python blocks)") |
| 112 | + print(f"{'='*60}") |
| 113 | + |
| 114 | + for i, (label, code) in enumerate(blocks): |
| 115 | + total += 1 |
| 116 | + success, output, elapsed_ms = run_python_block( |
| 117 | + code, label, html_file |
| 118 | + ) |
| 119 | + |
| 120 | + if success: |
| 121 | + passed += 1 |
| 122 | + print(f" ✅ [{i+1}] {label} ({elapsed_ms:.1f}ms)") |
| 123 | + else: |
| 124 | + failed += 1 |
| 125 | + fail_msg = f" ❌ [{i+1}] {label} in {html_file}" |
| 126 | + print(fail_msg) |
| 127 | + print(f" {output[:200]}") |
| 128 | + failures.append(f"{html_file} [{i+1}] {label}") |
| 129 | + |
| 130 | + print(f"\n{'='*60}") |
| 131 | + print(f" Results: {passed}/{total} passed, {failed} failed") |
| 132 | + print(f"{'='*60}") |
| 133 | + |
| 134 | + if failures: |
| 135 | + print("\nFailed examples:") |
| 136 | + for f in failures: |
| 137 | + print(f" - {f}") |
| 138 | + return 1 |
| 139 | + |
| 140 | + print("\n✅ All Python examples validated successfully!") |
| 141 | + return 0 |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + sys.exit(main()) |
0 commit comments