-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlean-split-cli.py
More file actions
executable file
·267 lines (226 loc) · 9.3 KB
/
Copy pathlean-split-cli.py
File metadata and controls
executable file
·267 lines (226 loc) · 9.3 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python3
"""CLI client for lean-split-tool module selection and flake generation."""
import argparse
import os
import re
import sys
SITE_URL = "https://meta-introspector.github.io/lean-split-tool"
def parse_dot_file(dot_path):
"""Parse modules.dot to extract module list"""
modules = set()
with open(dot_path, "r") as f:
content = f.read()
for m in re.finditer(r'"([^"]+)"\s*->|node\s+"([^"]+)"', content):
modules.add(m.group(1) or m.group(2))
return sorted(modules)
def search_modules(modules, term):
"""Search modules by term"""
term = term.lower()
return [m for m in modules if term in m.lower()]
def generate_nix_cmd(modules):
"""Generate nix build commands"""
return (
"nix build "
+ " \\\n ".join(
f'"github:meta-introspector/mathlib4?ref=feature/split&dir={m.replace(".", "/")}"'
for m in modules
)
+ " --no-write-lock-file"
)
def generate_flake(modules, repo=None, branch=None):
"""Generate flake.nix content"""
repo = repo or "meta-introspector/mathlib4"
branch = branch or "feature/split"
inputs = [' nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";']
for m in modules:
mod_name = m.replace(".", "_").replace("-", "_")
inputs.append(
f' {mod_name}.url = "github:{repo}?ref={branch}&dir={m.replace(".", "/")}";'
)
return f"""{{
description = "Generated mathlib module flake";
inputs = {{
{chr(10).join(inputs)}
}};
outputs = {{ self, nixpkgs, ... }}@inputs:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${{system}};
in {{
packages.${{system}}.default = pkgs.stdenv.mkDerivation {{
pname = "mathlib-modules";
version = "0.1.0";
src = ./.;
}};
}};
}}"""
def find_lean_project_root(start_dir):
"""Find the root directory of a Lean project (contains lean-toolchain or lakefile)."""
current = start_dir
while True:
if os.path.exists(os.path.join(current, "lean-toolchain")):
return current
if os.path.exists(os.path.join(current, "lakefile.lean")) or os.path.exists(os.path.join(current, "lakefile.toml")):
return current
parent = os.path.dirname(current)
if parent == current or not parent:
return start_dir # Return original if no root found
current = parent
def get_source_directory(project_root):
"""Determine if project uses Mathlib/ or RequestProject/ structure."""
mathlib_path = os.path.join(project_root, "Mathlib")
request_project_path = os.path.join(project_root, "RequestProject")
if os.path.exists(request_project_path):
return "RequestProject"
elif os.path.exists(mathlib_path):
return "Mathlib"
else:
# Fallback: check which directory has .lean files
for root, dirs, files in os.walk(project_root):
for f in files:
if f.endswith('.lean'):
# Check if in Mathlib or RequestProject subdirectory
if 'Mathlib' in root.split(os.sep):
return "Mathlib"
elif 'RequestProject' in root.split(os.sep):
return "RequestProject"
return "Mathlib" # Default assumption
def build_lean_module_list(project_root, source_dir):
"""Build a list of Lean modules from the project structure."""
modules = []
# Walk through the source directory to find .lean files
for root, dirs, files in os.walk(source_dir):
for f in files:
if f.endswith('.lean') and f != 'All.lean':
full_path = os.path.join(root, f)
rel_path = os.path.relpath(full_path, project_root)
# Convert path to module name
module_name = rel_path.replace('.lean', '').replace(os.sep, '.')
modules.append(module_name)
return sorted(set(modules)) # Deduplicate and sort
def parse_lean_module_file(lean_file_path):
"""Parse a .lean file to extract its module name."""
try:
with open(lean_file_path, 'r') as f:
for line in f:
if line.strip().startswith('module '):
return line.strip().split(' ', 1)[1].rstrip('\n')
except Exception:
pass
# Fallback: try to extract from path structure
rel_path = os.path.relpath(lean_file_path, os.path.dirname(lean_file_path))
return rel_path.replace('.lean', '').replace(os.sep, '.')
def main():
parser = argparse.ArgumentParser(
description="Lean Split CLI - Select and build Lean modules"
)
parser.add_argument(
"command",
choices=["list", "search", "nix", "flake", "build", "split"],
help="Command to run",
)
parser.add_argument(
"query", nargs="?", help="Search term, module list, or path (for split)"
)
parser.add_argument(
"--dot", default="site/modules.dot", help="Path to modules.dot file"
)
parser.add_argument("--output", "-o", help="Output file for flake generation")
parser.add_argument(
"--repo",
default="meta-introspector/mathlib4",
help="Custom mathlib fork (owner/repo format)",
)
parser.add_argument(
"--branch",
default="feature/split",
help="Branch for modular flakes",
)
args = parser.parse_args()
if os.path.exists(args.dot):
modules = parse_dot_file(args.dot)
else:
try:
import urllib.request
with urllib.request.urlopen(f"{SITE_URL}/modules.dot") as resp:
content = resp.read().decode()
modules = re.findall(r'"([^"]+)"', content)
except Exception as e:
print(f"Error fetching modules: {e}")
sys.exit(1)
if args.command == "list":
for m in modules[:50]:
print(m)
print(f"... ({len(modules)} total modules)")
elif args.command == "search":
if not args.query:
print("Error: search term required")
sys.exit(1)
results = search_modules(modules, args.query)[:50]
for m in results:
print(m)
print(f"Found {len(results)} modules matching '{args.query}'")
elif args.command == "nix":
if not args.query:
print("Error: module list required")
sys.exit(1)
selected = args.query.replace(",", " ").split()
valid = [
m for m in selected if m in modules or any(m in mod for mod in modules)
]
if not valid:
print(f"No valid modules found in: {args.query}")
print("Use 'search <term>' to find modules")
sys.exit(1)
print(generate_nix_cmd(valid))
elif args.command == "flake":
if not args.query:
print("Error: module list required")
sys.exit(1)
selected = args.query.replace(",", " ").split()
valid = [
m for m in selected if m in modules or any(m in mod for mod in modules)
]
content = generate_flake(valid, args.repo, args.branch)
if args.output:
with open(args.output, "w") as f:
f.write(content)
print(f"Written to {args.output}")
else:
print(content)
elif args.command == "split":
if not args.query:
print("Error: path to mathlib source required")
print(
"Usage: nix run .#lean-split-cli split /path/to/mathlib --repo owner/repo"
"\n nix run .#lean-split-cli split /path/to/project --repo owner/repo [--source-dir RequestProject]"
)
sys.exit(1)
project_root = find_lean_project_root(args.query)
source_structure = get_source_directory(project_root)
if source_structure == "RequestProject":
# For RequestProject structure, provide explicit module discovery
print(f"⚠️ Detected RequestProject structure in {args.query}")
print(f" ℹ️ Source directory: {source_structure}")
# Find the RequestProject directory
request_project_path = os.path.join(project_root, "RequestProject")
if not os.path.exists(request_project_path):
print(f"Error: RequestProject directory not found at {request_project_path}")
sys.exit(1)
# Build list of modules from RequestProject
modules_list = build_lean_module_list(project_root, request_project_path)
print(f"Found {len(modules_list)} Lean modules in RequestProject")
if not modules_list:
print("No .lean files found in RequestProject directory")
sys.exit(1)
# For now, we'll use the split script approach but need to handle differently
print(f"Splitting project at: {args.query}")
print(f"Target: {args.repo}#{args.branch}")
else:
# For Mathlib structure, use existing logic
source_dir = os.path.join(args.query, "Mathlib") if source_structure == "Mathlib" else args.query
print(f"Splitting mathlib at: {args.query}")
print(f"Source directory: {source_dir}")
print(f"Target: {args.repo}#{args.branch}")
if __name__ == "__main__":
main()