Repository navigation
Expand file tree
/
Copy pathcheck_examples.py
More file actions
executable file
·126 lines (103 loc) · 3.98 KB
/
Copy pathcheck_examples.py
File metadata and controls
executable file
·126 lines (103 loc) · 3.98 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
#!/usr/bin/env python3
"""Compiles the complete ```pony examples in this project's markdown.
A block counts as complete when it declares something at the top level, or is
only a `use` line. Statement fragments are skipped: they lean on names from the
prose around them, so compiling one would check the wrapper and not the page.
Exits non-zero and names the page and line of every example that did not
compile.
"""
import pathlib
import re
import subprocess
import sys
import tempfile
ROOT = pathlib.Path(__file__).resolve().parent
PAGES = ["README.md", "CHANGELOG.md"]
BLOCK = re.compile(r"^```pony\n(.*?)^```", re.S | re.M)
DECLARATION = re.compile(r"^(actor|class|primitive|trait|struct) \w+", re.M)
MAIN = re.compile(r"^(actor|class) Main\b", re.M)
def pages():
root_pages = [ROOT / name for name in PAGES]
return [page for page in root_pages if page.exists()] + sorted((ROOT / "docs").glob("*.md"))
def blocks():
for page in pages():
text = page.read_text()
for match in BLOCK.finditer(text):
line = text[: match.start()].count("\n") + 1
yield page.relative_to(ROOT), line, match.group(1)
def as_program(body):
"""The block as a program that can be compiled, or None for a fragment."""
lines = body.splitlines(True)
uses = []
while lines and lines[0].startswith("use "):
uses.append(lines.pop(0))
rest = "".join(lines)
if not rest.strip() or not DECLARATION.search(rest):
return None
program = "".join(uses) + ("\n" if uses else "") + rest
if not MAIN.search(program):
# A library example has no entry point of its own; an empty one makes
# the compiler link it.
program += "\nactor Main\n new create(env: Env) => None\n"
return program
def compile_example(directory, program):
"""The compiler's own words about the example, and whether it type checked.
The first passes only, so nothing is linked: an example needs the native
archive to link, and the page that shows it is checked where the archive is
not built. That it also links is what `./build.sh` proves.
"""
(directory / "main.pony").write_text(program)
finished = subprocess.run(
[
"ponyc",
"--pass",
"final",
"-p",
str(ROOT),
"-o",
str(directory),
str(directory),
],
capture_output=True,
text=True,
)
noise = ("Building ", "Writing ", "Linking ", "Generating ")
report = [
line
for line in (finished.stdout + finished.stderr).splitlines()
if line.strip() and not line.startswith(noise)
]
return finished.returncode == 0, report
def main():
checked = 0
fragments = 0
failed = 0
with tempfile.TemporaryDirectory() as workspace:
for index, (page, line, body) in enumerate(blocks(), start=1):
program = as_program(body)
if program is None:
fragments += 1
continue
checked += 1
directory = pathlib.Path(workspace) / f"example{index:02d}"
directory.mkdir()
ok, report = compile_example(directory, program)
print(("ok " if ok else "no ") + f"{page}:{line}")
if not ok:
failed += 1
for output in report:
print(" " + output)
print(f"\n{checked} examples type checked, {failed} did not, {fragments} fragments skipped")
if checked == 0:
print(
"no example was found, so nothing was checked: the pages this reads "
"are missing or their ```pony blocks are no longer marked as such",
file=sys.stderr,
)
return 1
return 1 if failed else 0
if __name__ == "__main__":
if subprocess.run(["ponyc", "--version"], capture_output=True).returncode != 0:
print("ponyc is not on the PATH, so the examples cannot be compiled", file=sys.stderr)
sys.exit(2)
sys.exit(main())