Minos Galanakis | 2c824b4 | 2025-03-20 09:28:45 +0000 | [diff] [blame^] | 1 | #! /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. |
| 11 | ret=0 |
| 12 | |
| 13 | if type python3 >/dev/null 2>/dev/null; then |
| 14 | PYTHON=python3 |
| 15 | else |
| 16 | PYTHON=python |
| 17 | fi |
| 18 | |
| 19 | check_version () { |
| 20 | $PYTHON - "$2" <<EOF |
| 21 | import packaging.version |
| 22 | import sys |
| 23 | import $1 as package |
| 24 | actual = package.__version__ |
| 25 | wanted = sys.argv[1] |
| 26 | if 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) |
| 29 | EOF |
| 30 | } |
| 31 | |
| 32 | can_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 | |
| 39 | can_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. |
| 49 | if [ "$1" = "--can-pylint" ]; then |
| 50 | can_pylint |
| 51 | exit |
| 52 | elif [ "$1" = "--can-mypy" ]; then |
| 53 | can_mypy |
| 54 | exit |
| 55 | fi |
| 56 | |
| 57 | echo '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 | |
| 63 | echo |
| 64 | echo 'Running mypy ...' |
| 65 | $PYTHON -m mypy framework/scripts/*.py framework/scripts/mbedtls_framework/*.py scripts/*.py tests/scripts/*.py || |
| 66 | ret=1 |
| 67 | |
| 68 | exit $ret |