From ba5cc1582281f171dfba5a2e467b9304693c2a4d Mon Sep 17 00:00:00 2001 From: Nicolas Bock Date: Fri, 3 Jun 2022 11:59:34 -0600 Subject: [PATCH] Add pre-commit hook script The pre-commit hook script needs to be copied locally to: $ cp scripts/pre-commit .git/hooks/pre-commit It will run the linter before committing. Since the linter takes a while to complete, it can be helpful to disable this check if desired. Adding the `--no-verify` command line option to `git commit` will do that: $ git commit --no-verify [options] Note that for reasons of reproducibility the linter runs inside a docker container. Therefore, the only pre-requisite for the hook is docker. Signed-off-by: Nicolas Bock --- build.sh | 22 +----- scripts/build_iris_offload_oneapi.sh | 4 +- scripts/build_lassen_xl_offload.sh | 6 +- scripts/build_mkl_gpu.sh | 3 - scripts/example_build.sh | 3 - scripts/indent.sh | 97 +++++++++++++++++++-------- scripts/magma_build.sh | 2 - scripts/pre-commit | 29 ++++++++ scripts/prepare-container-focal.sh | 62 ++++++++--------- scripts/prepare-container.sh | 66 +++++++++--------- scripts/run-local-docker-container.sh | 10 +-- 11 files changed, 173 insertions(+), 131 deletions(-) create mode 100755 scripts/pre-commit diff --git a/build.sh b/build.sh index e2ddd10d3..26a07469d 100755 --- a/build.sh +++ b/build.sh @@ -34,7 +34,7 @@ compile - Compile the sources install - Install the compiled sources testing - Run the test suite docs - Generate the API documentation -indent - Indent the sources +indent - Check the indentation of the sources check_indent - Check the indentation of the sources tags - Create tags file for vim and emacs dist - Generate a tar file (this only works with git) @@ -277,28 +277,10 @@ testing() { cd "${TOP_DIR}" } -indent() { - cd "${BUILD_DIR}" - "${TOP_DIR}/scripts/indent.sh" 2>&1 | tee --append "${LOG_FILE}" - check_pipe_error -} - check_indent() { cd "${TOP_DIR}" - if which bashate; then - git ls-files '*.sh' \ - | xargs --no-run-if-empty --verbose --max-args 1 bashate \ - 2>&1 | tee --append "${LOG_FILE}" - fi "${TOP_DIR}/scripts/indent.sh" 2>&1 | tee --append "${LOG_FILE}" check_pipe_error - git diff 2>&1 | tee --append "${LOG_FILE}" - check_pipe_error - LINES=$(git diff | wc -l) - if test ${LINES} -gt 0; then - echo "sources were not formatted correctly" - die - fi } tags() { @@ -374,7 +356,7 @@ if [[ $# -gt 0 ]]; then docs ;; "indent") - indent + check_indent ;; "check_indent") check_indent diff --git a/scripts/build_iris_offload_oneapi.sh b/scripts/build_iris_offload_oneapi.sh index e4a31ae0d..007d312a6 100644 --- a/scripts/build_iris_offload_oneapi.sh +++ b/scripts/build_iris_offload_oneapi.sh @@ -3,11 +3,11 @@ # Make sure all the paths are correct if [ -e build ];then - rm -r build + rm -r build fi if [ -e install ];then - rm -r install + rm -r install fi MY_PATH=$(pwd) diff --git a/scripts/build_lassen_xl_offload.sh b/scripts/build_lassen_xl_offload.sh index 78e3b9660..370d5caf0 100755 --- a/scripts/build_lassen_xl_offload.sh +++ b/scripts/build_lassen_xl_offload.sh @@ -28,10 +28,6 @@ export BLAS_LIBRARIES=${BLAS_LIBRARIES:="-L${LAPACK_DIR} -llapack -lblas"} ./build.sh configure pushd build -make -j +make -j make install popd - - - - diff --git a/scripts/build_mkl_gpu.sh b/scripts/build_mkl_gpu.sh index fc77c7b53..77b37219d 100755 --- a/scripts/build_mkl_gpu.sh +++ b/scripts/build_mkl_gpu.sh @@ -35,8 +35,6 @@ export CMAKE_C_FLAGS="-O3 -fiopenmp -fopenmp-targets=spir64 -D__STRICT_ANSI__ -D export EXTRA_LINK_FLAGS="-L/soft/restricted/CNDA/sdk/2021.04.30.001/oneapi/mkl/2021u3_20210525/lib/intel64 -lmkl_lapack95_lp64 -lmkl_intel_lp64 -lmkl_core -lmkl_intel_thread -lpthread -lm -lmkl_intel_ilp64 -lmkl_sycl -lstdc++ -Wl,-rpath,/soft/restricted/CNDA/sdk/2021.04.30.001/oneapi/kokkos/20210323-3.1/../../compiler/latest/linux/compiler/lib/intel64 -L/soft/restricted/CNDA/sdk/2021.04.30.001/oneapi/kokkos/20210323-3.1/../../compiler/latest/linux/compiler/lib/intel64 -liomp5 -lsycl -lOpenCL -lm -lpthread -ldl" - - ./build.sh configure #./build.sh install cd build @@ -46,4 +44,3 @@ make install cd ../install ln -s lib64 lib - diff --git a/scripts/example_build.sh b/scripts/example_build.sh index 8c7f8e094..5cd271bd7 100755 --- a/scripts/example_build.sh +++ b/scripts/example_build.sh @@ -19,6 +19,3 @@ export EXTRA_CFLAGS=${EXTRA_CFLAGS:=""} export EXTRA_LINK_FLAGS=${EXTRA_LINK_FLAGS:=""} ./build.sh configure - - - diff --git a/scripts/indent.sh b/scripts/indent.sh index 49b5b79aa..0205d4051 100755 --- a/scripts/indent.sh +++ b/scripts/indent.sh @@ -1,49 +1,90 @@ #!/bin/bash -set -x - : ${EMACS:=$(command -v emacs)} : ${INDENT_ARGS:="-gnu -nut -i4 -bli0 -cli4 -ppi0 -cbi0 -npcs -bfda"} +declare -a SH_FILES declare -a C_FILES declare -a FORTRAN_FILES if ! git status > /dev/null; then - cat < 0 )); then - for file in "$@"; do - case "${file##*.}" in - "c" | "h") - C_FILES[${#C_FILES[@]}]="$file" - ;; - "F90") - FORTRAN_FILES[${#FORTRAN_FILES[@]}]="$file" - ;; - *) - echo "unknown suffix" - ;; - esac - done + while (( $# > 0 )); do + case "${1##*.}" in + "c" | "h") + C_FILES[${#C_FILES[@]}]="$1" + ;; + "F90") + FORTRAN_FILES[${#FORTRAN_FILES[@]}]="$1" + ;; + "sh") + SH_FILES[${#SH_FILES[@]}]="$1" + ;; + *) + echo "unknown suffix" + ;; + esac + done else - readarray -t C_FILES < <(git ls-files -- '*.c' '*.h') - readarray -t FORTRAN_FILES < <(git ls-files -- '*.F90') + readarray -t SH_FILES < <(git ls-files -- '*.sh') + readarray -t C_FILES < <(git ls-files -- '*.c' '*.h') + readarray -t FORTRAN_FILES < <(git ls-files -- '*.F90') fi -for f in "${C_FILES[@]}"; do - sed -i -e 's:\s\+$::' "${f}" - indent ${INDENT_ARGS} "${f}" +success=0 +declare -a FAILED_FILES=() + +for file in "${SH_FILES[@]}"; do + if ! bashate --ignore E006 "${file}"; then + success=1 + FAILED_FILES[${#FAILED_FILES[@]}]=${file} + fi +done + +for file in "${C_FILES[@]}"; do + indent ${INDENT_ARGS} "${file}" -o "${file}.indented" + sed -i -e 's:\s\+$::' "${file}.indented" + if (( $(diff --brief "${file}" "${file}.indented" | wc -l) > 0 )); then + success=1 + FAILED_FILES[${#FAILED_FILES[@]}]=${file} + else + rm -f "${file}.indented" + fi done -for f in "${FORTRAN_FILES[@]}"; do - ${EMACS} --batch \ - "${f}" \ - --eval "(whitespace-cleanup)" \ - --eval "(indent-region (minibuffer-prompt-end) (point-max) nil)" \ - -f save-buffer +for file in "${FORTRAN_FILES[@]}"; do + ${EMACS} --batch \ + "${file}" \ + --eval "(whitespace-cleanup)" \ + --eval "(indent-region (minibuffer-prompt-end) (point-max) nil)" \ + --eval "(write-file (concat (buffer-name) \".indented\"))" + if (( $(diff --brief "${file}" "${file}.indented" | wc -l) > 0 )); then + success=1 + FAILED_FILES[${#FAILED_FILES[@]}]=${file} + else + rm -f "${file}.indented" + fi done + +if (( ${#FAILED_FILES[@]} > 0 )); then + echo + echo "Please inspect the following files for linting issues:" + echo + for file in "${FAILED_FILES[@]}"; do + if [[ ${file} =~ .sh$ ]]; then + echo "bashate ${file}" + else + echo "diff -Naur ${file} ${file}.indented" + fi + done + echo +fi + +exit ${success} diff --git a/scripts/magma_build.sh b/scripts/magma_build.sh index b3d7ae70a..a1f123540 100755 --- a/scripts/magma_build.sh +++ b/scripts/magma_build.sh @@ -19,5 +19,3 @@ export MAGMA_ROOT=$OLCF_MAGMA_ROOT ./build.sh testing #./build.sh install - - diff --git a/scripts/pre-commit b/scripts/pre-commit new file mode 100755 index 000000000..e01a02bf0 --- /dev/null +++ b/scripts/pre-commit @@ -0,0 +1,29 @@ +#!/bin/sh + +debug=0 + +while [ $# -gt 0 ]; do + case $1 in + --debug) + debug=1 + ;; + *) + # Ignoring + ;; + esac + shift +done + +if [ ${debug} -ne 0 ]; then + set -x +fi + +if ! ./scripts/run-local-docker-container.sh ./build.sh indent > ./indent.output 2>&1; then + cat < /etc/apt/trusted.gpg.d/toolchain.gpg < <(gpg --export 60C317803A41BA51845E371A1E9377A2BA9EF27F) cat < /etc/apt/trusted.gpg.d/emacs.gpg < <(gpg --export 873503A090750CDAEB0754D93FF0E01EEAAFC9CD) apt-key list for i in $(seq 5); do - ${SUDO} apt-get update && break + ${SUDO} apt-get update && break done ${SUDO} ln -fs /usr/share/zoneinfo/UTC /etc/localtime ${SUDO} apt-get install --assume-yes tzdata DEBIAN_FRONTEND=noninteractive ${SUDO} dpkg-reconfigure \ - --frontend noninteractive tzdata + --frontend noninteractive tzdata ${SUDO} apt-get install --assume-yes --no-install-recommends \ - apt-utils \ - build-essential \ - gfortran \ - bundler \ - cmake cmake-data \ - emacs27 \ - clang-9 llvm-9-dev libomp-9-dev \ - gcc-9 g++-9 gfortran-9 \ - gcc-10 g++-10 gfortran-10 \ - gcc-11 g++-11 gfortran-11 \ - git-core \ - indent \ - libblas-dev liblapack-dev \ - libscalapack-openmpi-dev \ - mpi-default-dev \ - openmpi-bin \ - openssh-client \ - python python3-pip python3-wheel python3-pkg-resources \ - sudo \ - texlive \ - valgrind \ - vim + apt-utils \ + build-essential \ + gfortran \ + bundler \ + cmake cmake-data \ + emacs27 \ + clang-9 llvm-9-dev libomp-9-dev \ + gcc-9 g++-9 gfortran-9 \ + gcc-10 g++-10 gfortran-10 \ + gcc-11 g++-11 gfortran-11 \ + git-core \ + indent \ + libblas-dev liblapack-dev \ + libscalapack-openmpi-dev \ + mpi-default-dev \ + openmpi-bin \ + openssh-client \ + python python3-pip python3-wheel python3-pkg-resources \ + sudo \ + texlive \ + valgrind \ + vim ${SUDO} pip install --system bashate diff --git a/scripts/prepare-container.sh b/scripts/prepare-container.sh index 0451d248f..8a4197ca0 100755 --- a/scripts/prepare-container.sh +++ b/scripts/prepare-container.sh @@ -5,14 +5,14 @@ set -e -u -x SUDO=$(which sudo || true) for i in $(seq 5); do - ${SUDO} apt-get update && break + ${SUDO} apt-get update && break done ${SUDO} apt-get install --assume-yes --no-install-recommends \ - apt-transport-https \ - ca-certificates \ - gnupg \ - wget + apt-transport-https \ + ca-certificates \ + gnupg \ + wget cat < /etc/apt/trusted.gpg.d/cmake.gpg" < <(gpg --export DBA92F17B25AD78F9F2D9F713DEC686D130FF5E4) cat < /etc/apt/trusted.gpg.d/toolchain.gpg" < <(gpg --export 60C317803A41BA51845E371A1E9377A2BA9EF27F) cat < /etc/apt/trusted.gpg.d/emacs.gpg" < <(gpg --export 873503A090750CDAEB0754D93FF0E01EEAAFC9CD) apt-key list for i in $(seq 5); do - ${SUDO} apt-get update && break + ${SUDO} apt-get update && break done ${SUDO} ln -fs /usr/share/zoneinfo/UTC /etc/localtime ${SUDO} apt-get install --assume-yes tzdata DEBIAN_FRONTEND=noninteractive ${SUDO} dpkg-reconfigure \ - --frontend noninteractive tzdata + --frontend noninteractive tzdata ${SUDO} apt-get install --assume-yes --no-install-recommends \ - apt-utils \ - build-essential \ - gfortran \ - bundler \ - cmake cmake-data \ - emacs27 \ - clang-11 llvm-11-dev libomp-11-dev \ - gcc-4.8 g++-4.8 gfortran-4.8 \ - gcc-9 g++-9 gfortran-9 \ - gcc-10 g++-10 gfortran-10 \ - gcc-11 g++-11 gfortran-11 \ - git-core \ - indent \ - libblas-dev liblapack-dev \ - libscalapack-openmpi-dev \ - mpi-default-dev \ - openmpi-bin \ - openssh-client \ - python python-pip python-wheel python-pkg-resources \ - sudo \ - texlive \ - valgrind \ - vim + apt-utils \ + build-essential \ + gfortran \ + bundler \ + cmake cmake-data \ + emacs27 \ + clang-11 llvm-11-dev libomp-11-dev \ + gcc-4.8 g++-4.8 gfortran-4.8 \ + gcc-9 g++-9 gfortran-9 \ + gcc-10 g++-10 gfortran-10 \ + gcc-11 g++-11 gfortran-11 \ + git-core \ + indent \ + libblas-dev liblapack-dev \ + libscalapack-openmpi-dev \ + mpi-default-dev \ + openmpi-bin \ + openssh-client \ + python python-pip python-wheel python-pkg-resources \ + sudo \ + texlive \ + valgrind \ + vim ${SUDO} pip install --system bashate diff --git a/scripts/run-local-docker-container.sh b/scripts/run-local-docker-container.sh index b4b410a9a..5c86648f3 100755 --- a/scripts/run-local-docker-container.sh +++ b/scripts/run-local-docker-container.sh @@ -3,7 +3,9 @@ : ${IMAGE:=nicolasbock/bml:latest} docker pull ${IMAGE} -docker run --interactive --tty --rm \ - --volume ${PWD}:/bml --workdir /bml \ - --user $(id --user):$(id --group) \ - ${IMAGE} +docker run --rm \ + $( (( $# == 0 )) && echo "--interactive --tty") \ + --volume "${PWD}":/bml \ + --workdir /bml \ + --user $(id --user):$(id --group) \ + ${IMAGE} "$@"