-
Notifications
You must be signed in to change notification settings - Fork 1
/
acp_cmd.py
148 lines (126 loc) · 4.88 KB
/
acp_cmd.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
import subprocess
import threading
import PySimpleGUI as sg
EXEC_ADB_DEFAULT = str('adb.exe')
mExecAdbPath = EXEC_ADB_DEFAULT
mExecAdbPathVerified = bool(False)
EXEC_FASTBOOT_DEFAULT = str('fastboot.exe')
mExecFastbootPath = EXEC_FASTBOOT_DEFAULT
mExecFastbootPathVerified = bool(False)
mWindow = sg.Window('EmptyWindow')
def setWindow(window):
global mWindow
mWindow = window
def getAdbPathDefault() -> str:
return EXEC_ADB_DEFAULT
def setAdbPath(path: str):
global mExecAdbPath
mExecAdbPath = path
def getAdbPath() -> str:
global mExecAdbPath
return mExecAdbPath
def setAdbPathVerified(verified: bool):
global mExecAdbPathVerified
mExecAdbPathVerified = verified
def getFastbootPathDefault() -> str:
return EXEC_FASTBOOT_DEFAULT
def setFastbootPath(path: str):
global mExecFastbootPath
mExecFastbootPath = path
def getFastbootPath() -> str:
global mExecFastbootPath
return mExecFastbootPath
def setFastbootPathVerified(verified: bool):
global mExecFastbootPathVerified
mExecFastbootPathVerified = verified
def verifyAdbPath(path: str) -> bool:
rc = False
mWindow['statusText'].update('Processing command: adb --version')
adbVersion = cmdExec(10, path + " --version").decode('latin1')
if adbVersion[:20] == "Android Debug Bridge":
rc = True
mWindow['statusCheckExec'].update(adbVersion + '\n', append=True)
return rc
def verifyFastbootPath(path: str) -> bool:
rc = False
mWindow['statusText'].update('Processing command: fastboot --version')
fastbootVersion = cmdExec(10, path + " --version").decode('latin1')
if fastbootVersion[:8] == "fastboot":
rc = True
mWindow['statusCheckExec'].update(fastbootVersion + '\n', append=True)
return rc
def cmdExec(timeout_sec: int, cmd: str) -> bytes:
rc = True
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
try:
stdoutdata = subprocess.check_output(cmd, stderr=subprocess.STDOUT, timeout=timeout_sec, startupinfo=startupinfo)
except subprocess.TimeoutExpired:
#print("Command TIMEOUT !!!")
mWindow['statusText'].update(mWindow['statusText'].get() + ' Timeout !')
rc = False
bytesRcError = bytes('command timeout', 'latin1')
except subprocess.CalledProcessError as e:
#print(e.output)
mWindow['statusText'].update(mWindow['statusText'].get() + ' Error !')
rc = False
bytesRcError = e.output
if rc == True:
mWindow['statusText'].update(mWindow['statusText'].get() + ' Done')
return stdoutdata
else:
return bytesRcError
def adbCmdExec(timeout_sec: int, cmd: str) -> bytes:
global mExecAdbPath
return cmdExec(timeout_sec, mExecAdbPath + " " + cmd)
def fastbootCmdExec(timeout_sec: int, cmd: str) -> bytes:
global mExecFastbootPath
return cmdExec(timeout_sec, mExecFastbootPath + " " + cmd)
def adbCmd(timeout_sec: int, command: str, blocking=False, donotprintcmd=False) -> bool:
global mExecAdbPath, mExecAdbPathVerified
rc = True
if timeout_sec < 1:
print("Arg timeout_sec must >= 1")
return False
if mExecAdbPathVerified == False:
mWindow['statusText'].update('ADB path not verified')
if verifyAdbPath(mExecAdbPath) == True:
mExecAdbPathVerified = True
else:
return False
if donotprintcmd == True:
mWindow['statusText'].update('Processing adb command ...')
else:
mWindow['statusText'].update('Processing command: adb ' + command)
if blocking == True:
cmdExec(timeout_sec, mExecAdbPath + " " + command)
else:
tCmd = threading.Thread(target=cmdExec, args=(timeout_sec, mExecAdbPath + " " + command))
tCmd.start()
return rc
def fastbootCmd(timeout_sec: int, command: str, blocking=False, donotprintcmd=False) -> bool:
global mExecFastbootPath, mExecFastbootPathVerified
rc = True
if timeout_sec < 1:
print("Arg timeout_sec must >= 1")
return False
if mExecFastbootPathVerified == False:
mWindow['statusText'].update('Fastboot path not verified')
if verifyFastbootPath(mExecFastbootPath) == True:
mExecFastbootPathVerified = True
else:
return False
if donotprintcmd == True:
mWindow['statusText'].update('Processing fastboot command ... ')
else:
mWindow['statusText'].update('Processing command: fastboot ' + command)
if blocking == True:
cmdExec(timeout_sec, mExecFastbootPath + " " + command)
else:
tCmd = threading.Thread(target=cmdExec, args=(timeout_sec, mExecFastbootPath + " " + command))
tCmd.start()
return rc
def adbRootCheck(window):
window['statusText'].update('Processing adb root ...')
outputExec = adbCmdExec(20, "root").decode('latin1')
window['statusText'].update('Root result: ' + outputExec.splitlines()[0])