"git@developer.sourcefind.cn:ox696c/ktransformers.git" did not exist on "cff68532cee0df4040bf3812a840951a56d28920"
Unverified Commit 207e8ed8 authored by Revan Sopher's avatar Revan Sopher Committed by GitHub
Browse files

Create GitHub Action for pylint script. (#9818)

* Create GitHub Action for pylint script.

* Fix indent

* Add back incremental flag from TF, remove allowlist.

Running a full-repo pylint with Github Actions CI takes >40 minutes,
too long for use on presubmit.

Allowlist is per-file, so the TF allowlist offers nothing here.

* Fetch master so our script has something to diff against.

* Checkout as branch rather than as merge.

This should (?) get us only our modified files.

* Diff to common ancestor.

* Terrible no-good commit introducing linter error.

* Point to locally fetched master.

* debug logging

* HEAD missing?

* Fetch full branches.

* Fetch master again.

* Revert "debug logging"

This reverts commit 6edde819537a162a2fe4010b0e66c82b088ea7a9.

* Revert "Terrible no-good commit introducing linter error."

This reverts commit ac41b830342a21a1711a9d77a11777276602fb96.
parent 0bb46a95
...@@ -15,14 +15,13 @@ ...@@ -15,14 +15,13 @@
# ============================================================================== # ==============================================================================
# #
# Pylint wrapper extracted from main TensorFlow, sharing same exceptions. # Pylint wrapper extracted from main TensorFlow, sharing same exceptions.
# As this is meant for smaller repos, drops "modified files" checking in favor # Specify --incremental to only check files touched since last commit on master,
# of full-repo checking. # otherwise will recursively check current directory (full repo takes long!).
set -euo pipefail set -euo pipefail
# Download latest configs from main TensorFlow repo. # Download latest configs from main TensorFlow repo.
wget -q -O /tmp/pylintrc https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/tools/ci_build/pylintrc wget -q -O /tmp/pylintrc https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/tools/ci_build/pylintrc
wget -q -O /tmp/pylint_allowlist https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/tools/ci_build/pylint_allowlist
SCRIPT_DIR=/tmp SCRIPT_DIR=/tmp
...@@ -41,13 +40,48 @@ num_cpus() { ...@@ -41,13 +40,48 @@ num_cpus() {
echo ${N_CPUS} echo ${N_CPUS}
} }
get_changed_files_in_last_non_merge_git_commit() {
git diff --name-only $(git merge-base master $(git branch --show-current))
}
# List Python files changed in the last non-merge git commit that still exist,
# i.e., not removed.
# Usage: get_py_files_to_check [--incremental]
get_py_files_to_check() { get_py_files_to_check() {
find . -name '*.py' if [[ "$1" == "--incremental" ]]; then
CHANGED_PY_FILES=$(get_changed_files_in_last_non_merge_git_commit | \
grep '.*\.py$')
# Do not include files removed in the last non-merge commit.
PY_FILES=""
for PY_FILE in ${CHANGED_PY_FILES}; do
if [[ -f "${PY_FILE}" ]]; then
PY_FILES="${PY_FILES} ${PY_FILE}"
fi
done
echo "${PY_FILES}"
else
find . -name '*.py'
fi
} }
do_pylint() { do_pylint() {
# Get all Python files, regardless of mode. if [[ $# == 1 ]] && [[ "$1" == "--incremental" ]]; then
PYTHON_SRC_FILES=$(get_py_files_to_check --incremental)
if [[ -z "${PYTHON_SRC_FILES}" ]]; then
echo "do_pylint will NOT run due to --incremental flag and due to the "\
"absence of Python code changes in the last commit."
return 0
fi
elif [[ $# != 0 ]]; then
echo "Invalid syntax for invoking do_pylint"
echo "Usage: do_pylint [--incremental]"
return 1
else
PYTHON_SRC_FILES=$(get_py_files_to_check) PYTHON_SRC_FILES=$(get_py_files_to_check)
fi
# Something happened. TF no longer has Python code if this branch is taken # Something happened. TF no longer has Python code if this branch is taken
if [[ -z ${PYTHON_SRC_FILES} ]]; then if [[ -z ${PYTHON_SRC_FILES} ]]; then
...@@ -92,13 +126,9 @@ do_pylint() { ...@@ -92,13 +126,9 @@ do_pylint() {
PYLINT_START_TIME=$(date +'%s') PYLINT_START_TIME=$(date +'%s')
OUTPUT_FILE="$(mktemp)_pylint_output.log" OUTPUT_FILE="$(mktemp)_pylint_output.log"
ERRORS_FILE="$(mktemp)_pylint_errors.log" ERRORS_FILE="$(mktemp)_pylint_errors.log"
PERMIT_FILE="$(mktemp)_pylint_permit.log"
FORBID_FILE="$(mktemp)_pylint_forbid.log"
rm -rf ${OUTPUT_FILE} rm -rf ${OUTPUT_FILE}
rm -rf ${ERRORS_FILE} rm -rf ${ERRORS_FILE}
rm -rf ${PERMIT_FILE}
rm -rf ${FORBID_FILE}
set +e set +e
# When running, filter to only contain the error code lines. Removes module # When running, filter to only contain the error code lines. Removes module
...@@ -124,48 +154,25 @@ do_pylint() { ...@@ -124,48 +154,25 @@ do_pylint() {
# W0622 redefined-builtin # W0622 redefined-builtin
grep -E '(\[E|\[W0311|\[W0312|\[C0330|\[C0301|\[C0326|\[W0611|\[W0622)' ${OUTPUT_FILE} > ${ERRORS_FILE} grep -E '(\[E|\[W0311|\[W0312|\[C0330|\[C0301|\[C0326|\[W0611|\[W0622)' ${OUTPUT_FILE} > ${ERRORS_FILE}
# Split the pylint reported errors into permitted ones and those we want to
# block submit on until fixed.
# We use `${ALLOW_LIST_FILE}` to record the errors we temporarily accept. Goal
# is to make that file only contain errors caused by difference between
# internal and external versions.
ALLOW_LIST_FILE="${SCRIPT_DIR}/pylint_allowlist"
if [[ ! -f "${ALLOW_LIST_FILE}" ]]; then
die "ERROR: Cannot find pylint allowlist file at ${ALLOW_LIST_FILE}"
fi
# We can split with just 2 grep invocations
grep -f ${ALLOW_LIST_FILE} ${ERRORS_FILE} > ${PERMIT_FILE}
grep -v -f ${ALLOW_LIST_FILE} ${ERRORS_FILE} > ${FORBID_FILE}
# Determine counts of errors # Determine counts of errors
N_PERMIT_ERRORS=$(wc -l ${PERMIT_FILE} | cut -d' ' -f1) N_FORBID_ERRORS=$(wc -l ${ERRORS_FILE} | cut -d' ' -f1)
N_FORBID_ERRORS=$(wc -l ${FORBID_FILE} | cut -d' ' -f1)
set -e set -e
# First print all allowed errors
echo ""
if [[ ${N_PERMIT_ERRORS} != 0 ]]; then
echo "Found ${N_PERMIT_ERRORS} allowlisted pylint errors:"
cat ${PERMIT_FILE}
fi
# Now, print the errors we should fix # Now, print the errors we should fix
echo "" echo ""
if [[ ${N_FORBID_ERRORS} != 0 ]]; then if [[ ${N_FORBID_ERRORS} != 0 ]]; then
echo "Found ${N_FORBID_ERRORS} non-allowlisted pylint errors:" echo "Found ${N_FORBID_ERRORS} pylint errors:"
cat ${FORBID_FILE} cat ${ERRORS_FILE}
fi fi
echo "" echo ""
if [[ ${N_FORBID_ERRORS} != 0 ]]; then if [[ ${N_FORBID_ERRORS} != 0 ]]; then
echo "FAIL: Found ${N_FORBID_ERRORS} non-allowlisted errors and ${N_PERMIT_ERRORS} allowlisted errors" echo "FAIL: Found ${N_FORBID_ERRORS} errors"
return 1 return 1
else else
echo "PASS: Found only ${N_PERMIT_ERRORS} allowlisted errors" echo "PASS: Found no errors"
fi fi
} }
do_pylint do_pylint "$@"
name: CI
on: push
jobs:
pylint:
runs-on: ubuntu-latest
steps:
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install pylint 2.4.4
run: |
python -m pip install --upgrade pip
pip install pylint==2.4.4
- name: Checkout code
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
- name: Fetch master for diff
run: git fetch origin master:master
- name: Run pylint script
run: bash ./.github/scripts/pylint.sh --incremental
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment