check-python-files.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #! /usr/bin/env sh
  2. # Copyright The Mbed TLS Contributors
  3. # SPDX-License-Identifier: Apache-2.0
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. # not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. # Purpose: check Python files for potential programming errors or maintenance
  17. # hurdles. Run pylint to detect some potential mistakes and enforce PEP8
  18. # coding standards. If available, run mypy to perform static type checking.
  19. # We'll keep going on errors and report the status at the end.
  20. ret=0
  21. if type python3 >/dev/null 2>/dev/null; then
  22. PYTHON=python3
  23. else
  24. PYTHON=python
  25. fi
  26. check_version () {
  27. $PYTHON - "$2" <<EOF
  28. import packaging.version
  29. import sys
  30. import $1 as package
  31. actual = package.__version__
  32. wanted = sys.argv[1]
  33. if packaging.version.parse(actual) < packaging.version.parse(wanted):
  34. sys.stderr.write("$1: version %s is too old (want %s)\n" % (actual, wanted))
  35. exit(1)
  36. EOF
  37. }
  38. can_pylint () {
  39. # Pylint 1.5.2 from Ubuntu 16.04 is too old:
  40. # E: 34, 0: Unable to import 'mbedtls_dev' (import-error)
  41. # Pylint 1.8.3 from Ubuntu 18.04 passed on the first commit containing this line.
  42. check_version pylint 1.8.3
  43. }
  44. can_mypy () {
  45. # mypy 0.770 is too old:
  46. # tests/scripts/test_psa_constant_names.py:34: error: Cannot find implementation or library stub for module named 'mbedtls_dev'
  47. # mypy 0.780 from pip passed on the first commit containing this line.
  48. check_version mypy.version 0.780
  49. }
  50. # With just a --can-xxx option, check whether the tool for xxx is available
  51. # with an acceptable version, and exit without running any checks. The exit
  52. # status is true if the tool is available and acceptable and false otherwise.
  53. if [ "$1" = "--can-pylint" ]; then
  54. can_pylint
  55. exit
  56. elif [ "$1" = "--can-mypy" ]; then
  57. can_mypy
  58. exit
  59. fi
  60. echo 'Running pylint ...'
  61. $PYTHON -m pylint -j 2 scripts/mbedtls_dev/*.py scripts/*.py tests/scripts/*.py || {
  62. echo >&2 "pylint reported errors"
  63. ret=1
  64. }
  65. # Check types if mypy is available
  66. if can_mypy; then
  67. echo
  68. echo 'Running mypy ...'
  69. $PYTHON -m mypy scripts/*.py tests/scripts/*.py ||
  70. ret=1
  71. fi
  72. exit $ret