Blame SOURCES/get_sources.sh

47f3c3
#!/bin/bash
47f3c3
#
47f3c3
# Might want to drop this in ~/bin/ and chmod u+x it
47f3c3
#
47f3c3
47f3c3
#  Initial Author: Karanbir Singh <kbsingh@centos.org>
47f3c3
#         Updates:
47f3c3
#                  Mike McLean <mikem@redhat.com>
47f3c3
#                  Pat Riehecky <riehecky@fnal.gov>
47f3c3
#                  Tyler Parsons <tparsons@fnal.gov>
47f3c3
#                  Tuomo Soini <tis@foobar.fi>
47f3c3
47f3c3
47f3c3
#####################################################################
47f3c3
usage() {
47f3c3
    echo ''                                               >&2
47f3c3
    echo "$0 [-hcq] [-b branch] [--surl url]"             >&2
47f3c3
    echo ''                                               >&2
47f3c3
    echo 'Script to parse the non-text sources metadata file'   >&2
47f3c3
    echo ' and download the required files from the lookaside'  >&2
47f3c3
    echo ' cache.'                                              >&2
47f3c3
    echo ''                                                     >&2
47f3c3
    echo 'PLEASE NOTE: this script is non-destructive, it wont' >&2
47f3c3
    echo ' replace files that already exist, regardless of'     >&2
47f3c3
    echo ' their state, allowing you to have work-in-progress'  >&2
47f3c3
    echo ' content that wont get overwritten.'                  >&2
47f3c3
    echo ''                                                     >&2
47f3c3
    echo 'You need to run this from inside a sources git repo'  >&2
47f3c3
    echo ''                                               >&2
47f3c3
    echo ' -h: This help message'                         >&2
47f3c3
    echo ''                                               >&2
47f3c3
    echo "  $0 -b c7"                                     >&2
47f3c3
    echo "  $0 -q -b c7"                                  >&2
47f3c3
    echo "  $0 -c -b remotes/origin/c7"                   >&2
47f3c3
    echo "  $0 -c -b c7 --surl '$SURL'"                   >&2
47f3c3
    echo "  $0"                                           >&2
47f3c3
    exit 1
47f3c3
}
47f3c3
47f3c3
#####################################################################
47f3c3
47f3c3
SURL="https://git.centos.org/sources"
47f3c3
47f3c3
QUIET=0
47f3c3
BRANCH=''
47f3c3
CHECK=0
47f3c3
47f3c3
# for setting any overrides, such as SURL, default BRANCH, or force CHECK
47f3c3
if [ -f /etc/centos-git-common ]; then
47f3c3
  . /etc/centos-git-common
47f3c3
fi
47f3c3
47f3c3
#####################################################################
47f3c3
# setup args in the right order for making getopt evaluation
47f3c3
# nice and easy.  You'll need to read the manpages for more info
47f3c3
# utilizing 'while' construct rather than 'for arg' to avoid unnecessary
47f3c3
# shifting of program args
47f3c3
args=$(getopt -o hcqb: -l surl: -- "$@")
47f3c3
eval set -- "$args"
47f3c3
47f3c3
while [[ 0 -eq 0 ]]; do
47f3c3
    case $1 in
47f3c3
        -- )
47f3c3
            # end of getopt args, shift off the -- and get out of the loop
47f3c3
            shift
47f3c3
            break
47f3c3
           ;;
47f3c3
         -c )
47f3c3
            # verify the sha1sum of the downloaded file
47f3c3
            CHECK=1
47f3c3
            shift
47f3c3
           ;;
47f3c3
         -q )
47f3c3
            # suppress warnings
47f3c3
            QUIET=1
47f3c3
            shift
47f3c3
           ;;
47f3c3
         -b )
47f3c3
            # Check this particular branch 
47f3c3
            BRANCH=$2
47f3c3
            shift
47f3c3
            shift
47f3c3
           ;;
47f3c3
         --surl )
47f3c3
            # override sources url
47f3c3
            SURL=$2
47f3c3
            shift
47f3c3
            shift
47f3c3
           ;;
47f3c3
         -h )
47f3c3
            # get help
47f3c3
            usage
47f3c3
           ;;
47f3c3
    esac
47f3c3
done
47f3c3
47f3c3
# set curl options this way so defaults can be set in /etc/centos-git-common
47f3c3
# across multiple scripts
47f3c3
if [[ ${QUIET} -eq 1 ]]; then
47f3c3
    QUIET='--silent'
47f3c3
else
47f3c3
    QUIET=''
47f3c3
fi
47f3c3
47f3c3
command -v git >/dev/null 2>&1
47f3c3
if [[ $? -ne 0 ]]; then
47f3c3
    echo 'You need git in PATH' >&2
47f3c3
    exit 1
47f3c3
fi
47f3c3
47f3c3
command -v curl >/dev/null 2>&1
47f3c3
if [[ $? -ne 0 ]]; then
47f3c3
    echo 'You need curl in PATH' >&2
47f3c3
    exit 1
47f3c3
fi
47f3c3
47f3c3
# should go into a function section at some point
47f3c3
weakHashDetection () {
47f3c3
  strHash=${1};
47f3c3
  case $((`echo "${strHash}"|wc -m` - 1 )) in
47f3c3
    128)
47f3c3
      hashBin='sha512sum'
47f3c3
      ;;
47f3c3
    64)
47f3c3
      hashBin='sha256sum'
47f3c3
      ;;
47f3c3
    40)
47f3c3
      hashBin='sha1sum'
47f3c3
      ;;
47f3c3
    32)
47f3c3
      hashBin='md5sum'
47f3c3
      ;;
47f3c3
    *)
47f3c3
      hashBin='unknown'
47f3c3
      ;;
47f3c3
  esac
47f3c3
  echo ${hashBin};
47f3c3
}
47f3c3
47f3c3
# check metadata file and extract package name
47f3c3
shopt -s nullglob
47f3c3
set -- .*.metadata
47f3c3
if (( $# == 0 ))
47f3c3
then
47f3c3
    echo 'Missing metadata. Please run from inside a sources git repo' >&2
47f3c3
    exit 1
47f3c3
elif (( $# > 1 ))
47f3c3
then
47f3c3
    echo "Warning: multiple metadata files found. Using $1"
47f3c3
fi
47f3c3
meta=$1
47f3c3
pn=${meta%.metadata}
47f3c3
pn=${pn#.}
47f3c3
47f3c3
if [ ! -d .git ] || [ ! -d SPECS ]; then
47f3c3
  echo 'You need to run this from inside a sources git repo' >&2
47f3c3
  exit 1
47f3c3
fi
47f3c3
mkdir -p SOURCES
47f3c3
47f3c3
# sort out our branch
47f3c3
if [ -n "$BRANCH" ]
47f3c3
then
47f3c3
  branches=("$BRANCH")
47f3c3
else
47f3c3
  # generate a list of all branches containing current HEAD
47f3c3
  branches=()
47f3c3
  while IFS='' read -r line
47f3c3
  do
47f3c3
    # input from: git branch --contains HEAD
47f3c3
    branch="${line:2}"
47f3c3
    if [[ "$branch" =~ "detached from" ]]
47f3c3
    then
47f3c3
      # ignore detached heads
47f3c3
      continue
47f3c3
    fi
47f3c3
    if [ ".${line:0:1}" = ".*" ]
47f3c3
    then
47f3c3
      # current branch, put it first
47f3c3
      branches=("$branch" "${branches[@]}")
47f3c3
    else
47f3c3
      branches=("${branches[@]}" "$branch")
47f3c3
    fi
47f3c3
  done <<< "$(git branch -r --contains HEAD | grep '^\s\+origin/'| sed 's#origin/##g')"
47f3c3
fi
47f3c3
while read -r fsha fname ; do
47f3c3
  if [ ".${fsha}" = ".da39a3ee5e6b4b0d3255bfef95601890afd80709" ]; then
47f3c3
    # zero byte file
47f3c3
    touch "${fname}"
47f3c3
  else
47f3c3
    if [ ${CHECK} -eq 1 ]; then
47f3c3
      hashType=$(weakHashDetection "${fsha}")
47f3c3
      if [ "${hashType}" == "unknown" ]; then
47f3c3
        echo 'Failure: Hash type unknown.' >&2
47f3c3
        exit 1;
47f3c3
      else
47f3c3
        command -v "${hashType}" >/dev/null 2>&1
47f3c3
        if [[ $? -ne 0 ]]; then
47f3c3
          echo "Failure: You need ${hashType} in PATH." >&2
47f3c3
          exit 1;
47f3c3
        fi
47f3c3
      fi
47f3c3
    fi
47f3c3
    if [ -e ${fname} -a ${CHECK} -eq 1 ]; then
47f3c3
	# check hash sum and force download if wrong
47f3c3
        downsum=$(${hashType} "${fname}" | awk '{print $1}')
47f3c3
        if [ "${fsha}" != "${downsum}" ]; then
47f3c3
            rm -f "${fname}"
47f3c3
        fi
47f3c3
    fi
47f3c3
    if [ ! -e "${fname}" ]; then
47f3c3
      for br in "${branches[@]}"
47f3c3
      do
47f3c3
        br=$(echo "${br}"| sed -e s'|remotes/origin/||')
47f3c3
        url="${SURL}/${pn}/${br}/${fsha}"
47f3c3
        echo "Retrieving ${url}"
47f3c3
        curl -L ${QUIET} -f "${url}" -o "${fname}" && break
47f3c3
      done
47f3c3
    else
47f3c3
      echo "${fname} exists. skipping"
47f3c3
    fi
47f3c3
    if [ ${CHECK} -eq 1 ]; then
47f3c3
        downsum=$(${hashType} "${fname}" | awk '{print $1}')
47f3c3
        if [ "${fsha}" != "${downsum}" ]; then
47f3c3
            rm -f "${fname}"
47f3c3
            echo "Failure: ${fname} hash does not match hash from the .metadata file" >&2
47f3c3
            exit 1;
47f3c3
        fi
47f3c3
    fi
47f3c3
  fi
47f3c3
done < "${meta}"