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