From e32ed180762adfe9d5cea3613f6064a36c6f8802 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Fri, 6 Apr 2018 16:00:23 -0400 Subject: [PATCH] jlink: fix flashing behavior on Windows The object returned by NamedTemporaryFile cannot be opened a second time while the file is still open on Windows. This is exactly what the JLink commander needs to do with the resulting file, however, breaking jlink flash on that platform. Fix this by using a temporary directory instead, and creating a file inside it. The resulting directory still gets cleaned up, but the resulting file can be read by the commander. Signed-off-by: Marti Bolivar --- scripts/support/runner/jlink.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/scripts/support/runner/jlink.py b/scripts/support/runner/jlink.py index 8d8c6b1034e..aaf317e8ea9 100644 --- a/scripts/support/runner/jlink.py +++ b/scripts/support/runner/jlink.py @@ -124,15 +124,18 @@ class JLinkBinaryRunner(ZephyrBinaryRunner): lines.append('g') # Start the CPU lines.append('q') # Close the connection and quit - with tempfile.NamedTemporaryFile(suffix='.jlink') as f: - f.writelines(bytes(line + '\n', 'utf-8') for line in lines) - f.flush() + # Don't use NamedTemporaryFile: the resulting file can't be + # opened again on Windows. + with tempfile.TemporaryDirectory(suffix='jlink') as d: + fname = os.path.join(d, 'runner.jlink') + with open(fname, 'wb') as f: + f.writelines(bytes(line + '\n', 'utf-8') for line in lines) cmd = ([self.commander] + ['-if', self.iface, '-speed', self.speed, '-device', self.device, - '-CommanderScript', f.name]) + '-CommanderScript', fname]) print('Flashing Target Device') self.check_call(cmd)