c4rt0 / centos-git-common

Forked from centos-git-common 2 years ago
Clone

Blame get_sources.sh

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