1# Copyright (c) 2023 Google 2# 3# SPDX-License-Identifier: Apache-2.0 4 5import argparse 6import os 7import sys 8 9from west.commands import Verbosity, WestCommand 10 11from zephyr_ext_common import ZEPHYR_SCRIPTS 12 13# Resolve path to twister libs and add imports 14twister_path = ZEPHYR_SCRIPTS 15os.environ["ZEPHYR_BASE"] = str(twister_path.parent) 16 17sys.path.insert(0, str(twister_path)) 18sys.path.insert(0, str(twister_path / "pylib" / "twister")) 19 20from twisterlib.environment import add_parse_arguments, parse_arguments, python_version_guard 21from twisterlib.twister_main import main 22 23TWISTER_DESCRIPTION = """\ 24Convenience wrapper for twister. The below options are shared with the twister 25script and have the same effects as if you ran twister directly. Refer to the 26twister documentation for more information. 27""" 28 29 30class Twister(WestCommand): 31 def __init__(self): 32 super(Twister, self).__init__( 33 "twister", 34 # Keep this in sync with the string in west-commands.yml. 35 "west twister wrapper", 36 TWISTER_DESCRIPTION, 37 accepts_unknown_args=True, 38 ) 39 python_version_guard() 40 41 def do_add_parser(self, parser_adder): 42 parser = parser_adder.add_parser( 43 self.name, 44 help=self.help, 45 formatter_class=argparse.RawDescriptionHelpFormatter, 46 description=self.description, 47 allow_abbrev=False 48 ) 49 50 parser = add_parse_arguments(parser) 51 52 return parser 53 54 def do_run(self, args, remainder): 55 self.dbg( 56 "args: {} remainder: {}".format(args, remainder), level=Verbosity.DBG_EXTREME 57 ) 58 59 options = parse_arguments(self.parser, args=remainder, options=args) 60 default_options = parse_arguments(self.parser, args=[], on_init=False) 61 ret = main(options, default_options) 62 sys.exit(ret) 63 64 def _parse_arguments(self, args, options): 65 """Helper function for testing purposes""" 66 return parse_arguments(self.parser, args, options) 67