scripts: Break check_format up into functions

Change-Id: I3e3bd44826453e0ff5cc5b01769e81b5fdd660ff
Signed-off-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/4073
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Tomasz Zawadzki <tomasz.zawadzki@intel.com>
This commit is contained in:
Ben Walker 2020-09-04 14:29:22 -07:00 committed by Tomasz Zawadzki
parent 9f6f02f22e
commit d61da0ccda

View File

@ -33,8 +33,11 @@ function array_contains_string() {
rc=0 rc=0
function check_permissions() {
echo -n "Checking file permissions..." echo -n "Checking file permissions..."
local rc=0
while read -r perm _res0 _res1 path; do while read -r perm _res0 _res1 path; do
if [ ! -f "$path" ]; then if [ ! -f "$path" ]; then
continue continue
@ -80,6 +83,12 @@ if [ $rc -eq 0 ]; then
echo " OK" echo " OK"
fi fi
return $rc
}
function check_c_style() {
local rc=0
if hash astyle; then if hash astyle; then
echo -n "Checking coding style..." echo -n "Checking coding style..."
if [ "$(astyle -V)" \< "Artistic Style Version 3" ]; then if [ "$(astyle -V)" \< "Artistic Style Version 3" ]; then
@ -111,14 +120,11 @@ if hash astyle; then
else else
echo "You do not have astyle installed so your code style is not being checked!" echo "You do not have astyle installed so your code style is not being checked!"
fi fi
return $rc
}
GIT_VERSION=$(git --version | cut -d' ' -f3) function check_comment_style() {
local rc=0
if version_lt "1.9.5" "${GIT_VERSION}"; then
# git <1.9.5 doesn't support pathspec magic exclude
echo " Your git version is too old to perform all tests. Please update git to at least 1.9.5 version..."
exit 0
fi
echo -n "Checking comment style..." echo -n "Checking comment style..."
@ -137,6 +143,12 @@ else
fi fi
rm -f comment.log rm -f comment.log
return $rc
}
function check_spaces_before_tabs() {
local rc=0
echo -n "Checking for spaces before tabs..." echo -n "Checking for spaces before tabs..."
git grep --line-number $' \t' -- './*' ':!*.patch' > whitespace.log || true git grep --line-number $' \t' -- './*' ':!*.patch' > whitespace.log || true
if [ -s whitespace.log ]; then if [ -s whitespace.log ]; then
@ -148,6 +160,12 @@ else
fi fi
rm -f whitespace.log rm -f whitespace.log
return $rc
}
function check_trailing_whitespace() {
local rc=0
echo -n "Checking trailing whitespace in output strings..." echo -n "Checking trailing whitespace in output strings..."
git grep --line-number -e ' \\n"' -- '*.[ch]' > whitespace.log || true git grep --line-number -e ' \\n"' -- '*.[ch]' > whitespace.log || true
@ -161,6 +179,12 @@ else
fi fi
rm -f whitespace.log rm -f whitespace.log
return $rc
}
function check_forbidden_functions() {
local rc=0
echo -n "Checking for use of forbidden library functions..." echo -n "Checking for use of forbidden library functions..."
git grep --line-number -w '\(atoi\|atol\|atoll\|strncpy\|strcpy\|strcat\|sprintf\|vsprintf\)' -- './*.c' ':!lib/rte_vhost*/**' > badfunc.log || true git grep --line-number -w '\(atoi\|atol\|atoll\|strncpy\|strcpy\|strcat\|sprintf\|vsprintf\)' -- './*.c' ':!lib/rte_vhost*/**' > badfunc.log || true
@ -173,6 +197,12 @@ else
fi fi
rm -f badfunc.log rm -f badfunc.log
return $rc
}
function check_cunit_style() {
local rc=0
echo -n "Checking for use of forbidden CUnit macros..." echo -n "Checking for use of forbidden CUnit macros..."
git grep --line-number -w 'CU_ASSERT_FATAL' -- 'test/*' ':!test/spdk_cunit.h' > badcunit.log || true git grep --line-number -w 'CU_ASSERT_FATAL' -- 'test/*' ':!test/spdk_cunit.h' > badcunit.log || true
@ -185,6 +215,12 @@ else
fi fi
rm -f badcunit.log rm -f badcunit.log
return $rc
}
function check_eof() {
local rc=0
echo -n "Checking blank lines at end of file..." echo -n "Checking blank lines at end of file..."
if ! git grep -I -l -e . -z './*' ':!*.patch' \ if ! git grep -I -l -e . -z './*' ':!*.patch' \
@ -197,6 +233,12 @@ else
fi fi
rm -f eofnl.log rm -f eofnl.log
return $rc
}
function check_posix_includes() {
local rc=0
echo -n "Checking for POSIX includes..." echo -n "Checking for POSIX includes..."
git grep -I -i -f scripts/posix.txt -- './*' ':!include/spdk/stdinc.h' ':!include/linux/**' ':!lib/rte_vhost*/**' ':!scripts/posix.txt' ':!*.patch' > scripts/posix.log || true git grep -I -i -f scripts/posix.txt -- './*' ':!include/spdk/stdinc.h' ':!include/linux/**' ':!lib/rte_vhost*/**' ':!scripts/posix.txt' ':!*.patch' > scripts/posix.log || true
if [ -s scripts/posix.log ]; then if [ -s scripts/posix.log ]; then
@ -208,6 +250,12 @@ else
fi fi
rm -f scripts/posix.log rm -f scripts/posix.log
return $rc
}
function check_naming_conventions() {
local rc=0
echo -n "Checking for proper function naming conventions..." echo -n "Checking for proper function naming conventions..."
# commit_to_compare = HEAD - 1. # commit_to_compare = HEAD - 1.
commit_to_compare="$(git log --pretty=oneline --skip=1 -n 1 | awk '{print $1}')" commit_to_compare="$(git log --pretty=oneline --skip=1 -n 1 | awk '{print $1}')"
@ -271,6 +319,12 @@ if ! $failed_naming_conventions; then
echo " OK" echo " OK"
fi fi
return $rc
}
function check_include_style() {
local rc=0
echo -n "Checking #include style..." echo -n "Checking #include style..."
git grep -I -i --line-number "#include <spdk/" -- '*.[ch]' > scripts/includes.log || true git grep -I -i --line-number "#include <spdk/" -- '*.[ch]' > scripts/includes.log || true
if [ -s scripts/includes.log ]; then if [ -s scripts/includes.log ]; then
@ -282,6 +336,12 @@ else
fi fi
rm -f scripts/includes.log rm -f scripts/includes.log
return $rc
}
function check_python_style() {
local rc=0
if hash pycodestyle 2> /dev/null; then if hash pycodestyle 2> /dev/null; then
PEP8=pycodestyle PEP8=pycodestyle
elif hash pep8 2> /dev/null; then elif hash pep8 2> /dev/null; then
@ -307,6 +367,12 @@ else
echo "You do not have pycodestyle or pep8 installed so your Python style is not being checked!" echo "You do not have pycodestyle or pep8 installed so your Python style is not being checked!"
fi fi
return $rc
}
function check_bash_style() {
local rc=0
# find compatible shfmt binary # find compatible shfmt binary
shfmt_bins=$(compgen -c | grep '^shfmt' || true) shfmt_bins=$(compgen -c | grep '^shfmt' || true)
for bin in $shfmt_bins; do for bin in $shfmt_bins; do
@ -384,6 +450,12 @@ else
echo "shfmt not detected, Bash style formatting check is skipped" echo "shfmt not detected, Bash style formatting check is skipped"
fi fi
return $rc
}
function check_bash_static_analysis() {
local rc=0
if hash shellcheck 2> /dev/null; then if hash shellcheck 2> /dev/null; then
echo -n "Checking Bash style..." echo -n "Checking Bash style..."
@ -447,6 +519,12 @@ else
echo "You do not have shellcheck installed so your Bash style is not being checked!" echo "You do not have shellcheck installed so your Bash style is not being checked!"
fi fi
return $rc
}
function check_changelog() {
local rc=0
# Check if any of the public interfaces were modified by this patch. # Check if any of the public interfaces were modified by this patch.
# Warn the user to consider updating the changelog any changes # Warn the user to consider updating the changelog any changes
# are detected. # are detected.
@ -484,4 +562,34 @@ else
echo "" echo ""
fi fi
return $rc
}
rc=0
check_permissions || rc=1
check_c_style || rc=1
GIT_VERSION=$(git --version | cut -d' ' -f3)
if version_lt "1.9.5" "${GIT_VERSION}"; then
# git <1.9.5 doesn't support pathspec magic exclude
echo " Your git version is too old to perform all tests. Please update git to at least 1.9.5 version..."
exit $rc
fi
check_comment_style || rc=1
check_spaces_before_tabs || rc=1
check_trailing_whitespace || rc=1
check_forbidden_functions || rc=1
check_cunit_style || rc=1
check_eof || rc=1
check_posix_includes || rc=1
check_naming_conventions || rc=1
check_include_style || rc=1
check_python_style || rc=1
check_bash_style || rc=1
check_bash_static_analysis || rc=1
check_changelog || rc=1
exit $rc exit $rc