1a8e1175bSopenharmony_ci#! /usr/bin/env sh
2a8e1175bSopenharmony_ci
3a8e1175bSopenharmony_ci# Copyright The Mbed TLS Contributors
4a8e1175bSopenharmony_ci# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5a8e1175bSopenharmony_ci
6a8e1175bSopenharmony_ci# Purpose: check Python files for potential programming errors or maintenance
7a8e1175bSopenharmony_ci# hurdles. Run pylint to detect some potential mistakes and enforce PEP8
8a8e1175bSopenharmony_ci# coding standards. Run mypy to perform static type checking.
9a8e1175bSopenharmony_ci
10a8e1175bSopenharmony_ci# We'll keep going on errors and report the status at the end.
11a8e1175bSopenharmony_ciret=0
12a8e1175bSopenharmony_ci
13a8e1175bSopenharmony_ciif type python3 >/dev/null 2>/dev/null; then
14a8e1175bSopenharmony_ci    PYTHON=python3
15a8e1175bSopenharmony_cielse
16a8e1175bSopenharmony_ci    PYTHON=python
17a8e1175bSopenharmony_cifi
18a8e1175bSopenharmony_ci
19a8e1175bSopenharmony_cicheck_version () {
20a8e1175bSopenharmony_ci    $PYTHON - "$2" <<EOF
21a8e1175bSopenharmony_ciimport packaging.version
22a8e1175bSopenharmony_ciimport sys
23a8e1175bSopenharmony_ciimport $1 as package
24a8e1175bSopenharmony_ciactual = package.__version__
25a8e1175bSopenharmony_ciwanted = sys.argv[1]
26a8e1175bSopenharmony_ciif packaging.version.parse(actual) < packaging.version.parse(wanted):
27a8e1175bSopenharmony_ci    sys.stderr.write("$1: version %s is too old (want %s)\n" % (actual, wanted))
28a8e1175bSopenharmony_ci    exit(1)
29a8e1175bSopenharmony_ciEOF
30a8e1175bSopenharmony_ci}
31a8e1175bSopenharmony_ci
32a8e1175bSopenharmony_cican_pylint () {
33a8e1175bSopenharmony_ci    # Pylint 1.5.2 from Ubuntu 16.04 is too old:
34a8e1175bSopenharmony_ci    #     E: 34, 0: Unable to import 'mbedtls_dev' (import-error)
35a8e1175bSopenharmony_ci    # Pylint 1.8.3 from Ubuntu 18.04 passed on the first commit containing this line.
36a8e1175bSopenharmony_ci    check_version pylint 1.8.3
37a8e1175bSopenharmony_ci}
38a8e1175bSopenharmony_ci
39a8e1175bSopenharmony_cican_mypy () {
40a8e1175bSopenharmony_ci    # mypy 0.770 is too old:
41a8e1175bSopenharmony_ci    #     tests/scripts/test_psa_constant_names.py:34: error: Cannot find implementation or library stub for module named 'mbedtls_dev'
42a8e1175bSopenharmony_ci    # mypy 0.780 from pip passed on the first commit containing this line.
43a8e1175bSopenharmony_ci    check_version mypy.version 0.780
44a8e1175bSopenharmony_ci}
45a8e1175bSopenharmony_ci
46a8e1175bSopenharmony_ci# With just a --can-xxx option, check whether the tool for xxx is available
47a8e1175bSopenharmony_ci# with an acceptable version, and exit without running any checks. The exit
48a8e1175bSopenharmony_ci# status is true if the tool is available and acceptable and false otherwise.
49a8e1175bSopenharmony_ciif [ "$1" = "--can-pylint" ]; then
50a8e1175bSopenharmony_ci    can_pylint
51a8e1175bSopenharmony_ci    exit
52a8e1175bSopenharmony_cielif [ "$1" = "--can-mypy" ]; then
53a8e1175bSopenharmony_ci    can_mypy
54a8e1175bSopenharmony_ci    exit
55a8e1175bSopenharmony_cifi
56a8e1175bSopenharmony_ci
57a8e1175bSopenharmony_ciecho 'Running pylint ...'
58a8e1175bSopenharmony_ci$PYTHON -m pylint scripts/mbedtls_dev/*.py scripts/*.py tests/scripts/*.py || {
59a8e1175bSopenharmony_ci    echo >&2 "pylint reported errors"
60a8e1175bSopenharmony_ci    ret=1
61a8e1175bSopenharmony_ci}
62a8e1175bSopenharmony_ci
63a8e1175bSopenharmony_ciecho
64a8e1175bSopenharmony_ciecho 'Running mypy ...'
65a8e1175bSopenharmony_ci$PYTHON -m mypy scripts/*.py tests/scripts/*.py ||
66a8e1175bSopenharmony_ci  ret=1
67a8e1175bSopenharmony_ci
68a8e1175bSopenharmony_ciexit $ret
69