From 4bb945300bf92e4708404102d2a3b54e540bfd6c Mon Sep 17 00:00:00 2001 From: Lukasz Mrugala Date: Mon, 21 Oct 2024 14:01:44 +0000 Subject: [PATCH] scripts: twister: Python Version Guard Twister shall now verify that the user does not use an obsolete Python version. If user's Python is deemed too old, it will raise a relevant error. This check will also happen when running Twister via west. Signed-off-by: Lukasz Mrugala --- scripts/pylib/twister/twisterlib/environment.py | 11 +++++++++++ scripts/twister | 5 ++++- scripts/west_commands/twister_cmd.py | 3 ++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/scripts/pylib/twister/twisterlib/environment.py b/scripts/pylib/twister/twisterlib/environment.py index c81b03355ce..2b330caf2ab 100644 --- a/scripts/pylib/twister/twisterlib/environment.py +++ b/scripts/pylib/twister/twisterlib/environment.py @@ -51,6 +51,17 @@ def _get_installed_packages() -> Generator[str, None, None]: yield dist.metadata['Name'] +def python_version_guard(): + min_ver = (3, 10) + if sys.version_info < min_ver: + min_ver_str = '.'.join([str(v) for v in min_ver]) + cur_ver_line = f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}" + print(f"Unsupported Python version {cur_ver_line}.") + print(f"Currently, Twister requires at least Python {min_ver_str}.") + print("Install a newer Python version and retry.") + sys.exit(1) + + installed_packages: List[str] = list(_get_installed_packages()) PYTEST_PLUGIN_INSTALLED = 'pytest-twister-harness' in installed_packages diff --git a/scripts/twister b/scripts/twister index c6382f9a9e3..db692ceb1cd 100755 --- a/scripts/twister +++ b/scripts/twister @@ -203,12 +203,15 @@ if not ZEPHYR_BASE: sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/twister/")) sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/build_helpers")) -from twisterlib.environment import add_parse_arguments, parse_arguments +from twisterlib.environment import add_parse_arguments, parse_arguments, python_version_guard from twisterlib.twister_main import main + if __name__ == "__main__": ret = 0 try: + python_version_guard() + parser = add_parse_arguments() options = parse_arguments(parser, sys.argv[1:]) default_options = parse_arguments(parser, [], on_init=False) diff --git a/scripts/west_commands/twister_cmd.py b/scripts/west_commands/twister_cmd.py index 266b60d2bf2..d9616c4e614 100644 --- a/scripts/west_commands/twister_cmd.py +++ b/scripts/west_commands/twister_cmd.py @@ -18,7 +18,7 @@ os.environ["ZEPHYR_BASE"] = str(twister_path.parent) sys.path.insert(0, str(twister_path)) sys.path.insert(0, str(twister_path / "pylib" / "twister")) -from twisterlib.environment import add_parse_arguments, parse_arguments +from twisterlib.environment import add_parse_arguments, parse_arguments, python_version_guard from twisterlib.twister_main import main TWISTER_DESCRIPTION = """\ @@ -37,6 +37,7 @@ class Twister(WestCommand): TWISTER_DESCRIPTION, accepts_unknown_args=True, ) + python_version_guard() def do_add_parser(self, parser_adder): parser = parser_adder.add_parser(