- add '-o' option
[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 # 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 if test "$1" = "-o" ; then
28   exec > "$2"
29   shift
30   shift
31 fi
32
33 dir="$1"
34 cd "$dir" || exit 1
35 if test -d repodata ; then
36   cd repodata || exit 2
37
38   primfile="/nonexist"
39   if test -f primary.xml || test -f primary.xml.gz || test -f primary.xml.bz2 ; then
40     primfile=`mktemp` || exit 3
41     (
42      # fake tag to combine primary.xml and extensions
43      # like susedata.xml, other.xml, filelists.xml
44      echo '<rpmmd>'
45      for i in primary.xml* susedata.xml*; do
46        test -s "$i" || continue
47        case $i in
48          *.gz) gzip -dc "$i";;
49          *.bz2) bzip2 -dc "$i";;
50          *) cat "$i";;
51        esac
52        # add a newline
53        echo
54        # only the first
55        break
56      done
57      for i in susedata.xml*; do
58        test -s "$i" || continue
59        case $i in
60          *.gz) gzip -dc "$i";;
61          *.bz2) bzip2 -dc "$i";;
62          *) cat "$i";;
63        esac
64        # only the first
65        break
66      done
67      echo '</rpmmd>'
68     ) | grep -v '\?xml' |  sed '1i\<?xml version="1.0" encoding="UTF-8"?>' | rpmmd2solv $parser_options > $primfile || exit 4
69   fi
70
71   prodfile="/nonexist"
72   if test -f product.xml; then
73     prodfile=`mktemp` || exit 3
74     (
75      echo '<products>'
76      for i in product*.xml*; do
77        case $i in
78          *.gz) gzip -dc "$i" ;;
79          *.bz2) bzip2 -dc "$i" ;;
80          *) cat "$i" ;;
81        esac
82      done
83      echo '</products>'
84     ) | grep -v '\?xml' | rpmmd2solv $parser_options > $prodfile || exit 4
85   fi
86
87   cmd=
88   patternfile="/nonexist"
89   for i in patterns.xml*; do
90     test -s "$i" || continue
91     case $i in
92       *.gz) cmd='gzip -dc' ;;
93       *.bz2) cmd='bzip2 -dc' ;;
94       *) cmd='cat' ;;
95     esac
96     break
97   done
98   if test -n "$cmd" ; then
99     patternfile=`mktemp` || exit 3
100     $cmd "$i" | rpmmd2solv $parser_options > $patternfile || exit 4
101   fi
102
103   # This contains repomd.xml
104   # for now we only read some keys like timestamp
105   cmd=
106   for i in repomd.xml*; do
107       test -s "$i" || continue
108       case $i in
109           *.gz) cmd="gzip -dc" ;;
110           *.bz2) cmd="bzip2 -dc" ;;
111           *) cmd="cat" ;;
112       esac
113       # only check the first repomd.xml*, in case there are more
114       break
115   done
116   repomdfile="/nonexist"
117   if test -n "$cmd"; then
118       # we have some repomd.xml*
119       repomdfile=`mktemp` || exit 3
120       $cmd "$i" | repomdxml2solv $parser_options > $repomdfile || exit 4
121   fi
122
123   # This contains suseinfo.xml, which is extensions to repomd.xml
124   # for now we only read some keys like expiration and products
125   cmd=
126   for i in suseinfo.xml*; do
127       test -s "$i" || continue
128       case $i in
129           *.gz) cmd="gzip -dc" ;;
130           *.bz2) cmd="bzip2 -dc" ;;
131           *) cmd="cat" ;;
132       esac
133       # only check the first suseinfo.xml*, in case there are more
134       break
135   done
136   suseinfofile="/nonexist"
137   if test -n "$cmd"; then
138       # we have some suseinfo.xml*
139       suseinfofile=`mktemp` || exit 3
140       $cmd "$i" | repomdxml2solv $parser_options > $suseinfofile || exit 4
141   fi
142
143   # This contains a updateinfo.xml* and maybe patches
144   cmd=
145   for i in updateinfo.xml*; do
146       case $i in
147           *.gz) cmd="gzip -dc" ;;
148           *.bz2) cmd="bzip2 -dc" ;;
149           *) cmd="cat" ;;
150       esac
151       # only check the first updateinfo.xml*, in case there are more
152       break
153   done
154   updateinfofile="/nonexist"
155   if test -n "$cmd"; then
156       # we have some updateinfo.xml*
157       updateinfofile=`mktemp` || exit 3
158       $cmd "$i" | updateinfoxml2solv $parser_options > $updateinfofile || exit 4
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   cmd=
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
195   # Now merge primary, patches, updateinfo, and deltainfo
196   if test -s $repomdfile; then
197     m_repomdfile=$repomdfile
198   fi
199   if test -s $suseinfofile; then
200     m_suseinfofile=$suseinfofile
201   fi
202   if test -s $primfile; then
203     m_primfile=$primfile
204   fi
205   if test -s $patternfile; then
206     m_patternfile=$patternfile
207   fi
208   if test -s $prodfile; then
209     m_prodfile=$prodfile
210   fi
211   if test -s $patchfile; then
212     m_patchfile=$patchfile
213   fi
214   if test -s $updateinfofile; then
215     m_updateinfofile=$updateinfofile
216   fi
217   if test -s $deltainfofile; then
218     m_deltainfofile=$deltainfofile
219   fi
220   mergesolv $m_repomdfile $m_suseinfofile $m_primfile $m_prodfile $m_patternfile $m_patchfile $m_updateinfofile $m_deltainfofile
221   rm -f $repomdfile $suseinfofile $primfile $patternfile $prodfile $patchfile $updateinfofile $deltainfofile
222
223 elif test_susetags; then
224   olddir=`pwd`
225   DESCR=`grep DESCRDIR content | cut -d ' ' -f 2`
226   if test -z $DESCR; then
227     DESCR=suse/setup/descr
228   fi
229   cd ${DESCR} || exit 2
230   (
231     # First packages
232     if test -s packages.gz; then
233       gzip -dc packages.gz
234     elif test -s packages.bz2; then
235       bzip2 -dc packages.bz2
236     elif test -s packages; then
237       cat packages
238     fi
239
240     # DU
241     if test -s packages.DU.gz; then
242       gzip -dc packages.DU.gz
243     elif test -s packages.DU.bz2; then
244       bzip2 -dc packages.DU.bz2
245     elif test -s packages.DU; then
246       cat packages.DU
247     fi
248
249     # Now default language
250     if test -s packages.en.gz; then
251       gzip -dc packages.en.gz
252     elif test -s packages.en.bz2; then
253       bzip2 -dc packages.en.bz2
254     elif test -s packages.en; then
255       cat packages.en
256     fi
257
258     # Now patterns.  Not simply those files matching *.pat{,.gz,bz2},
259     # but only those mentioned in the file 'patterns'
260     if test -f patterns; then
261       for i in `cat patterns`; do
262         test -s "$i" || continue
263         case $i in
264           *.gz) gzip -dc "$i" ;;
265           *.bz2) bzip2 -dc "$i" ;;
266           *) cat "$i" ;;
267         esac
268       done
269     fi
270
271     # Now all other packages.{lang}.  Needs to come last as it switches
272     # languages for all following susetags files
273     for i in packages.*; do
274       case $i in
275         *.gz) name="${i%.gz}" ; prog="gzip -dc" ;;
276         *.bz2) name="${i%.bz2}" ; prog="bzip2 -dc" ;;
277         *) name="$i"; prog=cat ;;
278       esac
279       case $name in
280         # ignore files we handled already
281         *.DU | *.en | *.FL | packages ) continue ;;
282         *) 
283           suff=${name#packages.}
284           echo "=Lan: $suff"
285           $prog "$i" ;;
286       esac
287     done
288
289   ) | susetags2solv -c "${olddir}/content" $parser_options || exit 4
290   cd "$olddir"
291 else
292   rpms=''
293   for r in *.rpm ; do
294     test -e "$r" || continue
295     rpms="$rpms
296 $r"
297   done
298   if test -n "$rpms" ; then
299       echo "${rpms#?}" | rpms2solv -m -
300   else
301       exit 1
302   fi
303 fi