Mohammad Azim Khan | 2179810 | 2018-07-06 00:41:08 +0100 | [diff] [blame] | 1 | #! /usr/bin/env sh |
| 2 | |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 3 | # Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | 7be4551 | 2020-08-12 02:31:02 +0200 | [diff] [blame] | 5 | |
| 6 | # Purpose: check Python files for potential programming errors or maintenance |
| 7 | # hurdles. Run pylint to detect some potential mistakes and enforce PEP8 |
Gilles Peskine | 254efe5 | 2022-02-28 16:06:36 +0100 | [diff] [blame] | 8 | # coding standards. Run mypy to perform static type checking. |
Gilles Peskine | 7be4551 | 2020-08-12 02:31:02 +0200 | [diff] [blame] | 9 | |
| 10 | # We'll keep going on errors and report the status at the end. |
| 11 | ret=0 |
Mohammad Azim Khan | 2179810 | 2018-07-06 00:41:08 +0100 | [diff] [blame] | 12 | |
Gilles Peskine | 56e99d6 | 2020-03-24 15:07:57 +0100 | [diff] [blame] | 13 | if type python3 >/dev/null 2>/dev/null; then |
| 14 | PYTHON=python3 |
Simon Butcher | e30d03e | 2020-03-16 11:30:46 +0000 | [diff] [blame] | 15 | else |
Gilles Peskine | 56e99d6 | 2020-03-24 15:07:57 +0100 | [diff] [blame] | 16 | PYTHON=python |
Simon Butcher | e30d03e | 2020-03-16 11:30:46 +0000 | [diff] [blame] | 17 | fi |
| 18 | |
Gilles Peskine | bdde5d0 | 2021-01-19 21:42:05 +0100 | [diff] [blame] | 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 | |
Gilles Peskine | 1cc6a8e | 2021-01-06 17:02:33 +0100 | [diff] [blame] | 32 | can_pylint () { |
Gilles Peskine | 2991b5f | 2021-01-19 21:19:02 +0100 | [diff] [blame] | 33 | # Pylint 1.5.2 from Ubuntu 16.04 is too old: |
David Horstmann | 9638ca3 | 2024-05-03 14:36:12 +0100 | [diff] [blame] | 34 | # E: 34, 0: Unable to import 'mbedtls_framework' (import-error) |
Gilles Peskine | 1cc6a8e | 2021-01-06 17:02:33 +0100 | [diff] [blame] | 35 | # Pylint 1.8.3 from Ubuntu 18.04 passed on the first commit containing this line. |
Gilles Peskine | bdde5d0 | 2021-01-19 21:42:05 +0100 | [diff] [blame] | 36 | check_version pylint 1.8.3 |
Gilles Peskine | 1cc6a8e | 2021-01-06 17:02:33 +0100 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | can_mypy () { |
Gilles Peskine | 0370c17 | 2021-01-19 21:58:09 +0100 | [diff] [blame] | 40 | # mypy 0.770 is too old: |
David Horstmann | 9638ca3 | 2024-05-03 14:36:12 +0100 | [diff] [blame] | 41 | # tests/scripts/test_psa_constant_names.py:34: error: Cannot find implementation or library stub for module named 'mbedtls_framework' |
Gilles Peskine | 0370c17 | 2021-01-19 21:58:09 +0100 | [diff] [blame] | 42 | # mypy 0.780 from pip passed on the first commit containing this line. |
| 43 | check_version mypy.version 0.780 |
Gilles Peskine | 1cc6a8e | 2021-01-06 17:02:33 +0100 | [diff] [blame] | 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 | |
Gilles Peskine | 6d82a7e | 2021-01-19 21:19:25 +0100 | [diff] [blame] | 57 | echo 'Running pylint ...' |
David Horstmann | dfba499 | 2024-06-04 16:19:14 +0100 | [diff] [blame] | 58 | $PYTHON -m pylint framework/scripts/*.py framework/scripts/mbedtls_framework/*.py scripts/*.py tests/scripts/*.py || { |
Gilles Peskine | 7be4551 | 2020-08-12 02:31:02 +0200 | [diff] [blame] | 59 | echo >&2 "pylint reported errors" |
| 60 | ret=1 |
| 61 | } |
| 62 | |
Gilles Peskine | 254efe5 | 2022-02-28 16:06:36 +0100 | [diff] [blame] | 63 | echo |
| 64 | echo 'Running mypy ...' |
David Horstmann | dfba499 | 2024-06-04 16:19:14 +0100 | [diff] [blame] | 65 | $PYTHON -m mypy framework/scripts/*.py framework/scripts/mbedtls_framework/*.py scripts/*.py tests/scripts/*.py || |
Gilles Peskine | 254efe5 | 2022-02-28 16:06:36 +0100 | [diff] [blame] | 66 | ret=1 |
Gilles Peskine | 7be4551 | 2020-08-12 02:31:02 +0200 | [diff] [blame] | 67 | |
| 68 | exit $ret |