Imported Upstream version 0.6.34
[platform/upstream/libsolv.git] / examples / repo2solv.sh
1 #! /bin/sh
2 # repo2solv
3 #
4 # this is a shell implementation of repo2solv that makes use of
5 # the different parser utilities
6 #
7 # give it a directory of a local mirror of a repo and this
8 # tries to detect the repo type and generate one SOLV file on stdout
9
10 get_DESCRDIR () {
11   local d=$(grep '^DESCRDIR' content | sed 's/^DESCRDIR[[:space:]]\+\(.*[^[:space:]]\)[[:space:]]*$/\1/')
12   if  test -z "$d"; then
13     echo suse/setup/descr
14   else
15     echo ${d}
16   fi
17 }
18
19 test_susetags() {
20   if test -s content; then
21     DESCR=$(get_DESCRDIR)
22     test -d $DESCR
23     return $?
24   else
25     return 1
26   fi
27 }
28
29 repomd_findfile() {
30   local t=$1
31   local p=$2
32   local f
33   if test -n "$t" -a -s repomd.xml ; then
34     f=`repomdxml2solv -q "$t:location" < repomd.xml 2>/dev/null`
35     f=${f##*/}
36     if test -f "$f" ; then
37       echo "$f"
38       return
39     fi
40   fi
41   if test -f "$p.bz2" ; then
42     echo "$p.bz2"
43   elif test -f "$p.gz" ; then
44     echo "$p.gz"
45   elif test -f "$p" ; then
46     echo "$p"
47   fi
48 }
49
50 repomd_types() {
51   test -s repomd.xml && repomdxml2solv -q type < repomd.xml
52 }
53
54 repomd_decompress() {
55   case $1 in
56    *.gz) gzip -dc "$1" ;;
57    *.bz2) bzip2 -dc "$1" ;;
58    *.lzma) lzma -dc "$1" ;;
59    *.xz) xz -dc "$1" ;;
60    *) cat "$1" ;;
61   esac
62 }
63
64 susetags_findfile() {
65   if test -s "$1.xz" ; then
66     echo "$1.xz"
67   elif test -s "$1.lzma" ; then
68     echo "$1.lzma"
69   elif test -s "$1.bz2" ; then
70     echo "$1.bz2"
71   elif test -s "$1.gz" ; then
72     echo "$1.gz"
73   fi
74 }
75
76 susetags_findfile_cat() {
77   if test -s "$1.xz" ; then
78     xz -dc "$1.xz"
79   elif test -s "$1.lzma" ; then
80     lzma -dc "$1.lzma"
81   elif test -s "$1.bz2" ; then
82     bzip2 -dc "$1.bz2"
83   elif test -s "$1.gz" ; then
84     gzip -dc "$1.gz"
85   elif test -s "$1" ; then
86     cat "$1"
87   fi
88 }
89
90 # signal an error if there is a problem
91 set -e
92
93 LANG=C
94 unset CDPATH
95 parser_options=${PARSER_OPTIONS:-}
96
97 findopt="-prune"
98 repotype=
99 addautooption=
100
101 while true ; do
102   if test "$1" = "-o" ; then
103     exec > "$2"
104     shift
105     shift
106   elif test "$1" = "-R" ; then
107     # recursive
108     findopt=
109     repotype=plaindir
110     shift
111   elif test "$1" = "-X" ; then
112     addautooption=-X
113     shift
114   elif test "$1" = "-A" ; then
115     shift
116   else
117     break
118   fi
119 done
120
121 dir="$1"
122 cd "$dir" || exit 1
123
124 if test -z "$repotype" ; then
125   # autodetect repository type
126   if test -d repodata -o -f repomd.xml; then
127     repotype=rpmmd
128   elif test_susetags ; then
129     repotype=susetags
130   else
131     repotype=plaindir
132   fi
133 fi
134
135 if test "$repotype" = rpmmd ; then
136   test -d repodata && {
137     cd repodata || exit 2
138   }
139
140   primfile=
141   primxml=`repomd_findfile primary primary.xml`
142   if test -n "$primxml" -a -s "$primxml" ; then
143     primfile=`mktemp` || exit 3
144     (
145      # fake tag to combine primary.xml and extensions
146      # like susedata.xml, other.xml, filelists.xml
147      echo '<rpmmd>'
148      if test -f $primxml ; then
149         repomd_decompress $primxml
150          # add a newline
151         echo
152      fi
153      susedataxml=`repomd_findfile susedata susedata.xml`
154      if test -f "$susedataxml" ; then
155         repomd_decompress "$susedataxml"
156      fi
157      # all the languages as well
158      for t in `repomd_types` ; do
159         case "$t" in
160             susedata.*)
161                 susedataxml=`repomd_findfile "$t" "$t.xml"`
162                 if test -f "$susedataxml" ; then
163                     repomd_decompress "$susedataxml"
164                 fi
165             ;;
166         esac
167      done
168      echo '</rpmmd>'
169     ) | sed 's/<?xml[^>]*>//g' | sed '1i\<?xml version="1.0" encoding="UTF-8"?>' | rpmmd2solv $parser_options > $primfile || exit 4
170   fi
171
172   prodfile=
173   prodxml=`repomd_findfile products products.xml`
174   if test -z "$prodxml" ; then
175     prodxml=`repomd_findfile product product.xml`
176   fi
177   if test -n "$prodxml" -a -s "$prodxml" ; then
178       prodfile=`mktemp` || exit 3
179       repomd_decompress "$prodxml" | rpmmd2solv $parser_options > $prodfile || exit 4
180   fi
181
182   patternfile=
183   patternxml=`repomd_findfile 'patterns' patterns.xml`
184   if test -n "$patternxml" -a -s "$patternxml" ; then
185       patternfile=`mktemp` || exit 3
186       repomd_decompress "$patternxml" | rpmmd2solv $parser_options > $patternfile || exit 4
187   fi
188
189   # This contains repomd.xml
190   # for now we only read some keys like timestamp
191   repomdfile=
192   repomdxml=`repomd_findfile '' repomd.xml`
193   if test -n "$repomdxml" -a -s "$repomdxml" ; then
194       repomdfile=`mktemp` || exit 3
195       repomd_decompress "$repomdxml" | repomdxml2solv $parser_options > $repomdfile || exit 4
196   fi
197
198   # This contains suseinfo.xml, which is an extension to repomd.xml
199   # for now we only read some keys like expiration and products
200   suseinfofile=
201   suseinfoxml=`repomd_findfile suseinfo suseinfo.xml`
202   if test -n "$suseinfoxml" -a -s "$suseinfoxml" ; then
203       suseinfofile=`mktemp` || exit 3
204       repomd_decompress "$suseinfoxml" | repomdxml2solv $parser_options > $suseinfofile || exit 4
205   fi
206
207   # This contains a updateinfo.xml* and maybe patches
208   updateinfofile=
209   updateinfoxml=`repomd_findfile updateinfo updateinfo.xml`
210   if test -n "$updateinfoxml" -a -s "$updateinfoxml" ; then
211       updateinfofile=`mktemp` || exit 3
212       repomd_decompress "$updateinfoxml" | updateinfoxml2solv $parser_options > $updateinfofile || exit 4
213   fi
214
215   # This contains a deltainfo.xml*
216   deltainfofile=
217   deltainfoxml=`repomd_findfile deltainfo deltainfo.xml`
218   if test -z "$deltainfoxml"; then 
219       deltainfoxml=`repomd_findfile prestodelta prestodelta.xml`
220   fi
221   if test -n "$deltainfoxml" -a -s "$deltainfoxml" ; then
222       deltainfofile=`mktemp` || exit 3
223       repomd_decompress "$deltainfoxml" | deltainfoxml2solv $parser_options > $deltainfofile || exit 4
224   fi
225
226   # This contains appdata
227   appdataxml=
228   appdatafile=
229   if test -x /usr/bin/appdata2solv ; then
230       appdataxml=`repomd_findfile appdata appdata.xml`
231   fi
232   if test -n "$appdataxml" -a -s "$appdataxml" ; then
233       appdatafile=`mktemp` || exit 3
234       repomd_decompress "$appdataxml" | appdata2solv $parser_options > $appdatafile || exit 4
235   fi
236
237   # Now merge primary, patches, updateinfo, and deltainfo
238   mergesolv $addautooption $repomdfile $suseinfofile $primfile $prodfile $patternfile $updateinfofile $deltainfofile $appdatafile
239   rm -f $repomdfile $suseinfofile $primfile $patternfile $prodfile $updateinfofile $deltainfofile $appdatafile
240
241 elif test "$repotype" = susetags ; then
242   olddir=`pwd`
243   DESCR=$(get_DESCRDIR)
244   cd ${DESCR} || exit 2
245   appdataxml=
246   appdatafile=
247   if test -x /usr/bin/appdata2solv ; then
248       appdataxml=`susetags_findfile appdata.xml`
249   fi
250   if test -n "$appdataxml" ; then
251       appdatafile=`mktemp` || exit 3
252       repomd_decompress "$appdataxml" | appdata2solv $parser_options > $appdatafile || exit 4
253       parser_options="-M $appdatafile $parser_options"
254   fi
255   (
256     # First packages
257     susetags_findfile_cat packages
258
259     # DU
260     susetags_findfile_cat packages.DU
261
262     # Now default language
263     susetags_findfile_cat packages.en
264
265     # DL (delta rpms)
266     susetags_findfile_cat packages.DL
267
268     # Now patterns.  Not simply those files matching *.pat{,.gz,bz2},
269     # but only those mentioned in the file 'patterns'
270     if test -f patterns ; then
271       for i in `cat patterns`; do
272         if test -s "$i" ; then
273           repomd_decompress "$i"
274         fi
275       done
276     fi
277
278     # Now all other packages.{lang}.  Needs to come last as it switches
279     # languages for all following susetags files
280     for i in packages.* ; do
281       case $i in
282         *.gz|*.bz2|*.xz|*.lzma) name="${i%.*}" ;;
283         *) name="$i" ;;
284       esac
285       case $name in
286         # ignore files we handled already
287         *.DU | *.en | *.FL | *.DL | packages ) continue ;;
288         *)
289           suff=${name#packages.}
290           echo "=Lan: $suff"
291           repomd_decompress "$i"
292       esac
293     done
294
295   ) | susetags2solv $addautooption -c "${olddir}/content" $parser_options || exit 4
296   test -n "$appdatafile" && rm -f "$appdatafile"
297   cd "$olddir"
298 elif test "$repotype" = plaindir ; then
299   find * -name .\* -prune -o $findopt -name \*.delta.rpm -o -name \*.patch.rpm -o -name \*.rpm -a -type f -print0 | rpms2solv $addautooption -0 -m -
300 else
301   echo "unknown repository type '$repotype'" >&2
302   exit 1
303 fi