Skip to content

Commit a8671c7

Browse files
author
Amitayush Thakur
committed
Added installation cmds to the library
1 parent 5c7d156 commit a8671c7

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ Homepage = "https://github.com/trishullab/itp-interface"
4545
Issues = "https://github.com/trishullab/itp-interface/issues"
4646

4747
[project.scripts]
48-
install-itp-interface = "itp_interface.main.install:install_itp_interface"
48+
install-itp-interface = "itp_interface.main.install:install_itp_interface"
49+
install-lean-repl = "itp_interface.main.install:install_lean_repl"

src/itp_interface/main/install.py

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,83 @@ def install_itp_interface():
1010
assert os.path.exists(os.path.join(repl_dir, "lean-toolchain")), f"lean-toolchain does not exist in {repl_dir}, build has failed"
1111
print("repl_dir: ", repl_dir)
1212
print("Building itp_interface")
13-
os.system(f"cd {repl_dir} && lake build repl")
13+
os.system(f"cd {repl_dir} && lake build repl")
14+
15+
def install_lean_repl():
16+
print("Updating Lean")
17+
itp_dir = os.path.dirname(os.path.dirname(file_path))
18+
tools_dir = os.path.join(itp_dir, "tools")
19+
repl_dir = os.path.join(tools_dir, "repl")
20+
assert os.path.exists(repl_dir), f"repl_dir: {repl_dir} does not exist"
21+
assert os.path.exists(os.path.join(repl_dir, "lean-toolchain")), f"lean-toolchain does not exist in {repl_dir}, build has failed"
22+
print("repl_dir: ", repl_dir)
23+
assert os.system("git --version") == 0, "git is not installed"
24+
print("[OK] git is installed")
25+
print("Checking if Lean version is set in environment variables as LEAN_VERSION")
26+
print("If not, defaulting to 4.7.0-rc2")
27+
lean_version = os.environ.get("LEAN_VERSION", "4.7.0-rc2")
28+
github_repo = "https://github.com/amit9oct/repl.git"
29+
if lean_version.strip() == "4.7.0-rc2":
30+
print("Lean version is set to 4.7.0-rc2, not cloning the REPL")
31+
else:
32+
# Clone the repl fresh
33+
print("Cloning the REPL fresh")
34+
os.system(f"rm -rf {repl_dir}")
35+
os.system(f"git clone {github_repo} {repl_dir}")
36+
# escape the version number
37+
lean_version_esc = lean_version.replace(".", "\.")
38+
print("Switching to the right REPL version", lean_version_esc)
39+
cmd_to_run = f"cd {repl_dir} && git log --grep \"v{lean_version_esc}\" --pretty=\"%h %s\""
40+
print("Running: ", cmd_to_run)
41+
output = os.popen(cmd_to_run).read()
42+
print("Output: ", output)
43+
if output == "":
44+
print(f"Could not find a commit with message containing {lean_version}")
45+
print("Probably this version does not exist in the git history of the REPL")
46+
lean_version = "4.7.0-rc2"
47+
print("Switching to v4.7.0-rc2 on commit 97182f0")
48+
os.system(f"cd {repl_dir} && git checkout 97182f0")
49+
else:
50+
# Split on first space
51+
for line in output.split("\n"):
52+
if line:
53+
commit, message = line.split(" ", 1)
54+
if lean_version in message:
55+
print(f"Switching to commit {commit}")
56+
os.system(f"cd {repl_dir} && git checkout {commit}")
57+
break
58+
# Make sure that .elan is installed
59+
print("Checking if .elan is installed")
60+
if os.path.exists(os.path.expanduser("~/.elan")):
61+
print("[OK] .elan is installed")
62+
else:
63+
print("Installing .elan")
64+
elan_url = "https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh"
65+
os.system(f"curl -sSL {elan_url} | sh")
66+
print("[OK] .elan installed")
67+
lean_repo = "leanprover-community/lean"
68+
# Create a temporary script to run
69+
shell_code = f"""
70+
source $HOME/.elan/env
71+
echo "Installing Lean 4 ({lean_repo}:{lean_version})..."
72+
elan toolchain install {lean_repo}:{lean_version}
73+
elan override set {lean_repo}:{lean_version}
74+
echo "Installed Lean 4 ({lean_repo}:{lean_version}) successfully!"
75+
export PATH=$PATH:$HOME/.elan/bin
76+
"""
77+
random_string = os.urandom(8).hex()
78+
with open(f"/tmp/{random_string}.sh", "w") as f:
79+
f.write(shell_code)
80+
os.system(f"chmod +x /tmp/{random_string}.sh")
81+
print("Running the script")
82+
os.system(f"bash /tmp/{random_string}.sh")
83+
print("Removing the script")
84+
os.system(f"rm /tmp/{random_string}.sh")
85+
assert os.system("lean --version") == 0, "Lean 4 is not installed aborting"
86+
print("[OK] Lean 4 installed successfully")
87+
print("NOTE: Please run `install-itp-interface` to finish the installation")
88+
89+
90+
91+
92+

0 commit comments

Comments
 (0)