[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
This commit is contained in:
ayakael 2018-03-28 19:26:38 -04:00
parent e05faf010f
commit d3c141cbd8
No known key found for this signature in database
GPG key ID: 575626A4AE5F4026
10 changed files with 108 additions and 78 deletions

View file

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

View file

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

View file

@ -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 </path/to/dir/dir> </path/to/target> </path/to/db_file> <condition_1> <condition_2> <...>
#
# CONDITIONS
# CONDITIONS are defined using the following format:
# <metadata field>=<value>
# 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
}

29
src/cmd_include Normal file
View file

@ -0,0 +1,29 @@
#!/bin/bash
# doc cmd_include {
#
# DESCRIPTION
# cmd_include - Include track that matches CONDITION in DB_FILE
#
# USAGE
# cmd_include </path/to/dir/dir> </path/to/target> </path/to/db_file> <condition_1> <condition_2> <...>
#
# CONDITIONS
# CONDITIONS are defined using the following format:
# <metadata field>=<value>
# 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
}

View file

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

View file

@ -19,23 +19,46 @@ db_set() {
local GIT_DIR="${1}"
local DB_FILE="${2}"
local SELECTED="${3}"; shift 3
local conditionList=(${@})
local CONDITION="${@}"
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)))
# 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)"
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
# 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
# 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
}

View file

@ -7,4 +7,5 @@
source /usr/lib/bash/bunc
IFS='
'
_OPT_DB_FILE=.dam.db

View file

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

View file

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

View file

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