- use repomdxml to query for the location (bnc#501425)
[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   if test -f product.xml; then
120     prodfile=`mktemp` || exit 3
121     (
122      echo '<products>'
123      for i in product*.xml*; do
124        repomd_decompress $i
125      done
126      echo '</products>'
127     ) | grep -v '\?xml' | rpmmd2solv $parser_options > $prodfile || exit 4
128   fi
129
130   patternfile=
131   patternxml=`repomd_findfile '' patterns.xml`
132   if test -n "$patternxml" -a -s "$patternxml" ; then
133       patternfile=`mktemp` || exit 3
134       repomd_decompress "$patternxml" | rpmmd2solv $parser_options > $patternfile || exit 4
135   fi
136
137   # This contains repomd.xml
138   # for now we only read some keys like timestamp
139   repomdfile=
140   repomdxml=`repomd_findfile '' repomd.xml`
141   if test -n "$repomdxml" -a -s "$repomdxml" ; then
142       repomdfile=`mktemp` || exit 3
143       repomd_decompress "$repomdxml" | repomdxml2solv $parser_options > $repomdfile || exit 4
144   fi
145
146   # This contains suseinfo.xml, which is an extension to repomd.xml
147   # for now we only read some keys like expiration and products
148   suseinfofile=
149   suseinfoxml=`repomd_findfile '' suseinfo.xml`
150   if test -n "$suseinfoxml" -a -s "$suseinfoxml" ; then
151       suseinfofile=`mktemp` || exit 3
152       repomd_decompress "$suseinfoxml" | repomdxml2solv $parser_options > $suseinfofile || exit 4
153   fi
154
155   # This contains a updateinfo.xml* and maybe patches
156   updateinfofile=
157   updateinfoxml=`repomd_findfile updateinfo updateinfo.xml`
158   if test -n "$updateinfoxml" -a -s "$updateinfoxml" ; then
159       updateinfofile=`mktemp` || exit 3
160       repomd_decompress "$updateinfoxml" | updateinfoxml2solv $parser_options > $updateinfofile || exit 4
161   fi
162
163   # This contains a deltainfo.xml*
164   deltainfofile=
165   deltainfoxml=`repomd_findfile deltainfo deltainfo.xml`
166   if test -n "$deltainfoxml" -a -s "$deltainfoxml" ; then
167       deltainfofile=`mktemp` || exit 3
168       repomd_decompress "$deltainfoxml" | deltainfoxml2solv $parser_options > $deltainfofile || exit 4
169   fi
170
171   # Now merge primary, patches, updateinfo, and deltainfo
172   mergesolv $repomdfile $suseinfofile $primfile $prodfile $patternfile $updateinfofile $deltainfofile
173   rm -f $repomdfile $suseinfofile $primfile $patternfile $prodfile $updateinfofile $deltainfofile
174
175 elif test "$repotype" = susetags ; then
176   olddir=`pwd`
177   DESCR=$(get_DESCRDIR)
178   cd ${DESCR} || exit 2
179   (
180     # First packages
181     if test -s packages.gz; then
182       gzip -dc packages.gz
183     elif test -s packages.bz2; then
184       bzip2 -dc packages.bz2
185     elif test -s packages; then
186       cat packages
187     fi
188
189     # DU
190     if test -s packages.DU.gz; then
191       gzip -dc packages.DU.gz
192     elif test -s packages.DU.bz2; then
193       bzip2 -dc packages.DU.bz2
194     elif test -s packages.DU; then
195       cat packages.DU
196     fi
197
198     # Now default language
199     if test -s packages.en.gz; then
200       gzip -dc packages.en.gz
201     elif test -s packages.en.bz2; then
202       bzip2 -dc packages.en.bz2
203     elif test -s packages.en; then
204       cat packages.en
205     fi
206
207     # Now patterns.  Not simply those files matching *.pat{,.gz,bz2},
208     # but only those mentioned in the file 'patterns'
209     if test -f patterns; then
210       for i in `cat patterns`; do
211         test -s "$i" || continue
212         case $i in
213           *.gz) gzip -dc "$i" ;;
214           *.bz2) bzip2 -dc "$i" ;;
215           *) cat "$i" ;;
216         esac
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) name="${i%.gz}" ; prog="gzip -dc" ;;
225         *.bz2) name="${i%.bz2}" ; prog="bzip2 -dc" ;;
226         *) name="$i"; prog=cat ;;
227       esac
228       case $name in
229         # ignore files we handled already
230         *.DU | *.en | *.FL | packages ) continue ;;
231         *)
232           suff=${name#packages.}
233           echo "=Lan: $suff"
234           $prog "$i" ;;
235       esac
236     done
237
238   ) | susetags2solv -c "${olddir}/content" $parser_options || exit 4
239   cd "$olddir"
240 elif test "$repotype" = plaindir ; then
241   find * -name .\* -prune -o $findopt -name \*.delta.rpm -o -name \*.patch.rpm -o -name \*.rpm -a -type f -print0 | rpms2solv -0 -m -
242 else
243   echo "unknown repository type '$repotype'" >&2
244   exit 1
245 fi