-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch-rpm-repo.sh
executable file
·141 lines (123 loc) · 2.84 KB
/
fetch-rpm-repo.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
usage() {
echo "Usage: $0 <primary.xml> <mirror> <destdir>"
}
read_file() {
uri="$1"
if [[ $uri = http://* ]]; then
cmd="curl -k $uri 2>/dev/null"
else
cmd="cat $uri"
fi
# unpackif necessary
if [[ $uri = *.gz ]]; then
eval $cmd | gzip -d
else
eval $cmd
fi
}
extract_data() {
name=
chksum=
chksumtype=
location=
while read line; do
if [[ $line = \<package\ * ]]; then
name=
chksum=
chksumtype=
location=
fi
if [[ $line = \<name* ]]; then
name=$(echo $line | sed -e "s;<name>;;g" -e "s;</name>;;g")
continue
fi
if [[ $line = \<checksum*sha256* ]]; then
chksum=$(echo $line | sed -e 's;<checksum type="sha256" pkgid="YES">;;g' -e "s;</checksum>;;g")
chksumtype=sha256
continue
fi
if [[ $line = \<location* ]]; then
location=$(echo $line | sed -e "s;<location href=;;g" -e "s;/>;;g" -e 's;";;g')
continue
fi
if [[ $line = \</package\> ]]; then
echo $name $mirror/$location $chksumtype $chksum
continue
fi
done
}
download() {
while read line; do
name=$(echo $line | cut -d' ' -f1)
location=$(echo $line | cut -d' ' -f2)
chksumtype=$(echo $line | cut -d' ' -f3)
chksum=$(echo $line | cut -d' ' -f4)
filename=`basename $location`
printf "%s: " $name
if [ -e "$destdir/$filename" ]; then
# check if chksum matches
matches=no
if [ "$chksumtype" = "sha256" ]; then
echo "$chksum $destdir/$filename" | sha256sum -c 2>/dev/null 1>/dev/null
if [ $? = 0 ]; then
matches=yes
fi
fi
# if checksum matches, keep the file
if [ "$matches" = "yes" ]; then
status="Skipped"
else
# get rid of the bad file
rm -f $filename
# then download
curl -k -o "$destdir/$filename" "$location" 1>/dev/null 2>/dev/null
if [ $? == 0 ]; then
# check if checksum matches now
matches=no
if [ "$chksumtype" = "sha256" ]; then
echo "$chksum $destdir/$filename" | sha256sum -c 2>/dev/null 1>/dev/null
if [ $? = 0 ]; then matches=yes; fi
fi
if [ "$matches" = "yes" ]; then
status="Replaced"
else
status="Failed"
rm -f $filename
fi
else
status="Failed"
fi
fi
else
# download
curl -k -o "$destdir/$filename" "$location" 1>/dev/null 2>/dev/null
if [ $? == 0 ]; then
# check if checksum matches
matches=no
if [ "$chksumtype" = "sha256" ]; then
echo "$chksum $destdir/$filename" | sha256sum -c 2>/dev/null 1>/dev/null
[ $? = 0 ] && matches=yes
fi
if [ "$matches" = "yes" ]; then
status="Done"
else
status="Failed"
rm -f $filename
fi
else
status="Failed"
fi
fi
printf "%s\n" $status
done
}
if [ "x$#" != "x3" ]; then
usage
exit 0
fi
metadata="$1"
mirror="$2"
destdir="$3"
#read_file "$metadata" | grep "<location href=" | strip_xml | prepend_mirror | download
read_file "$metadata" | extract_data | download