arrfab / centos-git-common

Forked from centos-git-common 5 years ago
Clone

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
733e0e
    echo "$0 [-hqts] [-d dist] [-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
733e0e
    echo ' -d: Use this %{dist} instead of automatic value'>&2
733e0e
    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
733e0e
    echo "  $0 -b c7 -s"                                  >&2
733e0e
    echo "  $0 -b c7 -d yourdisthere"                     >&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=""
733e0e
DIST=''
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
733e0e
args=$(getopt -o htsqb:c:d: -- "$@")
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
           ;;
733e0e
         -d )
733e0e
            # Set %{dist} to this instead the automatic value
733e0e
            DIST=$2
733e0e
            shift
dea6c0
            shift
733e0e
           ;;
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
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
a68f52
if [[ ${ALLOWSCL} -eq 1 ]]; then
a68f52
    rpm -q scl-utils-build >/dev/null 2>&1
a68f52
    if [[ $? -ne 0 ]]; then
a68f52
        echo 'Without scl-utils-build some SCL style sources be parsed' >&2
a68f52
        exit 1
a68f52
    fi
a68f52
fi
a68f52
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
3a0ccf
# determine automatically unless we've got one set
3a0ccf
if [[ "x${DIST}" == 'x' ]]; then
3a0ccf
    if [[ ${QUIET} -eq 1 ]]; then
3a0ccf
        DIST=$(return_disttag.sh -q)
3a0ccf
    else
3a0ccf
        DIST=$(return_disttag.sh)
3a0ccf
    fi
3a0ccf
fi
3a0ccf
db2f8e
# Determine if we are an SCL
db2f8e
ISSCL=0
db2f8e
if [[ ${ALLOWSCL} -eq 1 ]]; then
db2f8e
    mytestsclname='DA39A3EE5E6B4B0D3255BFEF95601890AFD80709'
3a0ccf
    mysclcheck=$(rpm --define "dist ${DIST}" --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
3a0ccf
        BASENAME=$(echo ${mysclcheck} | sed -e 's/DA39A3EE5E6B4B0D3255BFEF95601890AFD80709//')
3a0ccf
        RPMNAME=$(show_possible_srpms.sh | head -1)
3a0ccf
        SCLNAME=$(echo ${RPMNAME} | sed -e "s/${BASENAME}.src.rpm//")
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
3a0ccf
3a0ccf
        scl_check=$(rpm --define "dist ${DIST}" --define "scl ${SCLNAME}" -q --specfile "SPECS/${SPECFILE}" --qf '%{n}-%{v}-%{r}.src.rpm\n' 2>/dev/null | head -n 1)
3a0ccf
        if [[ "${RPMNAME}" != "${scl_check}" ]]; then
3a0ccf
            echo "$0 failed to verify SCL name" >&2
3a0ccf
            echo "Found:"                       >&2
3a0ccf
            echo "${SCLNAME}"                   >&2
3a0ccf
            echo "made:     ${scl_check}"       >&2
3a0ccf
            echo "expected: ${RPMNAME}"         >&2
3a0ccf
        fi
db2f8e
    fi
db2f8e
fi
db2f8e
db2f8e
# build our rpmopts list
db2f8e
RPMOPTS="-bs --nodeps"
733e0e
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