|
43 | 43 | from timeit import default_timer as timer |
44 | 44 | from platform import system |
45 | 45 | from datetime import datetime |
| 46 | +import time |
46 | 47 | import subprocess |
47 | 48 | import argparse |
48 | 49 | import shutil |
|
56 | 57 | INSTALL_OK = 1 |
57 | 58 | INSTALL_EXISTS = 2 |
58 | 59 |
|
59 | | -# Flags used for adb package listing |
| 60 | +# Flags for adb list packages |
60 | 61 | pkg_flags = {"all": "", # list all packages |
61 | 62 | "user": "-3", # list 3d party packages only (default) |
62 | 63 | "system": "-S"} # list system packages only |
63 | 64 |
|
64 | 65 |
|
65 | 66 | 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" |
73 | 78 | else: |
74 | 79 | raise ValueError("Unsupported OS") |
75 | 80 |
|
76 | | - return os_platform |
77 | | - |
78 | 81 |
|
79 | 82 | os_platform = detect_os() |
80 | 83 |
|
@@ -154,38 +157,40 @@ def adb_kill(): |
154 | 157 | """ |
155 | 158 | kills adb server |
156 | 159 | """ |
157 | | - adb_command("kill-server") |
| 160 | + adb_command("kill-server") |
158 | 161 |
|
159 | 162 |
|
160 | 163 | def adb_state(): |
161 | 164 | """ |
162 | 165 | gets the state of adb server if state is device then phone is connected |
163 | 166 | """ |
164 | | - state = adb_command("get-state") |
| 167 | + state = adb_command("get-state", ignore_return_code=True) |
165 | 168 |
|
166 | 169 | if "error" in state: |
167 | 170 | return False |
168 | 171 |
|
169 | 172 | return True |
170 | 173 |
|
171 | 174 |
|
172 | | -def adb_command(cmd): |
| 175 | +def adb_command(cmd, ignore_return_code=False): |
173 | 176 | if os_platform is "osx": |
174 | 177 | prefix = "./adb_osx/adb " |
175 | 178 | elif os_platform is "win": |
176 | | - prefix = "\\adb_osx\\adb.exe " |
| 179 | + prefix = "adb_win\\adb.exe " |
177 | 180 | elif os_platform is "linux": |
178 | 181 | prefix = "./adb_linux/adb " |
179 | 182 |
|
180 | 183 | cmd = prefix + cmd |
181 | 184 |
|
182 | 185 | exit_code, output = subprocess.getstatusoutput(cmd) |
| 186 | + if ignore_return_code: |
| 187 | + return output |
183 | 188 | if exit_code == 0: |
184 | 189 | return output |
185 | 190 | 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) |
187 | 193 |
|
188 | | - |
189 | 194 |
|
190 | 195 | def adb_install(source_path): |
191 | 196 | """ |
@@ -283,9 +288,19 @@ def main(): |
283 | 288 |
|
284 | 289 | adb_kill() # kill any instances of adb before starting if any |
285 | 290 |
|
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) |
289 | 304 |
|
290 | 305 | print("Starting adb server...") |
291 | 306 | adb_start() # start an instance of adb server |
|
0 commit comments