Blame into_srpm.sh

Pat Riehecky 87c8ae
#!/bin/bash -u
Pat Riehecky 87c8ae
#
Pat Riehecky 87c8ae
# Turn a centos git repo into a .src.rpm file
Pat Riehecky 87c8ae
#
Pat Riehecky 87c8ae
# Might want to drop this in ~/bin/ and chmod u+x it
Pat Riehecky 87c8ae
Pat Riehecky 87c8ae
#
Pat Riehecky 87c8ae
#  License: GPLv2
Pat Riehecky 87c8ae
#
Pat Riehecky 87c8ae
#  Initial Author: Pat Riehecky <riehecky at fnal.gov>
Pat Riehecky 87c8ae
#
Pat Riehecky 87c8ae
Pat Riehecky 87c8ae
#####################################################################
Pat Riehecky 87c8ae
usage() {
Pat Riehecky 87c8ae
    echo ''                                               >&2
c429fb
    echo "$0 [-htq] [-b branch] [-c shasum]"              >&2
Pat Riehecky 87c8ae
    echo ''                                               >&2
Pat Riehecky 87c8ae
    echo 'You need to run this from inside a sources git repo' >&2
Pat Riehecky 87c8ae
    echo ''                                               >&2
Pat Riehecky 87c8ae
    echo ' -h: This help message'                         >&2
Pat Riehecky 87c8ae
    echo ' -q: Suppress warnings'                         >&2
c429fb
    echo ' -t: Set srpm timestamp to commit date'         >&2
db2f8e
    echo ' -s: Allow building as a SCL style package'     >&2
Pat Riehecky 87c8ae
    echo ''                                               >&2
Pat Riehecky 87c8ae
    echo ' -b: specify a branch to examine'               >&2
Pat Riehecky 87c8ae
    echo "     defaults to repo's current branch"         >&2
Pat Riehecky 87c8ae
    echo "     NOTE: your repo will be set to this branch">&2
Pat Riehecky 87c8ae
    echo ''                                               >&2
Pat Riehecky 87c8ae
    echo ' -c: specify a commit id'                       >&2
Pat Riehecky 87c8ae
    echo "     defaults to repo's current commit id"      >&2
Pat Riehecky 87c8ae
    echo "     NOTE: your repo will be set to this commit">&2
Pat Riehecky 87c8ae
    echo ''                                               >&2
Pat Riehecky 87c8ae
    echo "  $0"                                           >&2
db2f8e
    echo "  $0 -b c7 -t"                                  >&2
Pat Riehecky 87c8ae
    echo "  $0 -b remotes/origin/c7"                      >&2
Pat Riehecky 87c8ae
    echo "  $0 -c 865ae5909b2b5d5fb37971b7ad7960f1fd5a5ffa" >&2
Pat Riehecky 87c8ae
    echo "  $0 -b c7 -c 865ae5909b2b5d5fb37971b7ad7960f1fd5a5ffa" >&2
Pat Riehecky 87c8ae
    exit 1
Pat Riehecky 87c8ae
}
Pat Riehecky 87c8ae
Pat Riehecky 87c8ae
#####################################################################
Pat Riehecky 87c8ae
Pat Riehecky 87c8ae
QUIET=0
c429fb
KEEPTIMESTAMP=0
db2f8e
ALLOWSCL=0
Pat Riehecky 87c8ae
COMMITHASH=""
Pat Riehecky 87c8ae
BRANCH=""
Pat Riehecky 87c8ae
Pat Riehecky 87c8ae
#####################################################################
Pat Riehecky 87c8ae
# setup args in the right order for making getopt evaluation
Pat Riehecky 87c8ae
# nice and easy.  You'll need to read the manpages for more info
Pat Riehecky 87c8ae
# utilizing 'while' construct rather than 'for arg' to avoid unnecessary
Pat Riehecky 87c8ae
# shifting of program args
db2f8e
args=$(getopt -o htsqb:c: -- "$@")
Pat Riehecky 87c8ae
eval set -- "$args"
Pat Riehecky 87c8ae
Pat Riehecky 87c8ae
while [[ 0 -eq 0 ]]; do
Pat Riehecky 87c8ae
    case $1 in
Pat Riehecky 87c8ae
        -- )
Pat Riehecky 87c8ae
            # end of getopt args, shift off the -- and get out of the loop
Pat Riehecky 87c8ae
            shift
Pat Riehecky 87c8ae
            break
Pat Riehecky 87c8ae
           ;;
c429fb
         -t )
db2f8e
            # keep timestamps of commit on srpm
c429fb
            KEEPTIMESTAMP=1
c429fb
            shift
c429fb
           ;;
Pat Riehecky 87c8ae
         -q )
Pat Riehecky 87c8ae
            # suppress warnings
Pat Riehecky 87c8ae
            QUIET=1
Pat Riehecky 87c8ae
            shift
Pat Riehecky 87c8ae
           ;;
db2f8e
         -s )
db2f8e
            # try to detect if this was an SCL and build as such
db2f8e
            ALLOWSCL=1
db2f8e
            shift
db2f8e
           ;;
Pat Riehecky 87c8ae
         -c )
Pat Riehecky 87c8ae
            # Use this commit id
Pat Riehecky 87c8ae
            COMMITHASH=$2
Pat Riehecky 87c8ae
            shift
Pat Riehecky 87c8ae
            shift
Pat Riehecky 87c8ae
           ;;
Pat Riehecky 87c8ae
         -b )
Pat Riehecky 87c8ae
            # Check this particular branch 
Pat Riehecky 87c8ae
            BRANCH=$2
Pat Riehecky 87c8ae
            shift
Pat Riehecky 87c8ae
            shift
Pat Riehecky 87c8ae
           ;;
Pat Riehecky 87c8ae
         -h )
Pat Riehecky 87c8ae
            # get help
Pat Riehecky 87c8ae
            usage
Pat Riehecky 87c8ae
           ;;
Pat Riehecky 87c8ae
    esac
Pat Riehecky 87c8ae
done
Pat Riehecky 87c8ae
Pat Riehecky 87c8ae
if [[ ! -d .git ]] || [[ ! -d SPECS ]]; then
Pat Riehecky 87c8ae
    echo 'You need to run this from inside a sources git repo' >&2
Pat Riehecky 87c8ae
    exit 1
Pat Riehecky 87c8ae
fi
Pat Riehecky 87c8ae
Pat Riehecky 87c8ae
which git >/dev/null 2>&1
Pat Riehecky 87c8ae
if [[ $? -ne 0 ]]; then
Pat Riehecky 87c8ae
    echo 'You need git in PATH' >&2
Pat Riehecky 87c8ae
    exit 1
Pat Riehecky 87c8ae
fi
Pat Riehecky 87c8ae
Pat Riehecky 87c8ae
which get_sources.sh >/dev/null 2>&1
Pat Riehecky 87c8ae
if [[ $? -ne 0 ]]; then
Pat Riehecky 87c8ae
    echo 'You need get_sources.sh from centos-git-common in PATH' >&2
Pat Riehecky 87c8ae
    exit 1
Pat Riehecky 87c8ae
fi
Pat Riehecky 87c8ae
Pat Riehecky 87c8ae
which return_disttag.sh >/dev/null 2>&1
Pat Riehecky 87c8ae
if [[ $? -ne 0 ]]; then
Pat Riehecky 87c8ae
    echo 'You need return_disttag.sh from centos-git-common in PATH' >&2
Pat Riehecky 87c8ae
    exit 1
Pat Riehecky 87c8ae
fi
Pat Riehecky 87c8ae
Pat Riehecky 87c8ae
which get_sources.sh >/dev/null 2>&1
Pat Riehecky 87c8ae
if [[ $? -ne 0 ]]; then
Pat Riehecky 87c8ae
    echo 'You need get_sources.sh from centos-git-common in PATH' >&2
Pat Riehecky 87c8ae
    exit 1
Pat Riehecky 87c8ae
fi
Pat Riehecky 87c8ae
db2f8e
which show_possible_srpms.sh >/dev/null 2>&1
db2f8e
if [[ $? -ne 0 ]]; then
db2f8e
    echo 'You need show_possible_srpms.sh from centos-git-common in PATH' >&2
db2f8e
    exit 1
db2f8e
fi
db2f8e
db2f8e
# Set us to requested branch for further operations
Pat Riehecky 87c8ae
if [[ "x${BRANCH}" != 'x' ]]; then
Pat Riehecky 87c8ae
    if [[ $QUIET -eq 1 ]]; then
Pat Riehecky 87c8ae
        git fetch >/dev/null
Pat Riehecky 87c8ae
        git checkout ${BRANCH} >/dev/null
Pat Riehecky 87c8ae
    else
Pat Riehecky 87c8ae
        git fetch
Pat Riehecky 87c8ae
        git checkout ${BRANCH}
Pat Riehecky 87c8ae
    fi
Pat Riehecky 87c8ae
fi
Pat Riehecky 87c8ae
db2f8e
# Set us to requested commit for further operations
Pat Riehecky 87c8ae
if [[ "x${COMMITHASH}" != 'x' ]]; then
Pat Riehecky 87c8ae
    if [[ $QUIET -eq 1 ]]; then
Pat Riehecky 87c8ae
        git fetch >/dev/null
Pat Riehecky 87c8ae
        git checkout ${COMMITHASH} >/dev/null
Pat Riehecky 87c8ae
    else
Pat Riehecky 87c8ae
        git fetch
Pat Riehecky 87c8ae
        git checkout ${COMMITHASH}
Pat Riehecky 87c8ae
    fi
Pat Riehecky 87c8ae
fi
Pat Riehecky 87c8ae
db2f8e
# Download archive from git.centos.org
Pat Riehecky 87c8ae
if [[ $QUIET -eq 1 ]]; then
Pat Riehecky 87c8ae
    get_sources.sh
Pat Riehecky 87c8ae
else
Pat Riehecky 87c8ae
    get_sources.sh -q
Pat Riehecky 87c8ae
fi
Pat Riehecky 87c8ae
db2f8e
SPECFILE=$(cd SPECS; ls *.spec)
db2f8e
db2f8e
# Determine if we are an SCL
db2f8e
ISSCL=0
db2f8e
if [[ ${ALLOWSCL} -eq 1 ]]; then
db2f8e
    mytestsclname='DA39A3EE5E6B4B0D3255BFEF95601890AFD80709'
db2f8e
    mysclcheck=$(rpm --define "dist el" --define "scl ${mytestsclname}" -q --specfile "SPECS/${SPECFILE}" --qf '%{n}-%{v}-%{r}\n' 2>/dev/null | head -n 1)
db2f8e
    echo ${mysclcheck} | grep -q ${mytestsclname}
db2f8e
    if [[ $? -eq 0 ]]; then
db2f8e
        ISSCL=1
db2f8e
        SCLNAME=$(show_possible_srpms.sh | cut -d'-' -f1 | sort -u)
db2f8e
        if [[ $(echo ${SCLNAME} | wc -l) -ne 1 ]]; then
db2f8e
            echo "$0 failed to determine unique SCL name" >&2
db2f8e
            echo "Found:"                                 >&2
db2f8e
            echo "${SCLNAME}"                             >&2
db2f8e
        fi
db2f8e
    fi
db2f8e
fi
db2f8e
db2f8e
# build our rpmopts list
db2f8e
RPMOPTS="-bs --nodeps"
db2f8e
if [[ ${QUIET} -eq 1 ]]; then
db2f8e
    DIST=$(return_disttag.sh -q)
db2f8e
else
db2f8e
    DIST=$(return_disttag.sh)
db2f8e
fi
db2f8e
db2f8e
# put it all together
db2f8e
if [[ ${ISSCL} -eq 1 ]]; then
db2f8e
    rpmbuild --define "%_topdir `pwd`" ${RPMOPTS} --define "%dist ${DIST}" --define "%scl ${SCLNAME}" SPECS/${SPECFILE}
Pat Riehecky 87c8ae
    RC=$?
Pat Riehecky 87c8ae
else
db2f8e
    rpmbuild --define "%_topdir `pwd`" ${RPMOPTS} --define "%dist ${DIST}" SPECS/${SPECFILE}
Pat Riehecky 87c8ae
    RC=$?
Pat Riehecky 87c8ae
fi
Pat Riehecky 87c8ae
c429fb
if [[ ${KEEPTIMESTAMP} -eq 1 ]]; then
c429fb
    timestamp=$(git log --pretty=%ai -1)
c429fb
    find . -type f -name \*.src.rpm -exec touch -d "${timestamp}" {} \;
c429fb
fi
c429fb
Pat Riehecky 87c8ae
if [[ ${RC} -ne 0 ]]; then
db2f8e
    if [[ ${QUIET} -eq 1 ]]; then
Pat Riehecky 87c8ae
        echo "$0 failed to recreate srpm" >&2
Pat Riehecky 87c8ae
    fi
Pat Riehecky 87c8ae
    exit 1
Pat Riehecky 87c8ae
fi