Skip to content

Commit 35d0a26

Browse files
caisqtensorflower-gardener
authored andcommitted
Add option flag --incremental to ci_sanity.sh
Speed up sanity check during CL builds by running pylint and pep8 checks on only the Python files changed in the last non-merge git commit. Change: 128431639
1 parent 668192f commit 35d0a26

File tree

1 file changed

+106
-12
lines changed

1 file changed

+106
-12
lines changed

tensorflow/tools/ci_build/ci_sanity.sh

+106-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
# Options:
2020
# run sanity checks: python 2&3 pylint checks and bazel nobuild
2121
# --pep8 run pep8 test only
22+
# --incremental Performs checks incrementally, by using the files changed in
23+
# the latest commit
2224

2325
# Current script directory
2426
SCRIPT_DIR=$( cd ${0%/*} && pwd -P )
@@ -39,11 +41,52 @@ num_cpus() {
3941
echo ${N_CPUS}
4042
}
4143

44+
# Helper functions for examining changed files in the last non-merge git
45+
# commit.
46+
47+
# Get the hash of the last non-merge git commit on the current branch.
48+
# Usage: get_last_non_merge_git_commit
49+
get_last_non_merge_git_commit() {
50+
echo $(git rev-list --no-merges -n 1 HEAD)
51+
}
52+
53+
# List files changed (i.e., added, removed or revised) in the last non-merge
54+
# git commit.
55+
# Usage: get_changed_files_in_last_non_merge_git_commit
56+
get_changed_files_in_last_non_merge_git_commit() {
57+
git diff-tree --no-commit-id --name-only -r $(get_last_non_merge_git_commit)
58+
}
59+
60+
# List Python files changed in the last non-merge git commit that still exist,
61+
# i.e., not removed.
62+
# Usage: get_py_files_to_check [--incremental]
63+
get_py_files_to_check() {
64+
if [[ "$1" == "--incremental" ]]; then
65+
CHANGED_PY_FILES=$(get_changed_files_in_last_non_merge_git_commit | \
66+
grep '.*\.py$')
67+
68+
# Do not include files removed in the last non-merge commit.
69+
PY_FILES=""
70+
for PY_FILE in ${CHANGED_PY_FILES}; do
71+
if [[ -f "${PY_FILE}" ]]; then
72+
PY_FILES="${PY_FILES} ${PY_FILE}"
73+
fi
74+
done
75+
76+
echo "${PY_FILES}"
77+
else
78+
echo $(find tensorflow -name '*.py')
79+
fi
80+
}
4281

4382
# Subfunctions for substeps
4483
# Run pylint
4584
do_pylint() {
46-
# Usage: do_pylint (PYTHON2 | PYTHON3)
85+
# Usage: do_pylint (PYTHON2 | PYTHON3) [--incremental]
86+
#
87+
# Options:
88+
# --incremental Performs check on only the python files changed in the
89+
# last non-merge git commit.
4790

4891
# Use this list to whitelist pylint errors
4992
ERROR_WHITELIST="^tensorflow/python/framework/function_test\.py.*\[E1123.*noinline "\
@@ -53,9 +96,9 @@ do_pylint() {
5396

5497
echo "ERROR_WHITELIST=\"${ERROR_WHITELIST}\""
5598

56-
if [[ $# != "1" ]]; then
99+
if [[ $# != "1" ]] && [[ $# != "2" ]]; then
57100
echo "Invalid syntax when invoking do_pylint"
58-
echo "Usage: do_pylint (PYTHON2 | PYTHON3)"
101+
echo "Usage: do_pylint (PYTHON2 | PYTHON3) [--incremental]"
59102
return 1
60103
fi
61104

@@ -68,7 +111,27 @@ do_pylint() {
68111
return 1
69112
fi
70113

71-
PYTHON_SRC_FILES=$(find tensorflow -name '*.py')
114+
if [[ "$2" == "--incremental" ]]; then
115+
PYTHON_SRC_FILES=$(get_py_files_to_check --incremental)
116+
NUM_PYTHON_SRC_FILES=$(echo ${PYTHON_SRC_FILES} | wc -w)
117+
118+
echo "do_pylint will perform checks on only the ${NUM_PYTHON_SRC_FILES} "\
119+
"Python file(s) changed in the last non-merge git commit due to the "\
120+
"--incremental flag:"
121+
echo "${PYTHON_SRC_FILES}"
122+
echo ""
123+
elif [[ -z "$2" ]]; then
124+
PYTHON_SRC_FILES=$(get_py_files_to_check)
125+
else
126+
echo "Invalid syntax when invoking do_pylint"
127+
echo "Usage: do_pylint (PYTHON2 | PYTHON3) [--incremental]"
128+
return 1
129+
fi
130+
131+
if [[ -z ${PYTHON_SRC_FILES} ]]; then
132+
echo "do_pylint found no Python files to check. Returning."
133+
return 0
134+
fi
72135

73136
PYLINTRC_FILE="${SCRIPT_DIR}/pylintrc"
74137

@@ -134,12 +197,32 @@ do_pylint() {
134197

135198
# Run pep8 check
136199
do_pep8() {
137-
# Usage: do_pep8
200+
# Usage: do_pep8 [--incremental]
201+
# Options:
202+
# --incremental Performs check on only the python files changed in the
203+
# last non-merge git commit.
138204

139205
PEP8_BIN="/usr/local/bin/pep8"
140-
PYTHON_SRC_FILES=$(find tensorflow -name '*.py')
141206
PEP8_CONFIG_FILE="${SCRIPT_DIR}/pep8"
142207

208+
if [[ "$1" == "--incremental" ]]; then
209+
PYTHON_SRC_FILES=$(get_py_files_to_check --incremental)
210+
NUM_PYTHON_SRC_FILES=$(echo ${PYTHON_SRC_FILES} | wc -w)
211+
212+
echo "do_pep8 will perform checks on only the ${NUM_PYTHON_SRC_FILES} "\
213+
"Python file(s) changed in the last non-merge git commit due to the "\
214+
"--incremental flag:"
215+
echo "${PYTHON_SRC_FILES}"
216+
echo ""
217+
else
218+
PYTHON_SRC_FILES=$(get_py_files_to_check)
219+
fi
220+
221+
if [[ -z ${PYTHON_SRC_FILES} ]]; then
222+
echo "do_pep8 found no Python files to check. Returning."
223+
return 0
224+
fi
225+
143226
if [[ ! -f "${PEP8_CONFIG_FILE}" ]]; then
144227
die "ERROR: Cannot find pep8 config file at ${PEP8_CONFIG_FILE}"
145228
fi
@@ -197,11 +280,22 @@ do_bazel_nobuild() {
197280
SANITY_STEPS=("do_pylint PYTHON2" "do_pylint PYTHON3" "do_bazel_nobuild")
198281
SANITY_STEPS_DESC=("Python 2 pylint" "Python 3 pylint" "bazel nobuild")
199282

200-
# Only run pep8 test if "--pep8" option supplied
201-
if [[ "$1" == "--pep8" ]]; then
202-
SANITY_STEPS=("do_pep8")
203-
SANITY_STEPS_DESC=("pep8 test")
204-
fi
283+
INCREMENTAL_FLAG=""
284+
285+
# Parse command-line arguments
286+
for arg in "$@"; do
287+
if [[ "${arg}" == "--pep8" ]]; then
288+
# Only run pep8 test if "--pep8" option supplied
289+
SANITY_STEPS=("do_pep8")
290+
SANITY_STEPS_DESC=("pep8 test")
291+
elif [[ "${arg}" == "--incremental" ]]; then
292+
INCREMENTAL_FLAG="--incremental"
293+
else
294+
echo "ERROR: Unrecognized command-line flag: $1"
295+
exit 1
296+
fi
297+
done
298+
205299

206300
FAIL_COUNTER=0
207301
PASS_COUNTER=0
@@ -218,7 +312,7 @@ while [[ ${COUNTER} -lt "${#SANITY_STEPS[@]}" ]]; do
218312
"${SANITY_STEPS[COUNTER]} (${SANITY_STEPS_DESC[COUNTER]}) ==="
219313
echo ""
220314

221-
${SANITY_STEPS[COUNTER]}
315+
${SANITY_STEPS[COUNTER]} ${INCREMENTAL_FLAG}
222316
RESULT=$?
223317

224318
if [[ ${RESULT} != "0" ]]; then

0 commit comments

Comments
 (0)