提交 8bd7d71c 编写于 作者: S storypku

Scripts: scripts/apollo_lint.sh ready for use inside/outside docker

上级 cb4166c3
#! /usr/bin/env bash
set -e
APOLLO_ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
source "${APOLLO_ROOT_DIR}/scripts/apollo.bashrc"
TEST_CMD="bazel test --distdir=${APOLLO_CACHE_DIR}"
TOP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
source "${TOP_DIR}/scripts/apollo.bashrc"
function check_pyflakes() {
: ${STAGE:=dev}
local pyflakes_cmd="$(command -v pyflakes)"
if [ -z "${pyflakes_cmd}" ]; then
sudo -H pip3 install pyflakes
function _cpp_lint_impl() {
bazel test --distdir="${APOLLO_CACHE_DIR}" --config=cpplint $@
}
function run_cpp_lint() {
pushd "${APOLLO_ROOT_DIR}" >/dev/null
local cpp_dirs="cyber"
if [[ "${STAGE}" == "dev" ]]; then
cpp_dirs="${cpp_dirs} modules"
fi
for prey in $(find ${cpp_dirs} \
-not \( -path "modules/drivers/gnss/third_party" -prune \) \
-name BUILD \
| xargs grep -l -E 'cc_library|cc_test|cc_binary|cuda_library' \
| xargs grep -L 'cpplint()' ); do
warning "unattended BUILD file found: ${prey}. Add cpplint() automatically."
sed -i '1i\load("//tools:cpplint.bzl", "cpplint")\n' "${prey}"
sed -i -e '$a\\ncpplint()' "${prey}"
local buidifier="$(command -v buildifier)"
if [ ! -z "${buidifier}" ]; then
${buidifier} -lint=fix "${prey}"
fi
done
popd >/dev/null
local targets="//cyber/..."
_cpp_lint_impl "${targets}"
if [[ "${STAGE}" == "dev" ]]; then
_cpp_lint_impl "$(bazel query //modules/... except //modules/tools/visualizer/...)"
fi
}
function check_shellcheck() {
function run_sh_lint() {
local shellcheck_cmd="$(command -v shellcheck)"
if [ -z "${shellcheck_cmd}" ]; then
sudo apt install shellcheck
if [ -z "${shellcheck_cmd}" ]; then
warning "Command 'shellcheck' not found. For Debian/Ubuntu systems," \
"please run the following command to install it: "
warning " sudo apt-get -y update"
warning " sudo apt-get -y install shellcheck"
exit 1
fi
local sh_dirs="cyber scripts docker tools"
if [[ "${STAGE}" == "dev" ]]; then
sh_dirs="modules third_party ${sh_dirs}"
fi
}
function run_cpp_lint() {
sh_dirs=$(printf "${APOLLO_ROOT_DIR}/%s " ${sh_dirs})
run find ${sh_dirs} -type f \( -name "*.sh" -or -name "*.bashrc" \) -exec \
shellcheck -x {} +
# Add cpplint rule to BUILD files that do not contain it.
for file in $(find cyber modules -name BUILD | \
grep -v gnss/third_party | grep -v modules/teleop/encoder/nvenc_sdk6 | \
xargs grep -l -E 'cc_library|cc_test|cc_binary' | xargs grep -L 'cpplint()')
do
sed -i '1i\load("//tools:cpplint.bzl", "cpplint")\n' $file
sed -i -e '$a\\ncpplint()' $file
for script in ${APOLLO_ROOT_DIR}/*.sh ; do
run shellcheck -x "${script}"
done
BUILD_TARGETS="`bazel query //modules/... except //modules/tools/visualizer/... union //cyber/...`"
${TEST_CMD} --config=cpplint -c dbg $BUILD_TARGETS
}
function run_bash_lint() {
function run_py_lint() {
local pyflakes_cmd="$(command -v pyflakes)"
if [ -z "${pyflakes_cmd}" ]; then
warning "Command pyflakes not found. You can install it manually via:"
warning " '[sudo -H] python3 -m pip install pyflakes'"
exit 1
fi
check_shellcheck
FILES=$(find "${APOLLO_ROOT_DIR}/cyber" -type f -name "*.sh" | grep -v ros)
echo "${FILES}" | xargs shellcheck
local py_dirs="cyber docker tools third_party"
if [[ "${STAGE}" == "dev" ]]; then
py_dirs="modules ${py_dirs}"
fi
py_dirs=$(printf "${APOLLO_ROOT_DIR}/%s " ${py_dirs})
run find ${py_dirs} -type f \( -name "*.py" \) -exec \
pyflakes {} \;
}
function run_python_lint() {
check_pyflakes
find ${APOLLO_ROOT_DIR}/modules ${APOLLO_ROOT_DIR}/cyber \
${APOLLO_ROOT_DIR}/docker \
${APOLLO_ROOT_DIR}/tools \
${APOLLO_ROOT_DIR}/third_party \
-type f -name "*.py" -exec pyflakes {} \;
function print_usage() {
info "Usage: $0 [Options]"
info "Options:"
info "${TAB}-h|--help Show this message and exit"
info "${TAB}py|python Lint Python code"
info "${TAB}sh|shell Lint Bash code"
info "${TAB}cpp Lint cpp code"
info "${TAB}all Lint all (C++/Python/Bash)"
info "${TAB}help Same as '--help'"
}
function run_lint() {
local cmd=$1
case $cmd in
python)
run_python_lint
if [ $? -eq 0 ]; then
success 'Python Lint passed!'
else
fail 'Python Lint failed!'
fi
;;
local cmd="$1"
case "${cmd}" in
py|python)
run_py_lint
;;
cpp)
run_cpp_lint
if [ $? -eq 0 ]; then
success 'CPP Lint passed!'
else
fail 'CPP Lint failed!'
fi
;;
shell)
run_bash_lint
if [ $? -eq 0 ]; then
success 'SHELL Lint passed!'
else
fail 'SHELL Lint failed!'
fi
;;
run_cpp_lint
;;
sh|shell)
run_sh_lint
;;
all)
run_cpp_lint
run_py_lint
run_sh_lint
;;
help)
print_usage
exit 0
;;
*)
run_python_lint
run_cpp_lint
run_bash_lint
;;
esac
print_usage
exit 1
esac
}
function main() {
run_lint "$@"
}
run_lint $@
main "$@"
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册