2222 CalledProcessError ,
2323 CompletedProcess ,
2424 check_output ,
25- run ,
2625)
2726from time import sleep
2827from typing import List , Optional
2928
30- import regex as re
3129from loguru import logger
3230
3331PLATFORM = sys .platform
@@ -43,7 +41,9 @@ def run_command(tool: str, command: List[str], bin_path: Path) -> CompletedProce
4341 else :
4442 full_command = [str (bin_path .joinpath (Path (f"{ tool } " )))] + command
4543 logger .info (f"Run command: { full_command } " )
46- with Popen (full_command , stdout = PIPE , stderr = STDOUT , bufsize = 1 , universal_newlines = True ) as p :
44+ with Popen (
45+ full_command , stdout = PIPE , stderr = STDOUT , bufsize = 1 , universal_newlines = True
46+ ) as p :
4747 for line in p .stdout :
4848 logger .info (line .strip ())
4949 yield line
@@ -59,7 +59,7 @@ def adb_reboot(bin_path: Path) -> bool:
5959 if (type (line ) == bool ) and line :
6060 logger .debug ("Reboot failed." )
6161 yield False
62- else :
62+ else :
6363 yield True
6464
6565
@@ -71,7 +71,7 @@ def adb_reboot_bootloader(bin_path: Path) -> bool:
7171 if (type (line ) == bool ) and not line :
7272 logger .error ("Reboot into bootloader failed." )
7373 yield False
74- return
74+ return
7575 sleep (1 )
7676 yield True
7777 # TODO: check if in fastboot mode
@@ -92,7 +92,7 @@ def adb_reboot_download(bin_path: Path) -> bool:
9292 if (type (line ) == bool ) and not line :
9393 logger .error ("Reboot into download mode failed." )
9494 yield False
95- else :
95+ else :
9696 # check if in download mode with heimdall?
9797 yield True
9898
@@ -105,7 +105,7 @@ def adb_sideload(bin_path: Path, target: str) -> bool:
105105 if (type (line ) == bool ) and line :
106106 logger .info (f"Sideloading { target } failed." )
107107 yield False
108- else :
108+ else :
109109 yield True
110110
111111
@@ -121,7 +121,7 @@ def adb_twrp_wipe_and_install(bin_path: Path, target: str, config_path: Path) ->
121121 if (type (line ) == bool ) and not line :
122122 logger .error ("Formatting data failed." )
123123 yield False
124- return
124+ return
125125 sleep (1 )
126126 # wipe some partitions
127127 for partition in ["cache" , "system" ]:
@@ -131,15 +131,15 @@ def adb_twrp_wipe_and_install(bin_path: Path, target: str, config_path: Path) ->
131131 if (type (line ) == bool ) and not line :
132132 logger .error (f"Wiping { partition } failed." )
133133 yield False
134- return
134+ return
135135 # activate sideload
136136 logger .info ("Wiping is done, now activate sideload." )
137137 for line in run_command ("adb" , ["shell" , "twrp" , "sideload" ], bin_path ):
138138 yield line
139139 if (type (line ) == bool ) and not line :
140140 logger .error ("Activating sideload failed." )
141141 yield False
142- return
142+ return
143143 # now flash os image
144144 sleep (5 )
145145 logger .info ("Sideload and install os image." )
@@ -149,7 +149,7 @@ def adb_twrp_wipe_and_install(bin_path: Path, target: str, config_path: Path) ->
149149 logger .error (f"Sideloading { target } failed." )
150150 # TODO: this might sometimes think it failed, but actually it's fine. So skip for now.
151151 # yield False
152- # return
152+ # return
153153 # wipe some cache partitions
154154 sleep (7 )
155155 for partition in ["dalvik" , "cache" ]:
@@ -164,17 +164,17 @@ def adb_twrp_wipe_and_install(bin_path: Path, target: str, config_path: Path) ->
164164 sleep (1 )
165165 if (type (line ) == bool ) and not line :
166166 yield False
167- return
167+ return
168168 break
169169 # finally reboot into os
170170 sleep (5 )
171171 logger .info ("Reboot into OS." )
172- for line in run_command ("adb" , ["reboot" ], bin_path ): # "shell", "twrp",
172+ for line in run_command ("adb" , ["reboot" ], bin_path ): # "shell", "twrp",
173173 yield line
174174 if (type (line ) == bool ) and not line :
175175 logger .error ("Rebooting failed." )
176176 yield False
177- return
177+ return
178178 else :
179179 yield True
180180
@@ -187,67 +187,69 @@ def fastboot_unlock_with_code(bin_path: Path, unlock_code: str) -> bool:
187187 if (type (line ) == bool ) and not line :
188188 logger .error (f"Unlocking with code { unlock_code } failed." )
189189 yield False
190- else :
190+ else :
191191 yield True
192192
193193
194194def fastboot_unlock (bin_path : Path ) -> bool :
195195 """Unlock the device with fastboot and without code."""
196- logger .info (f "Unlock the device with fastboot." )
196+ logger .info ("Unlock the device with fastboot." )
197197 for line in run_command ("adb" , ["flashing" , "unlock" ], bin_path ):
198198 yield line
199199 if (type (line ) == bool ) and not line :
200- logger .error (f "Unlocking failed." )
200+ logger .error ("Unlocking failed." )
201201 yield False
202- else :
202+ else :
203203 yield True
204204
205205
206206def fastboot_oem_unlock (bin_path : Path ) -> bool :
207207 """OEM unlock the device with fastboot and without code."""
208- logger .info (f "OEM unlocking the device with fastboot." )
208+ logger .info ("OEM unlocking the device with fastboot." )
209209 for line in run_command ("adb" , ["oem" , "unlock" ], bin_path ):
210210 yield line
211211 if (type (line ) == bool ) and not line :
212- logger .error (f "OEM unlocking failed." )
212+ logger .error ("OEM unlocking failed." )
213213 yield False
214- else :
214+ else :
215215 yield True
216216
217217
218218def fastboot_reboot (bin_path : Path ) -> bool :
219219 """Reboot with fastboot"""
220- logger .info (f "Rebooting device with fastboot." )
220+ logger .info ("Rebooting device with fastboot." )
221221 for line in run_command ("fastboot" , ["reboot" ], bin_path ):
222222 yield line
223223 if (type (line ) == bool ) and not line :
224- logger .error (f "Rebooting with fastboot failed." )
224+ logger .error ("Rebooting with fastboot failed." )
225225 yield False
226- else :
226+ else :
227227 yield True
228228
229229
230230def fastboot_flash_recovery (bin_path : Path , recovery : str ) -> bool :
231231 """Temporarily, flash custom recovery with fastboot."""
232- logger .info (f "Flash custom recovery with fastboot." )
232+ logger .info ("Flash custom recovery with fastboot." )
233233 for line in run_command ("fastboot" , ["boot" , f"{ recovery } " ], bin_path ):
234234 yield line
235235 if (type (line ) == bool ) and not line :
236- logger .error (f "Flashing recovery failed." )
236+ logger .error ("Flashing recovery failed." )
237237 yield False
238- else :
238+ else :
239239 yield True
240240
241241
242242def heimdall_flash_recovery (bin_path : Path , recovery : str ) -> bool :
243243 """Temporarily, flash custom recovery with heimdall."""
244- logger .info (f"Flash custom recovery with heimdall." )
245- for line in run_command ("heimdall" , ["flash" , "--no-reboot" , "--RECOVERY" , f"{ recovery } " ], bin_path ):
244+ logger .info ("Flash custom recovery with heimdall." )
245+ for line in run_command (
246+ "heimdall" , ["flash" , "--no-reboot" , "--RECOVERY" , f"{ recovery } " ], bin_path
247+ ):
246248 yield line
247249 if (type (line ) == bool ) and not line :
248- logger .error (f "Flashing recovery with heimdall failed." )
250+ logger .error ("Flashing recovery with heimdall failed." )
249251 yield False
250- else :
252+ else :
251253 yield True
252254
253255
@@ -287,5 +289,5 @@ def search_device(platform: str, bin_path: Path) -> Optional[str]:
287289 logger .info (device_code )
288290 return device_code
289291 except CalledProcessError :
290- logger .error (f "Failed to detect a device." )
292+ logger .error ("Failed to detect a device." )
291293 return None
0 commit comments