From 430d6fa0fe83eae44e9860d065b6d03533aa7ac4 Mon Sep 17 00:00:00 2001 From: Ron Stone Date: Thu, 30 Nov 2023 14:03:40 +0000 Subject: [PATCH] Add duplicate abbr check (r8, r7, r6, r5) Add a check for duplicate :abbr: anchor strings to prebuild phase and error on duplicates. Change-Id: I034c321a3c83aa4c0cbcf551fd90161e9e221c18 Signed-off-by: Ron Stone --- _utils.sh | 63 +++++++++++++++++++++++++++++++++++++++++++++-- dup-abbr-check.sh | 24 ++++++++++++++++++ tox.ini | 1 + 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100755 dup-abbr-check.sh diff --git a/_utils.sh b/_utils.sh index 5ad1d3bde..3b90c6cce 100644 --- a/_utils.sh +++ b/_utils.sh @@ -1,13 +1,14 @@ declare RED='\033[0;31m' +declare OG="\033[93m" 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; } +warn () { message $OG$@$NC; } +error () { message $RED$0:$?: $@$NC; exit 1; } # Check for and exit if file dependancies are not met. Takes a list of full or # relative paths. @@ -52,4 +53,62 @@ trimspaces () { echo $_s } +# Sets a global hash of acronyms and definitions from rst :abbr: defs. Also +# sets an array of hash keys to facilitate sorting. +# +# Takes path to the file to parse. Optional "1" flag as second option +# suppresses plural forms such as "PVCs". +get_abbrs () { + + local ABBREVS + declare -a -g acro_keys + declare -A -g acro_keyvals + local regex=":abbr:\`([A-Za-z]+)\s+\((.*)\)\`" + + [[ ! -z ${1+x} ]] && [[ -e $1 ]] && ABBREVS="$1" \ + || error "Can't find abbrevs file $1" + + [[ ! -z $2{+x} ]] && [[ ${2} == "1" ]] \ + && local strip_plurals=$2 + + while IFS= read -r line + do + if [[ $line =~ $regex ]]; then + + if [[ ${strip_plurals} -eq 1 ]] && [[ ${BASH_REMATCH[1]:0-1} == "s" ]]; then + message " Skipping pluralization \"${BASH_REMATCH[1]}\"" + continue + fi + acro_keys+=("${BASH_REMATCH[1]}") + acro_keyvals["${BASH_REMATCH[1]}"]="${BASH_REMATCH[2]}" + fi + done < "$ABBREVS" || error "Cannot read $ABBREVS" + +} + + +# Report duplicate :abbr: anchor strings. (Duplicate placeholders cause +# Sphinx warnings.) +# +# Takes an array of anchor strings. Echos duplicates and returns a duplicate +# count +check_abbr_dups () { + + local -a _anchors=("$@") + declare -a dups; declare -i _dup_count=0 + IFS=$'\n'; dups=($(sort -f <<<"${_anchors[*]}")); unset IFS + + message "... Checking for duplicate anchor strings" + + for ((i=0; i < ${#dups[@]}; i++)); do + if [[ ${dups[$i]} == ${dups[$i-1]} ]]; then + warn " Duplicate anchor string \"${dups[$i]}\" found" + ((_dup_count=$_dup_count+1)) + fi + done + + echo $_dup_count + +} + declare utils_loaded=1 \ No newline at end of file diff --git a/dup-abbr-check.sh b/dup-abbr-check.sh new file mode 100755 index 000000000..7c7aa0fb6 --- /dev/null +++ b/dup-abbr-check.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +# Read a file (typlically source/shared/abbrevs.txt) and report on duplicate +# entries + +. $(pwd)/_utils.sh +if [[ -z ${utils_loaded+x} ]]; then echo "Could not load utilities"; exit 1; fi + +declare ABBREVS="doc/source/shared/abbrevs.txt" +# declare ABBREVS=".stx-docs/doc/source/shared/abbrevs.txt" # for testing + +message "Checking for duplicate acronyms ..." + +# acro_keys: array of anchor strings +# acro_keyvals: hash of anchors/definitions +get_abbrs "${ABBREVS}" + +# Check for duplicate anchors in :abbr: defs. We've had problems with these +# in the past. +dup_count=$(check_abbr_dups "${acro_keys[@]}") + +[[ $dup_count -gt 0 ]] && error "Duplicate(s) MUST be fixed in $ABBREVS" + +confirmation "... Done" \ No newline at end of file diff --git a/tox.ini b/tox.ini index 5d749b42c..a0d412724 100644 --- a/tox.ini +++ b/tox.ini @@ -21,6 +21,7 @@ commands = python parser.py -l templates/alarms_template.rst -e tmp/events.yaml -s 100,200,300,400,500,700,800,900 -ts = -type Alarm -outputPath doc/source/fault-mgmt/openstack/ -sort Yes -product openstack -replace "|,OR" python parser.py -l templates/logs_template.rst -e tmp/events.yaml -s 100,200,300,400,500,700,800,900 -ts = -type Log -outputPath doc/source/fault-mgmt/openstack/ -sort Yes -product openstack -replace "|,OR" bash ./normalize-includes.sh + bash ./dup-abbr-check.sh [testenv:postbuild-docs] commands =