lint-python 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env bash
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one or more
  4. # contributor license agreements. See the NOTICE file distributed with
  5. # this work for additional information regarding copyright ownership.
  6. # The ASF licenses this file to You under the Apache License, Version 2.0
  7. # (the "License"); you may not use this file except in compliance with
  8. # the License. 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,
  14. # WITHOUT 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.
  17. #
  18. # This script is copied and modified from the project of Apache Spark
  19. SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"
  20. ROOT_DIR="$(dirname "$SCRIPT_DIR")"
  21. # Exclude auto-generated configuration file.
  22. PATHS_TO_CHECK="$( cd "$ROOT_DIR" && find . -name "*.py" )"
  23. PYCODESTYLE_REPORT_PATH="$ROOT_DIR/dev/pycodestyle-report.txt"
  24. PYLINT_REPORT_PATH="$ROOT_DIR/dev/pylint-report.txt"
  25. PYLINT_INSTALL_INFO="$ROOT_DIR/dev/pylint-info.txt"
  26. SPHINXBUILD=${SPHINXBUILD:=sphinx-build}
  27. SPHINX_REPORT_PATH="$ROOT_DIR/dev/sphinx-report.txt"
  28. cd "$ROOT_DIR"
  29. # compileall: https://docs.python.org/2/library/compileall.html
  30. python -B -m compileall -q -l $PATHS_TO_CHECK > "$PYCODESTYLE_REPORT_PATH"
  31. compile_status="${PIPESTATUS[0]}"
  32. # Get pycodestyle at runtime so that we don't rely on it being installed on the build server.
  33. # See: https://github.com/apache/spark/pull/1744#issuecomment-50982162
  34. # Updated to the latest official version of pep8. pep8 is formally renamed to pycodestyle.
  35. PYCODESTYLE_VERSION="2.3.1"
  36. PYCODESTYLE_SCRIPT_PATH="$ROOT_DIR/dev/pycodestyle-$PYCODESTYLE_VERSION.py"
  37. PYCODESTYLE_SCRIPT_REMOTE_PATH="https://raw.githubusercontent.com/PyCQA/pycodestyle/$PYCODESTYLE_VERSION/pycodestyle.py"
  38. if [ ! -e "$PYCODESTYLE_SCRIPT_PATH" ]; then
  39. curl --silent -o "$PYCODESTYLE_SCRIPT_PATH" "$PYCODESTYLE_SCRIPT_REMOTE_PATH"
  40. curl_status="$?"
  41. if [ "$curl_status" -ne 0 ]; then
  42. echo "Failed to download pycodestyle.py from \"$PYCODESTYLE_SCRIPT_REMOTE_PATH\"."
  43. exit "$curl_status"
  44. fi
  45. fi
  46. # Easy install pylint in /dev/pylint. To easy_install into a directory, the PYTHONPATH should
  47. # be set to the directory.
  48. # dev/pylint should be appended to the PATH variable as well.
  49. # Jenkins by default installs the pylint3 version, so for now this just checks the code quality
  50. # of python3.
  51. export "PYTHONPATH=$ROOT_DIR/dev/pylint"
  52. export "PYLINT_HOME=$PYTHONPATH"
  53. export "PATH=$PYTHONPATH:$PATH"
  54. # There is no need to write this output to a file
  55. # first, but we do so so that the check status can
  56. # be output before the report, like with the
  57. # scalastyle and RAT checks.
  58. python "$PYCODESTYLE_SCRIPT_PATH" --config=dev/tox.ini $PATHS_TO_CHECK >> "$PYCODESTYLE_REPORT_PATH"
  59. pycodestyle_status="${PIPESTATUS[0]}"
  60. if [ "$compile_status" -eq 0 -a "$pycodestyle_status" -eq 0 ]; then
  61. lint_status=0
  62. else
  63. lint_status=1
  64. fi
  65. if [ "$lint_status" -ne 0 ]; then
  66. echo "pycodestyle checks failed."
  67. cat "$PYCODESTYLE_REPORT_PATH"
  68. rm "$PYCODESTYLE_REPORT_PATH"
  69. exit "$lint_status"
  70. else
  71. echo "pycodestyle checks passed."
  72. rm "$PYCODESTYLE_REPORT_PATH"
  73. fi
  74. # Check that the documentation builds acceptably, skip check if sphinx is not installed.
  75. if hash "$SPHINXBUILD" 2> /dev/null; then
  76. cd python/docs
  77. make clean
  78. # Treat warnings as errors so we stop correctly
  79. SPHINXOPTS="-a -W" make html &> "$SPHINX_REPORT_PATH" || lint_status=1
  80. if [ "$lint_status" -ne 0 ]; then
  81. echo "pydoc checks failed."
  82. cat "$SPHINX_REPORT_PATH"
  83. echo "re-running make html to print full warning list"
  84. make clean
  85. SPHINXOPTS="-a" make html
  86. rm "$SPHINX_REPORT_PATH"
  87. exit "$lint_status"
  88. else
  89. echo "pydoc checks passed."
  90. rm "$SPHINX_REPORT_PATH"
  91. fi
  92. cd ../..
  93. else
  94. echo >&2 "The $SPHINXBUILD command was not found. Skipping pydoc checks for now"
  95. fi