2424 CompletedProcess ,
2525 check_output ,
2626)
27+ import shlex
2728from time import sleep
28- from typing import List , Optional
29+ from typing import List , Optional , Union
2930
3031from loguru import logger
3132
3233PLATFORM = sys .platform
3334
3435
3536def run_command (
36- tool : str , command : List [ str ] , bin_path : Path , enable_logging : bool = True
37- ) -> CompletedProcess :
37+ command : str , bin_path : Path , enable_logging : bool = True
38+ ) -> Union [ str , bool ] :
3839 """Run a command with a tool (adb, fastboot, heimdall)."""
39- yield f"${ ' ' .join ([tool ] + command )} "
40+ yield f"${ command } "
41+ # split the command and extract the tool part
42+ tool , * command = shlex .split (command )
4043 if tool not in ["adb" , "fastboot" , "heimdall" ]:
4144 raise Exception (f"Unknown tool { tool } . Use adb, fastboot or heimdall." )
4245 if PLATFORM == "win32" :
@@ -63,89 +66,75 @@ def run_command(
6366 logger .info (line .strip ())
6467 yield line .strip ()
6568
69+ # finally return if the command was successful
6670 yield p .returncode == 0
6771
6872
73+ def add_logging (step_desc : str , return_if_fail : bool = False ):
74+ def logging_decorator (func ):
75+ def logging (* args , ** kwargs ):
76+ logger .info (step_desc )
77+ for line in func (* args , ** kwargs ):
78+ if (type (line ) == bool ) and not line :
79+ logger .error (f"{ step_desc } Failed!" )
80+ if return_if_fail :
81+ return
82+ yield line
83+
84+ return logging
85+ return logging_decorator
86+
87+
88+ @add_logging ("Rebooting device with adb." )
6989def adb_reboot (bin_path : Path ) -> bool :
7090 """Run adb reboot on the device and return success."""
71- logger .info ("Rebooting device with adb." )
72- for line in run_command ("adb" , ["reboot" ], bin_path ):
91+ for line in run_command ("adb reboot" , bin_path ):
7392 yield line
74- if (type (line ) == bool ) and line :
75- logger .debug ("Reboot failed." )
76- yield False
77- else :
78- yield True
7993
8094
81- def adb_reboot_bootloader (bin_path : Path ) -> bool :
95+ @add_logging ("Rebooting device into bootloader with adb." , return_if_fail = True )
96+ def adb_reboot_bootloader (bin_path : Path ) -> Union [str , bool ]:
8297 """Reboot the device into bootloader and return success."""
83- logger .info ("Rebooting device into bootloader with adb." )
84- for line in run_command ("adb" , ["reboot" , "bootloader" ], bin_path ):
85- if (type (line ) == bool ) and not line :
86- logger .error ("Reboot into bootloader failed." )
98+ for line in run_command ("adb reboot bootloader" , bin_path ):
8799 yield line
88- sleep (1 )
89100
90101
91- def adb_reboot_download (bin_path : Path ) -> bool :
102+ @add_logging ("Rebooting device into download mode with adb." )
103+ def adb_reboot_download (bin_path : Path ) -> Union [str , bool ]:
92104 """Reboot the device into download mode of samsung devices and return success."""
93- logger .info ("Rebooting device into download mode with adb." )
94- for line in run_command ("adb" , ["reboot" , "download" ], bin_path ):
105+ for line in run_command ("adb reboot download" , bin_path ):
95106 yield line
96- if (type (line ) == bool ) and not line :
97- logger .error ("Reboot into download mode failed." )
98- yield False
99- else :
100- # check if in download mode with heimdall?
101- yield True
102107
103108
104- def adb_sideload (bin_path : Path , target : str ) -> bool :
109+ @add_logging ("Sideload the target to device with adb." )
110+ def adb_sideload (bin_path : Path , target : str ) -> Union [str , bool ]:
105111 """Sideload the target to device and return success."""
106- logger .info ("Rebooting device into bootloader with adb." )
107- for line in run_command ("adb" , ["sideload" , target ], bin_path ):
112+ for line in run_command (f"adb sideload { target } " , bin_path ):
113+ yield line
114+
115+
116+ @add_logging ("Activate sideloading in TWRP." , return_if_fail = True )
117+ def activate_sideload (bin_path : Path ) -> Union [str , bool ]:
118+ """Activate sideload with adb shell in twrp."""
119+ for line in run_command ("adb shell twrp sideload" , bin_path ):
108120 yield line
109- if (type (line ) == bool ) and line :
110- logger .info (f"Sideloading { target } failed." )
111- yield False
112- else :
113- yield True
114121
115122
116123def adb_twrp_copy_partitions (bin_path : Path , config_path : Path ):
117124 # some devices like one plus 6t or motorola moto g7 power need the partitions copied to prevent a hard brick
118125 logger .info ("Sideload copy_partitions script with adb." )
119126 # activate sideload
120- for line in run_command ( "adb" , [ "shell" , "twrp" , "sideload" ], bin_path ):
127+ for line in activate_sideload ( bin_path ):
121128 yield line
122- if (type (line ) == bool ) and not line :
123- logger .error ("Activating sideload failed." )
124- yield False
125- return
126129 # now sideload the script
127130 sleep (5 )
128131 logger .info ("Sideload the copy_partitions script" )
129- for line in run_command (
130- "adb" ,
131- [
132- "sideload" ,
133- f"{ config_path .parent .joinpath (Path ('copy-partitions-20220613-signed.zip' ))} " ,
134- ],
135- bin_path ,
136- ):
132+ for line in adb_sideload (bin_path = bin_path , target = f"{ config_path .parent .joinpath (Path ('copy-partitions-20220613-signed.zip' ))} " ):
137133 yield line
138- if (type (line ) == bool ) and not line :
139- logger .error ("Sideloading copy-partitions-20220613-signed.zip failed." )
140134 sleep (10 )
141135 # reboot into the bootloader again
142- logger .info ("Rebooting device into bootloader with adb." )
143- for line in run_command ("adb" , ["reboot" , "bootloader" ], bin_path ):
136+ for line in adb_reboot_bootloader (bin_path ):
144137 yield line
145- if (type (line ) == bool ) and not line :
146- logger .error ("Reboot into bootloader failed." )
147- yield False
148- return
149138 sleep (7 )
150139 # Copy partitions end #
151140 return True
@@ -333,64 +322,39 @@ def adb_twrp_install_addons(bin_path: Path, addons: List[str], is_ab: bool) -> b
333322 yield True
334323
335324
336- def fastboot_unlock_with_code (bin_path : Path , unlock_code : str ) -> bool :
325+ @add_logging ("Unlock the device with fastboot and code." )
326+ def fastboot_unlock_with_code (bin_path : Path , unlock_code : str ) -> Union [str , bool ]:
337327 """Unlock the device with fastboot and code given."""
338- logger .info (f"Unlock the device with fastboot and code: { unlock_code } ." )
339- for line in run_command ("fastboot" , ["oem" , "unlock" , f"{ unlock_code } " ], bin_path ):
328+ for line in run_command (f"fastboot oem unlock { unlock_code } " , bin_path ):
340329 yield line
341- if (type (line ) == bool ) and not line :
342- logger .error (f"Unlocking with code { unlock_code } failed." )
343- yield False
344- else :
345- yield True
346330
347331
348- def fastboot_unlock (bin_path : Path ) -> bool :
332+ @add_logging ("Unlock the device with fastboot without code." )
333+ def fastboot_unlock (bin_path : Path ) -> Union [str , bool ]:
349334 """Unlock the device with fastboot and without code."""
350- logger .info ("Unlock the device with fastboot." )
351- for line in run_command ("fastboot" , ["flashing" , "unlock" ], bin_path ):
335+ for line in run_command ("fastboot flashing unlock" , bin_path ):
352336 yield line
353- if (type (line ) == bool ) and not line :
354- logger .error ("Unlocking failed." )
355- yield False
356- else :
357- yield True
358337
359338
360- def fastboot_oem_unlock (bin_path : Path ) -> bool :
339+ @add_logging ("OEM unlocking the device with fastboot." )
340+ def fastboot_oem_unlock (bin_path : Path ) -> Union [str , bool ]:
361341 """OEM unlock the device with fastboot and without code."""
362- logger .info ("OEM unlocking the device with fastboot." )
363- for line in run_command ("fastboot" , ["oem" , "unlock" ], bin_path ):
342+ for line in run_command ("fastboot oem unlock" , bin_path ):
364343 yield line
365- if (type (line ) == bool ) and not line :
366- logger .error ("OEM unlocking failed." )
367- yield False
368- else :
369- yield True
370344
371345
372- def fastboot_get_unlock_data (bin_path : Path ) -> bool :
346+ @add_logging ("Get unlock data with fastboot" )
347+ def fastboot_get_unlock_data (bin_path : Path ) -> Union [str , bool ]:
373348 """Get the unlock data with fastboot"""
374- logger .info ("Get unlock data with fastboot" )
375- for line in run_command ("fastboot" , ["oem" , "get_unlock_data" ], bin_path ):
349+ for line in run_command ("fastboot oem get_unlock_data" , bin_path ):
376350 yield line
377- if (type (line ) == bool ) and not line :
378- logger .error ("Getting unlock data failed." )
379- yield False
380- else :
381- yield True
382351
383352
384- def fastboot_reboot (bin_path : Path ) -> bool :
353+ @add_logging ("Rebooting device with fastboot." )
354+ def fastboot_reboot (bin_path : Path ) -> Union [str , bool ]:
385355 """Reboot with fastboot"""
386- logger .info ("Rebooting device with fastboot." )
387- for line in run_command ("fastboot" , ["reboot" ], bin_path ):
356+ for line in run_command ("fastboot reboot" , bin_path ):
388357 yield line
389- if (type (line ) == bool ) and not line :
390- logger .error ("Rebooting with fastboot failed." )
391- yield False
392- else :
393- yield True
394358
395359
396360def fastboot_flash_recovery (bin_path : Path , recovery : str , is_ab : bool = True ) -> bool :
@@ -443,18 +407,13 @@ def fastboot_flash_boot(bin_path: Path, recovery: str) -> bool:
443407 yield True
444408
445409
446- def heimdall_flash_recovery (bin_path : Path , recovery : str ) -> bool :
410+ @add_logging ("Flash custom recovery with heimdall." )
411+ def heimdall_flash_recovery (bin_path : Path , recovery : str ) -> Union [str , bool ]:
447412 """Temporarily, flash custom recovery with heimdall."""
448- logger .info ("Flash custom recovery with heimdall." )
449413 for line in run_command (
450- "heimdall" , [ " flash" , " --no-reboot" , " --RECOVERY" , f" { recovery } "] , bin_path
414+ f "heimdall flash --no-reboot --RECOVERY { recovery } " , bin_path
451415 ):
452416 yield line
453- if (type (line ) == bool ) and not line :
454- logger .error ("Flashing recovery with heimdall failed." )
455- yield False
456- else :
457- yield True
458417
459418
460419def search_device (platform : str , bin_path : Path ) -> Optional [str ]:
0 commit comments