+}
+
+########## .pkgs format
+# comments start with a # - this is needed only if you use a keyword in a comment
+
+function pl_getPkgsAttribute () {
+ keyword=$1; shift
+ file=$1; shift
+ # remove any initial white spaces from the result
+ grep -v '^#' $file | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,," -e "s,^[[:space:]][[:space:]]*,,"
+}
+
+# for a given keyword like 'package' :
+# we support fcdistro-dependant format, for tokens (pkgname) without whitespace
+# you can e.g. use
+# package: pkg1 .. pkgn
+# package+f8: pkg1 .. pkgn
+# package-f8: pkg1 .. pkgn
+#
+# values can contain @arch@, @fcdistro@ or @pldistro@ that are replaced with the current values
+#
+# Usage: pl_parsePkgs keyword [-a arch] fcdistro pldistro pkgs-file[..s]
+# the reason for the -a option is for when we build the build vserver itself; in this case
+# pl_DISTRO_ARCH is the one we obtain from the root context, and that's wrong
+# specify -sa arch AFTER keyword, so as to keep pl_getPackages and pl_getGroups simple
+#
+function pl_parsePkgs () {
+
+ target_arch=$pl_DISTRO_ARCH
+ keyword=$1;shift
+ [ "$1" == "-a" ] && { shift; target_arch="$1"; shift; }
+ fcdistro=$1; shift
+ pldistro=$1; shift
+ # remaining arguments are paths to the pkgs files
+
+ # grab regular descriptions
+ all=$(grep -v '^#' "$@" | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,")
+ # grab additions
+ add=$(grep -v '^#' "$@" | grep --regexp="^${keyword}+${fcdistro}:" | sed -e "s,${keyword}+${fcdistro}:,,")
+ # grab exclusions
+ sub=$(grep -v '^#' "$@" | grep --regexp="^${keyword}-${fcdistro}:" | sed -e "s,${keyword}-${fcdistro}:,,")
+
+ for rpm in $all $add; do
+ for exclude in $sub; do
+ [ "$rpm" = "$exclude" ] && continue 2
+ done
+ echo "${rpm} " | sed -e "s,@arch@,${target_arch},g" -e "s,@fcdistro@,$fcdistro,g" -e "s,@pldistro@,$pldistro,g"
+ done
+ return 0
+}
+
+# usage: pl_getPackages [-a arch] fcdistro pldistro pkg-file[..s]
+function pl_getPackages() { pl_parsePkgs package "$@" ; }
+function pl_getGroups() { pl_parsePkgs group "$@" ; }
+
+# locates a pldistro-dependant file
+# tries first in build/<pldistro>/, then in build/planetlab/
+function pl_locateDistroFile () {
+ builddir=$1; shift
+ pldistro=$1; shift
+ pkgsfile=$1; shift
+
+ pkgspath=""
+ # locate it
+ paths="$builddir/config.$pldistro/$pkgsfile $builddir/config.planetlab/$pkgsfile"
+ for path in $paths; do
+ if [ -f $path ] ; then
+ pkgspath=$path
+ break
+ fi
+ done
+ if [ -z "$pkgspath" ] ; then
+ echo 1>&2 "pl_locateDistroFile - in $(pwd) : cannot locate $pkgsfile in $builddir"
+ echo 1>&2 "candidates were $paths"
+ echo "not-found-by-pl_locateDistroFile"
+ return 1
+ else
+ echo 1>&2 "pl_locateDistroFile: using $pkgspath"
+ echo $pkgspath
+ return 0
+ fi
+}
+
+function yumgroups_from_pkgs () {
+ builddir=$1; shift
+ pldistro=$1; shift
+ fcdistro=$1; shift
+ pkgsnames=$@
+
+ sedargs="-e s,@FCDISTRO@,$fcdistro,g"
+
+ cat <<__header
+<?xml version="1.0"?>
+<!DOCTYPE comps PUBLIC "-//Red Hat, Inc.//DTD Comps info//EN" "comps.dtd">
+<comps>
+__header
+
+ for pkgsname in $pkgsnames; do
+ pkgsfile=$(pl_locateDistroFile $builddir $pldistro $pkgsname)
+ packages=$(pl_getPackages $fcdistro $pldistro $pkgsfile)
+
+ groupname=$(pl_getPkgsAttribute groupname $pkgsfile | sed $sedargs)
+ groupdesc=$(pl_getPkgsAttribute groupdesc $pkgsfile | sed $sedargs)
+
+ if [ -z "$groupname" -o -z "$groupdesc" ] ; then
+ echo "Cannot find groupname: and groupdesc: in $pkgsfile -- skipped" 1>&2
+ continue
+ fi
+
+ cat << __group_header
+ <group>
+ <id>$(echo $groupname|tr A-Z a-z)</id>
+ <name>$groupname</name>
+ <description>$groupdesc</description>
+ <uservisible>true</uservisible>
+ <packagelist>
+__group_header
+ for package in $packages; do
+ echo "<packagereq type=\"mandatory\">$package</packagereq>"
+ done
+ cat << __group_footer
+ </packagelist>
+ </group>
+__group_footer
+ done
+cat <<__footer
+</comps>
+__footer
+}
+
+
+function build_fetch () {
+ curl --fail --silent --max-time 60 --output /dev/null "$1"
+}
+
+# tries to compute a valid yum.conf for that pldistro from the template in mirroring/
+# returns 0 and writes <dest_yumconf> on success
+# returns 1 on failure, in which case <dest_yumconf> is deleted
+function yumconf_mirrors () {
+ dest_yumconf=$1; shift
+ builddir=$1; shift
+ fcdistro=$1; shift
+ kexclude_line="$1" ; shift
+ mirrors="$@"
+
+ template=$builddir/mirroring/$fcdistro/yum.repos.d/building.repo.in
+
+ if [ ! -f $template ] ; then
+ echo "yumconf_mirrors: cannot locate template $template"
+ rm -f $dest_yumconf
+ return 1
+ fi
+
+ for mirror in $mirrors; do
+ if yumconf_mirror $dest_yumconf $template "$kexclude_line" $mirror; then
+ return 0
+ fi
+ done
+ rm -f $dest_yumconf
+ return 1
+}
+
+# computes a yum.conf from the template, and checks that all baseurl defined in there are valid repos
+# returns 0 on success and 1 on failure
+function yumconf_mirror () {
+ dest_yumconf=$1; shift
+ template=$1; shift
+ kexclude_line="$1" ; shift
+ mirror=$1; shift
+
+ sed -e "s,@MIRRORURL@,$mirror,g" \
+ -e "/baseurl=/i\\
+$kexclude_line" $template > $dest_yumconf
+
+ # capture all lines defining baseurl
+ baseurl_defs=$(grep '^baseurl=' $dest_yumconf)
+ if [ -z "$baseurl_defs" ] ; then
+ return 1
+ fi
+
+ for baseurl_def in $baseurl_defs; do
+ baseurl=$(echo $baseurl_def | sed \
+ -e s,baseurl=,, \
+ -e 's,$basearch,'"$pl_DISTRO_ARCH",g)
+ repomd=$baseurl/repodata/repomd.xml
+
+ echo "* Trying to fetch $repomd"
+ if ! build_fetch $repomd ; then
+ echo "* Failed to fetch $repomd"
+ return 1
+ fi
+ done
+ echo "* Selected mirror $mirror"
+ return 0
+}
+
+# from a yum.conf as generated above, computes the (first) gpgkey url
+function yumconf_gpgkey () {
+ dest_yumconf=$1; shift
+
+ first_line=$(grep '^gpgkey=' $dest_yumconf | head -1)
+ values=$(echo $first_line | sed -e s,gpgkey=,,)
+ value=$(echo $values | awk '{print $1;}' | sed -e 's,$basearch,'"$pl_DISTRO_ARCH",g)
+ [ -n "$value" ] || return 1
+ echo $value
+ return 0
+}
+
+# patches a yum conf to insert an exclude line in each declared repo
+function yumconf_exclude () {
+ repo=$1; shift
+ kexclude_line="$1" ; shift
+
+ sed -i -e "/baseurl=.*$/i\\
+$kexclude_line" $repo
+}