Renamed function print_present_meta to print_meta_flac

Renamed function print_future_meta to print_meta_mtag, and added ability to define what field to extract
Renamed function update_db to db_update, and updated to support new database format
Created function db_set to set selection of trackid depending on conditions
Updated cmd_deploy to support deploy_imageid
Updated cmd_update to support new db_update
This commit is contained in:
ayakael 2018-03-28 12:48:49 -04:00
parent 06a0491448
commit e05faf010f
No known key found for this signature in database
GPG key ID: 575626A4AE5F4026
10 changed files with 131 additions and 114 deletions

View file

@ -1,28 +1,27 @@
#! /bin/bash
###
# Deploys image ID using deploy_imageid function
###
# doc cmd_deploy {
#
# DESCRIPTION
# cmd_deploy - Command that deploys IMAGEIDs using deploy_imageid function
#
# USAGE
# cmd_deploy - </path/to/git/dir> </path/to/target> <imageid_1> <imageid_2> <...>
#
# }
cmd_deploy() {
local TARGET="${1}"; shift
local GIT_DIR="${1}"
local TARGET="${2}"; shift 2
local imageidList=(${@})
local MANIFEST="${TARGET}/${MANIFEST}"
local LAST_COMMIT="${TARGET}/${LAST_COMMIT}"
local EXCLUDE="${TARGET}/${EXCLUDE}"
if [[ ! -f "${MANIFEST}" ]] || [[ ! -f "${LAST_COMMIT}" ]] || [[ ! -f "${EXCLUDE}" ]]; then return 1; fi
local DB_FILE="${TARGET}/${DB_FILE}"
[[ ! -f "${DB_FILE}" ]] && return 1
for imageid in ${imageidList[@]}; do
_msg ECHO "Cleaning ${imageid} of ${TARGET}"
clean ${TARGET} ${imageid} ${MANIFEST} >${STDERR} 2>&1
[[ $? -ne 0 ]] && local ERRORS=true
if [[ -f "${imageid}.tags" ]] && is_not_excluded ${imageid} ${EXCLUDE}; then
deploy ${imageid} ${TARGET} ${MANIFEST}
[[ $? -ne 0 ]] && local ERRORS=true
fi
_msg EXEC "Deploying ${imageid} to ${TARGET}"
deploy_imageid ${GIT_DIR} ${TARGET} ${DB_FILE} ${imageid} >${STDERR} 2>&1
[[ $? -ne 0 ]] && { _msg WARN; local ERR=true; }
done
[[ ${ERRORS} ]] && return 2
[[ ${ERR} ]] && return 2
}

View file

@ -1,20 +0,0 @@
#!/bin/bash
###
# Command initializes TARGET directory by creating OPT_FILE_MANIFEST and OPT_FILE_EXCLUDE
# OPT_FILE_COMMIT
###
cmd_init() {
local TARGET="${1}"
local MANIFEST="${TARGET}/${MANIFEST}"
local LAST_COMMIT="${TARGET}/${LAST_COMMIT}"
local EXCLUDE="${TARGET}/${EXCLUDE}"
[[ -z "$(ls -A "${TARGET}")" ]] || return 1
touch "${MANIFEST}"
touch "${EXCLUDE}"
echo "COMMIT=$(git rev-list HEAD | tail -n 1)" > ${LAST_COMMIT}
[[ $? -eq 0 ]] && return 0 || return 2
}

View file

@ -1,38 +0,0 @@
#!/bin/bash
# doc cmd_select {
#
# DESCRIPTION
# cmd_select - Command selects all IMAGEID that matches the CONDITION
#
# USAGE
# cmd_select </path/to/git/dir> </path/to/db/file> <BOULEAN> <condition> [<...>]
#
# 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_select() {
local GIT_DIR="${1}"
local DB_FILE="${2}"
local SELECTED="${3}"; shift 3
local conditionList=(${@})
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 rowList=($(_cfg query '$3="'${imageid}'"' ${DB_FILE}))
for rowno in ${rowList[@]}; do
_cfg change SELECTED ${rowno} ${SELECTED} ${DB_FILE}
done
done
done
}

34
src/cmd_update Normal file
View file

@ -0,0 +1,34 @@
#!/bin/bash
# doc cmd_update{
#
# DESCRIPTION
# cmd_update - Updates database of TARGET from git HEAD to last commit hash.
#
# USAGE
# cmd_update </path/to/git/dir> </path/to/target/dir>
#
# }
cmd_update() {
local GIT_DIR="${1}"
local TARGET="${2}"
local DB_FILE="${TARGET}/${_OPT_DB_FILE}"
# 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}
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}}')
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[@]}
[[ $? -ne 0 ]] && { _msg WARN; local ERR=true; } || _msg OK
[[ ${ERR} ]] && return 1 || return 0
}

42
src/db_set Normal file
View file

@ -0,0 +1,42 @@
#!/bin/bash
# doc db_set {
#
# DESCRIPTION
# db_set - Command selects or deslects all TRACKIDs that matches the CONDITION
#
# USAGE
# db_set </path/to/git/dir> </path/to/db/file> <BOULEAN> <condition> [<...>]
#
# 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.
#
# }
db_set() {
local GIT_DIR="${1}"
local DB_FILE="${2}"
local SELECTED="${3}"; shift 3
local conditionList=(${@})
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
done
done
}

View file

@ -1,16 +1,16 @@
#!/bin/bash
# doc update_db {
# doc db_update {
#
# DESCRIPTION
# update_db - Updates TARGET's DB_FILE with defined IMAGEIDs
# db_update - Updates TARGET's DB_FILE with defined IMAGEIDs
#
# USAGE
# update_db <path/to/git/dir> <path/to/target/dir> <path/to/db/file> <IMAGEID> <...>
# db_update <path/to/git/dir> <path/to/target/dir> <path/to/db/file> <IMAGEID> <...>
#
# }
update_db() {
db_update() {
local GIT_DIR="${1}"
local TARGET="${2}"
local DB_FILE="${3}"; shift 3

View file

@ -21,10 +21,10 @@ deploy_trackid() {
local TRACKNO="$(print_track_no ${GIT_DIR} ${IMAGEID} ${TRACKID})"
## Path and metadata parsing
local FUTURE_META="$(print_future_meta ${GIT_DIR}/${IMAGEID}.tags ${TRACKNO})"
local FUTURE_META="$(print_meta_mtag ${GIT_DIR}/${IMAGEID}.tags ${TRACKNO})"
local FUTURE_PATH="$(print_future_path ${FUTURE_META}})"
local PRESENT_PATH="$(print_present_path ${DB_FILE} ${IMAGEID} ${TRACKID})"
local PRESENT_META="$(print_present_meta "${TARGET}/${PRESENT_PATH}")"
local PRESENT_META="$(print_meta_flac "${TARGET}/${PRESENT_PATH}")"
local ROW_NO=$(awk -v imageid=${IMAGEID} -v trackid=${TRACKID} 'BEGIN{FS="\t"}{if($2==imageid && $3==trackid){print NR}}' ${DB_FILE})
# If the track is selected, we will check if the trackid has already been deployed, if not deploy. If the metadata to be

View file

@ -1,28 +0,0 @@
#!/bin/bash
# doc print_future_meta {
#
# DESCRIPTION
# print_future_meta - Prints metadata of specified TRACKNUMBER, from textfile following
# the MTAG specification. Outputs as standard FIELD=VALUE that can then be pipped into
# metaflac. Can also be supplied with what tags to extract
#
# USAGE
# print_future_meta </path/to/mtag/file> <track no> [<field>]
#
# }
print_future_meta() {
local MTAG=${1}
local TRACKNUMBER="${2}"; shift 2
local tagList=(${@})
[[ -z "${tagList[@]}" ]] && local tagList=($(awk 'BEGIN{RS="\",*\n * \"";FS="\" : \""}{if($1!~"@"){print $1}}' ${MTAG} | awk '!seen[$0]++') IMAGEID)
for tag in ${tagList[@]}; do
[[ "${tag}" == "IMAGEID" ]] && { echo -n "IMAGEID=$(sed 's|.tags||g' <<< $(basename "${MTAG}"));"; continue; }
echo -n "${tag}=$(print_meta_field ${MTAG} ${TRACKNUMBER} ${tag});"
done
}

View file

@ -1,16 +1,16 @@
#!/bin/bash
# doc print_present_meta {
# doc print_meta_flac {
#
# DESCRIPTION
# print_present_meta - Prints metadata of remote flac in FIELD=VALUE; format.
# print_meta_flac - Prints metadata of remote flac in FIELD=VALUE; format.
#
# USAGE
# print_present_meta </path/to/flac
# print_meta_flac </path/to/flac>
#
# }
print_present_meta() {
print_meta_flac() {
local FILE="${1}"
if [[ -f ${FILE} ]]; then

28
src/print_meta_mtag Normal file
View file

@ -0,0 +1,28 @@
#!/bin/bash
# doc print_meta_mtag {
#
# DESCRIPTION
# print_meta_mtag - Prints metadata of specified TRACKNO, from textfile following
# the MTAG specification. Outputs as standard FIELD=VALUE that can then be pipped into
# metaflac. Can also be supplied with what tags to extract
#
# USAGE
# print_meta_mtag </path/to/mtag/file> <track no> [<field_1> <field_2> <...>]
#
# }
print_meta_mtag() {
local FILE=${1}
local TRACKNO="${2}"; shift 2
local tagList=(${@})
[[ -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; }
echo -n "${tag}=$(print_meta_field ${FILE} ${TRACKNO} ${tag});"
done
}