| 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 | 
| Bence Szépkúti | c7da1fe | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 | 
|  | 5 | # | 
|  | 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 7 | # not use this file except in compliance with the License. | 
|  | 8 | # You may obtain a copy of the License at | 
|  | 9 | # | 
|  | 10 | # http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 11 | # | 
|  | 12 | # Unless required by applicable law or agreed to in writing, software | 
|  | 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 15 | # See the License for the specific language governing permissions and | 
|  | 16 | # limitations under the License. | 
| Gilles Peskine | 7be4551 | 2020-08-12 02:31:02 +0200 | [diff] [blame] | 17 |  | 
|  | 18 | # Purpose: check Python files for potential programming errors or maintenance | 
|  | 19 | # hurdles. Run pylint to detect some potential mistakes and enforce PEP8 | 
| Gilles Peskine | 254efe5 | 2022-02-28 16:06:36 +0100 | [diff] [blame] | 20 | # coding standards. Run mypy to perform static type checking. | 
| Gilles Peskine | 7be4551 | 2020-08-12 02:31:02 +0200 | [diff] [blame] | 21 |  | 
|  | 22 | # We'll keep going on errors and report the status at the end. | 
|  | 23 | ret=0 | 
| Mohammad Azim Khan | 2179810 | 2018-07-06 00:41:08 +0100 | [diff] [blame] | 24 |  | 
| Gilles Peskine | 56e99d6 | 2020-03-24 15:07:57 +0100 | [diff] [blame] | 25 | if type python3 >/dev/null 2>/dev/null; then | 
|  | 26 | PYTHON=python3 | 
| Simon Butcher | e30d03e | 2020-03-16 11:30:46 +0000 | [diff] [blame] | 27 | else | 
| Gilles Peskine | 56e99d6 | 2020-03-24 15:07:57 +0100 | [diff] [blame] | 28 | PYTHON=python | 
| Simon Butcher | e30d03e | 2020-03-16 11:30:46 +0000 | [diff] [blame] | 29 | fi | 
|  | 30 |  | 
| Gilles Peskine | bdde5d0 | 2021-01-19 21:42:05 +0100 | [diff] [blame] | 31 | check_version () { | 
|  | 32 | $PYTHON - "$2" <<EOF | 
|  | 33 | import packaging.version | 
|  | 34 | import sys | 
|  | 35 | import $1 as package | 
|  | 36 | actual = package.__version__ | 
|  | 37 | wanted = sys.argv[1] | 
|  | 38 | if packaging.version.parse(actual) < packaging.version.parse(wanted): | 
|  | 39 | sys.stderr.write("$1: version %s is too old (want %s)\n" % (actual, wanted)) | 
|  | 40 | exit(1) | 
|  | 41 | EOF | 
|  | 42 | } | 
|  | 43 |  | 
| Gilles Peskine | 1cc6a8e | 2021-01-06 17:02:33 +0100 | [diff] [blame] | 44 | can_pylint () { | 
| Gilles Peskine | 2991b5f | 2021-01-19 21:19:02 +0100 | [diff] [blame] | 45 | # Pylint 1.5.2 from Ubuntu 16.04 is too old: | 
|  | 46 | #     E: 34, 0: Unable to import 'mbedtls_dev' (import-error) | 
| Gilles Peskine | 1cc6a8e | 2021-01-06 17:02:33 +0100 | [diff] [blame] | 47 | # 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] | 48 | check_version pylint 1.8.3 | 
| Gilles Peskine | 1cc6a8e | 2021-01-06 17:02:33 +0100 | [diff] [blame] | 49 | } | 
|  | 50 |  | 
|  | 51 | can_mypy () { | 
| Gilles Peskine | 0370c17 | 2021-01-19 21:58:09 +0100 | [diff] [blame] | 52 | # mypy 0.770 is too old: | 
|  | 53 | #     tests/scripts/test_psa_constant_names.py:34: error: Cannot find implementation or library stub for module named 'mbedtls_dev' | 
|  | 54 | # mypy 0.780 from pip passed on the first commit containing this line. | 
|  | 55 | check_version mypy.version 0.780 | 
| Gilles Peskine | 1cc6a8e | 2021-01-06 17:02:33 +0100 | [diff] [blame] | 56 | } | 
|  | 57 |  | 
|  | 58 | # With just a --can-xxx option, check whether the tool for xxx is available | 
|  | 59 | # with an acceptable version, and exit without running any checks. The exit | 
|  | 60 | # status is true if the tool is available and acceptable and false otherwise. | 
|  | 61 | if [ "$1" = "--can-pylint" ]; then | 
|  | 62 | can_pylint | 
|  | 63 | exit | 
|  | 64 | elif [ "$1" = "--can-mypy" ]; then | 
|  | 65 | can_mypy | 
|  | 66 | exit | 
|  | 67 | fi | 
|  | 68 |  | 
| Gilles Peskine | 6d82a7e | 2021-01-19 21:19:25 +0100 | [diff] [blame] | 69 | echo 'Running pylint ...' | 
| Gilles Peskine | e188734 | 2022-09-18 21:27:37 +0200 | [diff] [blame] | 70 | $PYTHON -m pylint scripts/mbedtls_dev/*.py scripts/*.py tests/scripts/*.py || { | 
| Gilles Peskine | 7be4551 | 2020-08-12 02:31:02 +0200 | [diff] [blame] | 71 | echo >&2 "pylint reported errors" | 
|  | 72 | ret=1 | 
|  | 73 | } | 
|  | 74 |  | 
| Gilles Peskine | 254efe5 | 2022-02-28 16:06:36 +0100 | [diff] [blame] | 75 | echo | 
|  | 76 | echo 'Running mypy ...' | 
|  | 77 | $PYTHON -m mypy scripts/*.py tests/scripts/*.py || | 
|  | 78 | ret=1 | 
| Gilles Peskine | 7be4551 | 2020-08-12 02:31:02 +0200 | [diff] [blame] | 79 |  | 
|  | 80 | exit $ret |