Commit d61da0cc authored by Ben Walker's avatar Ben Walker Committed by Tomasz Zawadzki
Browse files

scripts: Break check_format up into functions



Change-Id: I3e3bd44826453e0ff5cc5b01769e81b5fdd660ff
Signed-off-by: default avatarBen Walker <benjamin.walker@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/4073


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Reviewed-by: default avatarTomasz Zawadzki <tomasz.zawadzki@intel.com>
parent 9f6f02f2
Loading
Loading
Loading
Loading
+482 −374
Original line number Diff line number Diff line
@@ -33,8 +33,11 @@ function array_contains_string() {

rc=0

function check_permissions() {
	echo -n "Checking file permissions..."

	local rc=0

	while read -r perm _res0 _res1 path; do
		if [ ! -f "$path" ]; then
			continue
@@ -80,6 +83,12 @@ if [ $rc -eq 0 ]; then
		echo " OK"
	fi

	return $rc
}

function check_c_style() {
	local rc=0

	if hash astyle; then
		echo -n "Checking coding style..."
		if [ "$(astyle -V)" \< "Artistic Style Version 3" ]; then
@@ -111,14 +120,11 @@ if hash astyle; then
	else
		echo "You do not have astyle installed so your code style is not being checked!"
	fi
	return $rc
}

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 0
fi
function check_comment_style() {
	local rc=0

	echo -n "Checking comment style..."

@@ -137,6 +143,12 @@ else
	fi
	rm -f comment.log

	return $rc
}

function check_spaces_before_tabs() {
	local rc=0

	echo -n "Checking for spaces before tabs..."
	git grep --line-number $' \t' -- './*' ':!*.patch' > whitespace.log || true
	if [ -s whitespace.log ]; then
@@ -148,6 +160,12 @@ else
	fi
	rm -f whitespace.log

	return $rc
}

function check_trailing_whitespace() {
	local rc=0

	echo -n "Checking trailing whitespace in output strings..."

	git grep --line-number -e ' \\n"' -- '*.[ch]' > whitespace.log || true
@@ -161,6 +179,12 @@ else
	fi
	rm -f whitespace.log

	return $rc
}

function check_forbidden_functions() {
	local rc=0

	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
@@ -173,6 +197,12 @@ else
	fi
	rm -f badfunc.log

	return $rc
}

function check_cunit_style() {
	local rc=0

	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
@@ -185,6 +215,12 @@ else
	fi
	rm -f badcunit.log

	return $rc
}

function check_eof() {
	local rc=0

	echo -n "Checking blank lines at end of file..."

	if ! git grep -I -l -e . -z './*' ':!*.patch' \
@@ -197,6 +233,12 @@ else
	fi
	rm -f eofnl.log

	return $rc
}

function check_posix_includes() {
	local rc=0

	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
	if [ -s scripts/posix.log ]; then
@@ -208,6 +250,12 @@ else
	fi
	rm -f scripts/posix.log

	return $rc
}

function check_naming_conventions() {
	local rc=0

	echo -n "Checking for proper function naming conventions..."
	# commit_to_compare = HEAD - 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"
	fi

	return $rc
}

function check_include_style() {
	local rc=0

	echo -n "Checking #include style..."
	git grep -I -i --line-number "#include <spdk/" -- '*.[ch]' > scripts/includes.log || true
	if [ -s scripts/includes.log ]; then
@@ -282,6 +336,12 @@ else
	fi
	rm -f scripts/includes.log

	return $rc
}

function check_python_style() {
	local rc=0

	if hash pycodestyle 2> /dev/null; then
		PEP8=pycodestyle
	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!"
	fi

	return $rc
}

function check_bash_style() {
	local rc=0

	# find compatible shfmt binary
	shfmt_bins=$(compgen -c | grep '^shfmt' || true)
	for bin in $shfmt_bins; do
@@ -384,6 +450,12 @@ else
		echo "shfmt not detected, Bash style formatting check is skipped"
	fi

	return $rc
}

function check_bash_static_analysis() {
	local rc=0

	if hash shellcheck 2> /dev/null; then
		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!"
	fi

	return $rc
}

function check_changelog() {
	local rc=0

	# Check if any of the public interfaces were modified by this patch.
	# Warn the user to consider updating the changelog any changes
	# are detected.
@@ -484,4 +562,34 @@ else
		echo ""
	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