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