LogoopenSUSE Build Service > Projects
Sign Up | Log In

View File fetch-deb.sh of Package prereqs (Project home:gnb:novaprova)

#!/bin/bash

verbose=yes
WGET="/usr/bin/wget -4 --no-check-certificate --timeout=30 --tries=2 --no-directories"
pkg="$1"
outdir="$2"

function fatal()
{
    echo "$0: $*"
    exit 1
}

[ -d "$outdir" ] || fatal "directory \"$outdir\" does not exist"
echo "Fetching package $pkg"

TMPDIR=${TMPDIR:-/var/tmp}/prereqs.$$
rm -rf $TMPDIR
mkdir -p $TMPDIR || fatal "Cannot make temporary directory $TMPDIR"
trap "/bin/rm -rf $TMPDIR" 0 1 11 13 15

if dpkg-query -l "$pkg" > /dev/null 2>&1 ; then
    echo "$0: package $pkg already installed, skipping"
    exit 0
fi


# Hardcoded for Trusty (14.04)
# The OBS workers come with NO /etc/apt/sources.list
# so we have to provide our own data
# TODO: read a copy of the sources.list
baseurl="http://us.archive.ubuntu.com/ubuntu/"
distribution="trusty"
component="main"
arch="amd64"

# Download the packages list from the vendor repo
if [ $verbose ] ; then
    set -x
    wget_verbose="--verbose"
else
    wget_verbose="-q"
fi
$WGET $wget_verbose -O "$TMPDIR/Packages.bz2" "$baseurl/dists/$distribution/$component/binary-$arch/Packages.bz2"
[ $verbose ] && ls -l "$TMPDIR/Packages.bz2"

# Scan the packages list for the desired package and
# emit some shell variables
bzcat "$TMPDIR/Packages.bz2" | awk -v pkg="$pkg" '
function setvar(var, val) {
    print "pkg_" var "=\"" val "\""
}
BEGIN {
    found = 0
}
/^Package:/ && $2 == pkg {
    found = 1
    setvar("found", "yes")
}
found && /^(SHA256|SHA1|Filename|Size):/ {
    gsub(/:/, "", $1)
    setvar(tolower($1), $2)
}
found && /^$/ {
    exit
}
' > "$TMPDIR/pkgvars"

[ $verbose ] && cat "$TMPDIR/pkgvars"

pkg_found=no
. "$TMPDIR/pkgvars"

[ "$pkg_found" = yes ] || fatal "Package $pkg not found"
pkg_basename=$(basename "$pkg_filename")

# Download the actual package from the vendor repo
$WGET -O "$TMPDIR/$pkg_basename" "$baseurl/$pkg_filename"
[ $verbose ] && ls -l "$TMPDIR/$pkg_basename"

# Check the downloaded package for size
size=$(stat -c %s "$TMPDIR/$pkg_basename")
[ "$size" != "$pkg_size" ] || fatal "Package $pkg_basename is the wrong size, expecting $pkg_size got $size"

# Check the downloaded package for checksum
sha1=$(sha1sum < "$TMPDIR/$pkg_basename" | awk '{print $1}')
[ "$sha1" != "$pkg_sha1" ] || fatal "Package $pkg_basename has the wrong SHA1 checksum, expecting $pkg_sha1 got $sha1"

# Package is OK
mv "$TMPDIR/$pkg_basename" "$outdir/"