-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
170 lines (146 loc) · 7.46 KB
/
main.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
from posixpath import expanduser
import requests, os, zipfile, sys, time, winreg, ctypes
# Thanks to @flandolf for more like half of this script lmao
print("Android Platform Tools Installer v2.5")
print("By: @matejmajny and @flandolf")
print("OS: " + sys.platform + "/" + os.name)
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def download(link): #taken from my older project
with open("platform-tools.zip", "wb") as f:
print("Downloading platform-tools")
response = requests.get(link, stream=True)
total_length = response.headers.get('content-length')
if total_length is None: # no content length header
f.write(response.content)
else:
dl = 0
total_length = int(total_length)
for data in response.iter_content(chunk_size=4096):
dl += len(data)
f.write(data)
done = int(50 * dl / total_length)
sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50-done)) )
sys.stdout.flush()
def main():
if (os.name == "nt"): # Windows code (by Matt & Andy)
if not is_admin():
# Re-run the script with administrative privileges using the 'runas' verb
print("Admin privileges required, re-running script as admin...")
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
exit(0)
else:
# Check if adb is already installed
if (os.path.exists("C:\\platform-tools")):
print("Platform tools already installed, deleting...")
os.system("rmdir /s /q C:\\platform-tools")
print("Reinstalling...")
download("https://dl.google.com/android/repository/platform-tools-latest-windows.zip")
print("\nUnzipping...")
with zipfile.ZipFile("platform-tools.zip", 'r') as zip_ref:
zip_ref.extractall("C:\\")
print("Deleting zip and adding platform-tools to path...")
os.remove("platform-tools.zip")
# Define the path to modify the PATH environment variable in the registry
path_to_modify = r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
# Open the registry key for the path to modify
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, path_to_modify, 0, winreg.KEY_ALL_ACCESS)
# Read the current value of the PATH environment variable
current_path = winreg.QueryValueEx(key, "Path")[0]
# Modify the PATH environment variable by appending a new path to it
new_path = "C:\\platform-tools"
modified_path = current_path + ";" + new_path
# Write the modified PATH environment variable back to the registry
winreg.SetValueEx(key, "Path", 0, winreg.REG_EXPAND_SZ, modified_path)
# Close the registry key
winreg.CloseKey(key)
# Exit messages
print("All done! You can now use adb/fastboot commands anywhere!")
print("Do not forget to restart CMD/PowerShell or in some cases whole PC.")
input("Press ENTER to exit")
exit()
elif (os.name == "posix"): # Linux code (by dumpy), and ngl why is linux so complicated <------(also he loves PRs)
path = "$PATH"
logoutrequired = False
download("https://dl.google.com/android/repository/platform-tools-latest-linux.zip")
print("\nUnzipping...")
with zipfile.ZipFile("platform-tools.zip", 'r') as zip_ref:
zip_ref.extractall(expanduser("~"))
os.remove("platform-tools.zip")
print("Would you like to add to path?")
if(input('[Y]es [N]o: ').lower() == 'y'):
shell = os.readlink('/proc/%d/exe' % os.getppid())
print("Detected Shell: " + shell[shell.rfind('/')+1:])
if (shell.endswith("bash")):
with open(expanduser("~") + "/.bashrc", "r") as f:
# Check if already added
if (f.read().find("platform-tools") == -1):
with open(expanduser("~") + "/.bashrc", "a") as f:
f.write(r"export PATH=$PATH:~/platform-tools")
os.system(r'source ~/.bashrc')
print("Done!")
else:
print("Already added!")
elif (shell.endswith("zsh")):
with open(expanduser("~") + "/.zshrc", "r") as f:
# Check if already added
if (f.read().find("platform-tools") == -1):
print("Adding to zshrc...")
with open(expanduser("~") + "/.zshrc", "a") as f:
f.write(r'export PATH="$PATH:~/platform-tools"')
os.system(r'source ~/.zshrc')
print("Done!")
else:
print("Already added!")
elif (shell.endswith("fish")):
with open(expanduser("~") + "/.config/fish/config.fish", "r") as f:
if (f.read().find("platform-tools") == -1):
print("Adding to config.fish...")
with open(expanduser("~") + "/.config/fish/config.fish", "a") as f:
f.write("set PATH $PATH ~/platform-tools")
os.system(r'source ~/.config/fish/config.fish')
print("Done!")
else:
print("Already added!")
print("Adding executeable permissions...")
os.system(r'chmod +x ~/platform-tools/*')
print("Done!")
print("Checking if user is in plugdev group...")
if (os.system(r'groups | grep plugdev') == 0):
print("User is in plugdev group, nothing to do...")
print("Done!")
else:
print("User is not in plugdev group, adding... (SUDO required)")
os.system(r'sudo usermod -a -G plugdev $USER')
logoutrequired = True
print("Done!")
print("Done!")
print("Reloading udev rules...")
os.system('sudo udevadm control --reload-rules')
print("Done!")
print("Adding USB device permissions...")
os.system('sudo chmod 666 /dev/bus/usb/*/*')
print("Done!")
print("All done! You can now use adb/fastboot commands anywhere!")
else:
print("You can add it manually by adding ~/platform-tools to your PATH variable")
if (logoutrequired):
print("Please logout and login for changes to take effect!")
else:
print("No logout required!")
print("All done! You can now use adb/fastboot commands anywhere!")
else: # OS not supported
print("Your OS is not supported yet, please open an issue on GitHub")
input("Press ENTER to exit.")
exit()
def start():
tos = input("\nWith pressing ENTER you agree with Android SDK Platform-Tools terms of service. (anything else = no)\n")
if tos == "":
main()
else:
exit()
start()
exit(0)