forked from huawei-csl/pto-dsl
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate_all_examples.py
More file actions
213 lines (175 loc) · 6.02 KB
/
Copy pathvalidate_all_examples.py
File metadata and controls
213 lines (175 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
"""Run all examples by executing commands from each example README.
Environment:
PTODSL_TEST_DEVICE_ID: NPU device id used by example/test scripts (e.g. 0).
If unset, those scripts default to 0 (resolved to npu:0) and print a warning.
"""
from __future__ import annotations
import argparse
import re
import subprocess
import sys
import time
from dataclasses import dataclass
from pathlib import Path
from ptodsl.npu_info import DEVICE_ENV_VAR, get_test_device
README_NAME = "README.md"
SUPPORTED_LANGS = {"bash", "sh", "shell", ""}
@dataclass
class CommandResult:
command: str
returncode: int
stdout: str
stderr: str
@dataclass
class ExampleResult:
name: str
readme_path: Path
commands: list[str]
command_results: list[CommandResult]
status: str
elapsed_seconds: float = 0.0
error: str = ""
def discover_example_readmes(examples_root: Path) -> list[Path]:
readmes = [p for p in examples_root.rglob(README_NAME) if p.parent != examples_root]
return sorted(readmes)
def extract_commands(readme_path: Path) -> list[str]:
content = readme_path.read_text(encoding="utf-8")
fenced_blocks = re.findall(r"```([^\n`]*)\n(.*?)```", content, flags=re.DOTALL)
for lang, block in fenced_blocks:
normalized_lang = lang.strip().lower()
if normalized_lang not in SUPPORTED_LANGS:
continue
commands = []
for raw_line in block.splitlines():
line = raw_line.strip()
if not line or line.startswith("#"):
continue
if line.startswith("$ "):
line = line[2:].strip()
commands.append(line)
if commands:
return commands
return []
def run_example(
example_name: str, readme_path: Path, commands: list[str]
) -> ExampleResult:
example_start = time.time()
if not commands:
return ExampleResult(
name=example_name,
readme_path=readme_path,
commands=commands,
command_results=[],
status="FAILED",
elapsed_seconds=time.time() - example_start,
error="No runnable commands found in README fenced code blocks.",
)
command_results: list[CommandResult] = []
cwd = readme_path.parent
for command in commands:
completed = subprocess.run(
command,
shell=True,
cwd=str(cwd),
text=True,
capture_output=True,
)
command_results.append(
CommandResult(
command=command,
returncode=completed.returncode,
stdout=completed.stdout,
stderr=completed.stderr,
)
)
if completed.returncode != 0:
return ExampleResult(
name=example_name,
readme_path=readme_path,
commands=commands,
command_results=command_results,
status="FAILED",
elapsed_seconds=time.time() - example_start,
error=f"Command failed: {command}",
)
return ExampleResult(
name=example_name,
readme_path=readme_path,
commands=commands,
command_results=command_results,
status="PASSED",
elapsed_seconds=time.time() - example_start,
)
def print_header(total: int) -> None:
print("=" * 78)
print("example session starts")
print(f"collected {total} example(s)")
print("=" * 78)
def print_failure_details(result: ExampleResult) -> None:
print(f"{result.name}")
print(f" README: {result.readme_path}")
if result.error:
print(f" Error: {result.error}")
for command_result in result.command_results:
if command_result.returncode == 0:
continue
print(f" Failed command: {command_result.command}")
print(f" Exit code: {command_result.returncode}")
if command_result.stdout.strip():
print(" stdout:")
for line in command_result.stdout.rstrip().splitlines():
print(f" {line}")
if command_result.stderr.strip():
print(" stderr:")
for line in command_result.stderr.rstrip().splitlines():
print(f" {line}")
break
def main() -> int:
parser = argparse.ArgumentParser(
description="Validate all examples by running commands from each example README."
)
parser.add_argument(
"--root",
default=Path(__file__).resolve().parent,
type=Path,
help="Examples root directory. Defaults to this script directory.",
)
args = parser.parse_args()
examples_root = args.root.resolve()
device = get_test_device()
print(f"Using {DEVICE_ENV_VAR}={device}")
readmes = discover_example_readmes(examples_root)
print_header(len(readmes))
results: list[ExampleResult] = []
start = time.time()
for readme in readmes:
example_name = readme.parent.relative_to(examples_root).as_posix()
commands = extract_commands(readme)
result = run_example(example_name, readme, commands)
results.append(result)
print(f"{result.name:<48} {result.status:<7} [{result.elapsed_seconds:.2f}s]")
elapsed = time.time() - start
passed = [r for r in results if r.status == "PASSED"]
failed = [r for r in results if r.status == "FAILED"]
print()
print("=" * 78)
print("short summary info")
print("=" * 78)
for result in failed:
print(f"FAILED {result.name} [{result.elapsed_seconds:.2f}s]")
for result in passed:
print(f"PASSED {result.name} [{result.elapsed_seconds:.2f}s]")
print("=" * 78)
print(f"{len(passed)} passed, {len(failed)} failed in {elapsed:.2f}s")
if failed:
print()
print("=" * 78)
print("failure details")
print("=" * 78)
for result in failed:
print_failure_details(result)
print("-" * 78)
return 1 if failed else 0
if __name__ == "__main__":
sys.exit(main())