forked from Cross-PLN-Technical-Working-Group/adpn-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadpn-package-do
executable file
·192 lines (144 loc) · 6.55 KB
/
adpn-package-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
#!/bin/bash
#
# adpn-package-do: script to handle adpn package commands
#
# @version 2021.0918
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:
enclose Enclose a directory of data files in a BagIt preservation package.
validate Validate completeness and correctness of a Bagit-enclosed preservation package
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 "package" "${_CMDLINE[@]:1}"
EXITCODE="$?"
else
case "${CMD}" in
"enclose")
ADP_OUTPUT="$(mktemp)"
declare -a ADP_ARGV=( "${_ARGV[@]:2}" )
declare -A ADP_SWITCHES=( )
for SWITCH in "${!_SWITCHES[@]}" ; do
case "${SWITCH}" in
"output")
# DROP IT -- WE'LL SET IT OURSELVES BELOW
;;
*)
ADP_SWITCHES[${SWITCH}]="${_SWITCHES[$SWITCH]}"
;;
esac
done
declare -a ADPN_DO_PACKAGE_CMD=( adpn-do-package.py --output=application/json )
ADPN_DO_PACKAGE_CMD+=( "${ADP_ARGV[@]}" )
ADPN_DO_PACKAGE_CMD+=( "${ADP_SWITCHES[@]}" "${CONTEXT_SW[@]}" )
adpn_debug 15,adpn,package "${LINENO}" '$ %s > %s' "$( printf "%q " "${ADPN_DO_PACKAGE_CMD[@]}" )" "${ADP_OUTPUT}"
"${ADPN_DO_PACKAGE_CMD[@]}" > "${ADP_OUTPUT}"; EXITCODE="$?"
if [[ "${EXITCODE}" -lt 255 ]] ; then
JSON_PACKET="$( adpn_get_json_packets "${ADP_OUTPUT}" )"
adpn_debug 35,adpn,package "${LINENO}" 'printf "%%s" "%s" | [...]' "${JSON_PACKET}"
printf "%s" "${JSON_PACKET}"
fi
rm "${ADP_OUTPUT}"
;;
"validate")
PIPELINE="$( adpn_parameter_from "--pipeline" "staged:validated" )"
if [[ -n "${PIPELINE}" ]] ; then
PIPEDFROM=$( printf "%s" "${PIPELINE}" | cut --field=1 --delimiter=":" )
PIPEDTO=$( printf "%s" "${PIPELINE}" | cut --field=2 --delimiter=":" )
adpn_debug 2 "${LINENO}" "SWITCH='%s', PIPELINE='%s': PIPEDFROM='%s', PIPEDTO='%s'\n" "${_SWITCHES[pipeline]}" "${PIPELINE}" "${PIPEDFROM}" "${PIPEDTO}" 1>&2
fi
declare -A TEMP_CLEANUP=()
TEMP_CLEANUP[json_input]="$(mktemp)"
adpn_read_json_packet_source "${_ARGV[2]}" "${SCRIPT_CMD_NAME}" "${V}" | adpn_get_json_packets | adpn_select_json_packet "${PIPEDFROM}" "-" "head -n 1" "${PIPEDTO}" > "${TEMP_CLEANUP[json_input]}"
EXITCODE="$?"
declare -a AU_LOCALPATHS=( )
[[ "${EXITCODE}" -eq 0 ]] && JSON_TEXT="$( cat "${TEMP_CLEANUP[json_input]}" )" || JSON_TEXT=""
if [[ -n "${JSON_TEXT}" ]] ; then
PUB_FROM=$( adpn_parameter_from "--from" ".From Publisher<${JSON_TEXT}" ".From Peer<${JSON_TEXT}" )
SUBDIRECTORY=$( printf "%s" "${JSON_TEXT}" | adpn-json.py --key=parameters | grep -E "^(sub)?directory\s" | cut --field=2)
LOCAL_BASE=$( adpn_parameter_from "--base_dir" ".stage/base_dir/${PUB_FROM}" ".stage/base_dir" )
LOCAL_DIRECTORY="$( printf "%s/%s" "${LOCAL_BASE%%/}" "${SUBDIRECTORY}" )"
[[ -n "${LOCAL_DIRECTORY}" && -d "${LOCAL_DIRECTORY}" && -r "${LOCAL_DIRECTORY}" ]] && AU_LOCALPATHS+=( "${LOCAL_DIRECTORY}" ) &&
printf "* GETTING DIRECTORY FROM JSON: %s\n" "${LOCAL_DIRECTORY}" 1>&2
else
AU_LOCALPATHS+=( "${_ARGV[@]:2}" )
EXITCODE=0
fi
AU_LOCALPATHS+=( "${PWD}" )
AU_LOCALPATHS=( "${AU_LOCALPATHS[@]/#/\\}" )
AU_LOCALPATH="$( adpn_parameter_from "--local" "${AU_LOCALPATHS[@]}" )"
AU_LOCALPATH="$( readlink -f "${AU_LOCALPATH}" )"
AU_LOCALBASE="$( basename "${AU_LOCALPATH}" )"
if [[ -d "${AU_LOCALPATH}" && -d "${AU_LOCALPATH}/data" ]] ; then
declare -a PYTHON_OPTS=( ".python" )
( which python3 >/dev/null 2>&1 ) && PYTHON_OPTS+=( "\\$( which python3 )" )
( which python >/dev/null 2>&1 ) && PYTHON_OPTS+=( "\\$( which python )" )
PYTHON="$( adpn_parameter_from "${PYTHON_OPTS[@]}" )"
declare -a BAGIT_CMD=( "${PYTHON}" "${SCRIPTDIR}/externals/bagit-python/bagit.py" )
BAGIT_CMD+=( --validate "${AU_LOCALPATH}" )
[[ -n "${Q}" ]] && BAGIT_CMD+=( "${Q}" )
"${BAGIT_CMD[@]}" 2>&1 ; EXITCODE="$?"
if [[ -n "${_PARAM[passthru]}" && -n "${JSON_TEXT}" ]] ; then
USER_EMAIL="$( adpn_get_user_email )"
declare -a JSON_OVERLAY=( "${JSON_TEXT}" )
JSON_OVERLAY+=( "$( adpn-json.py --output=application/json --key="Ingest Step" --value="validated" --key="Validated By" --value="${USER_EMAIL}" )" )
[[ "${EXITCODE}" -eq 0 ]] && printf "\nOK: The bagged AU located at %s validated as complete and correct.\n" "${AU_LOCALPATH}" && printf "%s\n" "${JSON_OVERLAY[@]}" | adpn-json.py --cascade --output=application/json
fi
else
adpn_notice "${LINENO}" "REQUIRED: AU path [%s] must be an accessible, BagIt-formatted directory." "${AU_LOCALPATH}"
fi
for FILE in "${TEMP_CLEANUP[@]}" ; do
rm "${FILE}"
done
;;
"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}