Bash Python Cpp Static Check

Bash/Python/CPP 代码格式化及静态分析工具

包括 shfmt、shellcheck、black 、pylint和cppcheck。

选取标准

工具安装脚本

function get_latest_release() {
    curl --silent "https://api.github.com/repos/$1/releases/latest" | grep -Po '"tag_name":[ ]*"\K.*?(?=")'
}
function install_cppcheck() {
    sudo apt update
    sudo apt install -y cppcheck
}
function install_check_and_format() {
    # python3
    if [ -f /usr/bin/python ]; then
        sudo rm -f /usr/bin/python
    fi
    if [ -f /usr/bin/python3 ] && [ ! -f /usr/bin/python ]; then
        sudo ln -s /usr/bin/python3 /usr/bin/python
    fi
    #black pylint
    pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/
    pip3 install -i https://mirrors.ustc.edu.cn/pypi/web/simple pip -U
    pip3 install black pylint lastversion
    #install_shell_check
    ARCH=$(uname -m)
    while [ -z "$shellcheck_version" ]; do
        shellcheck_version=$(get_latest_release "koalaman/shellcheck")
    done
    wget -O - https://github.com/koalaman/shellcheck/releases/download/"$shellcheck_version"/shellcheck-"$shellcheck_version".linux."$ARCH".tar.xz | tar -xJf -
    chmod +x shellcheck-"$shellcheck_version"/shellcheck || true
    sudo mv shellcheck-"$shellcheck_version"/shellcheck /usr/bin/ || true
    rm -rf shellcheck-"$shellcheck_version"
    # shfmt
    case $ARCH in
        x86_64)
            arch=amd64
            ;;
        aarch64)
            arch=arm
            ;;
        mips64)
            arch=mips64el
            ;;
        *) ;;
    esac
    while [ -z "$shfmt_version" ]; do
        shfmt_version=$(get_latest_release "mvdan/sh")
    done
    wget https://github.com/mvdan/sh/releases/download/"$shfmt_version"/shfmt_"$shfmt_version"_linux_"$arch"
    sleep 1
    chmod +x shfmt_"$shfmt_version"_linux_"$arch" || exit
    sudo mv shfmt_"$shfmt_version"_linux_"$arch" /usr/bin/shfmt || exit
}
install_check_and_format
install_cppcheck

Bash

shfmt:格式化 Shell 脚本 https://github.com/mvdan/sh

Shell 解析器、格式化以及解释器,支持 POSIX Shell、Bash 和 mksh。

alias shfmt="shfmt -i 4 -bn -ci -sr -kp -l -w -d"

shellcheck:Shell 脚本静态分析工具 https://github.com/koalaman/shellcheck

针对 bash/sh Shell 脚本给出警告和建议,它能指出语法问题、语义问题以及比较隐含的错误。

alias shellcheck="shellcheck -s bash -x"
# shellcheck disable=SC2046,SC2086

Python

black:格式化 Python 代码 https://github.com/psf/black

完全遵循 Python 官方推荐的 PEP8 格式规范,避免为格式进行无谓争论,省时省力。

pylint:Python 代码静态分析工具 https://www.pylint.org

支持代码标准、错误检测以及重构辅助,同时也能创建 UML 图形,且完全可定制。

markdown

Prettier

vscode markdown 格式化插件,支持 js,css,html 等。

markdownlint

vscode markdown 语法检查工具。

cppcheck

cppcheck --enable=all --xml-version=2 src/ 2> cppcheck.xml
mkdir -p report
cppcheck-htmlreport --file=cppcheck.xml --source-dir=src --report-dir=report

详细参考:https://wenbo1188.github.io/2017/07/23/cppcheck-manual-chinese/

VS使用步骤

确保在 代码提交之前 执行上述工具。

ShellCheck 常见可忽略错误

for pkg in ${DEFAULT_PUBLIC_PACKAGES}; do
        echo "${pkg}" | sudo tee -a "${CHROOT_PATH}"/root/packages.list.tmp
done
dpkg-qurey -W --showformat='${Package}\\t${Status}\\n'

文档信息