@@ -70,9 +70,14 @@ def run_command(
7070
7171
7272def add_logging (step_desc : str , return_if_fail : bool = False ):
73+ """Logging decorator to wrap functions that yield lines.
74+
75+ Logs the `step_desc`.
76+ """
77+
7378 def logging_decorator (func ):
7479 def logging (* args , ** kwargs ):
75- logger .info (step_desc )
80+ logger .info (f" { step_desc } - Paramters: { kwargs } " )
7681 for line in func (* args , ** kwargs ):
7782 if (type (line ) == bool ) and not line :
7883 logger .error (f"{ step_desc } Failed!" )
@@ -143,6 +148,20 @@ def adb_twrp_copy_partitions(bin_path: Path, config_path: Path):
143148 return True
144149
145150
151+ @add_logging ("Perform a factory reset with adb and twrp." , return_if_fail = True )
152+ def adb_twrp_format_data (bin_path : Path ):
153+ """Perform a factory reset with twrp and adb."""
154+ for line in run_command ("adb shell twrp format data" , bin_path ):
155+ yield line
156+
157+
158+ @add_logging ("Wipe the selected partition with adb and twrp." , return_if_fail = True )
159+ def adb_twrp_wipe_partition (bin_path : Path , partition : str ):
160+ """Perform a factory reset with twrp and adb."""
161+ for line in run_command ("adb shell twrp wipe {partition}" , bin_path ):
162+ yield line
163+
164+
146165def adb_twrp_wipe_and_install (
147166 bin_path : Path ,
148167 target : str ,
@@ -158,94 +177,60 @@ def adb_twrp_wipe_and_install(
158177 logger .info ("Wipe and format data with twrp, then install os image." )
159178 sleep (7 )
160179 # now perform a factory reset
161- for line in run_command ( "adb" , [ "shell" , "twrp" , "format" , "data" ], bin_path ):
180+ for line in adb_twrp_format_data ( bin_path ):
162181 yield line
163- if (type (line ) == bool ) and not line :
164- logger .error ("Formatting data failed." )
165- yield False
166- return
182+
167183 sleep (1 )
168184 # wipe some partitions
169185 for partition in ["cache" , "system" ]:
170- for line in run_command ( "adb" , [ "shell" , "twrp" , "wipe" , partition ], bin_path ):
171- yield not line
186+ for line in adb_twrp_wipe_partition ( bin_path = bin_path , partition = partition ):
187+ yield line
172188 sleep (1 )
173- if (type (line ) == bool ) and not line :
174- logger .error (f"Wiping { partition } failed." )
175- yield False
176- return
189+
177190 # activate sideload
178191 logger .info ("Wiping is done, now activate sideload." )
179- for line in run_command ( "adb" , [ "shell" , "twrp" , "sideload" ], bin_path ):
192+ for line in activate_sideload ( bin_path = bin_path ):
180193 yield line
181- if (type (line ) == bool ) and not line :
182- logger .error ("Activating sideload failed." )
183- yield False
184- return
185194 # now flash os image
186195 sleep (5 )
187196 logger .info ("Sideload and install os image." )
188- for line in run_command ("adb" , ["sideload" , f"{ target } " ], bin_path ):
189- yield line
190- if (type (line ) == bool ) and not line :
191- logger .error (f"Sideloading { target } failed." )
192- # TODO: this might sometimes think it failed, but actually it's fine. So skip for now.
193- # yield False
194- # return
197+ for line in adb_sideload (bin_path = bin_path , target = target ):
198+ if line :
199+ yield line
195200 # wipe some cache partitions
196201 sleep (7 )
197202 for partition in ["dalvik" , "cache" ]:
198- for line in run_command ( "adb" , [ "shell" , "twrp" , "wipe" , partition ], bin_path ):
203+ for line in adb_twrp_wipe_partition ( bin_path = bin_path , partition = partition ):
199204 yield line
200- sleep (1 )
201205 if (type (line ) == bool ) and not line :
202206 logger .error (f"Wiping { partition } failed." )
203207 # TODO: if this fails, a fix can be to just sideload something and then adb reboot
204- for line in run_command (
205- "adb" ,
206- [ "sideload" , f" { config_path . parent . joinpath ( Path ( 'helper.txt' )) } " ] ,
207- bin_path ,
208+ sleep ( 1 )
209+ for line in adb_sideload (
210+ bin_path = bin_path ,
211+ target = f" { config_path . parent . joinpath ( Path ( 'helper.txt' )) } " ,
208212 ):
209213 yield line
210214 sleep (1 )
211- if (type (line ) == bool ) and not line :
212- yield False
213215 break
214216 # finally reboot into os or to fastboot for flashing addons
215217 sleep (7 )
216218 if install_addons :
217219 if is_ab :
218220 # reboot into the bootloader again
219- logger .info ("Rebooting device into bootloader with adb." )
220- for line in run_command ("adb" , ["reboot" , "bootloader" ], bin_path ):
221+ for line in adb_reboot_bootloader (bin_path ):
221222 yield line
222- if (type (line ) == bool ) and not line :
223- logger .error ("Reboot into bootloader failed." )
224- yield False
225- return
226223 sleep (3 )
227224 # boot to TWRP again
228- logger .info ("Boot custom recovery with fastboot." )
229- for line in run_command ("fastboot" , ["boot" , f"{ recovery } " ], bin_path ):
225+ for line in fastboot_flash_boot (bin_path = bin_path , recovery = recovery ):
230226 yield line
231- if (type (line ) == bool ) and not line :
232- logger .error ("Reboot into bootloader failed." )
233- yield False
234- return
235227 sleep (7 )
236228 else :
237229 # if not an a/b-device just stay in twrp
238230 pass
239231 else :
240- logger .info ("Reboot into OS." )
241- for line in run_command ("adb" , ["reboot" ], bin_path ):
232+ for line in adb_reboot (bin_path = bin_path ):
242233 yield line
243- if (type (line ) == bool ) and not line :
244- logger .error ("Rebooting failed." )
245- yield False
246- return
247- else :
248- yield True
249234
250235
251236def adb_twrp_install_addons (bin_path : Path , addons : List [str ], is_ab : bool ) -> bool :
@@ -259,70 +244,42 @@ def adb_twrp_install_addons(bin_path: Path, addons: List[str], is_ab: bool) -> b
259244 for addon in addons :
260245 # activate sideload
261246 logger .info ("Activate sideload." )
262- for line in run_command ( "adb" , [ "shell" , "twrp" , "sideload" ], bin_path ):
247+ for line in activate_sideload ( bin_path = bin_path ):
263248 yield line
264- if (type (line ) == bool ) and not line :
265- logger .error ("Activating sideload failed." )
266- yield False
267- return
268249 sleep (5 )
269250 # now flash os image
270- for line in run_command ("adb" , ["sideload" , f"{ addon } " ], bin_path ):
271- yield line
272- if (type (line ) == bool ) and not line :
273- logger .error (f"Sideloading { addon } failed." )
274- # TODO: this might sometimes think it failed, but actually it's fine. So skip for now.
275- # yield False
276- # return
251+ for line in adb_sideload (bin_path = bin_path , target = addon ):
252+ if line :
253+ yield line
277254 sleep (7 )
278255 # finally reboot into os
279256 if is_ab :
280257 # reboot into the bootloader again
281- logger .info ("Rebooting device into bootloader with adb." )
282- for line in run_command ("adb" , ["reboot" , "bootloader" ], bin_path ):
258+ for line in adb_reboot_bootloader (bin_path = bin_path ):
283259 yield line
284- if (type (line ) == bool ) and not line :
285- logger .error ("Reboot into bootloader failed." )
286- yield False
287- return
288260 sleep (3 )
289261 # switch active boot partition
290- logger .info ("Switch active boot partition" )
291- for line in run_command ("fastboot" , ["set_active" , "other" ], bin_path ):
262+ for line in fastboot_switch_partition (bin_path = bin_path ):
292263 yield line
293- if (type (line ) == bool ) and not line :
294- logger .error ("Switching boot partition failed." )
295- yield False
296- return
297264 sleep (1 )
298- for line in run_command ( "fastboot" , [ "set_active" , "other" ], bin_path ):
265+ for line in fastboot_switch_partition ( bin_path = bin_path ):
299266 yield line
300- if (type (line ) == bool ) and not line :
301- logger .error ("Switching boot partition failed." )
302- yield False
303- return
304267 sleep (1 )
305268 # reboot with fastboot
306269 logger .info ("Reboot into OS." )
307- for line in run_command ( "fastboot" , [ "reboot" ], bin_path ):
270+ for line in fastboot_reboot ( bin_path = bin_path ):
308271 yield line
309- if (type (line ) == bool ) and not line :
310- logger .error ("Rebooting failed." )
311- yield False
312- return
313- else :
314- yield True
315272 else :
316273 # reboot with adb
317- logger .info ("Reboot into OS." )
318- for line in run_command ("adb" , ["reboot" ], bin_path ):
274+ for line in adb_reboot (bin_path = bin_path ):
319275 yield line
320- if (type (line ) == bool ) and not line :
321- logger .error ("Rebooting failed." )
322- yield False
323- return
324- else :
325- yield True
276+
277+
278+ @add_logging ("Switch active boot partitions." , return_if_fail = True )
279+ def fastboot_switch_partition (bin_path : Path ) -> Union [str , bool ]:
280+ """Switch the active boot partition with fastboot."""
281+ for line in run_command ("fastboot set_active other" , bin_path ):
282+ yield line
326283
327284
328285@add_logging ("Unlock the device with fastboot and code." )
@@ -364,13 +321,11 @@ def fastboot_flash_recovery(bin_path: Path, recovery: str, is_ab: bool = True) -
364321 """Temporarily, flash custom recovery with fastboot."""
365322 if is_ab :
366323 logger .info ("Boot custom recovery with fastboot." )
367- for line in run_command ("fastboot" , [ " boot" , f" { recovery } "] , bin_path ):
324+ for line in run_command (f "fastboot boot { recovery } " , bin_path ):
368325 yield line
369326 else :
370327 logger .info ("Flash custom recovery with fastboot." )
371- for line in run_command (
372- "fastboot" , ["flash" , "recovery" , f"{ recovery } " ], bin_path
373- ):
328+ for line in run_command (f"fastboot flash recovery { recovery } " , bin_path ):
374329 yield line
375330 if (type (line ) == bool ) and not line :
376331 logger .error ("Flashing recovery failed." )
@@ -379,7 +334,7 @@ def fastboot_flash_recovery(bin_path: Path, recovery: str, is_ab: bool = True) -
379334 yield True
380335 # reboot
381336 logger .info ("Boot into TWRP with fastboot." )
382- for line in run_command ("fastboot" , [ " reboot" , " recovery"] , bin_path ):
337+ for line in run_command ("fastboot reboot recovery" , bin_path ):
383338 yield line
384339
385340 if (type (line ) == bool ) and not line :
@@ -392,7 +347,7 @@ def fastboot_flash_recovery(bin_path: Path, recovery: str, is_ab: bool = True) -
392347def fastboot_flash_boot (bin_path : Path , recovery : str ) -> bool :
393348 """Temporarily, flash custom recovery with fastboot to boot partition."""
394349 logger .info ("Flash custom recovery with fastboot." )
395- for line in run_command ("fastboot" , [ " flash" , " boot" , f" { recovery } "] , bin_path ):
350+ for line in run_command (f "fastboot flash boot { recovery } " , bin_path ):
396351 yield line
397352 if (type (line ) == bool ) and not line :
398353 logger .error ("Flashing recovery failed." )
@@ -401,7 +356,7 @@ def fastboot_flash_boot(bin_path: Path, recovery: str) -> bool:
401356 yield True
402357 # reboot
403358 logger .info ("Boot into TWRP with fastboot." )
404- for line in run_command ("fastboot" , [ " reboot"] , bin_path ):
359+ for line in run_command ("fastboot reboot" , bin_path ):
405360 yield line
406361 if (type (line ) == bool ) and not line :
407362 logger .error ("Booting recovery failed." )
0 commit comments