add a 'g' to the sed line
[platform/upstream/libsolv.git] / tools / repo2solv.sh
index 82cf382..11c6567 100755 (executable)
@@ -7,7 +7,7 @@
 get_DESCRDIR () {
   local d=$(grep '^DESCRDIR' content | sed 's/^DESCRDIR[[:space:]]\+\(.*[^[:space:]]\)[[:space:]]*$/\1/')
   if  test -z "$d"; then
-    echo suse/setup/desc
+    echo suse/setup/descr
   else
     echo ${d}
   fi
@@ -23,6 +23,63 @@ test_susetags() {
   fi
 }
 
+repomd_findfile() {
+  local t=$1
+  local p=$2
+  local f
+  if test -n "$t" -a -s repomd.xml ; then
+    f=`repomdxml2solv -q $t:location < repomd.xml 2>/dev/null`
+    f=${f##*/}
+    if test -f "$f" ; then
+      echo "$f"
+      return
+    fi
+  fi
+  if test -f "$p.bz2" ; then
+    echo "$p.bz2"
+  elif test -f "$p.gz" ; then
+    echo "$p.gz"
+  elif test -f "$p" ; then
+    echo "$p"
+  fi
+}
+
+repomd_decompress() {
+  case $1 in
+   *.gz) gzip -dc "$1" ;;
+   *.bz2) bzip2 -dc "$1" ;;
+   *.lzma) lzma -dc "$1" ;;
+   *.xz) xz -dc "$1" ;;
+   *) cat "$1" ;;
+  esac
+}
+
+susetags_findfile() {
+  if test -s "$1.xz" ; then
+    echo "$1.xz"
+  elif test -s "$1.lzma" ; then
+    echo "$1.lzma"
+  elif test -s "$1.bz2" ; then
+    echo "$1.bz2"
+  elif test -s "$1.gz" ; then
+    echo "$1.gz"
+  fi
+}
+
+susetags_findfile_cat() {
+  if test -s "$1.xz" ; then
+    xz -dc "$1.xz"
+  elif test -s "$1.lzma" ; then
+    lzma -dc "$1.lzma"
+  elif test -s "$1.bz2" ; then
+    bzip2 -dc "$1.bz2"
+  elif test -s "$1.gz" ; then
+    gzip -dc "$1.gz"
+  elif test -s "$1" ; then
+    cat "$1"
+  fi
+}
+
 # signal an error if there is a problem
 set -e
 
@@ -32,6 +89,7 @@ parser_options=${PARSER_OPTIONS:-}
 
 findopt="-prune"
 repotype=
+addautooption=
 
 while true ; do
   if test "$1" = "-o" ; then
@@ -43,6 +101,11 @@ while true ; do
     findopt=
     repotype=plaindir
     shift
+  elif test "$1" = "-X" ; then
+    addautooption=-X
+    shift
+  elif test "$1" = "-A" ; then
+    shift
   else
     break
   fi
@@ -53,7 +116,7 @@ cd "$dir" || exit 1
 
 if test -z "$repotype" ; then
   # autodetect repository type
-  if test -d repodata ; then
+  if test -d repodata -o -f repomd.xml; then
     repotype=rpmmd
   elif test_susetags ; then
     repotype=susetags
@@ -63,228 +126,140 @@ if test -z "$repotype" ; then
 fi
 
 if test "$repotype" = rpmmd ; then
-  cd repodata || exit 2
+  test -d repodata && {
+    cd repodata || exit 2
+  }
 
-  primfile="/nonexist"
-  if test -f primary.xml || test -f primary.xml.gz || test -f primary.xml.bz2 ; then
+  primfile=
+  primxml=`repomd_findfile primary primary.xml`
+  if test -n "$primxml" -a -s "$primxml" ; then
     primfile=`mktemp` || exit 3
     (
      # fake tag to combine primary.xml and extensions
      # like susedata.xml, other.xml, filelists.xml
      echo '<rpmmd>'
-     for i in primary.xml* susedata.xml*; do
-       test -s "$i" || continue
-       case $i in
-         *.gz) gzip -dc "$i";;
-        *.bz2) bzip2 -dc "$i";;
-        *) cat "$i";;
-       esac
-       # add a newline
-       echo
-       # only the first
-       break
-     done
-     for i in susedata.xml*; do
-       test -s "$i" || continue
-       case $i in
-         *.gz) gzip -dc "$i";;
-        *.bz2) bzip2 -dc "$i";;
-         *) cat "$i";;
-       esac
-       # only the first
-       break
-     done
+     if test -f $primxml ; then
+       repomd_decompress $primxml
+         # add a newline
+        echo
+     fi
+     susedataxml=`repomd_findfile susedata susedata.xml`
+     if test -f "$susedataxml" ; then
+       repomd_decompress "$susedataxml"
+     fi
      echo '</rpmmd>'
-    ) | grep -v '\?xml' |  sed '1i\<?xml version="1.0" encoding="UTF-8"?>' | rpmmd2solv $parser_options > $primfile || exit 4
+    ) | sed 's/<?xml[^>]*>//g' | sed '1i\<?xml version="1.0" encoding="UTF-8"?>' | rpmmd2solv $parser_options > $primfile || exit 4
   fi
 
-  prodfile="/nonexist"
-  if test -f product.xml; then
-    prodfile=`mktemp` || exit 3
-    (
-     echo '<products>'
-     for i in product*.xml*; do
-       case $i in
-         *.gz) gzip -dc "$i" ;;
-        *.bz2) bzip2 -dc "$i" ;;
-        *) cat "$i" ;;
-       esac
-     done
-     echo '</products>'
-    ) | grep -v '\?xml' | rpmmd2solv $parser_options > $prodfile || exit 4
+  prodfile=
+  prodxml=`repomd_findfile products products.xml`
+  if test -z "$prodxml" ; then
+    prodxml=`repomd_findfile product product.xml`
+  fi
+  if test -n "$prodxml" -a -s "$prodxml" ; then
+      prodfile=`mktemp` || exit 3
+      repomd_decompress "$prodxml" | rpmmd2solv $parser_options > $prodfile || exit 4
   fi
 
-  cmd=
-  patternfile="/nonexist"
-  for i in patterns.xml*; do
-    test -s "$i" || continue
-    case $i in
-      *.gz) cmd='gzip -dc' ;;
-      *.bz2) cmd='bzip2 -dc' ;;
-      *) cmd='cat' ;;
-    esac
-    break
-  done
-  if test -n "$cmd" ; then
-    patternfile=`mktemp` || exit 3
-    $cmd "$i" | rpmmd2solv $parser_options > $patternfile || exit 4
+  patternfile=
+  patternxml=`repomd_findfile 'patterns' patterns.xml`
+  if test -n "$patternxml" -a -s "$patternxml" ; then
+      patternfile=`mktemp` || exit 3
+      repomd_decompress "$patternxml" | rpmmd2solv $parser_options > $patternfile || exit 4
   fi
 
   # This contains repomd.xml
   # for now we only read some keys like timestamp
-  cmd=
-  for i in repomd.xml*; do
-      test -s "$i" || continue
-      case $i in
-         *.gz) cmd="gzip -dc" ;;
-         *.bz2) cmd="bzip2 -dc" ;;
-         *) cmd="cat" ;;
-      esac
-      # only check the first repomd.xml*, in case there are more
-      break
-  done
-  repomdfile="/nonexist"
-  if test -n "$cmd"; then
-      # we have some repomd.xml*
+  repomdfile=
+  repomdxml=`repomd_findfile '' repomd.xml`
+  if test -n "$repomdxml" -a -s "$repomdxml" ; then
       repomdfile=`mktemp` || exit 3
-      $cmd "$i" | repomdxml2solv $parser_options > $repomdfile || exit 4
+      repomd_decompress "$repomdxml" | repomdxml2solv $parser_options > $repomdfile || exit 4
   fi
 
-  # This contains suseinfo.xml, which is extensions to repomd.xml
+  # This contains suseinfo.xml, which is an extension to repomd.xml
   # for now we only read some keys like expiration and products
-  cmd=
-  for i in suseinfo.xml*; do
-      test -s "$i" || continue
-      case $i in
-         *.gz) cmd="gzip -dc" ;;
-         *.bz2) cmd="bzip2 -dc" ;;
-         *) cmd="cat" ;;
-      esac
-      # only check the first suseinfo.xml*, in case there are more
-      break
-  done
-  suseinfofile="/nonexist"
-  if test -n "$cmd"; then
-      # we have some suseinfo.xml*
+  suseinfofile=
+  suseinfoxml=`repomd_findfile suseinfo suseinfo.xml`
+  if test -n "$suseinfoxml" -a -s "$suseinfoxml" ; then
       suseinfofile=`mktemp` || exit 3
-      $cmd "$i" | repomdxml2solv $parser_options > $suseinfofile || exit 4
+      repomd_decompress "$suseinfoxml" | repomdxml2solv $parser_options > $suseinfofile || exit 4
   fi
 
   # This contains a updateinfo.xml* and maybe patches
-  cmd=
-  for i in updateinfo.xml*; do
-      test -s "$i" || continue
-      case $i in
-         *.gz) cmd="gzip -dc" ;;
-         *.bz2) cmd="bzip2 -dc" ;;
-         *) cmd="cat" ;;
-      esac
-      # only check the first updateinfo.xml*, in case there are more
-      break
-  done
-  updateinfofile="/nonexist"
-  if test -n "$cmd"; then
-      # we have some updateinfo.xml*
+  updateinfofile=
+  updateinfoxml=`repomd_findfile updateinfo updateinfo.xml`
+  if test -n "$updateinfoxml" -a -s "$updateinfoxml" ; then
       updateinfofile=`mktemp` || exit 3
-      $cmd "$i" | updateinfoxml2solv $parser_options > $updateinfofile || exit 4
+      repomd_decompress "$updateinfoxml" | updateinfoxml2solv $parser_options > $updateinfofile || exit 4
   fi
 
   # This contains a deltainfo.xml*
-  cmd=
-  for i in deltainfo.xml*; do
-      test -s "$i" || continue
-      case $i in
-         *.gz) cmd="gzip -dc" ;;
-         *.bz2) cmd="bzip2 -dc" ;;
-         *) cmd="cat" ;;
-      esac
-      # only check the first deltainfo.xml*, in case there are more
-      break
-  done
-  deltainfofile="/nonexist"
-  if test -n "$cmd"; then
-      # we have some deltainfo.xml*
+  deltainfofile=
+  deltainfoxml=`repomd_findfile deltainfo deltainfo.xml`
+  if test -z "$deltainfoxml"; then 
+      deltainfoxml=`repomd_findfile prestodelta prestodelta.xml`
+  fi
+  if test -n "$deltainfoxml" -a -s "$deltainfoxml" ; then
       deltainfofile=`mktemp` || exit 3
-      $cmd "$i" | deltainfoxml2solv $parser_options > $deltainfofile || exit 4
+      repomd_decompress "$deltainfoxml" | deltainfoxml2solv $parser_options > $deltainfofile || exit 4
   fi
 
-  # Now merge primary, patches, updateinfo, and deltainfo
-  if test -s $repomdfile; then
-    m_repomdfile=$repomdfile
-  fi
-  if test -s $suseinfofile; then
-    m_suseinfofile=$suseinfofile
-  fi
-  if test -s $primfile; then
-    m_primfile=$primfile
+  # This contains appdata
+  appdataxml=
+  appdatafile=
+  if test -x /usr/bin/appdata2solv ; then
+      appdataxml=`repomd_findfile appdata appdata.xml`
   fi
-  if test -s $patternfile; then
-    m_patternfile=$patternfile
+  if test -n "$appdataxml" -a -s "$appdataxml" ; then
+      appdatafile=`mktemp` || exit 3
+      repomd_decompress "$appdataxml" | appdata2solv $parser_options > $appdatafile || exit 4
   fi
-  if test -s $prodfile; then
-    m_prodfile=$prodfile
-  fi
-  if test -s $updateinfofile; then
-    m_updateinfofile=$updateinfofile
-  fi
-  if test -s $deltainfofile; then
-    m_deltainfofile=$deltainfofile
-  fi
-  mergesolv $m_repomdfile $m_suseinfofile $m_primfile $m_prodfile $m_patternfile $m_updateinfofile $m_deltainfofile
-  rm -f $repomdfile $suseinfofile $primfile $patternfile $prodfile $updateinfofile $deltainfofile
+
+  # Now merge primary, patches, updateinfo, and deltainfo
+  mergesolv $addautooption $repomdfile $suseinfofile $primfile $prodfile $patternfile $updateinfofile $deltainfofile $appdatafile
+  rm -f $repomdfile $suseinfofile $primfile $patternfile $prodfile $updateinfofile $deltainfofile $appdatafile
 
 elif test "$repotype" = susetags ; then
   olddir=`pwd`
   DESCR=$(get_DESCRDIR)
   cd ${DESCR} || exit 2
+  appdataxml=
+  appdatafile=
+  if test -x /usr/bin/appdata2solv ; then
+      appdataxml=`susetags_findfile appdata.xml`
+  fi
+  if test -n "$appdataxml" ; then
+      appdatafile=`mktemp` || exit 3
+      repomd_decompress "$appdataxml" | appdata2solv $parser_options > $appdatafile || exit 4
+      parser_options="-M $appdatafile $parser_options"
+  fi
   (
     # First packages
-    if test -s packages.gz; then
-      gzip -dc packages.gz
-    elif test -s packages.bz2; then
-      bzip2 -dc packages.bz2
-    elif test -s packages; then
-      cat packages
-    fi
+    susetags_findfile_cat packages
 
     # DU
-    if test -s packages.DU.gz; then
-      gzip -dc packages.DU.gz
-    elif test -s packages.DU.bz2; then
-      bzip2 -dc packages.DU.bz2
-    elif test -s packages.DU; then
-      cat packages.DU
-    fi
+    susetags_findfile_cat packages.DU
 
     # Now default language
-    if test -s packages.en.gz; then
-      gzip -dc packages.en.gz
-    elif test -s packages.en.bz2; then
-      bzip2 -dc packages.en.bz2
-    elif test -s packages.en; then
-      cat packages.en
-    fi
+    susetags_findfile_cat packages.en
 
     # Now patterns.  Not simply those files matching *.pat{,.gz,bz2},
     # but only those mentioned in the file 'patterns'
-    if test -f patterns; then
+    if test -f patterns ; then
       for i in `cat patterns`; do
-        test -s "$i" || continue
-        case $i in
-          *.gz) gzip -dc "$i" ;;
-         *.bz2) bzip2 -dc "$i" ;;
-         *) cat "$i" ;;
-       esac
+        if test -s "$i" ; then
+         repomd_decompress "$i"
+       fi
       done
     fi
 
     # Now all other packages.{lang}.  Needs to come last as it switches
     # languages for all following susetags files
-    for i in packages.*; do
+    for i in packages.* ; do
       case $i in
-       *.gz) name="${i%.gz}" ; prog="gzip -dc" ;;
-       *.bz2) name="${i%.bz2}" ; prog="bzip2 -dc" ;;
-       *) name="$i"; prog=cat ;;
+       *.gz|*.bz2|*.xz|*.lzma) name="${i%.*}" ;;
+       *) name="$i" ;;
       esac
       case $name in
        # ignore files we handled already
@@ -292,14 +267,15 @@ elif test "$repotype" = susetags ; then
        *)
          suff=${name#packages.}
          echo "=Lan: $suff"
-         $prog "$i" ;;
+         repomd_decompress "$i"
       esac
     done
 
-  ) | susetags2solv -c "${olddir}/content" $parser_options || exit 4
+  ) | susetags2solv $addautooption -c "${olddir}/content" $parser_options || exit 4
+  test -n "$appdatafile" && rm -f "$appdatafile"
   cd "$olddir"
 elif test "$repotype" = plaindir ; then
-  find * -name .\* -prune -o $findopt -name \*.delta.rpm -o -name \*.patch.rpm -o -name \*.rpm -a -type f -print0 | rpms2solv -0 -m -
+  find * -name .\* -prune -o $findopt -name \*.delta.rpm -o -name \*.patch.rpm -o -name \*.rpm -a -type f -print0 | rpms2solv $addautooption -0 -m -
 else
   echo "unknown repository type '$repotype'" >&2
   exit 1