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