leanfran 0.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
leanfran/sync.py ADDED
@@ -0,0 +1,265 @@
1
+ """``leanfran sync``: check the Lean blocks of a package and generate their Python modules."""
2
+
3
+ import ast
4
+ import importlib
5
+ import json
6
+ import os
7
+ import pathlib
8
+ import re
9
+ import shutil
10
+ import subprocess
11
+ import sys
12
+
13
+ from leanfran import embed, models
14
+
15
+ LAKEFILE = """name = "leanfran-python-workspace"
16
+ defaultTargets = ["LeanPythonEmbed"]
17
+
18
+ [[lean_lib]]
19
+ name = "LeanPythonEmbed"
20
+
21
+ [[lean_lib]]
22
+ name = "LeanPythonModels"
23
+ """
24
+
25
+ _LAKE_ERROR = re.compile(r"^(error|warning): (?:\./)*(LeanPython(?:Embed|Models)/[^:]+\.lean):(\d+):(\d+): (.*)$")
26
+
27
+
28
+ class SyncError(Exception):
29
+ def __init__(self, msg, code=254):
30
+ super().__init__(msg)
31
+ self.code = code
32
+
33
+
34
+ def project_root(pkg_dir):
35
+ p = pkg_dir.resolve()
36
+ for d in [p, *p.parents]:
37
+ if (d / "pyproject.toml").exists():
38
+ return d
39
+ return p.parent
40
+
41
+
42
+ def record_blocks(pkg_dir):
43
+ """Imports every module of the package and returns the blocks their `lean(...)` calls define."""
44
+ pkg_dir = pkg_dir.resolve()
45
+ pkg = pkg_dir.name
46
+ sys.path.insert(0, str(pkg_dir.parent))
47
+ embed._recording = []
48
+ errors = []
49
+ try:
50
+ for path in sorted(pkg_dir.rglob("*.py")):
51
+ rel = path.relative_to(pkg_dir)
52
+ if rel.parts[0] == "_lean" or "__pycache__" in rel.parts or rel.name == "__main__.py":
53
+ continue
54
+ parts = [pkg, *rel.with_suffix("").parts]
55
+ if parts[-1] == "__init__":
56
+ parts = parts[:-1]
57
+ name = ".".join(parts)
58
+ try:
59
+ importlib.import_module(name)
60
+ except Exception as e: # noqa: BLE001
61
+ errors.append(f"{name}: {type(e).__name__}: {e}")
62
+ return list(embed._recording), errors
63
+ finally:
64
+ embed._recording = None
65
+
66
+
67
+ def resolve_use(block, use, by_name):
68
+ q = use if ":" in use else f"{block.module}:{use}"
69
+ b = by_name.get(q)
70
+ if b is None:
71
+ raise SyncError(f"{block.file}:{block.line}: lean block {block.name!r} uses unknown block {use!r}", 252)
72
+ return b
73
+
74
+
75
+ def block_file(block, by_name):
76
+ imports = list(block.imports)
77
+ opens = []
78
+ if block.models:
79
+ for module in models.modules_source(list(block.models)):
80
+ ns = models.dotted(models.models_namespace(module))
81
+ imports.append(ns)
82
+ opens.append(ns)
83
+ for use in block.uses:
84
+ other = resolve_use(block, use, by_name)
85
+ ns = models.dotted(other.lean_module)
86
+ imports.append(ns)
87
+ opens.append(ns)
88
+ ns = models.dotted(block.lean_module)
89
+ head = "".join(f"import {i}\n" for i in dict.fromkeys(imports))
90
+ head += f"\nnamespace {ns}\n"
91
+ head += "".join(f"open {o}\n" for o in opens)
92
+ head += "\n"
93
+ return head, head + block.source + f"\n\nend {ns}\n"
94
+
95
+
96
+ def source_line(block):
97
+ """The line of the Python file where the block's source string starts."""
98
+ try:
99
+ tree = ast.parse(pathlib.Path(block.file).read_text())
100
+ except (OSError, SyntaxError):
101
+ return block.line
102
+ for node in ast.walk(tree):
103
+ if isinstance(node, ast.Call) and node.lineno <= block.line <= (node.end_lineno or node.lineno):
104
+ args = [*node.args, *(k.value for k in node.keywords if k.arg == "source")]
105
+ for a in args:
106
+ if isinstance(a, ast.Constant) and a.value == block.source:
107
+ return a.lineno
108
+ return block.line
109
+
110
+
111
+ def map_error(line, offsets):
112
+ m = _LAKE_ERROR.match(line.strip())
113
+ if not m:
114
+ return None
115
+ sev, path, ln, col, msg = m.groups()
116
+ info = offsets.get(path)
117
+ if info is None:
118
+ return f"{path}:{ln}:{col}: {sev}: {msg}"
119
+ block, head_lines, start = info
120
+ k = int(ln) - head_lines
121
+ if k < 1:
122
+ return f"{block.file}:{block.line}: {sev}: (lean block {block.name}, Lean {ln}:{col}) {msg}"
123
+ return f"{block.file}:{start + k - 1}: {sev}: (lean block {block.name}, Lean {k}:{col}) {msg}"
124
+
125
+
126
+ def lake_messages(output, offsets):
127
+ """The messages of a Lake build mapped to the Python files, with their continuation lines."""
128
+ out = []
129
+ lines = output.splitlines()
130
+ i = 0
131
+ while i < len(lines):
132
+ mapped = map_error(lines[i], offsets)
133
+ i += 1
134
+ if mapped is None:
135
+ continue
136
+ body = [mapped]
137
+ while (
138
+ i < len(lines)
139
+ and lines[i].strip()
140
+ and map_error(lines[i], offsets) is None
141
+ and (
142
+ lines[i].startswith(" ")
143
+ or not re.match(r"^(✖|✔|⣿|trace:|info:|error:|warning:|Some required|- )", lines[i])
144
+ )
145
+ ):
146
+ body.append(lines[i])
147
+ i += 1
148
+ out.append("\n".join(body))
149
+ return out
150
+
151
+
152
+ def sync_package(pkg_dir, cli=None, out=sys.stderr):
153
+ pkg_dir = pathlib.Path(pkg_dir).resolve()
154
+ cli = cli or os.environ.get("LEANFRAN_PYTHON_CLI") or shutil.which("leanfran-python")
155
+ if cli is None:
156
+ raise SyncError("the Lean CLI `leanfran-python` is not on PATH (set LEANFRAN_PYTHON_CLI)", 255)
157
+ gv = subprocess.run([cli, "--generator-version"], check=True, capture_output=True, text=True).stdout.strip()
158
+ if gv != embed.GENERATOR_VERSION:
159
+ raise SyncError(f"{cli} generates version {gv}, this package expects {embed.GENERATOR_VERSION}", 255)
160
+
161
+ blocks, import_errors = record_blocks(pkg_dir)
162
+ for e in import_errors:
163
+ print(f"leanfran: warning: importing {e}", file=out)
164
+ by_name = {}
165
+ for b in blocks:
166
+ if b.qualname in by_name:
167
+ raise SyncError(f"{b.file}:{b.line}: lean block {b.name!r} is defined twice in {b.module}", 252)
168
+ by_name[b.qualname] = b
169
+
170
+ root = project_root(pkg_dir)
171
+ ws = root / ".leanfran" / "workspace"
172
+ for d in ("LeanPythonEmbed", "LeanPythonModels"):
173
+ shutil.rmtree(ws / d, ignore_errors=True)
174
+ ws.mkdir(parents=True, exist_ok=True)
175
+ (ws / "lakefile.toml").write_text(LAKEFILE)
176
+
177
+ model_modules = {}
178
+ for b in blocks:
179
+ if b.models:
180
+ model_modules.update(models.modules_source(list(b.models)))
181
+ offsets = {}
182
+ for module, src in model_modules.items():
183
+ p = ws / pathlib.Path(*models.models_namespace(module)).with_suffix(".lean")
184
+ p.parent.mkdir(parents=True, exist_ok=True)
185
+ p.write_text(src)
186
+ for b in blocks:
187
+ head, text = block_file(b, by_name)
188
+ rel = pathlib.Path(*b.lean_module).with_suffix(".lean")
189
+ p = ws / rel
190
+ p.parent.mkdir(parents=True, exist_ok=True)
191
+ p.write_text(text)
192
+ offsets[str(rel)] = (b, head.count("\n"), source_line(b))
193
+ (ws / "LeanPythonEmbed.lean").write_text(
194
+ "".join(f"import {models.dotted(b.lean_module)}\n" for b in blocks) or "\n"
195
+ )
196
+ (ws / "LeanPythonModels.lean").write_text(
197
+ "".join(f"import {models.dotted(models.models_namespace(m))}\n" for m in model_modules) or "\n"
198
+ )
199
+
200
+ r = subprocess.run(
201
+ ["lake", "build", "LeanPythonEmbed", "LeanPythonModels"], cwd=ws, capture_output=True, text=True, check=False
202
+ )
203
+ failed = r.returncode != 0
204
+ messages = lake_messages(r.stdout + r.stderr, offsets)
205
+ for m in messages:
206
+ if failed or ": warning:" in m.split("\n")[0]:
207
+ print(f"leanfran: {m}", file=out)
208
+ if failed:
209
+ if not messages:
210
+ print(r.stdout + r.stderr, file=out)
211
+ raise SyncError("lake build failed", 254)
212
+
213
+ out_dir = pkg_dir / "_lean"
214
+ shutil.rmtree(out_dir, ignore_errors=True)
215
+ out_dir.mkdir()
216
+ jobs = []
217
+ for b in blocks:
218
+ type_modules = [
219
+ {"namespace": ".".join(models.models_namespace(m)), "module": m, "adopt": True}
220
+ for m in (models.modules_source(list(b.models)) if b.models else {})
221
+ ]
222
+ for use in b.uses:
223
+ other = resolve_use(b, use, by_name)
224
+ type_modules.append(
225
+ {"namespace": ".".join(other.lean_module), "module": f"{other.top_package}._lean.{other.stem}"}
226
+ )
227
+ jobs.append(
228
+ {
229
+ "module": ".".join(b.lean_module),
230
+ "namespace": ".".join(b.lean_module),
231
+ "output_file": str(out_dir / f"{b.stem}.py"),
232
+ "type_modules": type_modules,
233
+ }
234
+ )
235
+ batch = ws / "batch.json"
236
+ batch.write_text(json.dumps(jobs))
237
+ r = subprocess.run(
238
+ [
239
+ cli,
240
+ "module",
241
+ "generate",
242
+ "--batch",
243
+ str(batch),
244
+ "--search-path",
245
+ str(ws / ".lake/build/lib/lean"),
246
+ "--output",
247
+ "json",
248
+ ],
249
+ capture_output=True,
250
+ text=True,
251
+ check=False,
252
+ )
253
+ if r.returncode != 0:
254
+ print(r.stderr + r.stdout, file=out)
255
+ raise SyncError("leanfran-python module generate failed", r.returncode)
256
+ summaries = json.loads(r.stdout)
257
+ for s in summaries:
258
+ for sk in s.get("skipped", []):
259
+ print(f"leanfran: skipped {sk['name']}: {sk['reason']}", file=out)
260
+ (out_dir / "__init__.py").write_text('"""Python modules generated by `leanfran sync`. Do not edit."""\n')
261
+ registry = {}
262
+ for b in blocks:
263
+ registry[b.qualname] = embed.block_key(b)
264
+ (out_dir / "blocks.json").write_text(json.dumps(registry, indent=2, sort_keys=True) + "\n")
265
+ print(f"leanfran: {len(blocks)} blocks of {pkg_dir.name} checked and generated", file=out)