blob: 03a14728113c0dc6ba9b03b9bccfb544fd30ca63 [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
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02004# 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 Peskine7be45512020-08-12 02:31:02 +020017
18# Purpose: check Python files for potential programming errors or maintenance
19# hurdles. Run pylint to detect some potential mistakes and enforce PEP8
20# coding standards. If available, run mypy to perform static type checking.
21
22# We'll keep going on errors and report the status at the end.
23ret=0
Mohammad Azim Khan21798102018-07-06 00:41:08 +010024
Gilles Peskine56e99d62020-03-24 15:07:57 +010025if type python3 >/dev/null 2>/dev/null; then
26 PYTHON=python3
Simon Butchere30d03e2020-03-16 11:30:46 +000027else
Gilles Peskine56e99d62020-03-24 15:07:57 +010028 PYTHON=python
Simon Butchere30d03e2020-03-16 11:30:46 +000029fi
30
Gilles Peskine1cc6a8e2021-01-06 17:02:33 +010031can_pylint () {
32 # Pylint 1.5.2 from Ubuntu 16.04 is too old.
33 # Pylint 1.8.3 from Ubuntu 18.04 passed on the first commit containing this line.
34 $PYTHON -m pylint 2>/dev/null --version | awk '
35 BEGIN {status = 1}
36 /^(pylint[0-9]*|__main__\.py) +[0-9]+\.[0-9]+/ {
37 split($2, version, /[^0-9]+/);
38 status = !(version[1] >= 2 || (version[1] == 1 && version[2] >= 8));
39 exit; # executes the END block
40 }
41 END {exit status}
42 '
43}
44
45can_mypy () {
46 # Just check that mypy is present and looks sane. I don't know what
47 # minimum version is required. The check is not just "type mypy"
48 # becaues that passes if a mypy exists but is not installed for the current
49 # python version.
50 mypy --version 2>/dev/null >/dev/null
51}
52
53# With just a --can-xxx option, check whether the tool for xxx is available
54# with an acceptable version, and exit without running any checks. The exit
55# status is true if the tool is available and acceptable and false otherwise.
56if [ "$1" = "--can-pylint" ]; then
57 can_pylint
58 exit
59elif [ "$1" = "--can-mypy" ]; then
60 can_mypy
61 exit
62fi
63
Gilles Peskineb13ed702020-12-11 00:58:48 +010064$PYTHON -m pylint -j 2 scripts/mbedtls_dev/*.py scripts/*.py tests/scripts/*.py || {
Gilles Peskine7be45512020-08-12 02:31:02 +020065 echo >&2 "pylint reported errors"
66 ret=1
67}
68
69# Check types if mypy is available
70if type mypy >/dev/null 2>/dev/null; then
71 echo
72 echo 'Running mypy ...'
73 mypy scripts/*.py tests/scripts/*.py ||
74 ret=1
75fi
76
77exit $ret