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