From e284a58db95ddd7f33345067a6adcd89788b1859 Mon Sep 17 00:00:00 2001 From: DJ2LS Date: Thu, 22 Feb 2024 10:49:30 +0100 Subject: [PATCH] iterate through all rigctld binaries --- modem/helpers.py | 22 +++++++++++++--------- modem/rigctld.py | 21 ++++++++++++++++----- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/modem/helpers.py b/modem/helpers.py index 4a4c42d9..e8de2714 100644 --- a/modem/helpers.py +++ b/modem/helpers.py @@ -713,15 +713,16 @@ def get_flag(byte, flag_name, flag_dict): return get_bit(byte, position) -def find_binary_path(binary_name="rigctld", search_system_wide=False): +def find_binary_paths(binary_name="rigctld", search_system_wide=False): """ - Search for a binary within the current working directory and its subdirectories. - Optionally, check system-wide locations and PATH environment variable if not found. + Search for a binary within the current working directory, its subdirectories, and optionally, + system-wide locations and the PATH environment variable. :param binary_name: The base name of the binary to search for, without extension. :param search_system_wide: Boolean flag to enable or disable system-wide search. - :return: The full path to the binary if found, otherwise None. + :return: A list of full paths to the binary if found, otherwise an empty list. """ + binary_paths = [] # Initialize an empty list to store found paths # Adjust binary name for Windows if platform.system() == 'Windows': binary_name += ".exe" @@ -730,7 +731,7 @@ def find_binary_path(binary_name="rigctld", search_system_wide=False): root_path = os.getcwd() for dirpath, dirnames, filenames in os.walk(root_path): if binary_name in filenames: - return os.path.join(dirpath, binary_name) + binary_paths.append(os.path.join(dirpath, binary_name)) # If system-wide search is enabled, look in system locations and PATH if search_system_wide: @@ -739,13 +740,16 @@ def find_binary_path(binary_name="rigctld", search_system_wide=False): if platform.system() != 'Windows': system_paths.extend(['/usr/bin', '/usr/local/bin', '/bin']) else: - system_paths.extend(glob.glob("C:\\Program Files\\Hamlib*\\bin")) - system_paths.extend(glob.glob("C:\\Program Files (x86)\\Hamlib*\\bin")) - + system_paths.extend(glob.glob("C:\\Program Files\\Hamlib*\\bin")) + system_paths.extend(glob.glob("C:\\Program Files (x86)\\Hamlib*\\bin")) + for path in system_paths: potential_path = os.path.join(path, binary_name) if os.path.isfile(potential_path): - return potential_path + binary_paths.append(potential_path) + + return binary_paths + def kill_and_execute(binary_path, additional_args=None): diff --git a/modem/rigctld.py b/modem/rigctld.py index 5ec756bc..289b6f1f 100644 --- a/modem/rigctld.py +++ b/modem/rigctld.py @@ -260,15 +260,26 @@ class radio: def start_service(self): binary_name = "rigctld" - binary_path = helpers.find_binary_path(binary_name, search_system_wide=True) + binary_paths = helpers.find_binary_paths(binary_name, search_system_wide=True) additional_args = self.format_rigctld_args() - if binary_path: - self.log.info(f"Rigctld binary found at: {binary_path}") - helpers.kill_and_execute(binary_path, additional_args) - self.log.info(f"Executed rigctld...") + + if binary_paths: + for binary_path in binary_paths: + try: + self.log.info(f"Attempting to start rigctld using binary found at: {binary_path}") + helpers.kill_and_execute(binary_path, additional_args) + self.log.info("Successfully executed rigctld.") + break # Exit the loop after successful execution + except Exception as e: + pass + # let's keep this hidden for the user to avoid confusion + # self.log.warning(f"Failed to start rigctld with binary at {binary_path}: {e}") + else: + self.log.warning("Failed to start rigctld with all found binaries.", binaries=binary_paths) else: self.log.warning("Rigctld binary not found.") + def format_rigctld_args(self): config = self.config['RADIO'] # Accessing the 'RADIO' section of the INI file config_rigctld = self.config['RIGCTLD'] # Accessing the 'RIGCTLD' section of the INI file for custom args