blob: a51766f71433bb722f50e9d5e3bddf585c0bcbfc [file] [log] [blame]
Minos Galanakis2c824b42025-03-20 09:28:45 +00001#! /usr/bin/env sh
2
3# Copyright The Mbed TLS Contributors
4# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5
6# Purpose: check Python files for potential programming errors or maintenance
7# hurdles. Run pylint to detect some potential mistakes and enforce PEP8
8# coding standards. Run mypy to perform static type checking.
9
10# We'll keep going on errors and report the status at the end.
11ret=0
12
13if type python3 >/dev/null 2>/dev/null; then
14 PYTHON=python3
15else
16 PYTHON=python
17fi
18
19check_version () {
20 $PYTHON - "$2" <<EOF
21import packaging.version
22import sys
23import $1 as package
24actual = package.__version__
25wanted = sys.argv[1]
26if packaging.version.parse(actual) < packaging.version.parse(wanted):
27 sys.stderr.write("$1: version %s is too old (want %s)\n" % (actual, wanted))
28 exit(1)
29EOF
30}
31
32can_pylint () {
33 # Pylint 1.5.2 from Ubuntu 16.04 is too old:
34 # E: 34, 0: Unable to import 'mbedtls_framework' (import-error)
35 # Pylint 1.8.3 from Ubuntu 18.04 passed on the first commit containing this line.
36 check_version pylint 1.8.3
37}
38
39can_mypy () {
40 # mypy 0.770 is too old:
41 # framework/scripts/test_psa_constant_names.py:34: error: Cannot find implementation or library stub for module named 'mbedtls_framework'
42 # mypy 0.780 from pip passed on the first commit containing this line.
43 check_version mypy.version 0.780
44}
45
46# With just a --can-xxx option, check whether the tool for xxx is available
47# with an acceptable version, and exit without running any checks. The exit
48# status is true if the tool is available and acceptable and false otherwise.
49if [ "$1" = "--can-pylint" ]; then
50 can_pylint
51 exit
52elif [ "$1" = "--can-mypy" ]; then
53 can_mypy
54 exit
55fi
56
57echo 'Running pylint ...'
58$PYTHON -m pylint framework/scripts/*.py framework/scripts/mbedtls_framework/*.py scripts/*.py tests/scripts/*.py || {
59 echo >&2 "pylint reported errors"
60 ret=1
61}
62
63echo
64echo 'Running mypy ...'
65$PYTHON -m mypy framework/scripts/*.py framework/scripts/mbedtls_framework/*.py scripts/*.py tests/scripts/*.py ||
66 ret=1
67
68exit $ret