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