blob: 32b5baf5cda9ebe5dafcedd143978d70e9978741 [file] [log] [blame]
Mohammad Azim Khan21798102018-07-06 00:41:08 +01001#! /usr/bin/env sh
2
Bence Szépkúti1e148272020-08-07 13:07:28 +02003# Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00004# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine7be45512020-08-12 02:31:02 +02005
6# Purpose: check Python files for potential programming errors or maintenance
7# hurdles. Run pylint to detect some potential mistakes and enforce PEP8
Gilles Peskine254efe52022-02-28 16:06:36 +01008# coding standards. Run mypy to perform static type checking.
Gilles Peskine7be45512020-08-12 02:31:02 +02009
10# We'll keep going on errors and report the status at the end.
11ret=0
Mohammad Azim Khan21798102018-07-06 00:41:08 +010012
Gilles Peskine56e99d62020-03-24 15:07:57 +010013if type python3 >/dev/null 2>/dev/null; then
14 PYTHON=python3
Simon Butchere30d03e2020-03-16 11:30:46 +000015else
Gilles Peskine56e99d62020-03-24 15:07:57 +010016 PYTHON=python
Simon Butchere30d03e2020-03-16 11:30:46 +000017fi
18
Gilles Peskinebdde5d02021-01-19 21:42:05 +010019check_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
Gilles Peskine1cc6a8e2021-01-06 17:02:33 +010032can_pylint () {
Gilles Peskine2991b5f2021-01-19 21:19:02 +010033 # Pylint 1.5.2 from Ubuntu 16.04 is too old:
David Horstmanncd84bb22024-05-03 14:36:12 +010034 # E: 34, 0: Unable to import 'mbedtls_framework' (import-error)
Gilles Peskine1cc6a8e2021-01-06 17:02:33 +010035 # Pylint 1.8.3 from Ubuntu 18.04 passed on the first commit containing this line.
Gilles Peskinebdde5d02021-01-19 21:42:05 +010036 check_version pylint 1.8.3
Gilles Peskine1cc6a8e2021-01-06 17:02:33 +010037}
38
39can_mypy () {
Gilles Peskine0370c172021-01-19 21:58:09 +010040 # mypy 0.770 is too old:
David Horstmanncd84bb22024-05-03 14:36:12 +010041 # tests/scripts/test_psa_constant_names.py:34: error: Cannot find implementation or library stub for module named 'mbedtls_framework'
Gilles Peskine0370c172021-01-19 21:58:09 +010042 # mypy 0.780 from pip passed on the first commit containing this line.
43 check_version mypy.version 0.780
Gilles Peskine1cc6a8e2021-01-06 17:02:33 +010044}
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
Gilles Peskine6d82a7e2021-01-19 21:19:25 +010057echo 'Running pylint ...'
David Horstmanncd84bb22024-05-03 14:36:12 +010058$PYTHON -m pylint framework/scripts/mbedtls_framework/*.py scripts/*.py tests/scripts/*.py || {
Gilles Peskine7be45512020-08-12 02:31:02 +020059 echo >&2 "pylint reported errors"
60 ret=1
61}
62
Gilles Peskine254efe52022-02-28 16:06:36 +010063echo
64echo 'Running mypy ...'
David Horstmann299e7412024-05-03 16:01:36 +010065$PYTHON -m mypy framework/scripts/mbedtls_framework/*.py scripts/*.py tests/scripts/*.py ||
Gilles Peskine254efe52022-02-28 16:06:36 +010066 ret=1
Gilles Peskine7be45512020-08-12 02:31:02 +020067
68exit $ret