From d3c141cbd808093bcd36ef1ed164721ae6006b22 Mon Sep 17 00:00:00 2001 From: ayakael Date: Wed, 28 Mar 2018 19:26:38 -0400 Subject: [PATCH] [build.sh] Updated build script to remove debug eval command. [build.sh] Build script now has 'env' file at head and 'parser' file tail [cmd_deploy] Now uses the _OPT_DB_FILE variable instead of DB_FILE [cmd_deploy] now generates imageidList using DB_FILE if imageid not provided by user [cmd_exclude] Rewrote to operate as front end to db_set's exclude function [cmd_include] Added as front end to db_set's include function [cmd_update] Fixed some typos [db_set] Added as backend to DB_FILE trackid selection [env] Now defines _OPT_DB_FILE as .dam.db [parser] Updates parser to support cmd_deploy, cmd_include, cmd_exclude and cmd_update [print_meta_mtag] Last line now does not trail with ";" character --- build.sh | 5 +++-- src/cmd_deploy | 6 +++-- src/cmd_exclude | 45 +++++++++++++++++++------------------ src/cmd_include | 29 ++++++++++++++++++++++++ src/cmd_update | 6 ++--- src/db_set | 55 ++++++++++++++++++++++++++++++++------------- src/env | 1 + src/parser | 36 +++++------------------------ src/print_meta_flac | 1 - src/print_meta_mtag | 2 +- 10 files changed, 108 insertions(+), 78 deletions(-) create mode 100644 src/cmd_include diff --git a/build.sh b/build.sh index d4ecb29..63d4a36 100755 --- a/build.sh +++ b/build.sh @@ -4,7 +4,8 @@ EXEC=dam echo -e "#!/bin/bash\n" > ${EXEC} -for file in $(find src/ -type f); do +for file in src/env $(find src/ -type f -not -name env -not -name parser) src/parser; do awk '!/^ *#/ && NF' ${file} >> ${EXEC} done -echo 'eval $@' >> ${EXEC} + + diff --git a/src/cmd_deploy b/src/cmd_deploy index 41b78ca..37ab9f2 100644 --- a/src/cmd_deploy +++ b/src/cmd_deploy @@ -14,13 +14,15 @@ cmd_deploy() { local GIT_DIR="${1}" local TARGET="${2}"; shift 2 local imageidList=(${@}) - local DB_FILE="${TARGET}/${DB_FILE}" + local DB_FILE="${TARGET}/${_OPT_DB_FILE}" [[ ! -f "${DB_FILE}" ]] && return 1 + [[ -z "${imageidList[@]}" ]] && local imageidList=($(awk 'BEGIN{FS="\t"}{if($1=="true"){print $2}}' ${DB_FILE} | awk '!seen[$0]++')) + for imageid in ${imageidList[@]}; do _msg EXEC "Deploying ${imageid} to ${TARGET}" deploy_imageid ${GIT_DIR} ${TARGET} ${DB_FILE} ${imageid} >${STDERR} 2>&1 - [[ $? -ne 0 ]] && { _msg WARN; local ERR=true; } + [[ $? -ne 0 ]] && { _msg WARN; local ERR=true; } || _msg OK done [[ ${ERR} ]] && return 2 } diff --git a/src/cmd_exclude b/src/cmd_exclude index f993038..e245521 100644 --- a/src/cmd_exclude +++ b/src/cmd_exclude @@ -1,28 +1,29 @@ #!/bin/bash -### -# Command used to ADD or DELete an exception rule in OPT_EXCLUDE_FILE -### +# doc cmd_exclude { +# +# DESCRIPTION +# cmd_exclude - Excludes track that matches CONDITION in DB_FILE +# +# USAGE +# cmd_exclude <...> +# +# CONDITIONS +# CONDITIONS are defined using the following format: +# = +# The metadata field can be anything that can be injected as metadata in a Vorbis or id3tag. +# +# } cmd_exclude() { - local TARGET="${1}" - local EXCLUDE="${TARGET}/${EXCLUDE}" - local CMD="${2}"; shift 2 - local fieldList=("${@}") + local GIT_DIR="${1}" + local TARGET="${2}" + local DB_FILE="${TARGET}/${_OPT_DB_FILE}"; shift 2 + local conditionList==("${@}") - case ${CMD} in - add) - printf "%s\n" ${fieldList[@]} >> ${EXCLUDE} - ;; - - del) - for field in ${fieldList[@]}; do - sed -i "/${field}/d" ${EXCLUDE} - done - ;; - - ls) - cat ${EXCLUDE} - ;; - esac + for condition in ${conditionList[@]}; do + _msg EXEC "Excluding all tracks that match the following condition: ${condition}" + db_set ${GIT_DIR} ${DB_FILE} false "${condition}" + [[ $? -ne 0 ]] && { _msg WARN; return 1; } || { _msg OK; return 0; } + done } diff --git a/src/cmd_include b/src/cmd_include new file mode 100644 index 0000000..967fb39 --- /dev/null +++ b/src/cmd_include @@ -0,0 +1,29 @@ +#!/bin/bash + +# doc cmd_include { +# +# DESCRIPTION +# cmd_include - Include track that matches CONDITION in DB_FILE +# +# USAGE +# cmd_include <...> +# +# CONDITIONS +# CONDITIONS are defined using the following format: +# = +# The metadata field can be anything that can be injected as metadata in a Vorbis or id3tag. +# +# } + +cmd_include() { + local GIT_DIR="${1}" + local TARGET="${2}" + local DB_FILE="${TARGET}/${_OPT_DB_FILE}"; shift 2 + local conditionList==("${@}") + + for condition in ${conditionList[@]}; do + _msg EXEC "Including all tracks that match the following condition: ${condition}" + db_set ${GIT_DIR} ${DB_FILE} true "${condition}" + [[ $? -ne 0 ]] && { _msg WARN; return 1; } || { _msg OK; return 0; } + done +} diff --git a/src/cmd_update b/src/cmd_update index 250f311..fb4599f 100644 --- a/src/cmd_update +++ b/src/cmd_update @@ -18,17 +18,17 @@ cmd_update() { # In the event that LAST_COMMIT OR DB_FILE does not exist, echo out that the LAST_COMMIT # is the first COMMIT of GIT_DIR, thus stating that no database update has ever occured if [[ -z "$(grep LAST_COMMIT ${DB_FILE})" ]]; then - echo "LAST_COMMIT\t$(git -C "${GIT_DIR}" rev-list HEAD | tail -n 1)" >> ${DB_FILE} + echo -e "LAST_COMMIT\t$(git -C "${GIT_DIR}" rev-list HEAD | tail -n 1)" >> ${DB_FILE} fi # Defines what imageids needs updating by determining what files has changed since last # update via git diff --name-only. local NEW_COMMIT=$(git -C "${GIT_DIR}" rev-parse HEAD) - local OLD_COMMIT=$(awk 'BEGIN{FS="\t"}{if($1=="LAST_COMMIT"){print $2}}') + local OLD_COMMIT=$(awk 'BEGIN{FS="\t"}{if($1=="LAST_COMMIT"){print $2}}' ${DB_FILE}) imageidList=($(sed 's/\(.*\)\..*/\1/' <<< $(git -C "${GIT_DIR}" diff --name-only ${NEW_COMMIT} ${OLD_COMMIT}) | awk '!seen[$0]++' | grep SHA256)) _msg EXEC "Updating database from ${OLD_COMMIT} to ${NEW_COMMIT}" - db_update "${DIT_DIR}" "${TARGET}" "${DB_FILE}" ${imageidList[@]} + db_update "${GIT_DIR}" "${TARGET}" "${DB_FILE}" ${imageidList[@]} [[ $? -ne 0 ]] && { _msg WARN; local ERR=true; } || _msg OK [[ ${ERR} ]] && return 1 || return 0 } diff --git a/src/db_set b/src/db_set index f35dd42..ca015ec 100644 --- a/src/db_set +++ b/src/db_set @@ -19,23 +19,46 @@ db_set() { local GIT_DIR="${1}" local DB_FILE="${2}" local SELECTED="${3}"; shift 3 - local conditionList=(${@}) + local CONDITION="${@}" + + # Defines what imageids are imported into database, as we're only going to act on those + local FIELD="$(echo ${CONDITION} | cut -d'=' -f1)" + local VALUE="$(echo ${CONDITION} | cut -d'=' -f2)" + + # Determines which IMAGEIDs present in the DB_FILE match the CONDITION + local COUNT=1 + local dbimageidList=$(awk 'BEGIN{FS="\t"}{print $2}' ${DB_FILE} | awk '!seen[$0]++') + for dbimageid in ${dbimageidList[@]}; do + local MTAG="$(printf "${GIT_DIR}/%s.tags" ${dbimageid})" + local MATCH="$(grep -i -l "\"${FIELD}\" : \"${VALUE}\"" "${MTAG}")" + local imageidList[${COUNT}]="$(basename -s .tags ${MATCH} 2>/dev/null)" + local COUNT=$(( ${COUNT} + 1 )) + done - for condition in ${conditionList[@]}; do - local FIELD="$(echo ${condition} | cut -d'=' -f1)" - local VALUE="$(echo ${condition} | cut -d'=' -f2)" - local imageidList=($(sed 's/\(.*\)\..*/\1/' <<< $(grep -i -l '"'${FIELD}'" : "'${VALUE}'"' ${GIT_DIR}/*.tags))) - - for imageid in ${imageidList[@]}; do - local TOTALTRACKS=$(grep "@" ${imageid}.tags | wc -l) - local COUNT=1 - while [[ ${COUNT} -le ${TOTALTRACKS} ]]; do - [[ "$(print_meta_mtag "${GIT_DIR}/${imageid}.tags" ${COUNT} ${FIELD})" == "${VALUE}" ]] && local trackidList[${COUNT}]="$(print_meta_mtag "${GIT_DIR}/${imageid}.tags" ${COUNT} TRACKID)" - local COUNT=$(( ${COUNT} + 1 )) - done - for trackid in ${trackidList[@]}; do - awk -v selected=${SELECTED} -v trackid=${trackid} 'BEGIN{FS="\t";OFS="\t"}{if($4==trackid){$1=selected}{print $0}}' > ${DB_FILE}.tmp; mv ${DB_FILE}.tmp ${DB_FILE} - done + # Goes through imageidList to determine which TRACKID matches the CONDITION + for imageid in ${imageidList[@]}; do + + # Defines TOTALTRACKS of IMAGEID + local TOTALTRACKS=$(grep "@" ${GIT_DIR}/${imageid}.tags | wc -l) + + # Loop goes through all of the tracks in IMAGEID, and determines which of the TRACKIDs has a FIELD that matches the VALUE + local COUNT=1 + while [[ ${COUNT} -le ${TOTALTRACKS} ]]; do + + local TRACK_VALUE=$(print_meta_mtag "${GIT_DIR}/${imageid}.tags" ${COUNT} ${FIELD} | cut -d'=' -f2 | sed 's|.$||') + # If print_meta_mtag extract the same VALUE from FIELD as the CONDITION, the TRACKID of this TRACK is added into trackidList + if [[ "${TRACK_VALUE}" == "${VALUE}" ]]; then + local TRACKID=$(print_meta_mtag "${GIT_DIR}/${imageid}.tags" ${COUNT} TRACKID | cut -d'=' -f2 | sed 's|.$||') + _msg ECHO "Track ${COUNT} of ${imageid} matches condition. Adding to trackidList" + local trackidList[${COUNT}]="${TRACKID}" + fi + + local COUNT=$(( ${COUNT} + 1 )) + done + + # Changes state of trackids that matched + for trackid in ${trackidList[@]}; do + awk -v imageid=${imageid} -v selected=${SELECTED} -v trackid=${trackid} 'BEGIN{FS="\t";OFS="\t"}{if($2==imageid && $3==trackid){$1=selected}{print $0}}' ${DB_FILE} > ${DB_FILE}.tmp; mv ${DB_FILE}.tmp ${DB_FILE} done done } diff --git a/src/env b/src/env index 2ad1a06..8418882 100644 --- a/src/env +++ b/src/env @@ -7,4 +7,5 @@ source /usr/lib/bash/bunc IFS=' ' +_OPT_DB_FILE=.dam.db diff --git a/src/parser b/src/parser index e7ab855..0263f8e 100644 --- a/src/parser +++ b/src/parser @@ -10,45 +10,19 @@ case "${1}" in cmd_exclude ${@} ;; - init) + include) shift - cmd_init ${@} + cmd_include ${@} ;; deploy) shift - TARGET="${1}"; shift - imageidList=($(printf '%s\n' ${@} | sed 's/\(.*\)\..*/\1/')) - cmd_deploy ${TARGET} ${imageidList[@]} + cmd_deploy ${@} ;; update) shift - TARGET="${1}"; shift - LAST_COMMIT="${TARGET}/${LAST_COMMIT}" - NEW_COMMIT=$(git rev-parse HEAD) - OLD_COMMIT=$(grep COMMIT "${LAST_COMMIT}" | cut -d'=' -f2) - imageidList=($(sed 's/\(.*\)\..*/\1/' <<< $(git diff --name-only ${NEW_COMMIT} ${OLD_COMMIT}) | awk '!seen[$0]++' | grep SHA256)) - echo $LAST_COMMIT - echo $NEW_COMMIT - echo $OLD_COMMIT - echo ${imageidList[@]} - - cmd_deploy ${TARGET} ${imageidList[@]} - case $? in - 0) - _msg ECHO "Update completed succesfully" - sed -i "s/COMMIT=.*/COMMIT=${NEW_COMMIT}/" ${LAST_COMMIT} - ;; - - 1) - _msg ECHO "Missing exclude, manifest or exclude file. Please make sure you init" - ;; - - 2) - _msg ECHO "Update completed with errors. Please review and deploy problematic images manually." - sed -i "s/COMMIT=.*/COMMIT=${NEW_COMMIT}/" ${LAST_COMMIT} - ;; - esac + cmd_update ${@} + ;; esac diff --git a/src/print_meta_flac b/src/print_meta_flac index 9c66229..e70d2c9 100644 --- a/src/print_meta_flac +++ b/src/print_meta_flac @@ -15,6 +15,5 @@ print_meta_flac() { if [[ -f ${FILE} ]]; then awk 'BEGIN{FS=": ";ORS=";"}{if($1 ~ /comment\[/){print $2}}' <<< $(metaflac --list --block-type=VORBIS_COMMENT "${FILE}") | sed 's|.$||' - fi } diff --git a/src/print_meta_mtag b/src/print_meta_mtag index 37ac3d1..1d7691b 100644 --- a/src/print_meta_mtag +++ b/src/print_meta_mtag @@ -21,7 +21,7 @@ print_meta_mtag() { [[ -z "${tagList[@]}" ]] && local tagList=($(awk 'BEGIN{RS="\",*\n * \"";FS="\" : \""}{if($1!~"@"){print $1}}' ${FILE} | awk '!seen[$0]++') IMAGEID) for tag in ${tagList[@]}; do - [[ "${tag}" == "IMAGEID" ]] && { echo -n "IMAGEID=$(sed 's|.tags||g' <<< $(basename "${FILE}"));"; continue; } + [[ "${tag}" == "IMAGEID" ]] && { echo -n "IMAGEID=$(sed 's|.tags||g' <<< $(basename "${FILE}"))"; continue; } echo -n "${tag}=$(print_meta_field ${FILE} ${TRACKNO} ${tag});" done }