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