Skip to content

Commit

Permalink
scripts: twister: Python Version Guard
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
LukaszMrugala authored and nashif committed Nov 4, 2024
1 parent 5c9b0a5 commit 4bb9453
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
11 changes: 11 additions & 0 deletions scripts/pylib/twister/twisterlib/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion scripts/twister
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion scripts/west_commands/twister_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
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 = """\
Expand All @@ -37,6 +37,7 @@ def __init__(self):
TWISTER_DESCRIPTION,
accepts_unknown_args=True,
)
python_version_guard()

def do_add_parser(self, parser_adder):
parser = parser_adder.add_parser(
Expand Down

0 comments on commit 4bb9453

Please sign in to comment.