xref: /spdk/scripts/check_format.sh (revision b58a5d73ef70b9458ef589f5858c2a72c1f8e47c)
1#!/usr/bin/env bash
2
3readonly BASEDIR=$(readlink -f $(dirname $0))/..
4cd $BASEDIR
5
6# exit on errors
7set -e
8
9rc=0
10
11if hash astyle; then
12	echo -n "Checking coding style..."
13	rm -f astyle.log
14	touch astyle.log
15	# Exclude rte_vhost code imported from DPDK - we want to keep the original code
16	#  as-is to enable ongoing work to synch with a generic upstream DPDK vhost library,
17	#  rather than making diffs more complicated by a lot of changes to follow SPDK
18	#  coding standards.
19	astyle --options=.astylerc "*.c" --exclude="rte_vhost" >> astyle.log
20	astyle --options=.astylerc --exclude=test/cpp_headers "*.cpp" >> astyle.log
21	astyle --options=.astylerc "*.h" --exclude="rte_vhost" >> astyle.log
22	if grep -q "^Formatted" astyle.log; then
23		echo " errors detected"
24		git diff
25		sed -i -e 's/  / /g' astyle.log
26		grep --color=auto "^Formatted.*" astyle.log
27		echo "Incorrect code style detected in one or more files."
28		echo "The files have been automatically formatted."
29		echo "Remember to add the files to your commit."
30		rc=1
31	else
32		echo " OK"
33	fi
34	rm -f astyle.log
35else
36	echo "You do not have astyle installed so your code style is not being checked!"
37fi
38
39echo -n "Checking blank lines at end of file..."
40
41if ! git grep -I -l -e . -z | \
42	xargs -0 -P8 -n1 scripts/eofnl > eofnl.log; then
43	echo " Incorrect end-of-file formatting detected"
44	cat eofnl.log
45	rc=1
46else
47	echo " OK"
48fi
49rm -f eofnl.log
50
51if hash pep8; then
52	echo -n "Checking Python style..."
53
54	PEP8_ARGS+=" --ignore=E302" # ignore 'E302 expected 2 blank lines, found 1'
55	PEP8_ARGS+=" --max-line-length=140"
56
57	error=0
58	git ls-files '*.py' | xargs -n1 pep8 $PEP8_ARGS > pep8.log || error=1
59	if [ $error -ne 0 ]; then
60		echo " Python formatting errors detected"
61		cat pep8.log
62		rc=1
63	else
64		echo " OK"
65	fi
66	rm -f pep8.log
67fi
68
69exit $rc
70