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