Blame SOURCES/get_sources.sh

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