4b595e8fa4e8a6105e8b507ee4425cd2e2603959
[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   elif test "$1" = "-A" ; then
108     shift
109   else
110     break
111   fi
112 done
113
114 dir="$1"
115 cd "$dir" || exit 1
116
117 if test -z "$repotype" ; then
118   # autodetect repository type
119   if test -d repodata -o -f repomd.xml; then
120     repotype=rpmmd
121   elif test_susetags ; then
122     repotype=susetags
123   else
124     repotype=plaindir
125   fi
126 fi
127
128 if test "$repotype" = rpmmd ; then
129   test -d repodata && {
130     cd repodata || exit 2
131   }
132
133   primfile=
134   primxml=`repomd_findfile primary primary.xml`
135   if test -n "$primxml" -a -s "$primxml" ; then
136     primfile=`mktemp` || exit 3
137     (
138      # fake tag to combine primary.xml and extensions
139      # like susedata.xml, other.xml, filelists.xml
140      echo '<rpmmd>'
141      if test -f $primxml ; then
142         repomd_decompress $primxml
143          # add a newline
144         echo
145      fi
146      susedataxml=`repomd_findfile susedata susedata.xml`
147      if test -f "$susedataxml" ; then
148         repomd_decompress "$susedataxml"
149      fi
150      echo '</rpmmd>'
151     ) | grep -v '<?xml' |  sed '1i\<?xml version="1.0" encoding="UTF-8"?>' | rpmmd2solv $parser_options > $primfile || exit 4
152   fi
153
154   prodfile=
155   prodxml=`repomd_findfile products products.xml`
156   if test -z "$prodxml" ; then
157     prodxml=`repomd_findfile product product.xml`
158   fi
159   if test -n "$prodxml" -a -s "$prodxml" ; then
160       prodfile=`mktemp` || exit 3
161       repomd_decompress "$prodxml" | rpmmd2solv $parser_options > $prodfile || exit 4
162   fi
163
164   patternfile=
165   patternxml=`repomd_findfile 'patterns' patterns.xml`
166   if test -n "$patternxml" -a -s "$patternxml" ; then
167       patternfile=`mktemp` || exit 3
168       repomd_decompress "$patternxml" | rpmmd2solv $parser_options > $patternfile || exit 4
169   fi
170
171   # This contains repomd.xml
172   # for now we only read some keys like timestamp
173   repomdfile=
174   repomdxml=`repomd_findfile '' repomd.xml`
175   if test -n "$repomdxml" -a -s "$repomdxml" ; then
176       repomdfile=`mktemp` || exit 3
177       repomd_decompress "$repomdxml" | repomdxml2solv $parser_options > $repomdfile || exit 4
178   fi
179
180   # This contains suseinfo.xml, which is an extension to repomd.xml
181   # for now we only read some keys like expiration and products
182   suseinfofile=
183   suseinfoxml=`repomd_findfile suseinfo suseinfo.xml`
184   if test -n "$suseinfoxml" -a -s "$suseinfoxml" ; then
185       suseinfofile=`mktemp` || exit 3
186       repomd_decompress "$suseinfoxml" | repomdxml2solv $parser_options > $suseinfofile || exit 4
187   fi
188
189   # This contains a updateinfo.xml* and maybe patches
190   updateinfofile=
191   updateinfoxml=`repomd_findfile updateinfo updateinfo.xml`
192   if test -n "$updateinfoxml" -a -s "$updateinfoxml" ; then
193       updateinfofile=`mktemp` || exit 3
194       repomd_decompress "$updateinfoxml" | updateinfoxml2solv $parser_options > $updateinfofile || exit 4
195   fi
196
197   # This contains a deltainfo.xml*
198   deltainfofile=
199   deltainfoxml=`repomd_findfile deltainfo deltainfo.xml`
200   if test -z "$deltainfoxml"; then 
201       deltainfoxml=`repomd_findfile prestodelta prestodelta.xml`
202   fi
203   if test -n "$deltainfoxml" -a -s "$deltainfoxml" ; then
204       deltainfofile=`mktemp` || exit 3
205       repomd_decompress "$deltainfoxml" | deltainfoxml2solv $parser_options > $deltainfofile || exit 4
206   fi
207
208   # This contains appdata
209   appdataxml=
210   appdatafile=
211   if test -x /usr/bin/appdata2solv ; then
212       appdataxml=`repomd_findfile appdata appdata.xml`
213   fi
214   if test -n "$appdataxml" -a -s "$appdataxml" ; then
215       appdatafile=`mktemp` || exit 3
216       repomd_decompress "$appdataxml" | appdata2solv $parser_options > $appdatafile || exit 4
217   fi
218
219   # Now merge primary, patches, updateinfo, and deltainfo
220   mergesolv $addautooption $repomdfile $suseinfofile $primfile $prodfile $patternfile $updateinfofile $deltainfofile $appdatafile
221   rm -f $repomdfile $suseinfofile $primfile $patternfile $prodfile $updateinfofile $deltainfofile $appdatafile
222
223 elif test "$repotype" = susetags ; then
224   olddir=`pwd`
225   DESCR=$(get_DESCRDIR)
226   cd ${DESCR} || exit 2
227   appdataxml=
228   appdatafile=
229   if test -x /usr/bin/appdata2solv ; then
230       appdataxml=`susetags_findfile appdata.xml`
231   fi
232   if test -n "$appdataxml" ; then
233       appdatafile=`mktemp` || exit 3
234       repomd_decompress "$appdataxml" | appdata2solv $parser_options > $appdatafile || exit 4
235       parser_options="-M $appdatafile $parser_options"
236   fi
237   (
238     # First packages
239     susetags_findfile_cat packages
240
241     # DU
242     susetags_findfile_cat packages.DU
243
244     # Now default language
245     susetags_findfile_cat packages.en
246
247     # Now patterns.  Not simply those files matching *.pat{,.gz,bz2},
248     # but only those mentioned in the file 'patterns'
249     if test -f patterns ; then
250       for i in `cat patterns`; do
251         if test -s "$i" ; then
252           repomd_decompress "$i"
253         fi
254       done
255     fi
256
257     # Now all other packages.{lang}.  Needs to come last as it switches
258     # languages for all following susetags files
259     for i in packages.* ; do
260       case $i in
261         *.gz|*.bz2|*.xz|*.lzma) name="${i%.*}" ;;
262         *) name="$i" ;;
263       esac
264       case $name in
265         # ignore files we handled already
266         *.DU | *.en | *.FL | packages ) continue ;;
267         *)
268           suff=${name#packages.}
269           echo "=Lan: $suff"
270           repomd_decompress "$i"
271       esac
272     done
273
274   ) | susetags2solv $addautooption -c "${olddir}/content" $parser_options || exit 4
275   test -n "$appdatafile" && rm -f "$appdatafile"
276   cd "$olddir"
277 elif test "$repotype" = plaindir ; then
278   find * -name .\* -prune -o $findopt -name \*.delta.rpm -o -name \*.patch.rpm -o -name \*.rpm -a -type f -print0 | rpms2solv $addautooption -0 -m -
279 else
280   echo "unknown repository type '$repotype'" >&2
281   exit 1
282 fi