forked from Cross-PLN-Technical-Working-Group/adpn-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadpn-titlelist-do
executable file
·311 lines (233 loc) · 10.4 KB
/
adpn-titlelist-do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/bin/bash
#
# adpn-titlelist-do: script to handle adpn titlelist commands
#
# @version 2021.0830
SCRIPTPATH="$(which "$0")"
SCRIPTPATH="$( readlink --canonicalize "${SCRIPTPATH}" )"
SCRIPTDIR="$(dirname "${SCRIPTPATH}")"
SCRIPT="$(basename "${SCRIPTPATH}")"
source "${SCRIPTDIR}/adpn-define-aliases"
__USAGE__="Usage: ${SCRIPT} [--version] [--help] <CMD> [<ARGS>]"
__HELP__="[${SCRIPT}] Try '${SCRIPT} help' for more information."
__DOC__="""${__USAGE__}
--version Display the version of the script
--help Display these usage notes
The most commonly used ${SCRIPT} commands are:
sql Execute an SQL query and display the results
Exit codes:
0 = success (successful operation and expected result)
1-254 = error in executing command
255 = command not supported
"""
##########################################################################################
### COMMAND LINE: loop through switches ##################################################
##########################################################################################
declare -a _CMDLINE ; _CMDLINE=("$0")
shopt -s lastpipe
declare -a SWITCHFILES ; SWITCHFILES=()
if [[ -r "${CONFFILE}" ]] ; then
SWITCHFILES+=(${CONFFILE})
fi
CMDLINETXT=$(mktemp)
until [[ "$#" -lt 1 ]] ; do
_CMDLINE+=("$1")
printf "%s\n" "$1" >> "${CMDLINETXT}"
shift
done
SWITCHFILES+=(${CMDLINETXT})
adpn_command_line "${SWITCHFILES[@]}"
rm "${CMDLINETXT}"
##########################################################################################
### SCRIPT: DETERMINE COMMAND, THEN EXECUTE PIPELINE ####################################
##########################################################################################
EXITCODE=0
CMD="${_ARGV[1]}"
adpn_set_display_settings # V, VV, Q, QQ, DBG, DBGLEVEL, DDBG, SCRIPT_CMD_NAME / @see adpn-define-aliases
adpn_script_handle_version_or_help
if [[ ! -z "${_PARAM[remote]}" ]] ; then
adpn_script_handle_remote_execution -t "titlelist" "${_CMDLINE[@]:1}"
EXITCODE="$?"
else
case "${CMD}" in
""|"list")
# @method adpn titlelist list
# Display a list of available titles with their au_id codes.
#
# Usage: adpn titlelist list
#
# @version 2021.0830
if [[ -z "${CMD}" ]] ; then
CMD="list"
fi
#SQL_PREPARE=$( printf "PREPARE stmt1 FROM '%s';" "SELECT FROM au_titlelist
"${SCRIPTPATH}" sql --execute="SELECT * FROM au_titlelist"
EXITCODE="$?"
;;
"get")
SQL_COLS=$( adpn_parameter_from "--cols" '\*' )
SQL_COLS=$( printf "%s" "${SQL_COLS}" | sed -E 's/[^A-Za-z0-9_, *]+//g' )
declare -a SQL_COND=() SQL_VALUE=() SQL_SETS=()
[[ -n "${_PARAM[au_id]}" ]] && SQL_COND+=( "au_id=?" ) && SQL_VALUE+=( "@V_AU_ID" ) && SQL_SETS+=( "$( printf "@V_AU_ID=%d" "${_PARAM[au_id]}" )" )
if [[ "${_PARAM[au_title]}" ]] ; then
AU_TITLE="${_PARAM[au_title]}" && SQL_OPERATOR="="
[[ "${AU_TITLE}" =~ ^~(.*)$ ]] && SQL_OPERATOR=" RLIKE " && AU_TITLE="${BASH_REMATCH[1]}"
[[ "${AU_TITLE}" =~ ^%(.*)$ ]] && SQL_OPERATOR=" LIKE " && AU_TITLE="${BASH_REMATCH[1]}"
SQL_COND+=( "au_title${SQL_OPERATOR}?" )
SQL_VALUE+=( "@V_AU_TITLE" )
SQL_SETS+=( "$( printf "@V_AU_TITLE='%s'" "${AU_TITLE}" )" )
fi
SQL_VALUES=$( join_by ", " "${SQL_VALUE[@]}" )
SQL_SET_PAIRS=$( join_by ", " "${SQL_SETS[@]}" )
SQL_CONDS=$( join_by " AND " "${SQL_COND[@]}" )
declare -a SQL=()
SQL_SELECT="$( printf "SELECT %s FROM au_titlelist WHERE %s" "${SQL_COLS}" "${SQL_CONDS}" )"
SQL+=( "$( printf "PREPARE stmt1 FROM '%s'" "${SQL_SELECT}" )" )
SQL+=( "$( printf "SET %s" "${SQL_SET_PAIRS}" )" )
SQL+=( "$( printf 'EXECUTE stmt1 USING %s' "${SQL_VALUES}" )" )
SQL+=( "$( printf 'DEALLOCATE PREPARE stmt1' )" )
SQL_SCRIPT="$( join_by "; " "${SQL[@]}" )"
"${SCRIPTPATH}" sql --execute="${SQL_SCRIPT};" ${_SWITCHES[batch]}
EXITCODE="$?"
;;
"add")
#SQL_PREPARE=$( printf "PREPARE stmt1 FROM '%s';" "INSERT INTO au_titlelist (au_pub_id, au_pub, au_pub_name, au_pub_class, au_pub_xpath) VALUES (?, ?, ?, ?, ?)" )
#SQL_SET=$( printf 'SET @V_PUB_ID="%s", @V_PUB="%s", @V_PUB_NAME="%s", @V_PUB_CLASS="%s", @V_PUB_XPATH="%s"' "${ARG_ID}" "${ARG_NAME}" "${ARG_PUB_NAME}" "${ARG_PUB_CLASS}" "${ARG_PUB_XPATH}" )
#SQL_EXECUTE=$( printf 'EXECUTE stmt1 USING @V_PUB_ID, @V_PUB, @V_PUB_NAME, @V_PUB_CLASS, @V_PUB_XPATH' )
#SQL_DEALLOCATE=$( printf 'DEALLOCATE PREPARE stmt1' )
#SQL_SCRIPT=$( printf "%s; %s; %s; %s;" "${SQL_PREPARE}" "${SQL_SET}" "${SQL_EXECUTE}" "${SQL_DEALLOCATE}" )
adpn_notice "${LINENO}" "FAILED: Not yet implemented."
EXITCODE=255
;;
"remove")
#SQL_PREPARE=$( printf "PREPARE stmt1 FROM '%s';" "DELETE FROM au_publishers WHERE au_pub_id=?" )
#SQL_SET=$( printf 'SET @V_PUB_ID="%s"' "${ARG_ID}" )
#SQL_EXECUTE=$( printf 'EXECUTE stmt1 USING @V_PUB_ID' )
#SQL_DEALLOCATE=$( printf 'DEALLOCATE PREPARE stmt1' )
#SQL_SCRIPT=$( printf "%s; %s; %s; %s;" "${SQL_PREPARE}" "${SQL_SET}" "${SQL_EXECUTE}" "${SQL_DEALLOCATE}" )
#"${SCRIPTPATH}" sql --execute="${SQL_SCRIPT}"
#EXITCODE="$?"
adpn_notice "${LINENO}" "FAILED: Not yet implemented."
EXITCODE=255
;;
"post")
AV_OUTPUT="$(mktemp)" ; declare -a TEMP_CLEANUP=( "${AV_OUTPUT}" )
PIPELINE="$( adpn_parameter_from "--pipeline" "verified:ingested" )"
"${ADPN}" verify --from-here --pipeline="${PIPELINE}" "${_CMD_REST[@]}" > "${AV_OUTPUT}"
EXITCODE="$?"
cat "${AV_OUTPUT}" | grep -E -v "${ADPN_JSON_PACKET_REGEX}" ; echo
JSON_PACKET=$(grep -E "${ADPN_JSON_PACKET_REGEX}" "${AV_OUTPUT}" | cut --fields=2-)
if [[ "${EXITCODE}" -gt 0 ]] ; then
adpn_notice "${LINENO}" "adpn verify FAILED, exitcode: %d" "${EXITCODE}"
else
INGEST_FROM="$( adpn_parameter_from "--from" ".From Peer<${JSON_PACKET}" "<From (<PEER>): " )"
INGEST_TO="$( adpn_parameter_from "--for" "--to" "--peer" ".To Peer<${JSON_PACKET}" "<To (ALL|<PEER>): " )"
[[ -z "${_PARAM[dry-run]}" ]] && adpn-titlesdb-diff --before "${INGEST_TO}" ${V}
AIIT_SQLOUT="${_PARAM[sqldump]}"
[[ -z "${AIIT_SQLOUT}" ]] && AIIT_SQLOUT="$( mktemp )" && TEMP_CLEANUP+=( "${AIIT_SQLOUT}" )
[[ -z "${_PARAM[dry-run]}" ]] && adpn-ingest-into-titlesdb.py --snapshot
declare -a AIIT_CMDLINE=( "adpn-ingest-into-titlesdb.py" )
AIIT_CMDLINE+=( "${CONTEXT_SW[@]}" )
AIIT_CMDLINE+=( "--from=${INGEST_FROM}" "--to=${INGEST_TO}" )
AIIT_CMDLINE+=( "--passthru" )
[[ -n "${_SWITCHES[dry-run]}" ]] && AIIT_CMDLINE+=( "${_SWITCHES[dry-run]}" )
[[ -n "${PIPELINE}" ]] && AIIT_CMDLINE+=( "--pipeline=${PIPELINE}" )
printf "%s" "${JSON_PACKET}" | "${AIIT_CMDLINE[@]}" > "${AIIT_SQLOUT}" ; EXITCODE="$?"
if [[ "${EXITCODE}" -gt 0 ]] ; then
adpn_notice "${LINENO}" "adpn-ingest-into-titlesdb.py FAILED, exit code: %d" "${EXITCODE}"
else
[[ -z "${_PARAM[dry-run]}" ]] && { adpn-titlesdb-diff --after "${INGEST_TO}" ${V} ; EXITCODE="$?" ; }
if [[ "${EXITCODE}" -gt 0 ]] ; then
adpn_notice "${LINENO}" "adpn-titlesdb-diff FAILED, exit code: %d" "${EXITCODE}"
else
if [[ -n "${_SWITCHES[dry-run]}" ]] ; then
cat "${AIIT_SQLOUT}"
else
cat "${AIIT_SQLOUT}" | grep -E "${ADPN_JSON_PACKET_REGEX}"
fi
# Display final notice if this was called in its own right, not if by adpn promote &c.
[[ -z "${_PARAM[context]}" ]] && adpn_notice "${LINENO}" "DONE: Archival Unit posted to %s titlesdb." "${INGEST_TO}"
fi
fi
fi
for TMP in "${TEMP_CLEANUP[@]}" ; do
rm "${TMP}"
done
;;
"commit")
TDB_XML=$( adpn_parameter_from "--to" "${_ARGV[2]}" )
[[ -z "${TDB_XML}" ]] && TDB_XML="$( adpn_parameter_from ".props/titlesdb.xml" )"
if [[ -r "${TDB_XML}" ]] ; then
TDB_PATH=$( readlink -f "${TDB_XML}" )
TDB_DIR=$( dirname "${TDB_PATH}" )
TDB_FILE=$( basename "${TDB_PATH}" )
printf "\n"
printf "* Generating new static %s for network..." "${TDB_FILE}"
cd "${TDB_DIR}"
adpn-titlesdb-diff --before="${TDB_PATH}" --stype=1
printf " (ok)\n"
# Is this directory under git version control?
if [[ -d "./.git" ]] ; then
printf "* Comparing new %s to prior revision...\n\n" "${TDB_FILE}"
if [[ -z "${_PARAM[batch]}" ]] ; then
git add --patch "${TDB_FILE}" < /dev/tty
else
git add "${TDB_FILE}"
fi
COMMIT_MESSAGE="$( adpn_parameter_from "--message" "Updates to static titlesDb XML" )"
git commit --message "${COMMIT_MESSAGE}"
fi
else
adpn_notice "${LINENO}" "WARNING: Could not locate static titlesDb XML file at [%s], so the new version was not generated.\n" "${TDB_XML}"
fi
;;
"promote")
AI_OUTPUT="$(mktemp)"
"${ME}" post --for=ALL --pipeline=ingested:published "${_ADD_CONTEXT[@]}" "${_CMD_REST[@]}" > "${AI_OUTPUT}"
AI_ERRCODE="$?"
cat "${AI_OUTPUT}"
JSON_PACKET=$(grep -E "${ADPN_JSON_PACKET_REGEX}" "${AI_OUTPUT}" | cut --fields=2-)
rm "${AI_OUTPUT}"
if [[ "${AI_ERRCODE}" -gt 0 ]] ; then
printf "[%s:%d] ${SCRIPT} accept FAILED.\n" "${SCRIPT_CMD_NAME}" "${LINENO}" "${SCRIPT}" 1>&2
EXITCODE="${AI_ERRCODE}"
else
printf "\n" 1>&2
printf "[%s:%d] DONE: Archival Unit published to titlesdb.\n" "${SCRIPT_CMD_NAME}" "${LINENO}" 1>&2
AU_TITLE="$( adpn_parameter_from "--au_title" ".Ingest Title<${JSON_PACKET}" )"
declare -a AC_SW=()
AC_SW+=( "$( printf -- "--message=Promoted new AU (%s)" "${AU_TITLE}" )" )
[[ -n "${_PARAM[batch]}" ]] && AC_SW+=( "${_SWITCHES[batch]}" )
"${ME}" commit "${AC_SW[@]}" "${CONTEXT_SW[@]}"
EXITCODE="$?"
fi
;;
"sql")
MYSQL_HOST="$( adpn_parameter_from "--mysql-host" ".mysql/host" )"
MYSQL_DB="$( adpn_parameter_from "--mysql-db" ".mysql/db" )"
MYSQL_USER="$( adpn_parameter_from "--mysql-user" ".mysql/user" )"
MYSQL_PASSWORD="$( adpn_parameter_from "--mysql-password" ".mysql/password" )"
if [[ -z "${MYSQL_PASSWORD}" ]] ; then
MYSQL_PASSWORD_SWITCH=""
elif [[ "${MYSQL_PASSWORD}" == "-" ]] ; then
MYSQL_PASSWORD_SWITCH="--password"
else
MYSQL_PASSWORD_SWITCH=$(printf "%s--password=%s" "" "${MYSQL_PASSWORD}")
fi
mysql --user="${MYSQL_USER}" ${MYSQL_PASSWORD_SWITCH} --database="${MYSQL_DB}" --host="${MYSQL_HOST}" "${_SWITCHES[execute]}" ${_SWITCHES[batch]}
EXITCODE="$?"
;;
"version"|"help")
EXITCODE=0
;;
*)
echo "[${SCRIPT_CMD_NAME}] '${CMD}' command not understood." 1>&2
echo "${__HELP__}" 1>&2
EXITCODE=255
;;
esac
fi
##########################################################################################
### CLEANUP: exit with settled exit code. ################################################
##########################################################################################
exit ${EXITCODE}