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 <ronald.stone@windriver.com>
Change-Id: Ifd64e38fbe4324c7d6b4eccb725ef3d8367f6578
This commit is contained in:
Ron Stone 2023-07-26 17:51:49 +00:00
parent 8fda6e80c1
commit 090becd41e
6 changed files with 106 additions and 82 deletions

55
_utils.sh Normal file
View File

@ -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

View File

@ -1,22 +1,23 @@
#!/bin/bash #!/bin/bash
RED='\033[0;31m' . $(pwd)/_utils.sh
NC='\033[0m' # No Color if [[ -z ${utils_loaded+x} ]]; then echo "Could not load utilities"; exit 1; fi
declare -a dirtyFiles declare -a dirtyFiles
dirtyFiles=( $(git status --porcelain doc/source 2>/dev/null) ) 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 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[@]}; for file in ${dirtyFiles[@]};
do do
if [[ ${file} == "??" ]]; then continue; fi if [[ ${file} == "??" ]]; then continue; fi
echo -e "${RED}$file${NC}" if [[ ${file} == "M" ]]; then continue; fi
warn "$file"
done done
exit 1 exit 1
else else
echo "... OK" confirmation "... OK"
fi fi

View File

@ -4,6 +4,9 @@
if [ -z ${1+x} ]; then echo "Usage: ./htmlChecks.sh <htmlPath>" && exit 0; fi if [ -z ${1+x} ]; then echo "Usage: ./htmlChecks.sh <htmlPath>" && exit 0; fi
. $(pwd)/_utils.sh
if [[ -z ${utils_loaded+x} ]]; then echo "Could not load utilities"; exit 1; fi
htmlPath=$1 htmlPath=$1
RED='\033[0;31m' 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 ..." echo "Checking for \"grey bar\" formatting errors in output ..."
GREY_FILES=( $(grep -rl --include="*.html" "blockquote" .) ) GREY_FILES=( $(grep -rl --include="*.html" "blockquote" .) )
if [ ${#GREY_FILES[@]} != 0 ]; then 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[@]}; for FILE in ${GREY_FILES[@]};
do do
echo -e "${RED}$FILE${NC}" warn "$FILE"
done 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 error=1
fi fi
echo "Checking for \".. include::\" errors in output ..." echo "Checking for \".. include::\" errors in output ..."
INCLUDE_FILES=( $(grep -rl --include="*.html" -e "start-after" -e "end-before" .) ) INCLUDE_FILES=( $(grep -rl --include="*.html" -e "start-after" -e "end-before" .) )
if [ ${#INCLUDE_FILES[@]} != 0 ]; then 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[@]}; for FILE in ${INCLUDE_FILES[@]};
do do
echo -e "${RED}$FILE${NC}" warn "$FILE"
done 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 error=1
fi fi
echo "Checking for unexpanded substitution errors in output ..." echo "Checking for unexpanded substitution errors in output ..."
SUBS_FILES=( $(grep -rlo --include="*.html" --exclude="doc_contribute_guide.html" '[>\s]|\S\+|[<\s]' .) ) SUBS_FILES=( $(grep -rlo --include="*.html" --exclude="doc_contribute_guide.html" '[>\s]|\S\+|[<\s]' .) )
if [ ${#SUBS_FILES[@]} != 0 ]; then 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;}' 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 error=1
fi fi
@ -48,5 +51,5 @@ fi
if [[ $2 == "-W" ]] && [[ ${error} -eq 1 ]]; then if [[ $2 == "-W" ]] && [[ ${error} -eq 1 ]]; then
exit 1 exit 1
elif [[ ${error} -ne 1 ]]; then elif [[ ${error} -ne 1 ]]; then
echo "... OK" confirmation "... OK"
fi fi

View File

@ -1,9 +1,9 @@
#!/usr/bin/env bash #!/usr/bin/env bash
if ! hash uuidgen 2>/dev/null; then . $(pwd)/_utils.sh
echo >&2 "... uuidgen dependency not met. Please install." if [[ -z ${utils_loaded+x} ]]; then echo "Could not load utilities"; exit 1; fi
exit 1
fi check_util_deps uuidgen
INCLUDEDIR="$2/doc/source/_includes" INCLUDEDIR="$2/doc/source/_includes"
@ -18,14 +18,12 @@ charReplacements=(
ask_name () { ask_name () {
echo -e "`cat <<EOF message "`cat <<EOF
You are about to create a new reStructuredText file in You are about to create a new reStructuredText file in
${WD} ${WD}
or a content fragment file in doc/source/_includes
If this is not what you want, press CTL-C to quit and change to the directory If this is not what you want, press CTL-C to quit and change to the directory
you want to create the file in. you want to create the file in.
@ -36,7 +34,7 @@ ask_name () {
EOF`" EOF`"
while read -e -p 'Topic title: ' title ; do while read -e -p 'Topic title: ' title ; do
if [[ -z $title ]]; then if [[ -z $title ]]; then
continue continue
else else
@ -50,7 +48,7 @@ EOF`"
ask_type () { ask_type () {
echo -e "`cat <<EOF message "`cat <<EOF
Thanks. Now choose a topic type. Enter one of the following characters: Thanks. Now choose a topic type. Enter one of the following characters:
@ -62,7 +60,7 @@ ask_type () {
EOF`" EOF`"
while read -p 'Topic type: ' -n1 input; do while read -p 'Topic type: ' -n1 input; do
case $input in case $input in
@ -71,7 +69,7 @@ EOF`"
;; ;;
*) *)
echo -e "Enter a valid value" warn "\nEnter a valid value"
continue continue
;; ;;
@ -93,7 +91,7 @@ write_stub () {
echo "$1" > "${outdir}/${filename}.${ext}" echo "$1" > "${outdir}/${filename}.${ext}"
if [[ -f ${outdir}/${filename}.${ext} ]]; then if [[ -f ${outdir}/${filename}.${ext} ]]; then
echo -e "\nCreated ${outdir}/${filename}.${ext}" confirmation "\nCreated ${outdir}/${filename}.${ext}"
exit 0 exit 0
else else
exit 1 exit 1
@ -114,10 +112,7 @@ myuuid="${myuuid:24:35}"
ask_name ask_name
strike=$(for ((i=1; i<=${#title}; i++)); do strike=$(make_strike "${title}")
printf '=%.0s' "$i"
done)
ask_type ask_type

View File

@ -1,38 +1,18 @@
#!/usr/bin/env bash #!/usr/bin/env bash
. $(pwd)/_utils.sh
if [[ -z ${utils_loaded+x} ]]; then echo "Could not load utilities"; exit 1; fi
directive='pre-include::' directive='pre-include::'
d_begin=':start-after:' d_begin=':start-after:'
d_end=':end-before:' d_end=':end-before:'
inc_base='doc/source' inc_base='doc/source'
RED='\033[0;31m'
NC='\033[0m'
message () { echo -e "$@" 1>&2; }
error () { message $RED$@$NC; }
OIFS=$IFS; IFS=$'\n' OIFS=$IFS; IFS=$'\n'
parents=( $(grep -Rl '.. pre-include:: ' --exclude-dir=docs/build --include="*.r*st" --exclude-dir='.*' doc/*) ) parents=( $(grep -Rl '.. pre-include:: ' --exclude-dir=docs/build --include="*.r*st" --exclude-dir='.*' doc/*) )
IFS=$OIFS 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 () { get_substr () {
local _str=${1//\//\\/} local _str=${1//\//\\/}
@ -49,15 +29,6 @@ get_substr () {
fi fi
} }
trimspaces () {
local _s=$1
_s=${_s##*( )}
_s=${_s%%*( )}
echo $_s
}
get_inc_path () { get_inc_path () {
local _ppath=$1 local _ppath=$1
local _inc=$2 local _inc=$2

View File

@ -1,11 +1,10 @@
#!/usr/bin/env bash #!/usr/bin/env bash
RED='\033[0;31m' . $(pwd)/_utils.sh
GR='\033[0;32m' if [[ -z ${utils_loaded+x} ]]; then echo "Could not load utilities"; exit 1; fi
NC='\033[0m' # No color
today=$(date '+%Y-%m-%d') today=$(date '+%Y-%m-%d')
default_start="2020-01-01" default_start=$(date --date='-9 month' '+%Y-%m-%d')
config_file=".pickOptions.sh" config_file=".pickOptions.sh"
@ -37,7 +36,7 @@ get_branches () {
local refs="refs\/remotes" 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 for branch in br1 br2
do do
@ -51,7 +50,7 @@ get_branches () {
case $br in case $br in
"") "")
echo "Invalid entry" warn "Invalid entry"
continue continue
;; ;;
*) *)
@ -63,14 +62,14 @@ get_branches () {
done done
done done
if [[ "$br1" == "$br2" ]]; then 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 get_branches
fi fi
} }
get_dates () { get_dates () {
echo -e "Select a date range\n" message "Select a date range\n"
for date in begin end for date in begin end
do do
@ -94,7 +93,7 @@ get_dates () {
;; ;;
*) *)
if ! date -d $edate > /dev/null; then 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 continue
else else
declare -g ${date}=$edate declare -g ${date}=$edate
@ -109,7 +108,7 @@ get_dates () {
get_users () { get_users () {
echo -e "Select users\n" message "Select users\n"
for auth in auth1 auth2 for auth in auth1 auth2
do do
@ -120,7 +119,7 @@ get_users () {
repo=$br2 repo=$br2
fi 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") select os in $(git log --pretty=format:%an --after="$begin" --before="$end" $repo | sort | uniq; echo "None")
do do
@ -128,7 +127,7 @@ get_users () {
case $os in case $os in
None) None)
echo "No author selected, will show all authors." warn "No author selected, will show all authors."
declare -g ${auth}="" declare -g ${auth}=""
break break
;; ;;
@ -158,7 +157,7 @@ confirm_options () {
compare_branches () { 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 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 done
} }
@ -194,12 +193,12 @@ str="$str"
EOF`" > $config_file EOF`" > $config_file
echo " ... saved" confirmation " ... saved"
;; ;;
*) *)
echo " ... not saved" warn " ... not saved"
;; ;;
esac esac
@ -210,7 +209,7 @@ read_settings () {
if [[ -f ${config_file} ]]; then if [[ -f ${config_file} ]]; then
echo -e "\nFound saved options:\n" message "\nFound saved options:\n"
values=$(<$config_file) values=$(<$config_file)
@ -222,7 +221,7 @@ read_settings () {
values=${values/end=/"End Date: "} values=${values/end=/"End Date: "}
values=${values/str=/"Pick Search String: "} values=${values/str=/"Pick Search String: "}
echo -e "$values\n" message "$values\n"
read -p 'Reuse these options now? [y/n]: ' -n1 read_opts; read -p 'Reuse these options now? [y/n]: ' -n1 read_opts;
@ -232,10 +231,10 @@ read_settings () {
CONTEXT_DIR="${BASH_SOURCE%/*}" CONTEXT_DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$CONTEXT_DIR" ]]; then CONTEXT_DIR="$PWD"; fi if [[ ! -d "$CONTEXT_DIR" ]]; then CONTEXT_DIR="$PWD"; fi
. "$CONTEXT_DIR/$config_file" . "$CONTEXT_DIR/$config_file"
echo " ... read" confirmation " ... read"
;; ;;
*) *)
echo " ... not read" warn " ... not read"
;; ;;
esac esac