forked from Cross-PLN-Technical-Working-Group/adpn-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadpn-plugin-info
executable file
·173 lines (139 loc) · 5.79 KB
/
adpn-plugin-info
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
#!/bin/bash
#
# adpn-plugin-info: Extract and report properties and configuration parameters
# contained in a LOCKSS Plugin JAR package.
#
# This script depends on:
# - unzip (in order to extract the MANIFEST.MF and plugin XML file)
# - jar-manifest-property.py to parse the MANIFEST.MF file of the JAR package (in order to find and extract the plugin XML file)
# - lockss-plugin-props.py to parse and report data from the LOCKSS Plugin XML file
#
# @package adpn-plugin-info
#
# Usage: adpn-plugin-info [--help|<JARFILE>] [--format=<MIME>] [--quiet] [--<KEY>=<VALUE> ...]
#
# Key plugin properties, each required parameter, and each plugin property dependent on
# those parameters will be printed out as one line of a table which can be represented in
# plain text, TSV, or HTML table format.
#
# --help display these usage notes
# --format=<MIME> supported values: text/plain, text/tab-separated-values, text/html
# --quiet quiet mode, don't add section headers to text/plain or text/html output
#
# Parameters can be filled in using switches of the format --<KEY>=<VALUE>
# For example:
# --base_url=http://archives.alabama.gov/Lockss/ will set the parameter named 'base_url' to the value 'http://archives.alabama.gov/Lockss/'
# --subdirectory=NARA_documents will set the parameter named 'subdirectory' to the value 'NARA_documents'
#
# Exit codes:
# 0=successfully parsed plugin JAR and applied all required parameters
# 1=failed to parse plugin JAR
# 2=successfully parsed plugin JAR, but not all required parameters provided
#
# @version 2019.0523
##########################################################################################
### CONFIGURATION: Set up some constants, load up aliases ################################
##########################################################################################
SCRIPTPATH=$(which $0)
SCRIPTDIR=$(dirname "${SCRIPTPATH}")
SCRIPT=$(basename "${SCRIPTPATH}")
source "${SCRIPTDIR}/adpn-define-aliases"
MANIFEST=MANIFEST.MF
##########################################################################################
### DEPENDENCIES: check for required command-line tools and Python scripts ###############
##########################################################################################
DEPENDENCY_FAILURE=""
if [[ -z `which unzip` ]] ; then
echo 1>&2
echo Dependency Failure: unzip. This script requires the command-line UnZip tool. 1>&2
DEPENDENCY_FAILURE="${DEPENDENCY_FAILURE}+unzip"
fi
if [[ -z `which jar-manifest-property.py` ]] ; then
echo 1>&2
echo Dependency Failure: jar-manifest-property.py. This script requires the jar-manifest-property.py Python script. 1>&2
DEPENDENCY_FAILURE="${DEPENDENCY_FAILURE}+jar-manifest-property.py"
fi
if [[ -z `which lockss-plugin-props.py` ]] ; then
echo 1>&2
echo Dependency Failure: lockss-plugin-props.py. This script requires the lockss-plugin-props.py Python script. 1>&2
DEPENDENCY_FAILURE="${DEPENDENCY_FAILURE}+lockss-plugin-props.py"
fi
if [[ ! -z "${DEPENDENCY_FAILURE}" ]] ; then
exit 1
fi
##########################################################################################
### cmd line: --help tells us to display a usage note ####################################
##########################################################################################
if [[ "$1" =~ ^--help$ ]] ; then
adpn_help_notes "${SCRIPTPATH}"
exit $?
fi
##########################################################################################
### cmd line: get the name of the jar file, if provided on the command line ##############
##########################################################################################
if [ ! -z "$1" ] ; then
JARFILE=$1
shift
fi
CLEANUP=""
if [ "-" == "${JARFILE}" ] ; then
JARFILE=$(mktemp --suffix=.jar)
if [ -w "${JARFILE}" ] ; then
cat - > "${JARFILE}"
CLEANUP="rm ${JARFILE}"
fi
fi
if [ ! -r "${JARFILE}" ] ; then
if [ -r "${JARFILE}.jar" ] ; then
JARFILE="${JARFILE}.jar"
elif [ -r "${JARFILE}.zip" ] ; then
JARFILE="${JARFILE}.zip"
fi
fi
##########################################################################################
### cmd line: --manifest=(...) to specify the name/path to the MANIFEST.MF file ##########
##########################################################################################
if [[ "$1" =~ ^--manifest=(.*)$ ]] ; then
MANIFEST="${BASH_REMATCH[1]}"
shift
else
MANIFEST=MANIFEST.MF
fi
##########################################################################################
### cmd line: any remaining parameters will be passed along to lockss-plugin-props.py ####
##########################################################################################
CMDLINE="$*"
##########################################################################################
# Let's proceed ##########################################################################
##########################################################################################
EXITCODE=0
if [[ -r "${JARFILE}" && -s "${JARFILE}" ]] ; then
LINE=0
shopt -s lastpipe
( \
unzip -q -c "${JARFILE}" META-INF/${MANIFEST} \
| ${SCRIPTDIR}/jar-manifest-property.py - Lockss-Plugin Name \
) \
| while IFS="" read -a MANIFEST_LINE ; do
LINE=$((${LINE}+1))
if [[ "${LINE}" == 1 ]] ; then
#MANIFEST_LINE should be Lockss-Plugin="true"
if [[ "${MANIFEST_LINE}" != "true" ]] ; then
echo "[${SCRIPT}] error: Could not read ${MANIFEST} from ${JARFILE}"
break # while
fi
elif [[ "${LINE}" == 2 ]] ; then
#MANIFEST_LINE should be the relative path in the jarfile pointing to the XML
unzip -q -c "${JARFILE}" $MANIFEST_LINE | "${SCRIPTDIR}/lockss-plugin-props.py" - "$@"
EXITCODE=$?
break # while
fi
done
else
echo "[${SCRIPT}] error: Could not open JAR file '${JARFILE}'" 1>&2
EXITCODE=1
fi
if [ ! -z "${CLEANUP}" ] ; then
${CLEANUP}
fi
exit ${EXITCODE}