Skip to content

Commit 4dc9dc3

Browse files
committed
improved os detection , added wait message 3 reties, fixed wrong prefix in windows commands
1 parent 245d345 commit 4dc9dc3

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

apk_mass_install.py

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from timeit import default_timer as timer
4444
from platform import system
4545
from datetime import datetime
46+
import time
4647
import subprocess
4748
import argparse
4849
import shutil
@@ -56,25 +57,27 @@
5657
INSTALL_OK = 1
5758
INSTALL_EXISTS = 2
5859

59-
# Flags used for adb package listing
60+
# Flags for adb list packages
6061
pkg_flags = {"all": "", # list all packages
6162
"user": "-3", # list 3d party packages only (default)
6263
"system": "-S"} # list system packages only
6364

6465

6566
def detect_os():
66-
# detect platform
67-
if os.name == "posix" and system() == "Darwin":
68-
os_platform = "osx"
69-
elif os.name == "posix" and system() == "Linux":
70-
os_platform = "linux"
71-
elif os.name == "win":
72-
os_platform = "win"
67+
"""
68+
Detect running operating system
69+
"""
70+
system_ = system()
71+
72+
if os.name == "posix" and system_ == "Darwin":
73+
return "osx"
74+
elif os.name == "posix" and system_ == "Linux":
75+
return "linux"
76+
elif os.name == "nt" and system_ == "Windows":
77+
return "win"
7378
else:
7479
raise ValueError("Unsupported OS")
7580

76-
return os_platform
77-
7881

7982
os_platform = detect_os()
8083

@@ -154,38 +157,40 @@ def adb_kill():
154157
"""
155158
kills adb server
156159
"""
157-
adb_command("kill-server")
160+
adb_command("kill-server")
158161

159162

160163
def adb_state():
161164
"""
162165
gets the state of adb server if state is device then phone is connected
163166
"""
164-
state = adb_command("get-state")
167+
state = adb_command("get-state", ignore_return_code=True)
165168

166169
if "error" in state:
167170
return False
168171

169172
return True
170173

171174

172-
def adb_command(cmd):
175+
def adb_command(cmd, ignore_return_code=False):
173176
if os_platform is "osx":
174177
prefix = "./adb_osx/adb "
175178
elif os_platform is "win":
176-
prefix = "\\adb_osx\\adb.exe "
179+
prefix = "adb_win\\adb.exe "
177180
elif os_platform is "linux":
178181
prefix = "./adb_linux/adb "
179182

180183
cmd = prefix + cmd
181184

182185
exit_code, output = subprocess.getstatusoutput(cmd)
186+
if ignore_return_code:
187+
return output
183188
if exit_code == 0:
184189
return output
185190
else:
186-
raise ValueError("Exit code not 0, an error occurred\n{}".format(output))
191+
print("Exit code {}, an error occurred\n{}".format(exit_code, output))
192+
sys.exit(-1)
187193

188-
189194

190195
def adb_install(source_path):
191196
"""
@@ -283,9 +288,19 @@ def main():
283288

284289
adb_kill() # kill any instances of adb before starting if any
285290

286-
if not adb_state():
287-
print("No phone connected via USB")
288-
sys.exit(-1)
291+
tries = 0
292+
while True:
293+
if adb_state():
294+
break
295+
else:
296+
print("No phone connected waiting to connect phone")
297+
298+
tries += 1
299+
if tries == 3:
300+
print("\nFine i give up bye bye")
301+
sys.exit(-1)
302+
303+
time.sleep(3)
289304

290305
print("Starting adb server...")
291306
adb_start() # start an instance of adb server

0 commit comments

Comments
 (0)