lrossett / rpms / centpkg-minimal

Forked from rpms/centpkg-minimal 2 years ago
Clone

Blame SOURCES/get_sources.sh

323c96
#!/bin/bash
323c96
#
323c96
# Might want to drop this in ~/bin/ and chmod u+x it
323c96
#
323c96
323c96
#  Initial Author: Karanbir Singh <kbsingh@centos.org>
323c96
#         Updates:
323c96
#                  Pierre-Yves Chibon <pingou@pingoured.fr>
323c96
#                  Mike McLean <mikem@redhat.com>
323c96
#                  Pat Riehecky <riehecky@fnal.gov>
323c96
#                  Tyler Parsons <tparsons@fnal.gov>
323c96
#                  Tuomo Soini <tis@foobar.fi>
323c96
set -eux
323c96
323c96
323c96
#####################################################################
323c96
usage() {
323c96
    echo ''                                               >&2
323c96
    echo "$0 [-hcq] [-b branch] [--surl url]"             >&2
323c96
    echo ''                                               >&2
323c96
    echo 'Script to parse the non-text sources metadata file'   >&2
323c96
    echo ' and download the required files from the lookaside'  >&2
323c96
    echo ' cache.'                                              >&2
323c96
    echo ''                                                     >&2
323c96
    echo 'PLEASE NOTE: this script is non-destructive, it wont' >&2
323c96
    echo ' replace files that already exist, regardless of'     >&2
323c96
    echo ' their state, allowing you to have work-in-progress'  >&2
323c96
    echo ' content that wont get overwritten.'                  >&2
323c96
    echo ''                                                     >&2
323c96
    echo 'You need to run this from inside a sources git repo'  >&2
323c96
    echo ''                                               >&2
323c96
    echo ' -h: This help message'                         >&2
323c96
    echo ''                                               >&2
323c96
    echo "  $0 -b c7"                                     >&2
323c96
    echo "  $0 -q -b c7"                                  >&2
323c96
    echo "  $0 -c -b remotes/origin/c7"                   >&2
323c96
    echo "  $0 -c -b c7 --surl '$SURL'"                   >&2
323c96
    echo "  $0"                                           >&2
323c96
    exit 1
323c96
}
323c96
323c96
#####################################################################
323c96
323c96
SURL="https://git.centos.org/sources"
323c96
323c96
QUIET=0
323c96
BRANCH=''
323c96
CHECK=0
323c96
323c96
# for setting any overrides, such as SURL, default BRANCH, or force CHECK
323c96
if [ -f /etc/centos-git-common ]; then
323c96
  . /etc/centos-git-common
323c96
fi
323c96
323c96
#####################################################################
323c96
# setup args in the right order for making getopt evaluation
323c96
# nice and easy.  You'll need to read the manpages for more info
323c96
# utilizing 'while' construct rather than 'for arg' to avoid unnecessary
323c96
# shifting of program args
323c96
args=$(getopt -o hcqb: -l surl: -- "$@")
323c96
eval set -- "$args"
323c96
323c96
while [[ 0 -eq 0 ]]; do
323c96
    case $1 in
323c96
        -- )
323c96
            # end of getopt args, shift off the -- and get out of the loop
323c96
            shift
323c96
            break
323c96
           ;;
323c96
         -c )
323c96
            # verify the sha1sum of the downloaded file
323c96
            CHECK=1
323c96
            shift
323c96
           ;;
323c96
         -q )
323c96
            # suppress warnings
323c96
            QUIET=1
323c96
            shift
323c96
           ;;
323c96
         -b )
323c96
            # Check this particular branch
323c96
            BRANCH=$2
323c96
            shift
323c96
            shift
323c96
           ;;
323c96
         --surl )
323c96
            # override sources url
323c96
            SURL=$2
323c96
            shift
323c96
            shift
323c96
           ;;
323c96
         -h )
323c96
            # get help
323c96
            usage
323c96
           ;;
323c96
    esac
323c96
done
323c96
323c96
# set curl options this way so defaults can be set in /etc/centos-git-common
323c96
# across multiple scripts
323c96
if [[ ${QUIET} -eq 1 ]]; then
323c96
    QUIET='--silent'
323c96
else
323c96
    QUIET=''
323c96
fi
323c96
323c96
command -v git >/dev/null 2>&1
323c96
if [[ $? -ne 0 ]]; then
323c96
    echo 'You need git in PATH' >&2
323c96
    exit 1
323c96
fi
323c96
323c96
command -v curl >/dev/null 2>&1
323c96
if [[ $? -ne 0 ]]; then
323c96
    echo 'You need curl in PATH' >&2
323c96
    exit 1
323c96
fi
323c96
323c96
if [ ! -d .git ] && ([ ! -d SPECS ] || [[ ! -s sources ]] ); then
323c96
      echo 'You need to run this from inside a sources git repo' >&2
323c96
      exit 1
323c96
    fi
323c96
323c96
# sort out our branch
323c96
if [ -n "$BRANCH" ]
323c96
then
323c96
  branches=("$BRANCH")
323c96
else
323c96
  # generate a list of all branches containing current HEAD
323c96
  branches=()
323c96
  while IFS='' read -r line
323c96
  do
323c96
    # input from: git branch --contains HEAD
323c96
    branch="${line:2}"
323c96
    if [[ "$branch" =~ "detached from" ]]
323c96
    then
323c96
      # ignore detached heads
323c96
      continue
323c96
    fi
323c96
    if [ ".${line:0:1}" = ".*" ]
323c96
    then
323c96
      # current branch, put it first
323c96
      branches=("${branch}" ${branches[@]+"${branches[@]}"})
323c96
    else
323c96
      branches=(${branches[@]+"${branches[@]}"} "${branch}")
323c96
    fi
323c96
  done <<< "$(git branch -r --contains HEAD | sed 's#origin/##g')"
323c96
fi
323c96
323c96
if [[ -f sources ]]; then
323c96
    echo "Flat layout style"
323c96
    if [[ ! -s sources ]]; then
323c96
      echo "Empty sources file -- nothing to check"
323c96
    else
323c96
      # This section is for the "flat" dist-git layout, where the spec file and
323c96
      # patches are all present at the top level directory and the sha of the tarball
323c96
      # present in a 'sources' file.
323c96
      # This code was re-used from the fedpkg-pkg minimal project which is licensed
323c96
      # under GPLv3 or any later version.
323c96
323c96
      pkgname=$(basename "$PWD")
323c96
      # Read first word of first line. For old MD5 format it's the 32 character
323c96
      # hash. Otherwise let's assume the sources have the BSD format where lines
323c96
      # start with hash type.
323c96
      hashtype="$(head -n1 sources | cut -d' ' -f1 | tr '[:upper:]' '[:lower:]')"
323c96
      # The format is
323c96
      #   SHA512 (filename) = ABCDEF
323c96
      # We don't care about the equals sign. We also assume all hashes are
323c96
      # the same type, so we don't need to read it again for each line.
323c96
      while read -r _ filename _ hash || [[ -n "$filename" && -n "$hash" ]]; do
323c96
          if [ -z "$filename" ] || [ -z "$hash" ]; then
323c96
              continue
323c96
          fi
323c96
          # Remove parenthesis around tarball name
323c96
          filename=${filename#(}
323c96
          tarball=${filename%)}
323c96
          if [ ! -e "$tarball" ]; then
323c96
            for br in "${branches[@]}"
323c96
            do
323c96
              br=$(echo ${br}| sed -e s'|remotes/origin/||')
323c96
              # Try the branch-specific lookaside structure
323c96
              url="${SURL}/$pkgname/${br}/$hash"
323c96
              echo "Retrieving ${url}"
323c96
              HTTP_CODE=$(curl -L ${QUIET} -H Pragma: -o "./$tarball" -R -S --fail --retry 5 "${url}" --write-out "%{http_code}" || true)
323c96
              echo "Returned ${HTTP_CODE}"
323c96
              if [[ ${HTTP_CODE} -gt 199 && ${HTTP_CODE} -lt 300 ]] ; then
323c96
                 echo "bailing"
323c96
                 break
323c96
              fi
323c96
              # Try the hash-specific lookaside structure
323c96
              url="${SURL}/$pkgname/$tarball/$hashtype/$hash/$tarball"
323c96
              echo "Retrieving ${url}"
323c96
              curl -L ${QUIET} -H Pragma: -o "./$tarball" -R -S --fail --retry 5 "${url}" && break
323c96
            done
323c96
          else
323c96
            echo "$filename exists. skipping"
323c96
          fi
323c96
      done < sources
323c96
      "${hashtype}sum" -c sources
323c96
    fi
323c96
else
323c96
    echo "Exploded SRPM layout style"
323c96
    # This section is for the "non-flat" dist-git layout, where the spec file
323c96
    # is stored in a SPECS folder, the patches in a SOURCES folder and the sha
323c96
    # of the tarball of the project is present in a '.<pkg_name>.metadata' file.
323c96
323c96
    mkdir -p SOURCES
323c96
    # should go into a function section at some point
323c96
    weakHashDetection () {
323c96
      strHash=${1};
323c96
      case $((`echo ${strHash}|wc -m` - 1 )) in
323c96
        128)
323c96
          hashBin='sha512sum'
323c96
          ;;
323c96
        64)
323c96
          hashBin='sha256sum'
323c96
          ;;
323c96
        40)
323c96
          hashBin='sha1sum'
323c96
          ;;
323c96
        32)
323c96
          hashBin='md5sum'
323c96
          ;;
323c96
        *)
323c96
          hashBin='unknown'
323c96
          ;;
323c96
      esac
323c96
      echo ${hashBin};
323c96
    }
323c96
323c96
    # check metadata file and extract package name
323c96
    shopt -s nullglob
323c96
    set -- .*.metadata
323c96
    if (( $# == 0 ))
323c96
    then
323c96
        echo 'Missing metadata. Please run from inside a sources git repo' >&2
323c96
        exit 1
323c96
    elif (( $# > 1 ))
323c96
    then
323c96
        echo "Warning: multiple metadata files found. Using $1"
323c96
    fi
323c96
    meta=$1
323c96
    pn=${meta%.metadata}
323c96
    pn=${pn#.}
323c96
323c96
    while read -r fsha fname ; do
323c96
      if [ ".${fsha}" = ".da39a3ee5e6b4b0d3255bfef95601890afd80709" ]; then
323c96
        # zero byte file
323c96
        touch ${fname}
323c96
      else
323c96
        hashType=$(weakHashDetection ${fsha})
323c96
        if [ "${hashType}" == "unknown" ]; then
323c96
          echo 'Failure: Hash type unknown.' >&2
323c96
          exit 1;
323c96
        fi
323c96
        hashName=$(echo ${hashType}| sed -e s'|sum||')
323c96
323c96
        if [ ${CHECK} -eq 1 ]; then
323c96
          which ${hashType} >/dev/null 2>&1
323c96
          if [[ $? -ne 0 ]]; then
323c96
            echo "Failure: You need ${hashType} in PATH." >&2
323c96
            exit 1;
323c96
          fi
323c96
        fi
323c96
        if [ -e ${fname} -a ${CHECK} -eq 1 ]; then
323c96
            # check hash sum and force download if wrong
323c96
            downsum=$(${hashType} ${fname} | awk '{print $1}')
323c96
            if [ "${fsha}" != "${downsum}" ]; then
323c96
                rm -f ${fname}
323c96
            fi
323c96
        fi
323c96
        if [ ! -e "${fname}" ]; then
323c96
          for br in "${branches[@]}"
323c96
          do
323c96
            br=$(echo ${br}| sed -e s'|remotes/origin/||')
323c96
            # Try the branch-specific lookaside structure
323c96
            url="${SURL}/${pn}/${br}/${fsha}"
323c96
            echo "Retrieving ${url}"
323c96
            HTTP_CODE=$(curl -L ${QUIET} -H Pragma: -o "${fname}" -R -S --fail --retry 5 "${url}" --write-out "%{http_code}" || true)
323c96
            echo "Returned ${HTTP_CODE}"
323c96
            if [[ ${HTTP_CODE} -gt 199 && ${HTTP_CODE} -lt 300 ]] ; then
323c96
               echo "bailing"
323c96
               break
323c96
            fi
323c96
            # Try the hash-specific lookaside structure
323c96
            url="${SURL}/$pn/$fname/${hashName}/$fsha/$fname"
323c96
            echo "Retrieving ${url}"
323c96
            curl -L ${QUIET} -H Pragma: -o "${fname}" -R -S --fail --retry 5 "${url}" && break
323c96
          done
323c96
        else
323c96
          echo "${fname} exists. skipping"
323c96
        fi
323c96
        if [ ${CHECK} -eq 1 ]; then
323c96
            downsum=$(${hashType} ${fname} | awk '{print $1}')
323c96
            if [ "${fsha}" != "${downsum}" ]; then
323c96
                rm -f ${fname}
323c96
                echo "Failure: ${fname} hash does not match hash from the .metadata file" >&2
323c96
                exit 1;
323c96
            fi
323c96
        fi
323c96
      fi
323c96
    done < "${meta}"
323c96
323c96
fi