Skip to content

Commit 698db69

Browse files
mbolivar-nordicnashif
authored andcommitted
west sign: prefer 'python imgtool.py' on windows
We can't trust that a python file is executable on Windows, regardless of what the mode bits say. When we find that imgtool is a .py file, run 'python imgtool.py' instead of 'imgtool.py' on that platform. Fixes: #31876 Signed-off-by: Martí Bolívar <[email protected]>
1 parent b4903d4 commit 698db69

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

scripts/west_commands/sign.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import pathlib
99
import pickle
10+
import platform
1011
import shutil
1112
import subprocess
1213
import sys
@@ -288,15 +289,24 @@ def sign(self, command, build_dir, bcfg, formats):
288289
@staticmethod
289290
def find_imgtool(command, args):
290291
if args.tool_path:
291-
command.check_force(shutil.which(args.tool_path),
292-
'--tool-path {}: not an executable'.
293-
format(args.tool_path))
294-
return [args.tool_path]
292+
imgtool = args.tool_path
293+
if not os.path.isfile(imgtool):
294+
log.die(f'--tool-path {imgtool}: no such file')
295+
else:
296+
imgtool = shutil.which('imgtool') or shutil.which('imgtool.py')
297+
if not imgtool:
298+
log.die('imgtool not found; either install it',
299+
'(e.g. "pip3 install imgtool") or provide --tool-path')
300+
301+
if platform.system() == 'Windows' and imgtool.endswith('.py'):
302+
# Windows users may not be able to run .py files
303+
# as executables in subprocesses, regardless of
304+
# what the mode says. Always run imgtool as
305+
# 'python path/to/imgtool.py' instead of
306+
# 'path/to/imgtool.py' in these cases.
307+
# https://github.com/zephyrproject-rtos/zephyr/issues/31876
308+
return [sys.executable, imgtool]
295309

296-
imgtool = shutil.which('imgtool') or shutil.which('imgtool.py')
297-
if not imgtool:
298-
log.die('imgtool not found; either install it',
299-
'(e.g. "pip3 install imgtool") or provide --tool-path')
300310
return [imgtool]
301311

302312
@staticmethod

0 commit comments

Comments
 (0)