-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcheck-manifest.sh
executable file
·121 lines (104 loc) · 3.12 KB
/
check-manifest.sh
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
#!/bin/bash
function check_binaries()
{
for file in $@; do
ret=`which $file 2> /dev/null`
if [ ! -x "$ret" ]; then
echo "command $file not found" >&2
exit 1
fi
done
}
UPWD="-n"
GRANULEONLY=1
VERBOSE=0
CURLSILENT=" --silent"
while getopts "hu:vqG" opt; do
case $opt in
h)
printf "Download Sentinel product and compare the contents of the ZIP file to the manifest within.\n\n Usage:\n \
\t${argv[0]} [options] <product URL>\n \
\t${argv[0]} [options] <DHuS instance> <Product ID>\n \
\t${argv[0]} [options] <DHuS instance> <Product Name>\n \
\t-h \tDisplay this help\n \
\t-u <str>\tuser:password to use accessing the remote site.\n \
\t\t\tThis is passed directly to curl.\n \
\t-v \tVerbose: give more output and do not remove artifacts.\n \
\t-q \tQuiet: Suppress all standard output. Result indicated by retval.\n \
\t-G \tDo not grep the final output for 'GRANULE'.\n \
\n"
exit 0
;;
u)
UPWD="-u \"$OPTARG\""
;;
G)
GRANULEONLY=0
;;
v)
VERBOSE=1
CURLSILENT=""
;;
q)
VERBOSE=0
exec 1>&2
;;
esac
done
shift $(($OPTIND - 1))
if [ $# -eq 1 ]; then
URL=$1
>&2 echo Single download URL given: $URL
else
PRIM=$1
SEC=$2
echo "$SEC" | egrep -x '[0-9a-z\-]*' > /dev/null
if [ $? -eq 0 ]; then
URL="https://$(echo $PRIM | sed 's/^[htps:]*\/\///' | sed 's/\\*$//')/odata/v1/Products('$SEC')/\$value"
>&2 echo Assuming URL: $URL
else
HOST="https://$(echo $PRIM | sed 's/^[htps:]*\/\///' | sed 's/\\*$//')/"
# Treating SEC as poduct name. Stripping suffix in case someone used it
BN=`echo $SEC | sed 's/\.[^.]*$//'`
#Have product name translated to ID
ID=$(curl -s ${UPWD}${CURLSILENT} ${HOST}odata/v1/Products?%24format=text/csv\&%24select=Id\&%24filter=Name%20eq%20%27$BN%27 | tail -n 1 | sed 's/\r//' )
if [ "$ID" == 'Id' -o "$ID" == "" ]; then
>&2 echo Product with name \"$BN\" not found
exit 1
fi
URL="${HOST}odata/v1/Products('$ID')/\$value"
>&2 echo Assuming URL: $URL
fi
fi
check_binaries sed unzip basename cat curl grep egrep diff
#Download the product file
FN=`curl ${UPWD} -JO "$URL" | egrep -o "'.*'" | sed "s/'//g"`
if [ "$FN" == "" ]; then
>&2 echo Download failed
exit 1
fi
BN=`basename -s .zip $FN`
>&2 echo Got file $FN, unzipping manifest
#Get full manifest path within thi ZIP file and then extract it
MAN=`unzip -Z1 $FN | grep manifest`
unzip -qq $FN $MAN
#Take only 'href="..."' sections from the manifest, which is in XML, and write them in a list
cat $MAN | egrep -o 'href="[^"]*"' | sed 's/^href="[./]*//' | sed 's/"$//' | sort > $BN.manifest.lst
#List all files in the ZIP, remove the leading directory name and remove directory names (leave only regular files)
unzip -Z1 $FN | sed "s/$BN\\.SAFE\///" | egrep -v "/$" | sort > $BN.real.lst
if [ $GRANULEONLY -eq 1 ]; then
diff "$BN.manifest.lst" "$BN.real.lst" | grep 'GRANULE/'
if [ $? -gt 0 ]; then
let RET=0
else
let RET=1
fi
echo $RET
else
diff "$BN.manifest.lst" "$BN.real.lst"
RET=$?
fi
if [ $VERBOSE -ne 1 ]; then
rm -rf ${BN}.manifest.lst ${BN}.real.lst ${BN}.SAFE ${BN}.zip
fi
exit $RET