Get (optionally compressed) product.xml from repomd
[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 product product.xml`
120   if test -z "$prodxml" ; then
121     prodxml=`repomd_findfile products products.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.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.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 -n "$deltainfoxml" -a -s "$deltainfoxml" ; then
169       deltainfofile=`mktemp` || exit 3
170       repomd_decompress "$deltainfoxml" | deltainfoxml2solv $parser_options > $deltainfofile || exit 4
171   fi
172
173   # Now merge primary, patches, updateinfo, and deltainfo
174   mergesolv $repomdfile $suseinfofile $primfile $prodfile $patternfile $updateinfofile $deltainfofile
175   rm -f $repomdfile $suseinfofile $primfile $patternfile $prodfile $updateinfofile $deltainfofile
176
177 elif test "$repotype" = susetags ; then
178   olddir=`pwd`
179   DESCR=$(get_DESCRDIR)
180   cd ${DESCR} || exit 2
181   (
182     # First packages
183     if test -s packages.gz; then
184       gzip -dc packages.gz
185     elif test -s packages.bz2; then
186       bzip2 -dc packages.bz2
187     elif test -s packages; then
188       cat packages
189     fi
190
191     # DU
192     if test -s packages.DU.gz; then
193       gzip -dc packages.DU.gz
194     elif test -s packages.DU.bz2; then
195       bzip2 -dc packages.DU.bz2
196     elif test -s packages.DU; then
197       cat packages.DU
198     fi
199
200     # Now default language
201     if test -s packages.en.gz; then
202       gzip -dc packages.en.gz
203     elif test -s packages.en.bz2; then
204       bzip2 -dc packages.en.bz2
205     elif test -s packages.en; then
206       cat packages.en
207     fi
208
209     # Now patterns.  Not simply those files matching *.pat{,.gz,bz2},
210     # but only those mentioned in the file 'patterns'
211     if test -f patterns; then
212       for i in `cat patterns`; do
213         test -s "$i" || continue
214         case $i in
215           *.gz) gzip -dc "$i" ;;
216           *.bz2) bzip2 -dc "$i" ;;
217           *) cat "$i" ;;
218         esac
219       done
220     fi
221
222     # Now all other packages.{lang}.  Needs to come last as it switches
223     # languages for all following susetags files
224     for i in packages.*; do
225       case $i in
226         *.gz) name="${i%.gz}" ; prog="gzip -dc" ;;
227         *.bz2) name="${i%.bz2}" ; prog="bzip2 -dc" ;;
228         *) name="$i"; prog=cat ;;
229       esac
230       case $name in
231         # ignore files we handled already
232         *.DU | *.en | *.FL | packages ) continue ;;
233         *)
234           suff=${name#packages.}
235           echo "=Lan: $suff"
236           $prog "$i" ;;
237       esac
238     done
239
240   ) | susetags2solv -c "${olddir}/content" $parser_options || exit 4
241   cd "$olddir"
242 elif test "$repotype" = plaindir ; then
243   find * -name .\* -prune -o $findopt -name \*.delta.rpm -o -name \*.patch.rpm -o -name \*.rpm -a -type f -print0 | rpms2solv -0 -m -
244 else
245   echo "unknown repository type '$repotype'" >&2
246   exit 1
247 fi