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