19
19
# Options:
20
20
# run sanity checks: python 2&3 pylint checks and bazel nobuild
21
21
# --pep8 run pep8 test only
22
+ # --incremental Performs checks incrementally, by using the files changed in
23
+ # the latest commit
22
24
23
25
# Current script directory
24
26
SCRIPT_DIR=$( cd ${0%/* } && pwd -P )
@@ -39,11 +41,52 @@ num_cpus() {
39
41
echo ${N_CPUS}
40
42
}
41
43
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
+ }
42
81
43
82
# Subfunctions for substeps
44
83
# Run pylint
45
84
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.
47
90
48
91
# Use this list to whitelist pylint errors
49
92
ERROR_WHITELIST=" ^tensorflow/python/framework/function_test\.py.*\[E1123.*noinline " \
@@ -53,9 +96,9 @@ do_pylint() {
53
96
54
97
echo " ERROR_WHITELIST=\" ${ERROR_WHITELIST} \" "
55
98
56
- if [[ $# != " 1" ]]; then
99
+ if [[ $# != " 1" ]] && [[ $# != " 2 " ]] ; then
57
100
echo " Invalid syntax when invoking do_pylint"
58
- echo " Usage: do_pylint (PYTHON2 | PYTHON3)"
101
+ echo " Usage: do_pylint (PYTHON2 | PYTHON3) [--incremental] "
59
102
return 1
60
103
fi
61
104
@@ -68,7 +111,27 @@ do_pylint() {
68
111
return 1
69
112
fi
70
113
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
72
135
73
136
PYLINTRC_FILE=" ${SCRIPT_DIR} /pylintrc"
74
137
@@ -134,12 +197,32 @@ do_pylint() {
134
197
135
198
# Run pep8 check
136
199
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.
138
204
139
205
PEP8_BIN=" /usr/local/bin/pep8"
140
- PYTHON_SRC_FILES=$( find tensorflow -name ' *.py' )
141
206
PEP8_CONFIG_FILE=" ${SCRIPT_DIR} /pep8"
142
207
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
+
143
226
if [[ ! -f " ${PEP8_CONFIG_FILE} " ]]; then
144
227
die " ERROR: Cannot find pep8 config file at ${PEP8_CONFIG_FILE} "
145
228
fi
@@ -197,11 +280,22 @@ do_bazel_nobuild() {
197
280
SANITY_STEPS=(" do_pylint PYTHON2" " do_pylint PYTHON3" " do_bazel_nobuild" )
198
281
SANITY_STEPS_DESC=(" Python 2 pylint" " Python 3 pylint" " bazel nobuild" )
199
282
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
+
205
299
206
300
FAIL_COUNTER=0
207
301
PASS_COUNTER=0
@@ -218,7 +312,7 @@ while [[ ${COUNTER} -lt "${#SANITY_STEPS[@]}" ]]; do
218
312
" ${SANITY_STEPS[COUNTER]} (${SANITY_STEPS_DESC[COUNTER]} ) ==="
219
313
echo " "
220
314
221
- ${SANITY_STEPS[COUNTER]}
315
+ ${SANITY_STEPS[COUNTER]} ${INCREMENTAL_FLAG}
222
316
RESULT=$?
223
317
224
318
if [[ ${RESULT} != " 0" ]]; then
0 commit comments