Marti Bolivar | 8c44799 | 2019-01-30 13:53:40 -0700 | [diff] [blame] | 1 | # Copyright (c) 2018 Foundries.io |
| 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | |
| 5 | '''Helpers shared by multiple west extension command modules. |
| 6 | |
| 7 | Note that common helpers used by the flash and debug extension |
| 8 | commands are in run_common -- that's for common code used by |
| 9 | commands which specifically execute runners.''' |
| 10 | |
Martí Bolívar | 6dab163 | 2020-02-10 16:53:06 -0800 | [diff] [blame] | 11 | import os |
| 12 | from pathlib import Path |
| 13 | |
Marti Bolivar | 8c44799 | 2019-01-30 13:53:40 -0700 | [diff] [blame] | 14 | from west import log |
Marti Bolivar | 8c44799 | 2019-01-30 13:53:40 -0700 | [diff] [blame] | 15 | from west.commands import WestCommand |
| 16 | |
Torsten Rasmussen | e819fa4 | 2020-03-10 14:52:35 +0100 | [diff] [blame] | 17 | # This relies on this file being zephyr/scripts/foo/bar.py. |
| 18 | # If you move this file, you'll break it, so be careful. |
| 19 | THIS_ZEPHYR = Path(__file__).parent.parent.parent |
| 20 | ZEPHYR_BASE = Path(os.environ.get('ZEPHYR_BASE', THIS_ZEPHYR)) |
| 21 | |
| 22 | # FIXME we need a nicer way to handle imports from scripts and cmake than this. |
| 23 | ZEPHYR_SCRIPTS = ZEPHYR_BASE / 'scripts' |
| 24 | ZEPHYR_CMAKE = ZEPHYR_BASE / 'cmake' |
Marti Bolivar | 8c44799 | 2019-01-30 13:53:40 -0700 | [diff] [blame] | 25 | |
| 26 | class Forceable(WestCommand): |
| 27 | '''WestCommand subclass for commands with a --force option.''' |
| 28 | |
Ulf Magnusson | bb63416 | 2019-09-04 16:28:50 +0200 | [diff] [blame] | 29 | @staticmethod |
| 30 | def add_force_arg(parser): |
Marti Bolivar | 8c44799 | 2019-01-30 13:53:40 -0700 | [diff] [blame] | 31 | '''Add a -f / --force option to the parser.''' |
| 32 | parser.add_argument('-f', '--force', action='store_true', |
Martí Bolívar | 6e4c2b9 | 2020-06-25 12:58:58 -0700 | [diff] [blame] | 33 | help='ignore any errors and try to proceed') |
Marti Bolivar | 8c44799 | 2019-01-30 13:53:40 -0700 | [diff] [blame] | 34 | |
| 35 | def check_force(self, cond, msg): |
| 36 | '''Abort if the command needs to be forced and hasn't been. |
| 37 | |
| 38 | The "forced" predicate must be in self.args.forced. |
| 39 | |
| 40 | If cond and self.args.force are both False, scream and die |
| 41 | with message msg. Otherwise, return. That is, "cond" is a |
| 42 | condition which means everything is OK; if it's False, only |
| 43 | self.args.force being True can allow execution to proceed. |
| 44 | ''' |
| 45 | if not (cond or self.args.force): |
| 46 | log.err(msg) |
| 47 | log.die('refusing to proceed without --force due to above error') |