Skip to content

Commit 3bd95f8

Browse files
kartbennordicjm
authored andcommitted
[nrf fromtree] scripts: ci: add vermin (min python version check)
Add a compliance check that allows to flag when a given file requires a Python version higher than 3.10 (minimum supported version in Zephyr at the time of writing) since not all Python scripts are tested against 3.10 in CI and we want to avoid introducing changes that could break users. Signed-off-by: Benjamin Cabé <[email protected]> (cherry picked from commit 17ab862)
1 parent cff056b commit 3bd95f8

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ MaintainersFormat.txt
102102
ModulesMaintainers.txt
103103
Nits.txt
104104
Pylint.txt
105+
PythonCompat.txt
105106
Ruff.txt
106107
SphinxLint.txt
107108
SysbuildKconfig.txt

scripts/ci/check_compliance.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,6 +2001,66 @@ def run(self):
20012001
desc = f"Run 'ruff format {file}'"
20022002
self.fmtd_failure("error", "Python format error", file, desc=desc)
20032003

2004+
class PythonCompatCheck(ComplianceTest):
2005+
"""
2006+
Python Compatibility Check
2007+
"""
2008+
name = "PythonCompat"
2009+
doc = "Check that Python files are compatible with Zephyr minimum supported Python version."
2010+
2011+
MAX_VERSION = (3, 10)
2012+
MAX_VERSION_STR = f"{MAX_VERSION[0]}.{MAX_VERSION[1]}"
2013+
2014+
def run(self):
2015+
py_files = [f for f in get_files(filter="d") if f.endswith(".py")]
2016+
if not py_files:
2017+
return
2018+
cmd = ["vermin", "-f", "parsable", "--violations",
2019+
f"-t={self.MAX_VERSION_STR}", "--no-make-paths-absolute"] + py_files
2020+
try:
2021+
result = subprocess.run(cmd,
2022+
check=False,
2023+
capture_output=True,
2024+
cwd=GIT_TOP)
2025+
except Exception as ex:
2026+
self.error(f"Failed to run vermin: {ex}")
2027+
output = result.stdout.decode("utf-8")
2028+
failed = False
2029+
for line in output.splitlines():
2030+
parts = line.split(":")
2031+
if len(parts) < 6:
2032+
continue
2033+
filename, line_number, column, _, py3ver, feature = parts[:6]
2034+
if not line_number:
2035+
# Ignore all file-level messages
2036+
continue
2037+
2038+
desc = None
2039+
if py3ver.startswith('!'):
2040+
desc = f"{feature} is known to be incompatible with Python 3."
2041+
elif py3ver.startswith('~'):
2042+
# "no known reason it won't work", just skip
2043+
continue
2044+
else:
2045+
major, minor = map(int, py3ver.split(".")[:2])
2046+
if (major, minor) > self.MAX_VERSION:
2047+
desc = f"{feature} requires Python {major}.{minor}, which is higher than " \
2048+
f"Zephyr's minimum supported Python version ({self.MAX_VERSION_STR})."
2049+
2050+
if desc is not None:
2051+
self.fmtd_failure(
2052+
"error",
2053+
"PythonCompat",
2054+
filename,
2055+
line=int(line_number),
2056+
col=int(column) if column else None,
2057+
desc=desc,
2058+
)
2059+
failed = True
2060+
if failed:
2061+
self.failure("Some Python files use features that are not compatible with Python " \
2062+
f"{self.MAX_VERSION_STR}.")
2063+
20042064

20052065
class TextEncoding(ComplianceTest):
20062066
"""

scripts/requirements-actions.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ tabulate
3535
tomli>=1.1.0
3636
tox
3737
unidiff
38+
vermin
3839
west>=0.14.0
3940
xlsxwriter
4041
yamllint

scripts/requirements-actions.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,10 @@ urllib3==2.4.0 \
12351235
# elastic-transport
12361236
# pygithub
12371237
# requests
1238+
vermin==1.6.0 \
1239+
--hash=sha256:6266ca02f55d1c2aa189a610017c132eb2d1934f09e72a955b1eb3820ee6d4ef \
1240+
--hash=sha256:f1fa9ee40f59983dc40e0477eb2b1fa8061a3df4c3b2bcf349add462a5610efb
1241+
# via -r requirements-actions.in
12381242
virtualenv==20.31.2 \
12391243
--hash=sha256:36efd0d9650ee985f0cad72065001e66d49a6f24eb44d98980f630686243cf11 \
12401244
--hash=sha256:e10c0a9d02835e592521be48b332b6caee6887f332c111aa79a09b9e79efc2af

scripts/requirements-compliance.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ python-magic; sys_platform != "win32"
1313
ruff==0.11.11
1414
sphinx-lint
1515
unidiff
16+
vermin
1617
yamllint
1718
# zephyr-keep-sorted-stop

0 commit comments

Comments
 (0)