2019-01-31 04:53:40 +08:00
|
|
|
# Copyright (c) 2018 Foundries.io
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
'''Helpers shared by multiple west extension command modules.
|
|
|
|
|
|
|
|
Note that common helpers used by the flash and debug extension
|
|
|
|
commands are in run_common -- that's for common code used by
|
|
|
|
commands which specifically execute runners.'''
|
|
|
|
|
2020-02-11 08:53:06 +08:00
|
|
|
import os
|
2024-10-11 04:57:05 +08:00
|
|
|
import shlex
|
2020-02-11 08:53:06 +08:00
|
|
|
from pathlib import Path
|
|
|
|
|
2019-01-31 04:53:40 +08:00
|
|
|
from west import log
|
|
|
|
from west.commands import WestCommand
|
|
|
|
|
2020-03-10 21:52:35 +08:00
|
|
|
# This relies on this file being zephyr/scripts/foo/bar.py.
|
|
|
|
# If you move this file, you'll break it, so be careful.
|
|
|
|
THIS_ZEPHYR = Path(__file__).parent.parent.parent
|
|
|
|
ZEPHYR_BASE = Path(os.environ.get('ZEPHYR_BASE', THIS_ZEPHYR))
|
|
|
|
|
|
|
|
# FIXME we need a nicer way to handle imports from scripts and cmake than this.
|
|
|
|
ZEPHYR_SCRIPTS = ZEPHYR_BASE / 'scripts'
|
|
|
|
ZEPHYR_CMAKE = ZEPHYR_BASE / 'cmake'
|
2019-01-31 04:53:40 +08:00
|
|
|
|
|
|
|
class Forceable(WestCommand):
|
|
|
|
'''WestCommand subclass for commands with a --force option.'''
|
|
|
|
|
2019-09-04 22:28:50 +08:00
|
|
|
@staticmethod
|
|
|
|
def add_force_arg(parser):
|
2019-01-31 04:53:40 +08:00
|
|
|
'''Add a -f / --force option to the parser.'''
|
|
|
|
parser.add_argument('-f', '--force', action='store_true',
|
2020-06-26 03:58:58 +08:00
|
|
|
help='ignore any errors and try to proceed')
|
2019-01-31 04:53:40 +08:00
|
|
|
|
|
|
|
def check_force(self, cond, msg):
|
|
|
|
'''Abort if the command needs to be forced and hasn't been.
|
|
|
|
|
|
|
|
The "forced" predicate must be in self.args.forced.
|
|
|
|
|
|
|
|
If cond and self.args.force are both False, scream and die
|
|
|
|
with message msg. Otherwise, return. That is, "cond" is a
|
|
|
|
condition which means everything is OK; if it's False, only
|
|
|
|
self.args.force being True can allow execution to proceed.
|
|
|
|
'''
|
|
|
|
if not (cond or self.args.force):
|
|
|
|
log.err(msg)
|
|
|
|
log.die('refusing to proceed without --force due to above error')
|
2024-10-11 04:57:05 +08:00
|
|
|
|
|
|
|
def config_get_words(self, section_key, fallback=None):
|
|
|
|
unparsed = self.config.get(section_key)
|
|
|
|
self.dbg(f'west config {section_key}={unparsed}')
|
|
|
|
return fallback if unparsed is None else shlex.split(unparsed)
|
|
|
|
|
|
|
|
def config_get(self, section_key, fallback=None):
|
|
|
|
words = self.config_get_words(section_key)
|
|
|
|
if words is None:
|
|
|
|
return fallback
|
|
|
|
if len(words) != 1:
|
|
|
|
self.die(f'Single word expected for: {section_key}={words}. Use quotes?')
|
|
|
|
return words[0]
|