-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtaiga.py
executable file
·409 lines (393 loc) · 16.4 KB
/
taiga.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/python3
from pathlib import Path
from re import search
from operator import itemgetter
from simple_term_menu import TerminalMenu
import time
import os
import json
import sys
import math
#will be important for future versions
version = 0.4
###functions for creating menus
#generic menu
def opt_menu(options,text,index=0,hint=0):
if hint == 1:
menu = TerminalMenu(options,menu_cursor=(None),quit_keys=(""),menu_highlight_style=("bg_cyan","fg_black"),title=text,skip_empty_entries=True,cursor_index=index,status_bar="\nArrow keys:Navigate options\nEnter:Select\n/:Search\nPress the key in braces to quickly select the option",status_bar_style=("fg_green","italics"),shortcut_key_highlight_style=("fg_purple","bold"),clear_screen=True)
return menu.show()
else:
menu = TerminalMenu(options,menu_cursor=(None),quit_keys=(""),menu_highlight_style=("bg_cyan","fg_black"),title=text,skip_empty_entries=True,cursor_index=index,clear_screen=True)
return menu.show()
#configs menu
def conf_menu(items,text,skip,index=0):
if skip == 0:
options = []
sc = ["[0] ","[1] ","[2] ","[3] ","[4] ","[5] ","[6] ","[7] ","[8] ","[9] ","[a] ","[b] ","[c] ","[d] ","[e] ","[f] ","[g] ","[h] ","[i] ","[j] ","[k] ","[l] ","[m] ","[n] ","[o] ","[p] ","[q] ","[r] ","[s] ","[t] ","[u] ","[v] ","[w] ","[x] ","[y] ","[z] "]
n = 0
for i in items:
if len(items) < 11:
options.append("["+str(n)+"]"+i["name"])
else:
options.append(sc[n]+i["name"])
n+=1
menu = TerminalMenu(options,menu_cursor=(None),quit_keys=(""),menu_highlight_style=("bg_purple","fg_black"),title=text,cursor_index=index,status_bar="\nArrow keys:Navigate options\nEnter:Select\n/:Search\nPress the key in braces to quickly select the option",status_bar_style=("fg_green","italics"),shortcut_key_highlight_style=("fg_red","bold"),clear_screen=True)
return menu.show()
else:
options = ["[0] Skip"]
sc = ["[1] ","[2] ","[3] ","[4] ","[5] ","[6] ","[7] ","[8] ","[9] ","[a] ","[b] ","[c] ","[d] ","[e] ","[f] ","[g] ","[h] ","[i] ","[j] ","[k] ","[l] ","[m] ","[n] ","[o] ","[p] ","[q] ","[r] ","[s] ","[t] ","[u] ","[v] ","[w] ","[x] ","[y] ","[z] "]
n=1
for i in items:
if len(items) < 11:
options.append("["+str(n)+"]"+i["name"])
else:
options.append(sc[n]+i["name"])
n+=1
menu = TerminalMenu(options,menu_cursor=(None),quit_keys=(""),menu_highlight_style=("bg_blue","fg_black"),title=text,cursor_index=index,status_bar="\nArrow keys:Navigate options\nEnter:Select\n/:Search\nPress the key in braces to quickly select the option",status_bar_style=("fg_green","italics"),shortcut_key_highlight_style=("fg_blue","bold"),clear_screen=True)
return menu.show()-1
#selection menu
def select_menu(items,text,selected):
terminal_menu = TerminalMenu(items,menu_cursor=(None),quit_keys=(""),menu_highlight_style=("bg_yellow","fg_black"),multi_select=True,multi_select_select_on_accept=False,multi_select_empty_ok=True,title=text,multi_select_cursor_style=("fg_red","bold"),preselected_entries=selected,status_bar="\nArrow keys:Navigate options\nTab/Space:Toggle options\nEnter:Confirm selection\n/:Search",status_bar_style=("fg_cyan","italics"),exit_on_shortcut=False,clear_screen=True)
return terminal_menu.show()
#builds the script for execution
def script(build,distro,name = "taiga_"+str(math.ceil(time.time()))):
file = ""
try:
file = open(name,"x")
except:
file = open("taiga_"+str(math.ceil(time.time())*2),"x")
if 0 in build["options"]:
file.write("echo Configuring base system\n")
for b in distro["pre"]:
file.write(b+"\n")
if build["GD"]!=-1:
file.write("echo Installing graphics drivers\n")
for g in distro["GD"][build["GD"]]["comm"]:
file.write(g+"\n")
if build["DM"]!=-1:
file.write("echo Installing display manager\n")
for d in distro["DM"][build["DM"]]["comm"]:
file.write(d+"\n")
if build["DE"]!=-1:
file.write("echo Installing desktop environment\n")
for e in distro["DE"][build["DE"]]["comm"]:
file.write(e+"\n")
if len(build["tasks"])>0:
file.write("echo Configuring extra tasks\n")
for t in build["tasks"]:
file.write("echo " + distro["tasks"][t]["name"]+"\n")
for c in distro["tasks"][t]["comm"]:
file.write(c+"\n")
if 1 in build["options"]:
file.write("echo Final configuration\n")
for f in distro["post"]:
file.write(f+"\n")
if 2 in build["options"]:
file.write("echo Installation complete!Rebooting...\n")
cmd = os.system("which systemctl")
if cmd == 0:
file.write("sudo systemctl reboot\n")
else:
file.write("sudo reboot\n")
else:
file.write("echo Installation complete!")
file.close()
os.system("clear")
return file.name
#main menu
def main_menu(build,distros,index):
#create base options
options = ["[o] Distro:","[g] Graphics:","[m] Display manager:","[d] Desktop environment:","","[x] Extra tasks:"+str(len(build["tasks"])),"[a] Additional options","","[i] Install","[s] Save config to file","[b] Build script","[q] Quit installation"]
options[0]+=distros[build["distro"]]["name"]
if build["GD"]==-1:
options[1]+=" None"
else:
options[1]+=distros[build["distro"]]["GD"][build["GD"]]["name"]
if build["DM"]==-1:
options[2]+=" None"
else:
options[2]+=distros[build["distro"]]["DM"][build["DM"]]["name"]
if build["DE"]==-1:
options[3]+=" None"
else:
options[3]+=distros[build["distro"]]["DE"][build["DE"]]["name"]
#wait for choice from user
action = opt_menu(options,"TAIGA "+str(version),index,1)
match action:
case 0:
#change user distro
choice = conf_menu(distros,"Select your distro from the list of supported distros:",0,build["distro"])
build["distro"] = choice
build["GD"] = get_gpu(distros[build["distro"]])
build["DM"] = -1
build["DE"] = -1
build["tasks"] = []
main_menu(build,distros,action)
case 1:
#change graphics driver
choice = conf_menu(distros[build["distro"]]["GD"],"Select your graphics driver:",1,build["GD"]+1)
build["GD"]=choice
main_menu(build,distros,action)
case 2:
#change display manager
choice = conf_menu(distros[build["distro"]]["DM"],"Select your display manager:",1,build["DM"]+1)
build["DM"] = choice
main_menu(build,distros,action)
case 3:
#change desktop emvironment
choice = conf_menu(distros[build["distro"]]["DE"],"Select your desktop environment:",1,build["DE"]+1)
build["DE"] = choice
#autoassign display manager
i = 0
if build["DE"]!=-1:
for dm in distros[build["distro"]]["DM"]:
if dm["name"] == distros[build["distro"]]["DE"][build["DE"]]["DM"]:
build["DM"] = i
i+=1
main_menu(build,distros,action)
case 5:
#toggle tasks
build["tasks"].sort()
tasks = []
for t in distros[build["distro"]]["tasks"]:
tasks.append(t["name"])
choice = select_menu(tasks,"Extra tasks",build["tasks"])
build["tasks"] = list(choice or [])
main_menu(build,distros,action)
case 6:
#additional options
build["options"].sort()
choice = select_menu(["Base packages","Final configuration","Reboot after install"],"Select additional options",build["options"])
build["options"] = list(choice or [])
main_menu(build,distros,action)
case 8:
#install
#shows installation options
msg ="Final options:\nOS:"+ distros[build["distro"]]["name"]+"\n"
if build["GD"]!=-1:
msg += "Graphics:"+distros[build["distro"]]["GD"][build["GD"]]["name"]+"\n"
if build["DM"]!=-1:
msg += "Display manager:"+distros[build["distro"]]["DM"][build["DM"]]["name"]+"\n"
if build["DE"]!=-1:
msg += "Desktop environment:"+distros[build["distro"]]["DE"][build["DE"]]["name"]+"\n"
if len(build["tasks"])>0:
msg +="\nExtra tasks:\n"
for t in build["tasks"]:
msg += distros[build["distro"]]["tasks"][t]["name"]+"\n"
msg += "\nAdditional settings:\n"
if 0 not in build["options"]:
msg += "Skip base packages\n"
if 1 not in build["options"]:
msg += "Skip final configuration\n"
if 2 in build["options"]:
msg += "Reboot after install\n"
msg +="\nConfirm?"
#ask for confirmation
ok = opt_menu(["[y] Yes","[n] No"],msg)
if ok == 0:
#begin install
file = script(build,distros[build["distro"]])
try:
os.system("sh "+file)
except:
print("Installation failed.")
sys.exit()
else:
#return to main menu
main_menu(build,distros,action)
case 9:
#save config
file = ""
try:
name = input("Type file name here:\n") or "config_"+str(math.ceil(time.time()))+".json"
file = open(name,"x")
except:
file = open("config_"+str(math.ceil(time.time())*2)+".json","x")
config = {
"version":version,
"distro" : distros[build["distro"]]["name"],
"GD" : -1,
"DM" : -1,
"DE" : -1,
"tasks" : [],
"options":build["options"]
}
if build["GD"]!=-1:
config["GD"]=distros[build["distro"]]["GD"][build["GD"]]["name"]
if build["DM"]!=-1:
config["DM"]=distros[build["distro"]]["DM"][build["DM"]]["name"]
if build["DE"]!=-1:
config["DE"]=distros[build["distro"]]["DE"][build["DE"]]["name"]
if len(build["tasks"]) != 0 :
for t in build["tasks"]:
config["tasks"].append(distros[build["distro"]]["tasks"][t]["name"])
file.write(json.dumps(config))
file.close()
opt_menu(["Press enter to continue"],"Config saved to "+file.name+".You can now load the config using:\n./taiga "+file.name)
main_menu(build,distros,action)
case 10:
#save script
name = ""
try:
name = input("Type file name here:\n") or "taiga_"+str(math.ceil(time.time()))
except:
name = "taiga_"+str(math.ceil(time.time())*2)
file = script(build,distros[build["distro"]],name)
ok = opt_menu(["Press enter to continue"],"All commands have been saved to "+ file)
main_menu(build,distros,action)
case 11:
#exit
print("Installation aborted.")
sys.exit()
#load configs from args
def load(distros):
if len(sys.argv) < 2:
return 0
else:
build = {}
config = {
"version":version,
"distro" : -1,
"GD" : -1,
"DM" : -1,
"DE" : -1,
"tasks" : [],
"options":[0,1]
}
try:
file = open(sys.argv[1],"r")
build = json.loads(file.read())
file.close()
except:
#config is invalid
ok = opt_menu(["[d] Discard config","[x] Exit"],"Couldn't load the config.The config is either corrupted or doesn't exist.")
if ok == 0:
return 0
else:
print("Installation aborted")
sys.exit()
#checks if the config has all the necesary options
if "version" in build and "distro" in build and "GD" in build and "DM" in build and "DE" in build and "tasks" in build and "options" in build:
#convert from names to indexes
names = []
for d in distros:
names.append(d["name"])
try:
config["distro"]=names.index(build["distro"])
except:
config["distro"]=get_distro(distros)
names = []
if build["GD"]!=-1:
for d in distros[config["distro"]]["GD"]:
names.append(d["name"])
try:
config["GD"]=names.index(build["GD"])
except:
config["GD"]=get_gpu(distros[config["distro"]])
names = []
if build["DM"]!=-1:
for d in distros[config["distro"]]["DM"]:
names.append(d["name"])
try:
config["DM"]=names.index(build["DM"])
except:
config["DM"]=-1
names = []
if build["DE"]!=-1:
for d in distros[config["distro"]]["DE"]:
names.append(d["name"])
try:
config["DE"]=names.index(build["DE"])
except:
config["DE"]=-1
names = []
if len(build["tasks"])!=0:
for d in distros[config["distro"]]["tasks"]:
names.append(d["name"])
for t in build["tasks"]:
try:
config["tasks"].append(names.index(t))
except:
print("error")
config["options"]=build["options"]
config["version"]=build["version"]
return config
else:
ok = opt_menu(["[d] Discard config","[x] Exit"],"This config is incompatible with this version of TAIGA.")
if ok == 0:
return 0
else:
print("Installation aborted")
sys.exit()
#gets all data regarding distros
def serialize():
distros = []
workdir = Path.cwd()
files = os.listdir(str(workdir)+"/distros")
for f in files:
file = open(str(workdir)+"/distros/"+f,"r")
j = json.loads(file.read())
distros.append(j)
file.close()
distros = sorted(distros, key=itemgetter('order'))
return distros
#(attempt to) get the distro
def get_distro(distros):
for d in distros:
r = os.system(d["identify"])
if r == 0:
return d["order"]
return -1
#(attempt to) identify GPU
def get_gpu(distro):
os.system("lspci -v | grep VGA >> cache.file")
file = open("cache.file","r")
output = file.read()
file.close()
os.system("rm -rf cache.file")
i = 0
for v in distro["GD"]:
if search(v["name"].lower(),output.lower()):
return i
i+=1
return -1
#main function
def main():
distros = []
try:
distros = serialize()
except:
#something went wrong with collecting distro info
print("Couldn't load info about the distros.Redownload the script and try again.")
sys.exit(1)
#info about current config
build = load(distros)
distro = {}
if build == 0:
#no config was loaded,loads the default config
build = {
"version":version,
"distro" : -1,
"GD" : -1,
"DM" : -1,
"DE" : -1,
"tasks" : [],
"options":[0,1]
}
#gets info about the system
build["distro"] = get_distro(distros)
if build["distro"] != -1:
distro = distros[build["distro"]]
build["GD"]=get_gpu(distro)
else:
#couldn't identify the distro
build["distro"]= conf_menu(distros,"Your distro couldn't be recognized.Please select your distro manually",0)
if build["distro"]==None:
print("Installation aborted.")
sys.exit(1)
distro = distros[build["distro"]]
build["GD"]=get_gpu(distro)
main_menu(build,distros,0)
#just some boilerplate
if __name__ == "__main__":
main()