1818from loguru import logger
1919from subprocess import STDOUT , CalledProcessError , call , check_output
2020from time import sleep
21- from typing import Callable
21+ from typing import Callable , Optional
2222from pathlib import Path
2323
2424import flet
5050)
5151from installer_config import InstallerConfig , Step
5252from widgets import call_button , confirm_button , get_title
53+ from tool_utils import search_device , call_tool_with_command
5354
5455# Toggle to True for development purposes
5556DEVELOPMENT = False
@@ -178,66 +179,34 @@ def close_developer_options_dlg(self, e):
178179
179180 def search_devices (self , e ):
180181 """Search the device when the button is clicked."""
181- logger .info ("Search devices..." )
182- try :
183- # read device properties
184- # TODO: This is not windows ready...
185- if PLATFORM in ("linux" , "darwin" ):
186- output = check_output (
187- [
188- str (BIN_PATH .joinpath (Path ("adb" ))),
189- "shell" ,
190- "getprop" ,
191- "|" ,
192- "grep" ,
193- "ro.product.device"
194- ],
195- stderr = STDOUT ,
196- ).decode ()
197- elif PLATFORM == "windows" :
198- output = check_output (
199- [
200- str (BIN_PATH .joinpath (Path ("adb" ))),
201- "shell" ,
202- "getprop" ,
203- "|" ,
204- "findstr" ,
205- "ro.product.device"
206- ],
207- stderr = STDOUT ,
208- ).decode ()
182+ # search the device
183+ if DEVELOPMENT :
184+ # this only happens for testing
185+ device_code = DEVELOPMENT_CONFIG
186+ logger .info (f"Running search in development mode and loading config { device_code } .yaml." )
187+ else :
188+ device_code = search_device (platform = PLATFORM , bin_path = BIN_PATH )
189+ if device_code :
190+ self .device_name .value = device_code
209191 else :
210- raise Exception ( f"Unknown platform { PLATFORM } ." )
192+ self . device_name . value = "No device detected! Connect to USB and try again."
211193
212- output = output .split ("[" )[- 1 ][:- 2 ]
213- logger .info (f"Detected { output } " )
214- # write the device code to the text shown in the box
215- self .device_name .value = output .strip ()
194+ # load the config, if a device is detected
195+ if device_code :
196+ self .device_name .value = device_code
216197 # load config from file
217- path = CONFIG_PATH .joinpath (Path (f"{ output .strip ()} .yaml" ))
218- load_config_success = self .load_config (path )
198+ path = CONFIG_PATH .joinpath (Path (f"{ device_code } .yaml" ))
199+ device_name = self .load_config (path )
200+
219201 # display success in the application
220- if load_config_success :
202+ if device_name :
221203 self .config_found_box .value = True
222204 self .continue_button .disabled = False
223205 # overwrite the text field with the real name from the config
224- self .device_name .value = f"{ load_config_success } (code: { output . strip () } )"
206+ self .device_name .value = f"{ device_name } (code: { device_code } )"
225207 else :
226- # show alternative configs here
227- # select a new path and load again
228- pass
229- except CalledProcessError :
230- logger .info (f"Did not detect a device." )
231- if DEVELOPMENT :
232- path = CONFIG_PATH .joinpath (Path (f"{ DEVELOPMENT_CONFIG } .yaml" ))
233- load_config_success = self .load_config (path )
234- if load_config_success :
235- self .config_found_box .value = True
236- self .continue_button .disabled = False
237- else :
238- self .device_name .value = (
239- "No device detected! Connect to USB and try again."
240- )
208+ # failed to load config
209+ logger .info (f"Failed to load config from { path } ." )
241210 self .view .update ()
242211
243212
@@ -422,7 +391,7 @@ def confirm(self, e):
422391 self .view .controls .append (self .final_view )
423392 self .view .update ()
424393
425- def load_config (self , path : str ):
394+ def load_config (self , path : str ) -> Optional [ str ] :
426395 """Function to load a config file from path."""
427396 try :
428397 self .config = InstallerConfig .from_file (path )
@@ -432,7 +401,7 @@ def load_config(self, path: str):
432401 return self .config .metadata .get ("devicename" , "No device name in config." )
433402 except FileNotFoundError :
434403 logger .info (f"No device config found for { path } ." )
435- return False
404+ return None
436405
437406 def pick_image_result (self , e : FilePickerResultEvent ):
438407 self .selected_image .value = (
0 commit comments