-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinstall.py
executable file
·72 lines (55 loc) · 2.12 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
#!/usr/bin/env python3
# Installer for SUDO
# jthistle on GitHub, GPL licensed
import shutil, os
def main():
# Change this to your rc file if you are using a different shell or have you rc in a different location
RCFILE = os.path.expanduser("~/.bashrc")
# Make a backup
shutil.copyfile(RCFILE, RCFILE + ".old")
newcontents = False
# Tests for being able to open files needed
try:
open(RCFILE, "r")
except Exception as e:
print("Error when opening rc file: {}".format(e))
return
try:
open("./SUDO", "r")
except Exception as e:
print("Error when opening programme code: {}".format(e))
return
# Check for old installations
with open(RCFILE, "r") as rc:
contents = rc.read()
start = contents.find("# SUDO - shout at bash")
end = contents.find("# end SUDO")
foundStart = start != -1
foundEnd = end != -1
if foundStart and not foundEnd:
print("You have an older installation of SUDO. Please manually remove old version from your rc file to be able to install the new version.")
elif not foundStart and not foundEnd:
print("No current installation found.")
newcontents = contents
elif foundStart and foundEnd:
print("Found current installation, removing...")
newcontents = contents[:start] + contents[end+len("# end SUDO"):]
if newcontents:
newcontents = newcontents.strip()
# Add in new SUDO code
if newcontents:
programmeCode = ""
with open("./SUDO", "r") as codeFile:
programmeCode = codeFile.read()
if programmeCode != "":
with open(RCFILE, "w") as rc:
newcontents += "\n\n" + programmeCode + "\n"
rc.write(newcontents)
print("Successfully wrote new rc file.")
print("\nNow run: source {} \n".format(RCFILE))
else:
print("Couldn't read SUDO code")
else:
print("Finishing without installing.")
if __name__ == "__main__":
main()