- use repomdxml to query for the location (bnc#501425)
[platform/upstream/libsolv.git] / tools / repo2solv.sh
index e214589..65bfdce 100755 (executable)
@@ -4,12 +4,18 @@
 # give it a directory of a local mirror of a repo and this
 # tries to detect the repo type and generate one SOLV file on stdout
 
+get_DESCRDIR () {
+  local d=$(grep '^DESCRDIR' content | sed 's/^DESCRDIR[[:space:]]\+\(.*[^[:space:]]\)[[:space:]]*$/\1/')
+  if  test -z "$d"; then
+    echo suse/setup/descr
+  else
+    echo ${d}
+  fi
+}
+
 test_susetags() {
   if test -s content; then
-    DESCR=`grep DESCRDIR content | cut -d ' ' -f 2`
-    if test -z $DESCR; then
-      DESCR=suse/setup/descr
-    fi
+    DESCR=$(get_DESCRDIR)
     test -d $DESCR
     return $?
   else
@@ -17,178 +23,158 @@ test_susetags() {
   fi
 }
 
-# this should signal an error if there is a problem
-set -e 
+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" ;;
+   *) cat "$1" ;;
+  esac
+}
+
+# signal an error if there is a problem
+set -e
 
 LANG=C
 unset CDPATH
 parser_options=${PARSER_OPTIONS:-}
 
+findopt="-prune"
+repotype=
+
+while true ; do
+  if test "$1" = "-o" ; then
+    exec > "$2"
+    shift
+    shift
+  elif test "$1" = "-R" ; then
+    # recursive
+    findopt=
+    repotype=plaindir
+    shift
+  else
+    break
+  fi
+done
 
 dir="$1"
 cd "$dir" || exit 1
-if test -d repodata; then
-  cd repodata || exit 2
 
-  # This contains a primary.xml* and maybe patches
-  for i in primary.xml*; do
-    case $i in
-      *.gz) cmd="gzip -dc" ;;
-      *.bz2) cmd="bzip2 -dc" ;;
-      *) cmd="cat" ;;
-    esac
-    # only check the first primary.xml*, in case there are more
-    break
-  done
-  primfile="/nonexist"
-  if test -n "$cmd"; then
-    # we have some primary.xml*
-    primfile=`mktemp` || exit 3
-    $cmd $i | rpmmd2solv $parser_options > $primfile || exit 4
+if test -z "$repotype" ; then
+  # autodetect repository type
+  if test -d repodata ; then
+    repotype=rpmmd
+  elif test_susetags ; then
+    repotype=susetags
+  else
+    repotype=plaindir
   fi
+fi
 
-  # This contains a susedata.xml* with extended primary data
- if test -f susedata.xml || test -f susedata.xml.gz || test -f susedata.xml.bz2 ; then
-     for i in susedata.xml*; do
-         case $i in
-             *.gz) cmd="gzip -dc" ;;
-             *.bz2) cmd="bzip2 -dc" ;;
-             *) cmd="cat" ;;
-         esac
-    # only check the first susedata.xml*, in case there are more
-         break
-     done
-     susedatafile="/nonexist"
-     if test -n "$cmd"; then
-    # we have some susedata.xml*
-         susedatafile=`mktemp` || exit 3
-         $cmd $i | rpmmd2solv $parser_options > $susedatafile || exit 4
+if test "$repotype" = rpmmd ; then
+  cd repodata || exit 2
+
+  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>'
+     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
   fi
 
-  prodfile="/nonexist"
+  prodfile=
   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
+       repomd_decompress $i
      done
      echo '</products>'
     ) | grep -v '\?xml' | rpmmd2solv $parser_options > $prodfile || exit 4
   fi
 
+  patternfile=
+  patternxml=`repomd_findfile '' 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 expiration
-  if test -f repomd.xml || test -f repomd.xml.gz || test -f repomd.xml.bz2 ; then
-      for i in repomd.xml*; do
-          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=`mktemp` || exit 3
-          $cmd $i | repomdxml2solv $parser_options > $repomdfile || exit 4
-      fi
+  # for now we only read some keys like timestamp
+  repomdfile=
+  repomdxml=`repomd_findfile '' repomd.xml`
+  if test -n "$repomdxml" -a -s "$repomdxml" ; then
+      repomdfile=`mktemp` || exit 3
+      repomd_decompress "$repomdxml" | repomdxml2solv $parser_options > $repomdfile || exit 4
   fi
 
-  # This contains a updateinfo.xml* and maybe patches
-  if test -f updateinfo.xml || test -f updateinfo.xml.gz || test -f updateinfo.xml.bz2 ; then
-      for i in updateinfo.xml*; do
-          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=`mktemp` || exit 3
-          $cmd $i | updateinfoxml2solv $parser_options > $updateinfofile || exit 4
-      fi
+  # This contains suseinfo.xml, which is an extension to repomd.xml
+  # for now we only read some keys like expiration and products
+  suseinfofile=
+  suseinfoxml=`repomd_findfile '' suseinfo.xml`
+  if test -n "$suseinfoxml" -a -s "$suseinfoxml" ; then
+      suseinfofile=`mktemp` || exit 3
+      repomd_decompress "$suseinfoxml" | repomdxml2solv $parser_options > $suseinfofile || exit 4
   fi
 
-  patchfile="/nonexist"
-  if test -f patches.xml; then
-    patchfile=`mktemp` || exit 3
-    (
-     echo '<patches>'
-     for i in patch-*.xml*; do
-       case $i in
-         *.gz) gzip -dc "$i" ;;
-        *.bz2) bzip2 -dc "$i" ;;
-        *) cat "$i" ;;
-       esac
-     done
-     echo '</patches>'
-    ) | grep -v '\?xml' | patchxml2solv $parser_options > $patchfile || exit 4
+  # This contains a updateinfo.xml* and maybe patches
+  updateinfofile=
+  updateinfoxml=`repomd_findfile updateinfo updateinfo.xml`
+  if test -n "$updateinfoxml" -a -s "$updateinfoxml" ; then
+      updateinfofile=`mktemp` || exit 3
+      repomd_decompress "$updateinfoxml" | updateinfoxml2solv $parser_options > $updateinfofile || exit 4
   fi
 
   # This contains a deltainfo.xml*
-  if test -f deltainfo.xml || test -f deltainfo.xml.gz || test -f deltainfo.xml.bz2 ; then
-      for i in deltainfo.xml*; do
-          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=`mktemp` || exit 3
-          $cmd $i | deltainfoxml2solv $parser_options > $deltainfofile || exit 4
-      fi
+  deltainfofile=
+  deltainfoxml=`repomd_findfile deltainfo deltainfo.xml`
+  if test -n "$deltainfoxml" -a -s "$deltainfoxml" ; then
+      deltainfofile=`mktemp` || exit 3
+      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 $susedatafile; then
-    m_susedata=$susedatafile
-  fi
-  if test -s $primfile; then
-    m_primfile=$primfile
-  fi
-  if test -s $prodfile; then
-    m_prodfile=$prodfile
-  fi
-  if test -s $patchfile; then
-    m_patchfile=$patchfile
-  fi
-  if test -s $updateinfofile; then
-    m_updateinfofile=$updateinfofile
-  fi
-  if test -s $deltainfofile; then
-    m_deltainfofile=$deltainfofile
-  fi
-  mergesolv $m_repomdfile $m_primfile $m_susedatafile $m_prodfile $m_patchfile $m_updateinfofile $m_deltainfofile
-  rm -f $repomdfile $primfile $prodfile $patchfile $updateinfofile $deltainfofile
+  mergesolv $repomdfile $suseinfofile $primfile $prodfile $patternfile $updateinfofile $deltainfofile
+  rm -f $repomdfile $suseinfofile $primfile $patternfile $prodfile $updateinfofile $deltainfofile
 
-elif test_susetags; then
+elif test "$repotype" = susetags ; then
   olddir=`pwd`
-  DESCR=`grep DESCRDIR content | cut -d ' ' -f 2`
-  if test -z $DESCR; then
-    DESCR=suse/setup/descr
-  fi
+  DESCR=$(get_DESCRDIR)
   cd ${DESCR} || exit 2
   (
     # First packages
@@ -242,24 +228,18 @@ elif test_susetags; then
       case $name in
        # ignore files we handled already
        *.DU | *.en | *.FL | packages ) continue ;;
-       *) 
+       *)
          suff=${name#packages.}
          echo "=Lan: $suff"
-         eval "$prog '$i'" ;;
+         $prog "$i" ;;
       esac
     done
 
   ) | susetags2solv -c "${olddir}/content" $parser_options || exit 4
   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 -
 else
-  rpms=''
-  for r in *.rpm ; do
-    rpms="$rpms$r
-"
-  done
-  if test -n "$rpms" ; then
-      echo "$rpms" | rpms2solv -m -
-  else
-      exit 1
-  fi
+  echo "unknown repository type '$repotype'" >&2
+  exit 1
 fi