From 3b595ebd3edefc8ce2cce751f1ec6af5fd880ce2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20R=C3=B8nningstad?= Date: Wed, 12 Aug 2020 14:50:39 +0200 Subject: [PATCH] nrfjprog.py: Small refactor of get_board_snr() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move some functionality out into separate functions for clarity. Signed-off-by: Øyvind Rønningstad --- scripts/west_commands/runners/nrfjprog.py | 27 +++++++++++++++-------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/scripts/west_commands/runners/nrfjprog.py b/scripts/west_commands/runners/nrfjprog.py index 44b44584299..70e5991dc44 100644 --- a/scripts/west_commands/runners/nrfjprog.py +++ b/scripts/west_commands/runners/nrfjprog.py @@ -60,6 +60,20 @@ class NrfJprogBinaryRunner(ZephyrBinaryRunner): if not self.snr: self.snr = self.get_board_snr() + def get_boards(self): + snrs = self.check_output(['nrfjprog', '--ids']) + snrs = snrs.decode(sys.getdefaultencoding()).strip().splitlines() + if not snrs: + raise RuntimeError('"nrfjprog --ids" did not find a board; ' + 'is the board connected?') + return snrs + + @staticmethod + def verify_snr(snr): + if snr == '0': + raise RuntimeError('"nrfjprog --ids" returned 0; ' + 'is a debugger already connected?') + def get_board_snr(self): # Use nrfjprog --ids to discover connected boards. # @@ -68,16 +82,11 @@ class NrfJprogBinaryRunner(ZephyrBinaryRunner): # multiple boards and we are connected to a terminal, in which # case use print() and input() to ask what the user wants. - snrs = self.check_output(['nrfjprog', '--ids']) - snrs = snrs.decode(sys.getdefaultencoding()).strip().splitlines() - if not snrs: - raise RuntimeError('"nrfjprog --ids" did not find a board; ' - 'is the board connected?') - elif len(snrs) == 1: + snrs = self.get_boards() + + if len(snrs) == 1: board_snr = snrs[0] - if board_snr == '0': - raise RuntimeError('"nrfjprog --ids" returned 0; ' - 'is a debugger already connected?') + self.verify_snr(board_snr) return board_snr elif not sys.stdin.isatty(): raise RuntimeError(