This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
devSetup.py
637 lines (575 loc) · 21.2 KB
/
devSetup.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#!/usr/bin/python3
#Made by Tom Gouville https://github.com/Any0ne22/
import subprocess
import os
import base64
import sys
purple='\033[1;35m'
nc='\033[0m'
dset= purple + "<" + nc + "dev$etup" + purple + ">"
def logo():
print(purple)
print(" __ __ __ \n /\\ \\ /\\ \\_ /\\ \\__ \n \\_\\ \\ __ __ __ \\/'__`\\ __\\ \\ ,_\\ __ __ _____ \n /'_` \\ /'__`\\/\\ \\/\\ \\ /\\ \\_\\_\\ /'__`\\ \\ \\/ /\\ \\/\\ \\/\\ '__`\\ \n/\\ \\L\\ \\/\\ __/\\ \\ \\_/ |\\ \\____ \\/\\ __/\\ \\ \\_\\ \\ \\_\\ \\ \\ \\L\\ \\\n\\ \\___,_\\ \\____\\\\ \\___/ \\/\\ \\_\\ \\ \\____\\\\ \\__\\\\ \\____/\\ \\ ,__/\n \\/__,_ /\\/____/ \\/__/ \\ `\\_ _/\\/____/ \\/__/ \\/___/ \\ \\ \\/ \n `\\_/\\_\\ \\ \\_\\ \n \\/_/ \\/_/ ")
print(nc)
def isRoot():
#Verify whether the user is root or not
if subprocess.check_output("whoami").decode("utf-8") == "root\n":
return True
else:
return False
def isInstalled(commandName):
if subprocess.check_output(["whereis",commandName]).decode("utf-8") != commandName+":\n":
return True
return False
def readFile(filename):
paramFile = open(filename, "r")
paramList = [x.replace("\n", "") for x in paramFile if (x!="" and x[0]!="#")]
paramFile.close()
return paramList
def writeFile(filename, data):
paramFile = open(filename, "w")
for x in data:
paramFile.write(x + "\n")
paramFile.close()
subprocess.run("chmod -R 777 " + filename, shell=True)
def installAptitude(parameters):
if not p.aptitudeInstalled:
print("Aptitude is not installed\n")
return
cmd = "apt-get install "
for x in readFile(parameters.pkgsList):
cmd += x + " "
print(dset + purple + " Installing Aptitude packages" + nc)
print(cmd)
subprocess.run(cmd, shell=True)
print("\n")
def updateAptitudePkgsList(parameters, merge=False):
if not p.aptitudeInstalled:
print("Aptitude is not installed\n")
return
print(dset + purple + " Saving your Aptitude package list" + nc)
pkgsList = subprocess.check_output(["apt", "list", "--installed"]).decode("utf-8")
pkgsList = pkgsList.split("\n")[1:-1]
newList = [x.split("/")[0].split(" ")[0] for x in pkgsList]
if merge:
#Merging the two lists into one
return list(set(parameters.pkgsList + newList))
else:
return newList
def installPacman(parameters):
if not p.pacmanInstalled:
print("Pacman is not installed\n")
return
cmd = "pacman -Sy "
for x in readFile(parameters.pkgsList):
cmd += x + " "
print(dset + purple + " Installing Pacman packages" + nc)
print(cmd)
subprocess.run(cmd, shell=True)
print("\n")
def updatePacmanPkgsList(parameters, merge=False):
if not p.pacmanInstalled:
print("Pacman is not installed\n")
return
print(dset + purple + " Saving your Pacman package list" + nc)
pkgsList = subprocess.check_output(["pacman", "-Qe"]).decode("utf-8")
pkgsList = pkgsList.split("\n")[:-1]
if merge:
#Merging the two lists into one
return list(set(parameters.pkgsList + pkgsList))
else:
return pkgsList
def installSnap(parameters):
if not p.snapInstalled:
print("Snap is not installed\n")
return
print(dset + purple + " Installing Snap packages" + nc)
cmd = "snap install "
for x in readFile(parameters.snapPkgs):
print(cmd + x)
subprocess.run(cmd, shell=True)
print("\n")
def updateSnapPkgsList(parameters, merge=False):
if not p.snapInstalled:
print("Snap is not installed\n")
return
print(dset + purple + " Saving your Snap package list" + nc)
pkgsList = subprocess.check_output(["snap", "list"]).decode("utf-8")
pkgsList = pkgsList.split("\n")[1:-1]
pkgsList = [x.split(" ")[0] for x in pkgsList]
if merge:
#Merging the two lists into one
return list(set(parameters.pkgsList + pkgsList))
else:
return pkgsList
def setConfigFiles(parameters):
print(dset + purple + " Installing config files" + nc)
for x in readFile(parameters.cfgList):
[a, b] = x.split(":")
cmd = "cp -R ./config/" + a + " " + b.replace("$HOME", parameters.homeDirectory)
print(cmd)
subprocess.run(cmd, shell=True)
print("\n")
def updateConfigFiles(parameters):
print(dset + purple + " Updating the config files" + nc)
for x in readFile(parameters.cfgList):
[a, b] = x.split(":")
cmd = "cp -f " + b.replace("$HOME", parameters.homeDirectory) + " Profiles/" + parameters.profile + "/config/" + a
print("Saving " + b.replace("$HOME", parameters.homeDirectory) + " as " + a)
print(cmd)
subprocess.run(cmd, shell=True)
print("\n")
def installZipFiles(parameters):
print(dset + purple + " Installing zip files" + nc)
for x in readFile(parameters.zipList):
[a, b] = x.split(":")
cmd = "unzip ./zip/" + a + " -d " + b.replace("$HOME", parameters.homeDirectory)
print(cmd)
subprocess.run(cmd, shell=True)
print("\n")
def installVSCodeExtensions(parameters):
if not p.VSCodeInstalled:
print("VS Code is not installed\n")
return
print(dset + purple + " Installing VSCode extensions" + nc)
for x in readFile(parameters.vsexts):
cmd = "su " + parameters.homeDirectory[6:] + " -c \"code --install-extension " + x + "\""
print(cmd)
subprocess.run(cmd, shell=True)
print("\n")
def updateVSCodeExtensions(parameters, merge=False):
if not p.VSCodeInstalled:
print("VS Code is not installed\n")
return
print(dset + purple + " Saving your VSCode extensions list" + nc)
extsList = subprocess.check_output(["su", parameters.homeDirectory[6:],"-c","code --list-extensions"]).decode("utf-8")
if merge:
#Merging the two lists into one
return list(set(parameters.vsexts + extsList.split("\n")[:-1]))
else:
return extsList.split("\n")[:-1]
#CLI commands
def command(args):
cmdList = {"setprofile": set_profile, "install": install, "save": save, "add": add, "remove": remove, "help": helpcmd}
if args[0] in cmdList:
cmdList[args[0]](args[1:])
else:
print("Unknown command " + args[0])
def set_profile(args):
if len(args) == 0:
print("Error: you haven't specified a profile name")
print("\n")
return
profile = args[0]
if os.path.isdir("Profiles/" + profile):
print(dset + purple + " Profile set to \"" + profile + "\"" + nc)
p.setProfile(profile)
return True
else:
print("Profile \"" + profile + "\" doesn't exist.")
return False
def install(args):
options = {"all": install_all, "packages": install_packages, "config": install_config, "zip": install_zip, "VSexts": install_VSCodeExts}
if args[0] in options:
options[args[0]](args[1:])
else:
print("Unknown option " + args[0])
def save(args):
options = {"packages": save_packages, "config": save_config, "VSexts": save_VSCodeExts}
if args[0] in options:
options[args[0]](args[1:])
else:
print("Unknown option " + args[0])
def add(args):
options = {"package": add_package, "snap": add_snap, "config": add_config, "zip": add_zip, "VSext": add_VSCodeExt, "profile": add_profile}
if args[0] in options:
options[args[0]](args[1:])
else:
print("Unknown option " + args[0])
def remove(args):
options = {"package": remove_package, "snap": remove_snap, "config": remove_config, "zip": remove_zip, "VSext": remove_VSCodeExt, "profile": remove_profile}
if args[0] in options:
options[args[0]](args[1:])
else:
print("Unknown option " + args[0])
def helpcmd(args):
print("Welcome to the help section, unfortunately it has not been created yet")
def install_all(args):
if len(args) > 0:
if not set_profile(args): return
install_packages(['auto'])
install_config([])
install_zip([])
install_VSCodeExts([])
def install_packages(args):
options = {"auto": install_packages_auto, "aptitude": installAptitude, "pacman": installPacman, "snap": installSnap}
if args[0] in options:
option = args.pop(0)
if len(args) > 0:
if not set_profile(args): return
options[option](p)
else:
print("Unknown option " + args[0])
def install_packages_auto(parameters):
if p.aptitudeInstalled:
#If Aptitude is installed
installAptitude(parameters)
elif p.pacmanInstalled:
#If Pacman is installed
installPacman(parameters)
if p.snapInstalled:
#If Snap is installed
installSnap(parameters)
def install_config(args):
setConfigFiles(p)
def install_zip(args):
installZipFiles(p)
def install_VSCodeExts(args):
installVSCodeExtensions(p)
def save_packages(args):
options = {"auto": save_packages_auto, "aptitude": save_aptitude, "pacman": save_pacman, "snap": save_snap}
if args[0] in options:
options[args[0]](args[1:])
else:
print("Unknown option " + args[0])
def save_packages_auto(args):
if p.aptitudeInstalled:
#If Aptitude is installed
save_aptitude([])
elif p.pacmanInstalled:
#If Pacman is installed
save_pacman([])
if p.snapInstalled:
#If Snap is installed
save_snap([])
def update_parameters(updateFunction, fileName, interactive, merge):
pkgs = []
if merge:
pkgs = updateFunction(p, True)
else:
pkgs = updateFunction(p, False)
writeFile(fileName, pkgs)
if interactive:
print("Would you like to edit the list? (y/N)")
if input("> ") == "y":
subprocess.run(p.editor + " " + fileName, shell=True)
print("\n")
def save_aptitude(args):
if not p.aptitudeInstalled:
print("Aptitude is not installed\n")
return
merge = False
if len(args) > 0 and args[0] == "merge":
args.pop(0)
merge = True
if len(args) > 0:
if not set_profile(args): return
update_parameters(updateAptitudePkgsList, p.pkgsList, p.interactive, merge)
def save_pacman(args):
if not p.pacmanInstalled:
print("Pacman is not installed\n")
return
merge = False
if len(args) > 0 and args[0] == "merge":
args.pop(0)
merge = True
if len(args) > 0:
if not set_profile(args): return
update_parameters(updatePacmanPkgsList, p.pkgsList, p.interactive, merge)
def save_snap(args):
if not p.snapInstalled:
print("Snap is not installed\n")
return
merge = False
if len(args) > 0 and args[0] == "merge":
args.pop(0)
merge = True
if len(args) > 0:
if not set_profile(args): return
update_parameters(updateSnapPkgsList, p.snapPkgs, p.interactive, merge)
def save_config(args):
if len(args) > 0:
if not set_profile(args): return
updateConfigFiles(p)
def save_VSCodeExts(args):
if not p.VSCodeInstalled:
print("VS Code is not installed\n")
return
merge = False
if len(args) > 0 and args[0] == "merge":
args.pop(0)
merge = True
if len(args) > 0:
if not set_profile(args): return
update_parameters(updateVSCodeExtensions, p.vsexts, p.interactive, merge)
def add_package(args):
if len(args) == 0:
print("You haven't specified a package name")
print("\n")
else:
package = args.pop(0)
if len(args) > 0:
if not set_profile(args): return
print(dset + purple + " Adding \"" + package + "\" to your package list" + nc)
f = open(p.pkgsList, "a")
f.writelines(package + "\n")
f.close()
print("\n")
def add_snap(args):
if len(args) == 0:
print("You haven't specified a package name")
print("\n")
else:
package = args.pop(0)
if len(args) > 0:
if not set_profile(args): return
print(dset + purple + " Adding \"" + package + "\" to your Snap packages" + nc)
f = open(p.snapPkgs, "a")
f.writelines(package + "\n")
f.close()
print("\n")
def add_config(args):
if len(args) == 0:
print("You haven't specified a configuration file")
print("\n")
else:
configFile = args.pop(0)
filename = ""
if len(args) == 2:
filename = args.pop(0)
if not set_profile(args): return
elif len(args) == 1:
if not set_profile(args): return
filename = configFile.split("/")[-1]
else:
filename = configFile.split("/")[-1]
print(dset + purple + " Adding \"" + configFile + "\" as \"" + filename + "\" to your configuration files" + nc)
if os.path.isfile(configFile):
f = open(p.cfgList, "a")
f.writelines(filename + ":" + configFile.replace("~", "$HOME") + "\n")
f.close()
print("Please use \"save config\" to make a copy of this config file")
else:
print("Error: file " + configFile + " doesn't exist")
print("\n")
def add_zip(args):
if len(args) == 0:
print("You haven't specified a zip file")
print("\n")
else:
filename = args.pop(0)
path = ""
if len(args) == 2:
path = args.pop(0)
if not set_profile(args): return
elif len(args) == 1:
path = args[0]
else:
print("You haven't specified the extraction path")
print("\n")
return
print(dset + purple + " Adding \"" + filename + "\" to your zip files" + nc)
if os.path.isfile("Profiles/" + p.profile + "/zip/" + filename):
f = open(p.zipList, "a")
f.writelines(filename + ":" + path.replace("~", "$HOME") + "\n")
f.close()
else:
print("Error: file " + filename + " not found in Profiles/" + p.profile + "/zip/")
print("\n")
def add_VSCodeExt(args):
if len(args) == 0:
print("You haven't specified an extension name")
print("\n")
else:
ext = args.pop(0)
if len(args) > 0:
if not set_profile(args): return
print(dset + purple + " Adding \"" + ext + "\" to your VSCode extensions" + nc)
f = open(p.vsexts, "a")
f.writelines(ext + "\n")
f.close()
print("\n")
def add_profile(args):
if len(args) == 0:
print("Error: you haven't specified a profile name")
print("\n")
return
profile = args[0]
print(dset + purple + " Creating profile \"" + profile + "\"" + nc)
if os.path.isdir("Profiles/" + profile):
print("Profile \"" + profile + "\" already exists.")
else:
os.mkdir("Profiles/" + profile)
os.mkdir("Profiles/" + profile + "/config")
os.mkdir("Profiles/" + profile + "/zip")
open("Profiles/" + profile + "/packages.txt", "a").close()
open("Profiles/" + profile + "/snapPackages.txt", "a").close()
open("Profiles/" + profile + "/configfiles.txt", "a").close()
open("Profiles/" + profile + "/vscodeextensions.txt", "a").close()
open("Profiles/" + profile + "/zip.txt", "a").close()
subprocess.run("chmod -R 777 " + "Profiles/" + profile, shell=True)
print("Profile \"" + profile + "\" successfully created.")
print("\n")
def remove_package(args):
if len(args) == 0:
print("You haven't specified a package name")
print("\n")
else:
package = args.pop(0)
if len(args) > 0:
if not set_profile(args): return
print(dset + purple + " Removing \"" + package + "\" from your package list" + nc)
packages = readFile(p.pkgsList)
try:
packages.remove(package)
except:
pass
writeFile(p.pkgsList, packages)
print("\n")
def remove_snap(args):
if len(args) == 0:
print("You haven't specified a package name")
print("\n")
else:
package = args.pop(0)
if len(args) > 0:
if not set_profile(args): return
print(dset + purple + " Removing \"" + package + "\" from your snap package list" + nc)
packages = readFile(p.snapPkgs)
try:
packages.remove(package)
except:
pass
writeFile(p.snapPkgs, packages)
print("\n")
def remove_config(args):
if len(args) == 0:
print("You haven't specified a config file name")
print("\n")
else:
filename = args.pop(0)
if len(args) > 0:
if not set_profile(args): return
print(dset + purple + " Removing \"" + filename + "\" from your config file list" + nc)
configs = readFile(p.cfgList)
for i in range(len(configs)):
if configs[i].split(":")[0] == filename:
configs.pop(i)
os.remove("Profiles/" + p.profile + "/config/" + filename)
break
writeFile(p.cfgList, configs)
print("\n")
def remove_zip(args):
if len(args) == 0:
print("You haven't specified a zip file name")
print("\n")
else:
filename = args.pop(0)
if len(args) > 0:
if not set_profile(args): return
print(dset + purple + " Removing \"" + filename + "\" from your zip files list" + nc)
zips = readFile(p.cfgList)
for i in range(len(zips)):
if zips[i].split(":")[0] == filename:
zips.pop(i)
os.remove("Profiles/" + p.profile + "/zip/" + filename)
break
writeFile(p.cfgList, zips)
print("\n")
def remove_VSCodeExt(args):
if len(args) == 0:
print("You haven't specified an extension name")
print("\n")
else:
ext = args.pop(0)
if len(args) > 0:
if not set_profile(args): return
print(dset + purple + " Removing \"" + ext + "\" from your VS Code extensions list" + nc)
exts = readFile(p.vsexts)
try:
exts.remove(ext)
except:
pass
writeFile(p.vsexts, exts)
print("\n")
def remove_profile(args):
if len(args) == 0:
print("Error: you haven't specified a profile name")
print("\n")
return
profile = args[0]
print(dset + purple + " Removing profile \"" + profile + "\"" + nc)
if not os.path.isdir("Profiles/" + profile):
print("Profile \"" + profile + "\" doesn't exist. Nothing to do.")
else:
for root, dirs, files in os.walk("Profiles/" + profile, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir("Profiles/" + profile)
print("Profile \"" + profile + "\" successfully removed.")
print("\n")
class Parameters:
def __init__(self):
if not os.path.isdir("Profiles/Default"):
add_profile(["Default"])
self.profile = "Default"
self.homeDirectory = os.environ.get('HOME')
self.pkgsList = "Profiles/Default/packages.txt"
self.snapPkgs = "Profiles/Default/snapPackages.txt"
self.cfgList = "Profiles/Default/configfiles.txt"
self.vsexts = "Profiles/Default/vscodeextensions.txt"
self.zipList = "Profiles/Default/zip.txt"
self.interactive = False
self.editor = ""
self.snapInstalled = False
self.VSCodeInstalled = False
self.aptitudeInstalled = False
self.pacmanInstalled = False
self.checkInstalledPackages()
def setProfile(self, profileName):
self.profile = profileName
self.pkgsList = "Profiles/" + profileName + "/packages.txt"
self.snapPkgs = "Profiles/" + profileName + "/snapPackages.txt"
self.cfgList = "Profiles/" + profileName + "/configfiles.txt"
self.vsexts = "Profiles/" + profileName + "/vscodeextensions.txt"
self.zipList = "Profiles/" + profileName + "/zip.txt"
def checkInstalledPackages(self):
self.snapInstalled = isInstalled("snap")
self.VSCodeInstalled = isInstalled("code")
self.aptitudeInstalled = isInstalled("apt-get")
self.pacmanInstalled = isInstalled("pacman")
if __name__ == '__main__':
if not isRoot():
print("please run as root")
exit()
if not os.path.isdir("Profiles/"):
os.mkdir("Profiles/")
global p
p = Parameters()
logo()
#Get the default editor
editor = subprocess.check_output(["whereis", "editor"]).decode("utf-8").split(" ")
if len(editor) > 1:
editor = editor[1]
elif os.environ.get('EDITOR') != None:
editor = os.environ.get('EDITOR')
else:
#If there is no default editor, choose nano
editor = subprocess.check_output(["whereis", "nano"]).decode("utf-8").split(" ")[1]
p.editor = editor
if len(sys.argv) > 1:
args = sys.argv[1:]
command(args)
else:
#Interactive mode
p.interactive = True
while 1:
cmd = input("> ")
args = cmd.split(" ")
if args[0] == "exit":
print("Goodbye!")
exit()
else:
command(args)