File checkin.sh of Package ceph

#!/bin/bash
#
# checkin.sh
#
# This script automates generation of a new tarball and spec file from a
# git clone or repo+branch combination for the "ceph" package in OBS.
#

set -e

BASEDIR=$(pwd)

function usage {
    set +x
    echo "Usage:"
    echo "  ${0} [-h,--help] [-e,--existing CLONE]"
    echo "             [-r,--repo REPO] [-b,--branch BRANCH]"
    echo "             [--pre-checkin]"
    echo ""
    echo "Options:"
    echo "    --existing     Use existing ceph clone CLONE"
    echo "    --repo         Make a fresh clone of ceph repo REPO"
    echo "    --branch       Use branch BRANCH with fresh clone"
    echo "    --pre-checkin  Just run the pre-checkin routine (ceph only)"
    echo ""
    echo "Notes:"
    echo "    If existing clone is given, repo and branch are ignored."
    echo "    Repo defaults to https://github.com/ceph/ceph.git"
    echo "    Branch defaults to reef"
    exit 1
}

function packages_files {
  local spec_file=$1
  local exclude_pkg=$2
  local in_files=false
  local result=""
  
  while IFS='' read -r line || [[ -n "$line" ]]; do
  
    case $line in
    "%files "*)
        pkg=$(echo "$line" | sed 's/%files.* \(\w\+\)/\1/g')
        if [[ $pkg == $exclude_pkg ]]; then
          in_files=false
          continue
        fi
        in_files=true
        continue
        ;;
    "%post"* | "%pre"* | "%preun"* | "%postun"* | "%changelog")
      in_files=false
      continue
      ;;
    esac
    
    result=""
    changed=true
    while [[ $in_files == true ]] && [[ $changed == true ]]; do
      changed=false
      case $line in
      "%if"* | "" | "%defattr"* | "%endif"* | "%else"* | "#"* | "%docdir"*)
        break
        ;;
      "%dir"*)
        break # for now we'll ignore %dir entries
        ;;
      *)
        result=$(echo "$line" | sed -e 's/%config \(.*\)/\1/g' \
                                   -e 's/%config(.*) \(.*\)/\1/g' \
                                   -e 's/%attr(.*) \(.*\)/\1/g' \
                                   -e 's/%exclude \(.*\)/\1/g')
        if [[ ! $line == $result ]]; then
          changed=true
        	line=$result
  	continue
        fi
        ;;
      esac
      
      echo "$result"
    done
  done < $spec_file
}

function get_package_summary {
  local spec_file=$1
  local pkg_name=$2
  local in_package=false
  
  while IFS='' read -r line || [[ -n "$line" ]]; do
  
    case $line in
    "%package "*)
      pkg=`echo "$line" | sed 's/%package.* \(\w\+\)/\1/g'`
      if [[ ! $pkg == $pkg_name ]]; then
        in_package=false
      else
        in_package=true
      fi
      ;;
    "%prep"* | "%build"* | "%install"*)
      in_package=false
      ;;
    "Summary:"*)
      if [[ $in_package == true ]]; then
        echo $line
        break
      fi
      ;;
    esac
  done < $spec_file
}

function get_package_requires {
  local spec_file=$1
  local pkg_name=$2
  local in_package=false
  
  while IFS='' read -r line || [[ -n "$line" ]]; do
  
    case $line in
    "%package "*)
      pkg=`echo "$line" | sed 's/%package.* \(\w\+\)/\1/g'`
      if [[ ! $pkg == $pkg_name ]]; then
        in_package=false
      else
        in_package=true
      fi
      ;;
    "%prep"* | "%build"* | "%install"*)
      in_package=false
      ;;
    "Requires:"*)
      if [[ $in_package == true ]]; then
        echo $line
      fi
      ;;
    esac
  done < $spec_file
}

function get_package_description {
  local spec_file=$1
  local pkg_name=$2
  local in_desc=false
  
  while IFS='' read -r line || [[ -n "$line" ]]; do
  
    case $line in
    "%description "*)
      pkg=`echo "$line" | sed 's/%description.* \(\w\+\)/\1/g'`
      if [[ ! $pkg == $pkg_name ]]; then
        in_desc=false
      else
        in_desc=true
      fi
      continue
      ;;
    "%prep"* | "%build"* | "%install"* | "%package"* | "%if"* | "%endif"*)
      in_desc=false
      continue
      ;;
    esac
   
    if [[ $in_desc == true ]]; then
      echo "$line"
    fi
  done < $spec_file
}

function munge_spec_file {
  sed -i -e 's/%bcond_without ceph_test_package/%bcond_with ceph_test_package/' ceph.spec
}

function transform_spec_file {
  local spec_file=$1
  local rm_files=$3
  local summary=$4
  local description=$5
  local requires=$6
  local in_package=false
  local in_files=false
  local in_desc=false
  
  while IFS='' read -r line || [[ -n "$line" ]]; do
  
    case $line in
    "%bcond_without ceph_test_package"*)
      line="%bcond_with ceph_test_package"
      ;;
    "Source0:"*)
      line=`echo $line | sed 's/%{name}/ceph/g'`
      ;;
    "ExclusiveArch:"*)
      line=`echo $line | sed 's/s390\S*//g'`
      ;;
    "%autosetup -p1")
      line="%autosetup -p1 -n ceph-%version"
      ;;
    "Requires:"*)
      if [[ $in_package == false ]]; then
        line=""
      fi
      ;;
    "Requires(post):"*)
      if [[ $in_package == false ]]; then
        IFS=''
        for r in "$requires"; do
          echo "$r"
        done
      fi
      ;;
    "%description"*)
      if [[ $in_package == false ]]; then
        in_desc=true
        echo "$line"
        echo "$description"
        echo ""
        continue
      fi
      ;;
    "%package "*)
        in_package=true
        continue
        ;;
    "%prep"* | "%build"* | "%install"*)
      in_package=false
      in_desc=false
      ;;
    "%files "*)
        pkg=`echo "$line" | sed 's/%files.* \(\w\+\)/\1/g'`
        if [[ $pkg == $include_pkg ]]; then
          in_files=false
        else
          in_files=true
          continue
        fi
        ;;
    "%changelog"*)
      in_files=false
      ;;
    "%clean"*)
      in_package=false
      IFS=''
      for rf in "$rm_files"; do
        echo "$rf"
      done
      echo ""
      echo "dirs=\`find %{buildroot} -type d -empty\`"
      echo "while [[ -n \$dirs ]]; do"
      echo "  for d in \$dirs; do"
      echo "    rm -rf \$d"
      echo "  done"
      echo "dirs=\`find %{buildroot} -type d -empty\`"
      echo "done"
      echo ""
      ;;
    "#"* | "%"*)
      in_desc=false
      ;;
    esac

  if [[ $in_package == false ]] && [[ $in_files == false ]] && [[ $in_desc == false ]]; then
    echo $line
  else
    if [[ $line == "%if"* ]] || [[ $line == "%endif"* ]]; then
      echo $line
    fi
  fi

  done < $spec_file
}

function generate_rm_exclude_files {
  local exclude_list=$1
  local buildroot=$2
  for f in $exclude_list; do
      echo "rm -rf $buildroot$f"
  done
}

function insert_line_before {
  local FILE=$1
  local line_to_insert=$2
  local match_regex=$3
  sed -i "/${match_regex}/i $line_to_insert" $FILE
}

function copy_changes_file {
  local dest_pkg=$1
  cp ceph.changes ${dest_pkg}.changes
}

function _pre_checkin {
    set -f 

    munge_spec_file
    # SPEC_FILE="ceph.spec"
    # rm_files=$(generate_rm_exclude_files "$files" "%{buildroot}")
    # summ=$(get_package_summary $SPEC_FILE $PACKAGE)
    # desc=$(get_package_description $SPEC_FILE $PACKAGE)
    # reqs=$(get_package_requires $SPEC_FILE $PACKAGE)
    # transform_spec_file $SPEC_FILE $PACKAGE "$rm_files" "$summ" "$desc" "$reqs" > $PACKAGE.spec
    # insert_line_before "${PACKAGE}.spec" "Source99: ceph-rpmlintrc" "_insert_obs_source_lines_here"
    # insert_line_before "${PACKAGE}.spec" "Source98: README-ceph-test.txt" "^Source99:"
    # insert_line_before "${PACKAGE}.spec" "Source97: README-checkin.txt" "^Source98:"
    # insert_line_before "${PACKAGE}.spec" "Source96: checkin.sh" "^Source97:"

    set +f
}

function _error_exit {
    echo >&2 $1
    exit $2
}

function _check_ceph_clone {
    local OPT="$1"
    if [ -z "$OPT" ] ; then
        _error_exit "Empty string sent to internal function _check_ceph_clone"
    fi
    if [ -e "$OPT/make-dist" ] ; then
        echo "$OPT looks like a ceph clone; using it"
    else
        _error_exit "$OPT does not appear to be a ceph clone" 1
    fi
}

function _verify_git_describe {
    git describe --match 'v*'
    echo "Does this version number looks sane? y/[N]"
    read a
    if [ "x$a" != "xy" ] ; then
        _error_exit "Aborting!" 1
    fi
}

GETOPT=$(getopt -o b:e:hr: --long "branch:,existing:,help,repo:,pre-checkin" \
       -n 'checkin.sh' -- "$@")
test "$?" -eq 0 || _error_exit "Terminating..." 1
eval set -- "$GETOPT"

EXISTING=""
REPO="https://github.com/ceph/ceph.git"
BRANCH="reef"
PRECHECKIN=""
while true ; do
    case "$1" in
        -b|--branch)   BRANCH="$2"   ; shift 2 ;;
        -e|--existing) EXISTING="$2" ; shift 2 ;;
        -h|--help)     usage ;; # does not return
        -r|--repo)     REPO="$2"     ; shift 2 ;;
        --pre-checkin) PRECHECKIN="$1" ; shift ;;
        --) shift ; break ;;
        *) echo "Internal error" ; exit 1 ;;
    esac
done

# expand repo shortcuts
# FIXME: use bash regex instead of perl here
if [ "$(perl -e '"'"$REPO"'" =~ m/(^\w+$)/; print "$1\n";')" ] ; then
    REPO="https://github.com/$REPO/ceph.git"
elif [ "$(perl -e '"'"$REPO"'" =~ m/(^\w+\/\w+$)/; print "$1\n";')" ] ; then
    REPO="https://github.com/$REPO.git"
fi

if [ -n "$PRECHECKIN" ] ; then
    _pre_checkin
    exit 0
fi

if [ -n "$EXISTING" ] ; then
    if [ ! -d "$EXISTING" ] ; then
        _error_exit "Alleged directory ->$EXISTING<- is not a directory" 1
    fi
    if [ ! -r "$EXISTING" ] ; then
        _error_exit "I cannot read directory ->$EXISTING<-" 1
    fi
    if [ ! -w "$EXISTING" ] ; then
        _error_exit "I cannot write to directory ->$EXISTING<-" 1
    fi
    if [ ! -x "$EXISTING" ] ; then
        _error_exit "I cannot cd to directory ->$EXISTING<-" 1
    fi
    CLONE="$EXISTING"
else
    echo "Will make fresh clone of repo ->$REPO<- branch ->$BRANCH<-"
    # TMPDIR=$(mktemp -d --tmpdir=$BASEDIR)  # does not work due to http://tracker.ceph.com/issues/39556
    TMPDIR=$(mktemp -d)
    echo "Created temporary temporary $TMPDIR"
    git clone --branch $BRANCH $REPO $TMPDIR
    CLONE="$TMPDIR"
fi
_check_ceph_clone "$CLONE"

pushd $CLONE
#_verify_git_describe
if [ -z "$TMPDIR" ] ; then
    echo "Deleting stale tarballs from previous runs"
    rm -rf *.bz2
fi
echo "Running make-dist inside clone"
./make-dist
popd

echo "Running \"osc rm *bz2\" to nuke previous tarball"
if type osc > /dev/null 2>&1 ; then
    osc rm *bz2 || true
else
    _error_exit "osc not installed - cannot continue" 1
fi

if stat --printf='' *.bz2 2>/dev/null ; then
    _error_exit "There are still files ending in bz2 in the current directory - clean up yourself!" 1
fi

echo "Copying new spec file and tarball from $CLONE"
cp $CLONE/ceph.spec $CLONE/ceph*bz2 .

if [ -n "$TMPDIR" ] ; then
    echo "Nuking the clone"
    rm -rf $TMPDIR
fi

echo "Running \"osc add *bz2\" to register the new tarball"
osc add *bz2

_pre_checkin

echo "Done! Run \"osc ci --noservice\" to commit."
openSUSE Build Service is sponsored by