From 5c66aaa1f229e75a256c58b0751ce8ae9c82aa86 Mon Sep 17 00:00:00 2001 From: Landon Curt Noll Date: Tue, 13 Aug 2024 22:55:41 -0700 Subject: [PATCH] use more bin/jval-wrapper.sh instead of direct grep of JSON files We use the `bin/jval-wrapper.sh` tool to obtain information from JSON files instead of attempting using a "GROSS HACK" that involved using `grep(1)` (and friends) as those "GROSS HACK"s assumed that the layout of the JSON was canonical. The `bin/jval-wrapper.sh` tool is able to deal with various layouts of JSON including non-canonical layouts. Moreover, by using the `bin/jval-wrapper.sh` tool we greatly reduce the places in bin tools that contain "GROSS HACK"s. Adjusted `bin/subst.entry-index.sh` to use `bin/jval-wrapper.sh`. Changed `bin/subst.entry-index.sh` VERSION from "1.4 2024-08-09" to "1.5 2024-08-13". Adjusted `bin/entry2csv.sh` to use `bin/jval-wrapper.sh`. Changed `bin/entry2csv.sh` VERSION from "1.1.1 2024-07-22" to "1.2 2024-08-13". Performed `make entry2csv` to test the above under macOS. Adjusted `bin/csv2entry.sh` to use `bin/jval-wrapper.sh`. Changed `bin/entry2csv.sh` VERSION from "1.1.1 2024-07-22" to "1.2 2024-08-13". Performed `make csv2entry` to test the above under macOS. --- bin/csv2entry.sh | 42 ++++++++++++++++------------- bin/entry2csv.sh | 58 ++++++++++------------------------------ bin/subst.entry-index.sh | 19 +++++++------ 3 files changed, 49 insertions(+), 70 deletions(-) diff --git a/bin/csv2entry.sh b/bin/csv2entry.sh index 979d6d023..d4abfb080 100755 --- a/bin/csv2entry.sh +++ b/bin/csv2entry.sh @@ -142,7 +142,7 @@ shopt -s lastpipe # explicitly run the last command of the pipe line in the curr # set variables referenced in the usage message # -export VERSION="1.1.1 2024-07-22" +export VERSION="1.2 2024-08-13" NAME=$(basename "$0") export NAME export V_FLAG=0 @@ -479,6 +479,25 @@ if [[ ! -s $TOP_FILE ]]; then fi +# verify that we have a bin subdirectory +# +export BIN_PATH="$TOPDIR/bin" +if [[ ! -d $BIN_PATH ]]; then + echo "$0: ERROR: bin is not a directory under topdir: $BIN_PATH" 1>&2 + exit 6 +fi +export BIN_DIR="bin" + + +# find the jval-wrapper.sh tool +# +JVAL_WRAPPER="$BIN_PATH/jval-wrapper.sh" +if [[ ! -x $JVAL_WRAPPER ]]; then + echo "$0: ERROR: cannot find the bin/jval-wrapper.sh executable" 1>&2 + exit 5 +fi + + # verify jparse tool # if [[ -z "$JPARSE_TOOL" ]]; then @@ -517,6 +536,9 @@ if [[ $V_FLAG -ge 3 ]]; then echo "$0: debug[3]: YEAR_PRIZE_CSV_DIR=$YEAR_PRIZE_CSV_DIR" 1>&2 echo "$0: debug[3]: CD_FAILED=$CD_FAILED" 1>&2 echo "$0: debug[3]: TOP_FILE=$TOP_FILE" 1>&2 + echo "$0: debug[3]: BIN_PATH=$BIN_DIR" 1>&2 + echo "$0: debug[3]: BIN_DIR=$BIN_DIR" 1>&2 + echo "$0: debug[3]: JVAL_WRAPPER=$JVAL_WRAPPER" 1>&2 echo "$0: debug[3]: DOT_ENTRY_JSON_BASENAME=$DOT_ENTRY_JSON_BASENAME" 1>&2 echo "$0: debug[3]: ENTRY_JSON_FORMAT_VERSION=$ENTRY_JSON_FORMAT_VERSION" 1>&2 echo "$0: debug[3]: NO_COMMENT=$NO_COMMENT" 1>&2 @@ -787,15 +809,6 @@ fi # author handles w/o author/author_handle.json file and # author/author_handle.json file that do not reference the entry id # -# XXX - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - XXX -# XXX - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - XXX -# XXX - until we have the jnamval command, we must FAKE PARSE the .entry.json file - XXX -# XXX - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - XXX -# XXX - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - XXX -# -export PATTERN_BEFORE_ENTRY_ID='^[[:space:]]*{[[:space:]]*"entry_id"[[:space:]]*:[[:space:]]*"' -export PATTERN_AFTER_ENTRY_ID='"[[:space:]]*},?[[:space:]]*$' -# if [[ $V_FLAG -ge 1 ]]; then echo "$0: debug[1]: about to: scan for unknown author handles" 1>&2 fi @@ -873,18 +886,11 @@ sed -e 's/,/ /g' "$AUTHOR_WINS_CSV" | while read -r AUTHOR_HANDLE ENTRY_ID_SET; # verify that the author/author_handle.json file refers to the entry id # - # XXX - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - XXX - # XXX - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - XXX - # XXX - until we have the jnamval command, we must FAKE PARSE the .entry.json file - XXX - # XXX - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - XXX - # XXX - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - XXX - # else # check if the author/author_handle.json file refers to the entry id # - FOUND=$(grep -E "${PATTERN_BEFORE_ENTRY_ID}${ENTRY_ID}${PATTERN_AFTER_ENTRY_ID}" \ - "$AUTHOR_HANDLE_JSON" 2>/dev/null) + FOUND=$("$JVAL_WRAPPER" -w -b "$AUTHOR_HANDLE_JSON" '$..entry_id') if [[ -z $FOUND ]]; then echo "$0: ERROR: line: $line of author_wins.csv: $AUTHOR_WINS_CSV" \ "entry id: $ENTRY_ID not in winning_entry_set of: $AUTHOR_HANDLE_JSON" 1>&2 diff --git a/bin/entry2csv.sh b/bin/entry2csv.sh index b29223b54..44bf26b2c 100755 --- a/bin/entry2csv.sh +++ b/bin/entry2csv.sh @@ -118,7 +118,7 @@ shopt -s lastpipe # explicitly run the last command of the pipe line in the curr # set variables referenced in the usage message # -export VERSION="1.1.1 2024-07-22" +export VERSION="1.2 2024-08-13" NAME=$(basename "$0") export NAME export V_FLAG=0 @@ -151,12 +151,6 @@ export AUTHOR_DIR="author" # # Write the award name to standard output (stdout) # -# XXX - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - XXX -# XXX - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - XXX -# XXX - until we have the jnamval command, we must FAKE PARSE the .entry.json file - XXX -# XXX - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - XXX -# XXX - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - XXX -# # usage: # output_award YYYY/dir/.entry.json # @@ -167,7 +161,6 @@ export AUTHOR_DIR="author" function output_award { local ENTRY_JSON_PATH; # the .entry.json path - local AWARD_STRING; # winning entry award string # parse args # @@ -191,12 +184,7 @@ function output_award # obtain the award string # - AWARD_STRING=$(grep -F '"award" : "' "$ENTRY_JSON_PATH" | sed -e 's/^.*"award" : "//' -e 's/",//') - if [[ -z $AWARD_STRING ]]; then - echo "$0: ERROR: in output_award: no award found in .entry.json file: $ENTRY_JSON_PATH" 1>&2 - return 5 - fi - echo "$AWARD_STRING" + "$JVAL_WRAPPER" -w -b "$ENTRY_JSON_PATH" '$..award' return 0 } @@ -205,12 +193,6 @@ function output_award # # Write the author handle(s) for the YYYY/dir entry to standard output (stdout) # -# XXX - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - XXX -# XXX - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - XXX -# XXX - until we have the jnamval command, we must FAKE PARSE the .entry.json file - XXX -# XXX - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - XXX -# XXX - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - XXX -# # usage: # output_author_handles YYYY/dir/.entry.json # @@ -221,7 +203,6 @@ function output_award function output_author_handles { local ENTRY_JSON_PATH; # the .entry.json path - local author_handle; # a found author handle # parse args # @@ -245,12 +226,7 @@ function output_author_handles # extract author handles from .entry.json # - grep -F author_handle "$ENTRY_JSON_PATH" | - awk 'NF == 5 { print $4; }' | - tr -d '"' | - while read -r author_handle; do - echo "$author_handle" - done + "$JVAL_WRAPPER" -w -b "$ENTRY_JSON_PATH" '$..author_handle' return 0 } @@ -457,6 +433,15 @@ fi export BIN_DIR="bin" +# find the jval-wrapper.sh tool +# +JVAL_WRAPPER="$BIN_PATH/jval-wrapper.sh" +if [[ ! -x $JVAL_WRAPPER ]]; then + echo "$0: ERROR: cannot find the bin/jval-wrapper.sh executable" 1>&2 + exit 5 +fi + + # verify we have our awk script # export MANIFEST_ENTRY_CSV_ENTRY_AWK="$BIN_DIR/manifest.csv.entry.awk" @@ -515,6 +500,7 @@ if [[ $V_FLAG -ge 3 ]]; then echo "$0: debug[3]: TOP_FILE=$TOP_FILE" 1>&2 echo "$0: debug[3]: BIN_PATH=$BIN_DIR" 1>&2 echo "$0: debug[3]: BIN_DIR=$BIN_DIR" 1>&2 + echo "$0: debug[3]: JVAL_WRAPPER=$JVAL_WRAPPER" 1>&2 echo "$0: debug[3]: MANIFEST_ENTRY_CSV_ENTRY_AWK=$MANIFEST_ENTRY_CSV_ENTRY_AWK" 1>&2 fi @@ -1088,15 +1074,6 @@ fi # author handles w/o author/author_handle.json file and # author/author_handle.json file that do not reference the entry id # -# XXX - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - XXX -# XXX - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - XXX -# XXX - until we have the jnamval command, we must FAKE PARSE the .entry.json file - XXX -# XXX - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - XXX -# XXX - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - XXX -# -export PATTERN_BEFORE_ENTRY_ID='^[[:space:]]*{[[:space:]]*"entry_id"[[:space:]]*:[[:space:]]*"' -export PATTERN_AFTER_ENTRY_ID='"[[:space:]]*},?[[:space:]]*$' -# if [[ $V_FLAG -ge 1 ]]; then echo "$0: debug[1]: about to: scan for unknown author handles" 1>&2 fi @@ -1174,18 +1151,11 @@ sed -e 's/,/ /g' "$AUTHOR_WINS_CSV" | while read -r AUTHOR_HANDLE ENTRY_ID_SET; # verify that the author/author_handle.json file refers to the entry id # - # XXX - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - XXX - # XXX - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - XXX - # XXX - until we have the jnamval command, we must FAKE PARSE the .entry.json file - XXX - # XXX - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - GROSS HACK - XXX - # XXX - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - XXX - # else # check if the author/author_handle.json file refers to the entry id # - FOUND=$(grep -E "${PATTERN_BEFORE_ENTRY_ID}${ENTRY_ID}${PATTERN_AFTER_ENTRY_ID}" \ - "$AUTHOR_HANDLE_JSON" 2>/dev/null) + FOUND=$("$JVAL_WRAPPER" -w -b "$AUTHOR_HANDLE_JSON" '$..entry_id') if [[ -z $FOUND ]]; then echo "$0: ERROR: line: $line of author_wins.csv: $AUTHOR_WINS_CSV" \ "entry id: $ENTRY_ID not in winning_entry_set of: $AUTHOR_HANDLE_JSON" 1>&2 diff --git a/bin/subst.entry-index.sh b/bin/subst.entry-index.sh index 36dce1436..575af632e 100755 --- a/bin/subst.entry-index.sh +++ b/bin/subst.entry-index.sh @@ -103,7 +103,7 @@ shopt -s globstar # enable ** to match all files and zero or more directories an # set variables referenced in the usage message # -export VERSION="1.4 2024-08-09" +export VERSION="1.5 2024-08-13" NAME=$(basename "$0") export NAME export V_FLAG=0 @@ -181,7 +181,6 @@ $NAME version: $VERSION" function output_award { local ENTRY_JSON_PATH; # the .entry.json path - local AWARD_STRING; # winning entry award string # parse args # @@ -205,12 +204,7 @@ function output_award # obtain the award string # - AWARD_STRING=$(grep -F '"award" : "' "$ENTRY_JSON_PATH" | sed -e 's/^.*"award" : "//' -e 's/",//') - if [[ -z $AWARD_STRING ]]; then - echo "$0: ERROR: in output_award: no award found in .entry.json file: $ENTRY_JSON_PATH" 1>&2 - return 5 - fi - echo "$AWARD_STRING" + "$JVAL_WRAPPER" -w -b "$ENTRY_JSON_PATH" '$..award' return 0 } @@ -362,6 +356,14 @@ if [[ ! -d $BIN_PATH ]]; then fi export BIN_DIR="bin" +# find the jval-wrapper.sh tool +# +JVAL_WRAPPER="$BIN_PATH/jval-wrapper.sh" +if [[ ! -x $JVAL_WRAPPER ]]; then + echo "$0: ERROR: cannot find the bin/jval-wrapper.sh executable" 1>&2 + exit 5 +fi + # verify that ENTRY_PATH is a entry directory # if [[ ! -d $ENTRY_PATH ]]; then @@ -483,6 +485,7 @@ if [[ $V_FLAG -ge 3 ]]; then echo "$0: debug[3]: INC_DIR=$INC_DIR" 1>&2 echo "$0: debug[3]: BIN_PATH=$BIN_PATH" 1>&2 echo "$0: debug[3]: BIN_DIR=$BIN_DIR" 1>&2 + echo "$0: debug[3]: JVAL_WRAPPER=$JVAL_WRAPPER" 1>&2 echo "$0: debug[3]: YEAR_DIR=$YEAR_DIR" 1>&2 echo "$0: debug[3]: ENTRY_DIR=$ENTRY_DIR" 1>&2 echo "$0: debug[3]: ENTRY_ID=$ENTRY_ID" 1>&2