1616import sys
1717from pathlib import Path
1818from subprocess import (
19+ Popen ,
1920 PIPE ,
2021 STDOUT ,
2122 CalledProcessError ,
3435
3536def run_command (tool : str , command : List [str ], bin_path : Path ) -> CompletedProcess :
3637 """Run a command with a tool (adb, fastboot, heimdall)."""
38+ yield f"${ ' ' .join ([tool ] + command )} "
3739 if tool not in ["adb" , "fastboot" , "heimdall" ]:
3840 raise Exception (f"Unknown tool { tool } . Use adb, fastboot or heimdall." )
3941 if PLATFORM == "win32" :
4042 full_command = [str (bin_path .joinpath (Path (f"{ tool } " ))) + ".exe" ] + command
4143 else :
4244 full_command = [str (bin_path .joinpath (Path (f"{ tool } " )))] + command
43-
4445 logger .info (f"Run command: { full_command } " )
45- result = run (full_command , stdout = PIPE , stderr = PIPE , universal_newlines = True )
46- # result contains result.returncode, result.stdout, result.stderr
47- return result
46+ with Popen (full_command , stdout = PIPE , stderr = STDOUT , bufsize = 1 , universal_newlines = True ) as p :
47+ for line in p .stdout :
48+ logger .info (line )
49+ yield line
50+
51+ yield p .returncode == 0
4852
4953
5054def adb_reboot (bin_path : Path ) -> bool :
5155 """Run adb reboot on the device and return success."""
5256 logger .info ("Rebooting device with adb." )
53- result = run_command ("adb" , ["reboot" ], bin_path )
54- if result .returncode != 0 :
57+ for line in run_command ("adb" , ["reboot" ], bin_path ):
58+ yield line
59+ if (type (line ) == bool ) and line :
5560 logger .info ("Reboot failed." )
56- return False
57- return True
61+ yield False
62+ else :
63+ yield True
5864
5965
6066def adb_reboot_bootloader (bin_path : Path ) -> bool :
6167 """Reboot the device into bootloader and return success."""
6268 logger .info ("Rebooting device into bootloader with adb." )
63- result = run_command ("adb" , ["reboot" , "bootloader" ], bin_path )
64- if result .returncode != 0 :
69+ for line in run_command ("adb" , ["reboot" , "bootloader" ], bin_path ):
70+ yield line
71+ if (type (line ) == bool ) and not line :
6572 logger .info ("Reboot into bootloader failed." )
66- return False
73+ yield False
74+ return
75+ sleep (1 )
6776 # check if in fastboot mode
68- result = run_command ("fastboot" , ["devices" ], bin_path )
69- if result .returncode != 0 :
70- logger .info ("Reboot into bootloader failed." )
71- logger .info (result .returncode )
72- logger .info (result .stdout )
73- logger .info (result .stderr )
74- return False
75- return True
77+ for line in run_command ("fastboot" , ["devices" ], bin_path ):
78+ yield line
79+ if (type (line ) == bool ) and not line :
80+ logger .info ("No fastboot mode detected. Reboot into bootloader failed." )
81+ yield False
82+ else :
83+ yield True
7684
7785
7886def adb_reboot_download (bin_path : Path ) -> bool :
7987 """Reboot the device into download mode of samsung devices and return success."""
8088 logger .info ("Rebooting device into download mode with adb." )
81- result = run_command ("adb" , ["reboot" , "download" ], bin_path )
82- if result .returncode != 0 :
89+ for line in run_command ("adb" , ["reboot" , "download" ], bin_path ):
90+ yield line
91+ if (type (line ) == bool ) and not line :
8392 logger .info ("Reboot into download mode failed." )
84- return False
85- # check if in download mode with heimdall?
86- return True
93+ yield False
94+ else :
95+ # check if in download mode with heimdall?
96+ yield True
8797
8898
8999def adb_sideload (bin_path : Path , target : str ) -> bool :
90100 """Sideload the target to device and return success."""
91101 logger .info ("Rebooting device into bootloader with adb." )
92- result = run_command ("adb" , ["sideload" , target ], bin_path )
93- if result .returncode != 0 :
102+ for line in run_command ("adb" , ["sideload" , target ], bin_path ):
103+ yield line
104+ if (type (line ) == bool ) and line :
94105 logger .info (f"Sideloading { target } failed." )
95- return False
96- return True
106+ yield False
107+ else :
108+ yield True
97109
98110
99111def adb_twrp_wipe_and_install (bin_path : Path , target : str ) -> bool :
@@ -103,109 +115,132 @@ def adb_twrp_wipe_and_install(bin_path: Path, target: str) -> bool:
103115 """
104116 logger .info ("Wipe and format data with twrp, then install os image." )
105117 sleep (7 )
106- result = run_command ("adb" , ["shell" , "twrp" , "format" , "data" ], bin_path )
107- if result .returncode != 0 :
118+ for line in run_command ("adb" , ["shell" , "twrp" , "format" , "data" ], bin_path ):
119+ yield line
120+ if (type (line ) == bool ) and not line :
108121 logger .info ("Formatting data failed." )
109- return False
122+ yield False
123+ return
124+ sleep (1 )
110125 # wipe some partitions
111126 for partition in ["cache" , "system" ]:
112- result = run_command ("adb" , ["shell" , "twrp" , "wipe" , partition ], bin_path )
127+ for line in run_command ("adb" , ["shell" , "twrp" , "wipe" , partition ], bin_path ):
128+ yield not line
113129 sleep (1 )
114- if result . returncode != 0 :
130+ if ( type ( line ) == bool ) and not line :
115131 logger .info (f"Wiping { partition } failed." )
116- return False
132+ yield False
133+ return
117134 # activate sideload
118135 logger .info ("Wiping is done, now activate sideload." )
119- result = run_command ("adb" , ["shell" , "twrp" , "sideload" ], bin_path )
120- if result .returncode != 0 :
136+ for line in run_command ("adb" , ["shell" , "twrp" , "sideload" ], bin_path ):
137+ yield line
138+ if (type (line ) == bool ) and not line :
121139 logger .info ("Activating sideload failed." )
122- return False
140+ yield False
141+ return
123142 # now flash os image
124143 sleep (5 )
125144 logger .info ("Sideload and install os image." )
126- result = run_command ("adb" , ["sideload" , f"{ target } " ], bin_path )
127- if result .returncode != 0 :
145+ for line in run_command ("adb" , ["sideload" , f"{ target } " ], bin_path ):
146+ yield line
147+ if (type (line ) == bool ) and not line :
128148 logger .info (f"Sideloading { target } failed." )
129- return False
149+ yield False
150+ return
130151 # wipe some cache partitions
131152 sleep (5 )
132153 for partition in ["cache" , "dalvik" ]:
133- result = run_command ("adb" , ["shell" , "twrp" , "wipe" , partition ], bin_path )
154+ for line in run_command ("adb" , ["shell" , "twrp" , "wipe" , partition ], bin_path ):
155+ yield line
134156 sleep (1 )
135- if result . returncode != 0 :
157+ if ( type ( line ) == bool ) and not line :
136158 logger .info (f"Wiping { partition } failed." )
137- return False
159+ yield False
160+ return
138161 # finally reboot into os
139162 sleep (5 )
140163 logger .info ("Reboot into OS." )
141- result = run_command ("adb" , ["shell" , "twrp" , "reboot" ], bin_path )
142- if result .returncode != 0 :
164+ for line in run_command ("adb" , ["shell" , "twrp" , "reboot" ], bin_path ):
165+ yield line
166+ if (type (line ) == bool ) and not line :
143167 logger .info ("Rebooting with twrp failed." )
144- return False
145-
146- return True
168+ yield False
169+ return
170+ else :
171+ yield True
147172
148173
149174def fastboot_unlock_with_code (bin_path : Path , unlock_code : str ) -> bool :
150175 """Unlock the device with fastboot and code given."""
151176 logger .info (f"Unlock the device with fastboot and code: { unlock_code } ." )
152- result = run_command ("fastboot" , ["oem" , "unlock" , f"{ unlock_code } " ], bin_path )
153- if result .returncode != 0 :
177+ for line in run_command ("adb" , ["oem" , "unlock" , f"{ unlock_code } " ], bin_path ):
178+ yield line
179+ if (type (line ) == bool ) and not line :
154180 logger .info (f"Unlocking with code { unlock_code } failed." )
155- return False
156- return True
181+ yield False
182+ else :
183+ yield True
157184
158185
159186def fastboot_unlock (bin_path : Path ) -> bool :
160187 """Unlock the device with fastboot and without code."""
161188 logger .info (f"Unlock the device with fastboot." )
162- result = run_command ("fastboot" , ["flashing" , "unlock" ], bin_path )
163- if result .returncode != 0 :
189+ for line in run_command ("adb" , ["flashing" , "unlock" ], bin_path ):
190+ yield line
191+ if (type (line ) == bool ) and not line :
164192 logger .info (f"Unlocking failed." )
165- return False
166- return True
193+ yield False
194+ else :
195+ yield True
167196
168197
169198def fastboot_oem_unlock (bin_path : Path ) -> bool :
170199 """OEM unlock the device with fastboot and without code."""
171200 logger .info (f"OEM unlocking the device with fastboot." )
172- result = run_command ("fastboot" , ["oem" , "unlock" ], bin_path )
173- if result .returncode != 0 :
201+ for line in run_command ("adb" , ["oem" , "unlock" ], bin_path ):
202+ yield line
203+ if (type (line ) == bool ) and not line :
174204 logger .info (f"OEM unlocking failed." )
175- return False
176- return True
205+ yield False
206+ else :
207+ yield True
177208
178209
179210def fastboot_reboot (bin_path : Path ) -> bool :
180211 """Reboot with fastboot"""
181212 logger .info (f"Rebooting device with fastboot." )
182- result = run_command ("fastboot" , ["reboot" ], bin_path )
183- if result .returncode != 0 :
213+ for line in run_command ("fastboot" , ["reboot" ], bin_path ):
214+ yield line
215+ if (type (line ) == bool ) and not line :
184216 logger .info (f"Rebooting with fastboot failed." )
185- return False
186- return True
217+ yield False
218+ else :
219+ yield True
187220
188221
189222def fastboot_flash_recovery (bin_path : Path , recovery : str ) -> bool :
190223 """Temporarily, flash custom recovery with fastboot."""
191224 logger .info (f"Flash custom recovery with fastboot." )
192- result = run_command ("fastboot" , ["boot" , f"{ recovery } " ], bin_path )
193- if result .returncode != 0 :
225+ for line in run_command ("fastboot" , ["boot" , f"{ recovery } " ], bin_path ):
226+ yield line
227+ if (type (line ) == bool ) and not line :
194228 logger .info (f"Flashing recovery failed." )
195- return False
196- return True
229+ yield False
230+ else :
231+ yield True
197232
198233
199234def heimdall_flash_recovery (bin_path : Path , recovery : str ) -> bool :
200235 """Temporarily, flash custom recovery with heimdall."""
201236 logger .info (f"Flash custom recovery with heimdall." )
202- result = run_command (
203- "heimdall" , [ "flash" , "--no-reboot" , "--RECOVERY" , f" { recovery } " ], bin_path
204- )
205- if result . returncode != 0 :
206- logger . info ( f"Flashing recovery failed." )
207- return False
208- return True
237+ for line in run_command ("heimdall" , [ "flash" , "--no-reboot" , "--RECOVERY" , f" { recovery } " ], bin_path ):
238+ yield line
239+ if ( type ( line ) == bool ) and not line :
240+ logger . info ( f"Flashing recovery with heimdall failed." )
241+ yield False
242+ else :
243+ yield True
209244
210245
211246def search_device (platform : str , bin_path : Path ) -> Optional [str ]:
0 commit comments