From 090becd41e6148b5d239de187251e633ade6b330 Mon Sep 17 00:00:00 2001 From: Ron Stone Date: Wed, 26 Jul 2023 17:51:49 +0000 Subject: [PATCH] Consolidate utils (r8,4 r7, r6, r5) Many bash functions are duplicated across scripts. This commit consolidates them on one file for more efficient managment. Some incidental updates were made: - set default start date in pickCompare to today-9 months instead of 2020-01-01. This reduces clutter when selecting authors and stale gerrits in output. - fix a routine used by normalize-includes Signed-off-by: Ron Stone Change-Id: Ifd64e38fbe4324c7d6b4eccb725ef3d8367f6578 --- _utils.sh | 55 +++++++++++++++++++++++++++++++++++++++++++ dirtyCheck.sh | 13 +++++----- htmlChecks.sh | 21 ++++++++++------- new-topic.sh | 27 +++++++++------------ normalize-includes.sh | 35 +++------------------------ pickCompare.sh | 37 ++++++++++++++--------------- 6 files changed, 106 insertions(+), 82 deletions(-) create mode 100644 _utils.sh diff --git a/_utils.sh b/_utils.sh new file mode 100644 index 000000000..5ad1d3bde --- /dev/null +++ b/_utils.sh @@ -0,0 +1,55 @@ + +declare RED='\033[0;31m' +declare GR='\033[0;32m' +declare NC='\033[0m' + +# Output functions. Colorize various types of messages. +message () { echo -e "$@" 1>&2; } +confirmation () { message $GR$@$NC; } +warn () { message $RED$@$NC; } +error () { message $RED$@$NC; exit 1; } + +# Check for and exit if file dependancies are not met. Takes a list of full or +# relative paths. +check_file_deps () { + for filereq in $@ + do + if [ ! -f "${filereq}" ] && [ ! -L "${filereq}" ]; then error "${filereq} not found. Quiting."; exit 1; fi + done +} + +# Check for and exit if command dependancies are not met. Takes a list of +# executables. +check_util_deps () { + for dep in $@ + do + if ! hash $dep 2>/dev/null; then + error >&2 "... $dep dependency not met. Please install." + exit 1 + fi + done +} + +# Creates an rST title over/underscore string of the same length +# as the argument. Section strings are not supported. Returned output +# is a sequence of equal signs (=). +make_strike () { + local _title="$1" + local _strike + _strike=$(for ((i=1; i<=${#_title}; i++)); do + printf '=%.0s' "$i" + done) + echo $_strike +} + +# Trim leading and trailing whitespaces from string. +trimspaces () { + local _s=$1 + + _s="${_s#"${_s%%[![:space:]]*}"}" + _s="${_s#"${_s%%[![:space:]]*}"}" + + echo $_s +} + +declare utils_loaded=1 \ No newline at end of file diff --git a/dirtyCheck.sh b/dirtyCheck.sh index 334a27930..d35c93e59 100755 --- a/dirtyCheck.sh +++ b/dirtyCheck.sh @@ -1,22 +1,23 @@ #!/bin/bash -RED='\033[0;31m' -NC='\033[0m' # No Color +. $(pwd)/_utils.sh +if [[ -z ${utils_loaded+x} ]]; then echo "Could not load utilities"; exit 1; fi declare -a dirtyFiles dirtyFiles=( $(git status --porcelain doc/source 2>/dev/null) ) -echo "Checking status of doc/source" +message "Checking status of doc/source" if [ ${#dirtyFiles[@]} -ne 0 ]; then - echo -e "${RED}Repo is dirty. Please stash, add or manually delete the following files:${NC}" + warn "Repo is dirty. Please stash, add or manually delete the following files:\n" for file in ${dirtyFiles[@]}; do if [[ ${file} == "??" ]]; then continue; fi - echo -e "${RED}$file${NC}" + if [[ ${file} == "M" ]]; then continue; fi + warn "$file" done exit 1 else - echo "... OK" + confirmation "... OK" fi \ No newline at end of file diff --git a/htmlChecks.sh b/htmlChecks.sh index a15de00f0..815c4f6a5 100755 --- a/htmlChecks.sh +++ b/htmlChecks.sh @@ -4,6 +4,9 @@ if [ -z ${1+x} ]; then echo "Usage: ./htmlChecks.sh " && exit 0; fi +. $(pwd)/_utils.sh +if [[ -z ${utils_loaded+x} ]]; then echo "Could not load utilities"; exit 1; fi + htmlPath=$1 RED='\033[0;31m' @@ -14,33 +17,33 @@ cd ${htmlPath} || { echo "Can't change to ${htmlPath}" && exit 0; } echo "Checking for \"grey bar\" formatting errors in output ..." GREY_FILES=( $(grep -rl --include="*.html" "blockquote" .) ) if [ ${#GREY_FILES[@]} != 0 ]; then - echo "Found ${#GREY_FILES[@]} HTML file(s) with greybar formatting issues:" + warn "Found ${#GREY_FILES[@]} HTML file(s) with greybar formatting issues:" for FILE in ${GREY_FILES[@]}; do - echo -e "${RED}$FILE${NC}" + warn "$FILE" done - echo "Using a browser, locate vertical grey bars in the left margin of the above file(s), then correct the issue(s) in the corresponding rST file(s)." + warn "Using a browser, locate vertical grey bars in the left margin of the above file(s), then correct the issue(s) in the corresponding rST file(s)." error=1 fi echo "Checking for \".. include::\" errors in output ..." INCLUDE_FILES=( $(grep -rl --include="*.html" -e "start-after" -e "end-before" .) ) if [ ${#INCLUDE_FILES[@]} != 0 ]; then - echo "Found ${#INCLUDE_FILES[@]} HTML file(s) with exposed \"start-after\" and \"end-before\" _include argument(s):" + warn "Found ${#INCLUDE_FILES[@]} HTML file(s) with exposed \"start-after\" and \"end-before\" _include argument(s):" for FILE in ${INCLUDE_FILES[@]}; do - echo -e "${RED}$FILE${NC}" + warn "$FILE" done - echo "Correct the issue(s) in the corresponding rST file(s)." + warn "Correct the issue(s) in the corresponding rST file(s)." error=1 fi echo "Checking for unexpanded substitution errors in output ..." SUBS_FILES=( $(grep -rlo --include="*.html" --exclude="doc_contribute_guide.html" '[>\s]|\S\+|[<\s]' .) ) if [ ${#SUBS_FILES[@]} != 0 ]; then - echo -e "Found ${#SUBS_FILES[@]} HTML file(s) that may have unexpanded substitution(s):\n${RED}" + warn "Found ${#SUBS_FILES[@]} HTML file(s) that may have unexpanded substitution(s):\n${RED}" grep -ro --include="*.html" --exclude="doc_contribute_guide.html" '[>\s]|\S\+|[<\s]' . | awk -F: '{if(f!=$1)print ""; f=$1; print $0;}' - echo -e "${NC}\nCorrect the issue(s) in the corresponding rST file(s).\nHINT: Substitions are not allowed in code blocks, :ref:s,\n:doc:s, or with in rST markup such as **, \`\`, and so on." + warn "\nCorrect the issue(s) in the corresponding rST file(s).\nHINT: Substitions are not allowed in code blocks, :ref:s,\n:doc:s, or with in rST markup such as **, \`\`, and so on." error=1 fi @@ -48,5 +51,5 @@ fi if [[ $2 == "-W" ]] && [[ ${error} -eq 1 ]]; then exit 1 elif [[ ${error} -ne 1 ]]; then - echo "... OK" + confirmation "... OK" fi diff --git a/new-topic.sh b/new-topic.sh index 1de95e478..7f89ef2af 100644 --- a/new-topic.sh +++ b/new-topic.sh @@ -1,9 +1,9 @@ #!/usr/bin/env bash -if ! hash uuidgen 2>/dev/null; then - echo >&2 "... uuidgen dependency not met. Please install." - exit 1 -fi +. $(pwd)/_utils.sh +if [[ -z ${utils_loaded+x} ]]; then echo "Could not load utilities"; exit 1; fi + +check_util_deps uuidgen INCLUDEDIR="$2/doc/source/_includes" @@ -18,14 +18,12 @@ charReplacements=( ask_name () { - echo -e "`cat < "${outdir}/${filename}.${ext}" if [[ -f ${outdir}/${filename}.${ext} ]]; then - echo -e "\nCreated ${outdir}/${filename}.${ext}" + confirmation "\nCreated ${outdir}/${filename}.${ext}" exit 0 else exit 1 @@ -114,10 +112,7 @@ myuuid="${myuuid:24:35}" ask_name -strike=$(for ((i=1; i<=${#title}; i++)); do - printf '=%.0s' "$i" -done) - +strike=$(make_strike "${title}") ask_type diff --git a/normalize-includes.sh b/normalize-includes.sh index 361bd1882..40849b704 100755 --- a/normalize-includes.sh +++ b/normalize-includes.sh @@ -1,38 +1,18 @@ #!/usr/bin/env bash +. $(pwd)/_utils.sh +if [[ -z ${utils_loaded+x} ]]; then echo "Could not load utilities"; exit 1; fi + directive='pre-include::' d_begin=':start-after:' d_end=':end-before:' inc_base='doc/source' -RED='\033[0;31m' -NC='\033[0m' - -message () { echo -e "$@" 1>&2; } - -error () { message $RED$@$NC; } OIFS=$IFS; IFS=$'\n' parents=( $(grep -Rl '.. pre-include:: ' --exclude-dir=docs/build --include="*.r*st" --exclude-dir='.*' doc/*) ) IFS=$OIFS -check_file_deps () { - for filereq in $@ - do - if [ ! -f "${filereq}" ] && [ ! -L "${filereq}" ]; then error "${filereq} not found. Quiting."; exit 1; fi - done -} - -check_util_deps () { - for dep in $@ - do - if ! hash $dep 2>/dev/null; then - error >&2 "... $dep dependency not met. Please install." - exit 1 - fi - done -} - get_substr () { local _str=${1//\//\\/} @@ -49,15 +29,6 @@ get_substr () { fi } -trimspaces () { - local _s=$1 - - _s=${_s##*( )} - _s=${_s%%*( )} - - echo $_s -} - get_inc_path () { local _ppath=$1 local _inc=$2 diff --git a/pickCompare.sh b/pickCompare.sh index 37ddea336..063439c33 100755 --- a/pickCompare.sh +++ b/pickCompare.sh @@ -1,11 +1,10 @@ #!/usr/bin/env bash -RED='\033[0;31m' -GR='\033[0;32m' -NC='\033[0m' # No color +. $(pwd)/_utils.sh +if [[ -z ${utils_loaded+x} ]]; then echo "Could not load utilities"; exit 1; fi today=$(date '+%Y-%m-%d') -default_start="2020-01-01" +default_start=$(date --date='-9 month' '+%Y-%m-%d') config_file=".pickOptions.sh" @@ -37,7 +36,7 @@ get_branches () { local refs="refs\/remotes" - echo -e "Select the two branches you want to compare:\n" + message "Select the two branches you want to compare:\n" for branch in br1 br2 do @@ -51,7 +50,7 @@ get_branches () { case $br in "") - echo "Invalid entry" + warn "Invalid entry" continue ;; *) @@ -63,14 +62,14 @@ get_branches () { done done if [[ "$br1" == "$br2" ]]; then - echo -e "\n${RED}Comparing $br1 with itself makes no sense. Please pick two branches.${NC}\n" + warn "Comparing $br1 with itself makes no sense. Please pick two branches.\n" get_branches fi } get_dates () { - echo -e "Select a date range\n" + message "Select a date range\n" for date in begin end do @@ -94,7 +93,7 @@ get_dates () { ;; *) if ! date -d $edate > /dev/null; then - echo -e "${RED}$edate is not valid. Try again.${NC}" + warn "$edate is not valid. Try again." continue else declare -g ${date}=$edate @@ -109,7 +108,7 @@ get_dates () { get_users () { - echo -e "Select users\n" + message "Select users\n" for auth in auth1 auth2 do @@ -120,7 +119,7 @@ get_users () { repo=$br2 fi - echo -e "Optionally, select a ${GR}$repo${NC} author to filter by:\n" + message "Optionally, select a ${GR}$repo${NC} author to filter by:\n" select os in $(git log --pretty=format:%an --after="$begin" --before="$end" $repo | sort | uniq; echo "None") do @@ -128,7 +127,7 @@ get_users () { case $os in None) - echo "No author selected, will show all authors." + warn "No author selected, will show all authors." declare -g ${auth}="" break ;; @@ -158,7 +157,7 @@ confirm_options () { compare_branches () { for pick in $({ git log --pretty=format:%s%n --after="$begin" --before="$end" --author="$auth1" $br1 & git log --pretty=format:%s%n --after="$begin" --before="$end" --author="$auth2" $br2; } | grep "(.*$str.*)$" | sort | uniq -u); do - echo -e "${RED}" $(git log --grep=$pick --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:"%cd, %s [ %h ]" --after=$begin --before=$end --author=$auth1 --author=$auth2 $br1 $br2) "${NC}" + confirmation $(git log --grep=$pick --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:"%cd, %s [ %h ]" --after=$begin --before=$end --author=$auth1 --author=$auth2 $br1 $br2) "${NC}" done } @@ -194,12 +193,12 @@ str="$str" EOF`" > $config_file - echo " ... saved" + confirmation " ... saved" ;; *) - echo " ... not saved" + warn " ... not saved" ;; esac @@ -210,7 +209,7 @@ read_settings () { if [[ -f ${config_file} ]]; then - echo -e "\nFound saved options:\n" + message "\nFound saved options:\n" values=$(<$config_file) @@ -222,7 +221,7 @@ read_settings () { values=${values/end=/"End Date: "} values=${values/str=/"Pick Search String: "} - echo -e "$values\n" + message "$values\n" read -p 'Reuse these options now? [y/n]: ' -n1 read_opts; @@ -232,10 +231,10 @@ read_settings () { CONTEXT_DIR="${BASH_SOURCE%/*}" if [[ ! -d "$CONTEXT_DIR" ]]; then CONTEXT_DIR="$PWD"; fi . "$CONTEXT_DIR/$config_file" - echo " ... read" + confirmation " ... read" ;; *) - echo " ... not read" + warn " ... not read" ;; esac