forked from Sharktheone/arch-mojo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
247 lines (201 loc) · 8.46 KB
/
install.py
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
import os
import shutil
import subprocess
import sys
import urllib.request
from getpass import getpass
# TODO use shutil to copy files
arch = "x86_64-linux-gnu"
def param(name: str):
try:
return os.environ[name]
except:
return None
home = param("HOME")
if home is None:
home = "~"
WORKING_DIR = "~/.local/arch-mojo/"
install_global = False
onlyMojo = False
fedora = False
mojo_lib_path_from_home = ".local/lib/mojo"
mojo_lib_path = f"{home}/{mojo_lib_path_from_home}"
skip_next_arg = False
token = None
modular = shutil.which("modular") is not None
authenticated = False
if modular:
authenticated = "user.id" in subprocess.run(["modular", "config-list"], capture_output=True).stdout.decode("utf-8")
for arg in sys.argv:
if skip_next_arg:
skip_next_arg = False
continue
if arg.startswith("--dir="):
WORKING_DIR = arg.split("=")[1]
elif arg.startswith("-d="):
WORKING_DIR = arg.split("=")[1]
elif arg == "--global":
install_global = True
elif arg == "-g":
install_global = True
elif arg == "--mojo":
onlyMojo = True
elif arg == "-m":
onlyMojo = True
elif arg == "--fedora":
fedora = True
elif arg == "-f":
fedora = True
elif arg == "--modular-token":
index = sys.argv.index(arg) + 1
if index >= len(sys.argv):
print("No token provided")
exit(1)
token = sys.argv[index]
if token == "" or not token.startswith("mut_") or not len(token) == 36:
print("Invalid token")
exit(1)
skip_next_arg = True
elif arg == "--help" \
or arg == "-h":
print("Usage: python3 install.py [options]")
print("Options:")
print(" --dir=<path> | -d=<path> : Set the working directory")
print(" --global | -g : Install the libs globally")
print(" --help | -h : Show this help message")
print(" --mojo | -m : Only install mojo (modular must be installed)")
print(" --fedora | -f : Install for fedora")
print(" --modular-token <token> : Set the modular token")
exit(0)
WORKING_DIR = WORKING_DIR.replace("~", param("HOME"))
if WORKING_DIR[-1] != "/":
WORKING_DIR += "/"
if onlyMojo and not modular:
print("Modular must be installed to install mojo")
exit(1)
try:
os.makedirs(WORKING_DIR)
except FileExistsError:
pass
if fedora:
os.system("sudo dnf install binutils")
urllib.request.urlretrieve("https://ftp.debian.org/debian/pool/main/n/ncurses/libtinfo6_6.4-4_amd64.deb",
f"{WORKING_DIR}libtinfo.deb")
os.system(f"cd {WORKING_DIR} && ar -vx libtinfo.deb && tar -xf data.tar.xz")
if install_global:
os.system(f"sudo cp {WORKING_DIR}lib/{arch}/libtinfo.so.6.4 /usr/lib/")
os.system("sudo ln -s /usr/lib/libtinfo.so.6.4 /usr/lib/libtinfo.so.6")
else:
os.system(f"mkdir -p {mojo_lib_path}")
os.system(f"cp {WORKING_DIR}lib/{arch}/libtinfo.so.6.4 {mojo_lib_path}/libtinfo.so.6")
# install modular if not installed
if not modular:
# download PKGBUILD
urllib.request.urlretrieve("https://raw.githubusercontent.com/Sharktheone/arch-mojo/main/PKGBUILD",
f"{WORKING_DIR}PKGBUILD")
os.system(f"cd {WORKING_DIR} && makepkg -si")
# authenticate in modular
if not authenticated:
if token is None:
token = param("MODULAR_TOKEN")
if token is None:
token = getpass("Please enter your Modular auth token: ")
os.system(f"LD_LIBRARY_PATH=$LD_LIBRARY_PATH:{mojo_lib_path} modular auth {token}")
# download ncurses lib
urllib.request.urlretrieve("https://ftp.debian.org/debian/pool/main/n/ncurses/libncurses6_6.4-4_amd64.deb",
f"{WORKING_DIR}libncurses.deb")
urllib.request.urlretrieve("https://ftp.debian.org/debian/pool/main/libe/libedit/libedit2_3.1-20221030-2_amd64.deb",
f"{WORKING_DIR}libedit.deb")
os.system(f"cd {WORKING_DIR} && ar -vx libncurses.deb && tar -xf data.tar.xz")
os.system(f"cd {WORKING_DIR} && ar -vx libedit.deb && tar -xf data.tar.xz")
# copy libs
if install_global:
os.system(f"sudo cp {WORKING_DIR}lib/{arch}/libncurses.so.6.4 /lib/libncurses.so.6.4")
os.system(f"sudo cp {WORKING_DIR}usr/lib/{arch}/libform.so.6.4 /usr/lib/libform.so.6.4")
os.system(f"sudo cp {WORKING_DIR}usr/lib/{arch}/libpanel.so.6.4 /usr/lib/libpanel.so.6.4")
os.system(f"sudo cp {WORKING_DIR}usr/lib/{arch}/libedit.so.2.0.70 /usr/lib/libedit.so.2.0.70")
os.system("sudo ln -s /lib/libncurses.so.6.4 /lib/libncurses.so.6")
os.system("sudo ln -s /usr/lib/libform.so.6.4 /usr/lib/libform.so.6")
os.system("sudo ln -s /usr/lib/libpanel.so.6.4 /usr/lib/libpanel.so.6")
os.system("sudo ln -s /usr/lib/libedit.so.2.0.70 /usr/lib/libedit.so.2")
else:
os.system(f"mkdir -p {mojo_lib_path}")
os.system(f"cp {WORKING_DIR}lib/{arch}/libncurses.so.6.4 {mojo_lib_path}/libncurses.so.6")
os.system(f"cp {WORKING_DIR}usr/lib/{arch}/libform.so.6.4 {mojo_lib_path}/libform.so.6")
os.system(f"cp {WORKING_DIR}usr/lib/{arch}/libpanel.so.6.4 {mojo_lib_path}/libpanel.so.6")
os.system(f"cp {WORKING_DIR}usr/lib/{arch}/libedit.so.2.0.70 {mojo_lib_path}/libedit.so.2")
# install mojo
mojo = shutil.which(f"PATH=$PATH:{home}/.modular/pkg/packages.modular.com_mojo/bin/ mojo") is not None
if mojo:
print("Mojo is already installed... cleaning up")
os.system(f"PATH=$PATH:{home}/.modular/pkg/packages.modular.com_mojo/bin/ modular clean")
os.system(f"LD_LIBRARY_PATH=$LD_LIBRARY_PATH:{mojo_lib_path} modular install mojo")
# fix crashdb directory not found:
os.makedirs(f"{home}/.modular/crashdb", exist_ok=True)
def rc_path():
shell = param("SHELL")
if shell is not None:
match shell.split("/")[-1]:
case "bash":
return get_shell("bash", f"{param('HOME')}/.bashrc")
case "zsh":
return get_shell("zsh", f"{param('HOME')}/.zshrc")
case _:
path = input(
"Please enter the path to your shell rc file (e.g. ~/.bashrc for bash) or press ENTER to skip:")
if path == "":
return None
return path.replace("~", param("HOME"))
else:
return get_shell()
def get_shell(found=None, file=None):
if found is not None:
yn = input(f"Found {found} shell, add exports to {file}? [y/N/other]")
yn = yn.lower()
if yn == "y":
return file
elif yn == "n":
return None
elif yn == "other" or yn == "o":
return get_shell()
elif yn == "":
print("Skipping...")
return None
else:
print("Invalid input")
return get_shell(found, file)
else:
return get_shell_path()
def get_shell_path():
path = input(
"Please enter the path to your shell rc file (e.g. ~/.bashrc for bash) or press ENTER to skip:")
if path == "":
return None
return path.replace("~", param("HOME"))
def print_manual_instructions():
print("Please add the following to your shell rc file:")
print(f"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/{mojo_lib_path_from_home}")
print("export PATH=$PATH:~/.modular/pkg/packages.modular.com_mojo/bin/")
rc_pth = rc_path()
if rc_pth is None:
print("Skipping rc file installation")
print_manual_instructions()
exit(0)
rc_file = open(rc_pth, "a")
if rc_file is None:
print(f"Could not open {rc_pth}, skipping...")
print_manual_instructions()
exit(0)
# check if exports are already in rc file
if param("LD_LIBRARY_PATH") is None or ( # either if the lib path is not set or
not param("LD_LIBRARY_PATH").__contains__(f"~/{mojo_lib_path_from_home}") and # the relative
not param("LD_LIBRARY_PATH").__contains__(mojo_lib_path)): # and the absolute path are not in the lib path
print("wrote lib path")
rc_file.write(f"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/{mojo_lib_path_from_home}\n")
if param("PATH") is None or (
not param("PATH").__contains__("~/.modular/pkg/packages.modular.com_mojo/bin/") and
not param("PATH").__contains__(f"{home}.modular/pkg/packages.modular.com_mojo/bin/")):
print("wrote path")
rc_file.write("export PATH=$PATH:~/.modular/pkg/packages.modular.com_mojo/bin/\n")
rc_file.close()
print(f"Please restart your shell or run `source {rc_pth}` to complete the installation")