import logging import subprocess from pathlib import Path from typing import Sequence, Union logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger("Download") APPLICATION_NAME = "nucleo-wb55-dongle-usb" #APPLICATION_NAME = "nucleo-wb55-dongle-blinky" PORT="USB1" CLI_PATH = Path("/Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app/Contents/MacOs/bin/STM32_Programmer_CLI") ELF_FILE = Path.home() / Path(f"Documents/git/nucleo-playground-workspace/{APPLICATION_NAME}/Debug/{APPLICATION_NAME}.elf") def execute_command( command: Union[str, Sequence[str]], ): logger.debug(f"Command is: {command}") process = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) def _poll_stdout() -> str: return process.stdout.readline().decode() exit_code = None command_logger = logger.getChild("Programmer") while exit_code is None: for line in iter(_poll_stdout, ''): command_logger.info(line.rstrip()) exit_code = process.poll() for line in iter(_poll_stdout, ''): command_logger.info(line.rstrip()) # flush pipe logger.debug(f"Coomand finished with exit code {exit_code}") return exit_code def download(): command = [ str(CLI_PATH), "-c", f"port={PORT}", "-w", str(ELF_FILE), ] execute_command(command) if __name__ == "__main__": download()