Skip to content

Commit

Permalink
Minor cleanup in shell scripts
Browse files Browse the repository at this point in the history
+ Fix comments, headers, quotes
+ Only run F90 line length checks on F90 sources.
  • Loading branch information
KineticTheory committed May 24, 2021
1 parent 2dd63a2 commit faea552
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
1 change: 1 addition & 0 deletions environment/git/pre-commit-cmake-format
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ do
cp -f "${file}" "${tmpfile1}"
debugprint "$CMF -i ${tmpfile1}"
$CMF -c "${SCRIPTPATH}/../../.cmake-format.py" -i "${tmpfile1}" &> /dev/null
printf "==> cmake-format -c .cmake-format.py -i %s\n" "$file_nameonly"
diff -u "${file}" "${tmpfile1}" | \
sed -e "1s|--- |--- a/|" -e "2s|+++ ${tmpfile1}|+++ b/${file}|" >> "$patch"
debugprint "rm $tmpfile1"
Expand Down
71 changes: 36 additions & 35 deletions tools/check_style.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
#!/bin/bash
# -*- Mode: sh -*-

# ------------------------------------------------------------------------------------------------ #
# File : tools/check_style.sh
# Date : Tuesday, May 31, 2016, 14:48 pm
# Author: Kelly Thompson <[email protected]>
# Note : Copyright (C) 2016-2021, Triad National Security, LLC., All rights are reserved.
#
# Runs various lint programs in the current directory and list locally modified files that are not
# compliant with the current coding standard (see .clang_format in the top level source directory.)
# - clang-format for C and C++ code.
# - emacs for Fortran90
# - cmake-format and cmake-tidy for CMake scripts.
# ------------------------------------------------------------------------------------------------ #

#--------------------------------------------------------------------------------------------------#
# Environment
Expand Down Expand Up @@ -48,9 +56,8 @@ print_use()
echo " "
echo "All arguments are optional."
echo " -f Show diff and fix files (when possible)."
echo -n " -t Run as a pre-commit check, print list of non-conformant "
echo "files and return"
echo " with exit code = 1 (implies -d)."
echo -n " -t Run as a pre-commit check, print list of non-conformant files and return with"
echo " exit code = 1 (implies -d)."
echo " "
}

Expand All @@ -68,14 +75,14 @@ fi
gcf=$(which "git-clang-format${cfver}")
cf=$(which "clang-format${cfver}")
# if not found, try to find applications w/o version postfix.
if ! [[ -f ${gcf} ]]; then
if ! [[ -f "${gcf}" ]]; then
gcf=$(which git-clang-format)
fi
if ! [[ -f ${cf} ]]; then
if ! [[ -f "${cf}" ]]; then
gcf=$(which clang-format)
fi
# if still not found, abort.
if [[ ! ${gcf} ]]; then
if [[ ! "${gcf}" ]]; then
echo "ERROR: git-clang-format${cfver} was not found in your PATH."
echo "pwd="
pwd
Expand All @@ -85,7 +92,7 @@ if [[ ! ${gcf} ]]; then
#else
# echo "Using $gcf --binary $cf"
fi
if [[ ! ${cf} ]]; then
if [[ ! "${cf}" ]]; then
echo "ERROR: clang-format${cfver} was not found in your PATH."
echo "pwd="
pwd
Expand All @@ -105,12 +112,12 @@ foundissues=0 # 0 == ok
#--------------------------------------------------------------------------------------------------#
# Command options
#--------------------------------------------------------------------------------------------------#

while getopts ":fht" opt; do
case $opt in
f) fix_mode=1 ;; # also modify code (as possible)
h) print_use; exit 0 ;;
t) fix_mode=0
;;
t) fix_mode=0 ;;
\?) echo "" ;echo "invalid option: -$OPTARG"; print_use; exit 1 ;;
:) echo "" ;echo "option -$OPTARG requires an argument."; print_use; exit 1 ;;
esac
Expand All @@ -119,6 +126,7 @@ done
#--------------------------------------------------------------------------------------------------#
# Test C++ code with git-clang-format
#--------------------------------------------------------------------------------------------------#

echo -ne "\n--------------------------------------------------------------------------------\n"
echo -ne "Checking modified C/C++ code for style conformance...\n\n"

Expand Down Expand Up @@ -160,7 +168,7 @@ fi
rm -f "${patchfile_c}"

# ------------------------------------------------------------------------------------------------ #
# List of modified files
# Liast of modified files
# - Used for cmake-format and Emacs F90 processing.
# ------------------------------------------------------------------------------------------------ #

Expand Down Expand Up @@ -312,17 +320,17 @@ if [[ $EMACS ]]; then
fi
fi

if [[ -x $EMACS ]]; then
if [[ -x "$EMACS" ]]; then

echo -ne "\n--------------------------------------------------------------------------------\n"
echo -e "Checking modified F90 code for style conformance (indentation)..\n"

patchfile_f90=$(mktemp /tmp/emf90.patch.XXXXXXXX)

# file types to parse.
FILE_EXTS=".f90 .F90"
# FILE_ENDINGS_INCLUDE="foobar"
# FILE_ENDINGS_EXLUCDE="_f.h _f77.h _f90.h"
FILE_EXTS=".f90 .F90 .f .F"
# FILE_ENDINGS_INCLUDE="CMakeLists.txt"
# FILE_ENDINGS_EXLCUDE=".cmake.in"

# Loop over all modified F90 files. Create one patch containing all changes to these files
for file in $modifiedfiles; do
Expand All @@ -334,7 +342,7 @@ if [[ -x $EMACS ]]; then
file_nameonly=$(basename "${file}")
tmpfile1="/tmp/f90-format-${file_nameonly}"
cp -f "${file}" "${tmpfile1}"
$EMACS -batch "${tmpfile1}" -l "${rscriptdir}/../environment/git/f90-format.el" \
"$EMACS" -batch "${tmpfile1}" -l "${rscriptdir}/../environment/git/f90-format.el" \
-f emacs-format-f90-sources &> /dev/null
# color output is possible if diff -version >= 3.4 with option `--color`
diff ${DIFFCOLOR} -u "${file}" "${tmpfile1}" | \
Expand All @@ -343,25 +351,21 @@ if [[ -x $EMACS ]]; then

done

unset FILE_EXTS
unset FILE_ENDINGS_INCLUDE
unset FILE_ENDINGS_EXCLUDE

# If the patch file is size 0, then no changes are needed.
if [[ -s "$patchfile_f90" ]]; then
foundissues=1
echo "FAIL: some F90 files do not conform to this project's style requirements:"
echo -n "FAIL: some F90 files do not conform to this project's style requirements:"
# Modify files, if requested
if [[ ${fix_mode} == 1 ]]; then
if [[ "${fix_mode}" == 1 ]]; then
echo -e " The following patch has been applied to your file.\n"
run "git apply $patchfile_f90"
cat "${patchfile_f90}"
cat "$patchfile_f90"
else
echo -e " run ${0##*/} with option -f to automatically apply this patch.\n"
cat "${patchfile_f90}"
echo -ne " run ${0##*/} with option -f to automatically apply this patch.\n"
cat "$patchfile_f90"
fi
else
echo "PASS: Changes to F90 sources conform to this project's style requirements."
echo -n "PASS: Changes to F90 sources conform to this project's style requirements."
fi
rm -f "${patchfile_f90}"

Expand All @@ -370,16 +374,12 @@ fi
#--------------------------------------------------------------------------------------------------#
# Check mode (Test F90 code line length with bash)
#--------------------------------------------------------------------------------------------------#
echo -e "\n--------------------------------------------------------------------------------"

echo -ne "\n--------------------------------------------------------------------------------"
echo -e "Checking modified F90 code for style conformance (line length)..\n"

tmpfile2=$(mktemp /tmp/f90-format-line-len.XXXXXXXX)

# file types to parse.
FILE_EXTS=".f90 .F90"
# FILE_ENDINGS_INCLUDE="foobar"
# FILE_ENDINGS_EXCLUDE="_f.h _f77.h _f90.h"

# Loop over all modified F90 files. Create one patch containing all changes to these files
for file in $modifiedfiles; do

Expand All @@ -388,6 +388,7 @@ for file in $modifiedfiles; do
if ! matches_extension "$file"; then continue; fi

header_printed=0
lineno=0

# shellcheck disable=SC2162
while read line; do
Expand Down Expand Up @@ -419,10 +420,10 @@ unset FILE_ENDINGS_EXCLUDE
# If there are issues, report them
if [[ $(wc -l < "${tmpfile2}") -gt 0 ]]; then
foundissues=1
echo -e "FAIL: some F90 files do not conform to this project's style requirements:\n"
cat "${tmpfile2}"
echo -ne "\nPlease reformat lines listed above to fit into 100 columns and attempt running"
echo -ne "\n${0##*/} again. These issues cannot be fixed with the -f option."
echo -ne "FAIL: some F90 files do not conform to this project's style requirements:\n"
cat "$tmpfile2"
echo -ne "\nPlease reformat lines listed above to fit into 80 columns and attempt running\n"
echo -ne "${0##*/} again. These issues cannot be fixed with the -f option."
fi

#--------------------------------------------------------------------------------------------------#
Expand Down
4 changes: 2 additions & 2 deletions tools/common.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/bash -l
# -*- Mode: sh -*-
#--------------------------------------------------------------------------------------------------#
# File : regression/sripts/common.sh
# File : tools/common.sh
# Date : Tuesday, May 31, 2016, 14:48 pm
# Author: Kelly Thompson
# Note : Copyright (C) 2016-2020, Triad National Security, LLC., All rights are reserved.
# Note : Copyright (C) 2016-2021, Triad National Security, LLC., All rights are reserved.
#
# Summary: Misc bash functions useful during development of code.
#--------------------------------------------------------------------------------------------------#
Expand Down

0 comments on commit faea552

Please sign in to comment.