From c19364360c614914059337b486bbc2c372bf4d59 Mon Sep 17 00:00:00 2001 From: dw-0 Date: Sat, 5 Oct 2024 08:07:47 +0200 Subject: [PATCH] fix: correctly find connected USB DFU devices (#555) Signed-off-by: Dominik Willner --- .../klipper_firmware/firmware_utils.py | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/kiauh/components/klipper_firmware/firmware_utils.py b/kiauh/components/klipper_firmware/firmware_utils.py index a611958..b5b165c 100644 --- a/kiauh/components/klipper_firmware/firmware_utils.py +++ b/kiauh/components/klipper_firmware/firmware_utils.py @@ -7,7 +7,15 @@ # This file may be distributed under the terms of the GNU GPLv3 license # # ======================================================================= # -from subprocess import PIPE, STDOUT, CalledProcessError, Popen, check_output, run +from subprocess import ( + DEVNULL, + PIPE, + STDOUT, + CalledProcessError, + Popen, + check_output, + run, +) from typing import List from components.klipper import KLIPPER_DIR @@ -32,8 +40,10 @@ def find_firmware_file() -> bool: f3 = "klipper.bin" f4 = "klipper.uf2" fw_file_exists: bool = ( - target.joinpath(f1).exists() and target.joinpath(f2).exists() - ) or target.joinpath(f3).exists() or target.joinpath(f4).exists() + (target.joinpath(f1).exists() and target.joinpath(f2).exists()) + or target.joinpath(f3).exists() + or target.joinpath(f4).exists() + ) return target_exists and fw_file_exists @@ -62,9 +72,13 @@ def find_uart_device() -> List[str]: def find_usb_dfu_device() -> List[str]: try: - command = '"lsusb | grep "DFU" | cut -d " " -f 6 2>/dev/null"' - output = check_output(command, shell=True, text=True) - return output.splitlines() + output = check_output("lsusb", shell=True, text=True, stderr=DEVNULL) + device_list = [] + if output: + devices = output.splitlines() + device_list = [d.split(" ")[5] for d in devices if "DFU" in d] + return device_list + except CalledProcessError as e: Logger.print_error("Unable to find a USB DFU device!") Logger.print_error(e, prefix=False)