Script "get_source_for_pkg.sh"
#!/bin/sh

# ------------------------
# get_source_for_pkg.sh
# version 2.4.3.1
# ------------------------

# check for parameters
if [ ! $# -eq 1 ]; then
  echo "ERROR: exactly one parameter (package title), please. Abort."
  exit 1
fi

[ -z "${BUILD_FARM}" ] && BUILD_FARM="${HOME}/build-farm"
if [ ! -d "${BUILD_FARM}" ]
then
  echo "ERROR: build-farm directory '${BUILD_FARM}' doesn't exist. Abort."
  exit 1
fi

[ -z "${PKGBS_DIR}" ] && PKGBS_DIR="${BUILD_FARM}"/BUILD-SCRIPTS
[ -d "${PKGBS_DIR}" ] || PKGBS_DIR=`pwd`

if [ ! -f "${PKGBS_DIR}"/vcsdir_to_builddir.sh ]; then
  echo "ERROR: build farm is broken; can't find script 'vcsdir_to_builddir.sh'. Abort."
  exit 1
fi

# check for required binaries
REQD_BINS=""
which awk >/dev/null 2>&1 || type awk >/dev/null 2>&1 || REQD_BINS="awk,${REQD_BINS}"
if [ -n "${REQD_BINS}" ]; then
  echo -e `basename ${0}`: "Required binaries (${REQD_BINS}\010) are not present."
  exit 1
fi

PKG_TITLE=`basename "${1}"`
PKG_MODE=`dirname "${1}"`
[ -z "${PKG_MODE}" ] && PKG_MODE="."

printf "%s '%s'" "source code for package" "${PKG_TITLE}"

SRCINFO_FILE="${PKGBS_DIR}/${PKG_MODE}"/SRCINFO/srcinfo-"${PKG_TITLE}"
if [ ! -f "${SRCINFO_FILE}" ]; then
  echo ": srcinfo not found. Abort."
  exit 1
else
  echo ""
fi

_srcinfo_first_line=`head -1 "${SRCINFO_FILE}"`
printf "\t%s" "type: "
pkg_src_type=`echo "${_srcinfo_first_line}" | awk '{print $1}'`
echo "${pkg_src_type}"
printf "\t%s" "location: "
pkg_src_location=`echo "${_srcinfo_first_line}" | awk '{print $2}'`
echo "${pkg_src_location}"

srcinfo_some_option=`echo "${_srcinfo_first_line}" | awk '{print $3}'`
if [ ! -z "${srcinfo_some_option}" ]; then
  printf "\t%s" "option: "
  echo "${srcinfo_some_option}"
fi

case "${pkg_src_type}" in
  git )
    # check for required binaries
    REQD_BINS=""
    which git >/dev/null 2>&1 || type git >/dev/null 2>&1 || REQD_BINS="git,${REQD_BINS}"
    if [ -n "${REQD_BINS}" ]; then
      echo -e `basename ${0}` "(for git):" "Required binaries (${REQD_BINS}\010) are not present."
      exit 1
    fi

    git_base_directory="${BUILD_FARM}"/"${PKG_MODE}"
    if [ ! -d "${BUILD_FARM}"/"${PKG_MODE}"/"${PKG_TITLE}"-git ] && [ -d "${BUILD_FARM}"/"${PKG_TITLE}"-git ]; then
      git_base_directory="${BUILD_FARM}"
    fi

    git_clone_options=""
    if [ ! -z "${srcinfo_some_option}" ]; then
      if [ -d "${git_base_directory}"/"${PKG_TITLE}"-git ]; then
        if [ -d "${git_base_directory}"/"${PKG_TITLE}"-git.oldrepo ]; then
            printf "removing \"oldrepo\" directory ('%s')... " "${git_base_directory}"/"${PKG_TITLE}"-git.oldrepo
            rm -rf "${git_base_directory}"/"${PKG_TITLE}"-git.oldrepo 2>/dev/null
            echo "ok"
        fi
        mv "${git_base_directory}"/"${PKG_TITLE}"-git "${git_base_directory}"/"${PKG_TITLE}"-git.oldrepo
      fi
      git_clone_options=`printf -- "--branch %s" "${srcinfo_some_option}"`
    fi

    if [ ! -d "${git_base_directory}"/"${PKG_TITLE}"-git ]; then
      # note: it's very important not to quote ${git_clone_options} here
      git clone ${git_clone_options} "${pkg_src_location}" "${git_base_directory}"/"${PKG_TITLE}"-git
      if [ ! -d "${git_base_directory}"/"${PKG_TITLE}"-git ]; then
        echo "ERROR: git clone error. Abort."
        exit 1
      fi
      echo "cloned git repository: '${pkg_src_location}' -->" "${git_base_directory}"/"${PKG_TITLE}"-git
    else
      echo "found local git repository:" "${git_base_directory}"/"${PKG_TITLE}"-git
    fi

    cd "${git_base_directory}"/"${PKG_TITLE}"-git
    git_desc_version=`git describe 2>/dev/null | sed 's|/|-|'`
    [ -z "${git_desc_version}" ] && git_desc_version=`git describe --tags 2>/dev/null`
    [ -z "${git_desc_version}" ] && git_desc_version=`git describe --always 2>/dev/null`
    echo "version from git describe:" "${git_desc_version}"

    builddir_success=""
    "${PKGBS_DIR}"/vcsdir_to_builddir.sh --srcpkg --quiet --force
    [ $? -eq 0 ] && builddir_success="yes"
    cd "${PKGBS_DIR}"

    if [ -z ${builddir_success} ]; then
      echo "ERROR: something terrible is happen, can't get build directory from git. Abort."
      exit 1
    fi
    ;;
  hg )
    # check for required binaries
    REQD_BINS=""
    which hg >/dev/null 2>&1 || type hg >/dev/null 2>&1 || REQD_BINS="hg,${REQD_BINS}"
    if [ -n "${REQD_BINS}" ]; then
      echo -e `basename ${0}` "(for hg):" "Required binaries (${REQD_BINS}\010) are not present."
      exit 1
    fi

    if [ ! -d "${BUILD_FARM}"/"${PKG_MODE}"/"${PKG_TITLE}"-hg ]; then
      hg clone "${pkg_src_location}" "${BUILD_FARM}"/"${PKG_MODE}"/"${PKG_TITLE}"-hg
      echo "cloned mercurial repository: '${pkg_src_location}' -->" "${BUILD_FARM}"/"${PKG_MODE}"/"${PKG_TITLE}"-hg
    else
      echo "found local mercurial repository:" "${BUILD_FARM}"/"${PKG_MODE}"/"${PKG_TITLE}"-hg
    fi

    cd "${BUILD_FARM}"/"${PKG_MODE}"/"${PKG_TITLE}"-hg
    ##hg_desc_version=`hg summary 2>/dev/null | head -1 | awk '{print $2}'`
    hg_desc_version=`hg parent --template '{latesttag}-{latesttagdistance}-{node|short}\n'`
    [ -z "${hg_desc_version}" ] && hg_desc_version=hg-`date +"%Y%m%d"`
    echo "version from hg parent:" "${hg_desc_version}"

    builddir_success=""
    "${PKGBS_DIR}"/vcsdir_to_builddir.sh --srcpkg --quiet --force
    [ $? -eq 0 ] && builddir_success="yes"
    cd "${PKGBS_DIR}"

    if [ -z ${builddir_success} ]; then
      echo "ERROR: something terrible is happen, can't get build directory from mercurial. Abort."
      exit 1
    fi
    ;;
  svn )
    # svn co http://svn.python.org/projects/python/trunk python-svn
    echo "sorry, subversion support is still in progress. Abort."
    exit 1
    ;;
  ftp | http )
    # check for required binaries
    REQD_BINS=""
    which wget >/dev/null 2>&1 || type wget >/dev/null 2>&1 || REQD_BINS="wget,${REQD_BINS}"
    which tar >/dev/null 2>&1 || type tar >/dev/null 2>&1 || REQD_BINS="tar,${REQD_BINS}"
    which file >/dev/null 2>&1 || type file >/dev/null 2>&1 || REQD_BINS="file,${REQD_BINS}"
    if [ -n "${REQD_BINS}" ]; then
      echo -e `basename ${0}` "(for ftp/http):" "Required binaries (${REQD_BINS}\010) are not present."
      exit 1
    fi

    tarball_or_src_name=`basename "${pkg_src_location}"`
    if [ -f "${BUILD_FARM}"/${tarball_or_src_name} ]; then
      echo "found old tarball/source ('${BUILD_FARM}/${tarball_or_src_name}'), removing it"
      rm "${BUILD_FARM}/${tarball_or_src_name}"
    fi

    echo "wgetting ${pkg_src_location}..."
    wget "${pkg_src_location}" --directory-prefix="${BUILD_FARM}"
    if [ $? -ne 0 ]; then
      echo "ERROR: wget error. Abort."
      exit 1
    fi

    type_of_file=`file "${BUILD_FARM}/${tarball_or_src_name}" | cut -d':' -f2`
    is_bzip2=`echo "${type_of_file}" | grep bzip2`
    is_xz=`echo "${type_of_file}" | grep xz`
    is_gzip=`echo "${type_of_file}" | grep gzip`
    is_tar=`echo "${type_of_file}" | grep tar`
    is_infozip=`echo "${type_of_file}" | grep "Zip archive data"`
    is_c_file=`echo "${type_of_file}" | grep "C program"`

    if [ ! -z "${is_bzip2}" ] || [ ! -z "${is_xz}" ] || [ ! -z "${is_gzip}" ] || [ ! -z "${is_tar}" ] || [ ! -z "${is_infozip}" ]
    then
      printf "extracting '%s'" "${tarball_or_src_name}"
      rm -rf "${BUILD_FARM}"/.temp-for_extraction
      mkdir -p "${BUILD_FARM}"/.temp-for_extraction
      mv "${BUILD_FARM}/${tarball_or_src_name}" "${BUILD_FARM}"/.temp-for_extraction/
      cd "${BUILD_FARM}"/.temp-for_extraction

      unc_output=""
      if [ ! -z "${is_bzip2}" ]; then
        printf " (bzip2)..."
        unc_output=`tar xjf "${tarball_or_src_name}" 2>&1 | grep -v "warning:"`
      elif [ ! -z "${is_xz}" ]; then
        printf " (xz)..."
        unc_output=`tar xJf "${tarball_or_src_name}" 2>&1 | grep -v "warning:"`
      elif [ ! -z "${is_gzip}" ]; then
        printf " (gzip)..."
        unc_output=`tar xzf "${tarball_or_src_name}" 2>&1 | grep -v "warning:"`
      elif [ ! -z "${is_tar}" ]; then
        printf " (tar)..."
        unc_output=`tar xf "${tarball_or_src_name}" 2>&1 | grep -v "warning:"`
      elif [ ! -z "${is_infozip}" ]; then
        printf " (infozip)..."
        REQD_BINS=""
        which unzip >/dev/null 2>&1 || type unzip >/dev/null 2>&1 || REQD_BINS="unzip,${REQD_BINS}"
        if [ ! -z "${REQD_BINS}" ]; then
          echo -e `basename ${0}` "(for ftp/http, pkzip archive):" "Required binaries (${REQD_BINS}\010) are not present."
          exit 1
        fi
        infozip_dir="${PKG_TITLE}"-`echo "${tarball_or_src_name}" | sed 's/[^-]*-//'` ## cut -d'-' -f2-
        prev_wdir=`pwd`
        mkdir -p "${infozip_dir}" && cd "${infozip_dir}"
        unc_output=`unzip ../"${tarball_or_src_name}" 1>/dev/null 2>&1`
        cd "${prev_wdir}"
      fi

      if [ -z "${unc_output}" ]; then
        echo " ok"
      else
        echo " ERROR ("`echo "${unc_output}" | sed 's/\n/ /'`"). Abort."
        mv "${BUILD_FARM}"/.temp-for_extraction/* "${BUILD_FARM}"/
        rm -rf "${BUILD_FARM}"/.temp-for_extraction
        exit 1
      fi
    elif [ ! -z "${is_c_file}" ]
    then
      printf "creating directory for source file: "
      src_dir_name=`echo "${tarball_or_src_name}" | sed 's/.c//'`
      mkdir -p "${BUILD_FARM}/${src_dir_name}"
      cp "${BUILD_FARM}/${tarball_or_src_name}" "${BUILD_FARM}/${src_dir_name}"/
      echo "${BUILD_FARM}/${src_dir_name}"
      cd "${BUILD_FARM}"
    else
      echo "ERROR: unknown compression method or unknown source file. Abort."
      exit 1
    fi

    if [ -f ./"${tarball_or_src_name}" ]; then
      printf "removing downloaded tarball ('%s')... " "${tarball_or_src_name}"
      rm -f ./"${tarball_or_src_name}" && echo "ok"
    else
      printf "WARNING: downloaded tarball ('%s') is missing!\n" "${tarball_or_src_name}"
    fi

    unpacked_tarball=`ls -t | grep "${PKG_TITLE}" 2>/dev/null | head -1`
    if [ -z "${unpacked_tarball}" ]; then
      # deal with 'a-b' package titles
      tarball_title=`echo "${PKG_TITLE}" | sed 's/_/-/g'`
      unpacked_tarball=`ls -t | grep "${tarball_title}" 2>/dev/null | head -1`
      if [ ! -z "${unpacked_tarball}" ]; then
        unpacked_tarball_no_minuses=`echo "${unpacked_tarball}" | sed "s/${tarball_title}/${PKG_TITLE}/"`
        rm -rf ./"${unpacked_tarball_no_minuses}"
        mv ./"${unpacked_tarball}" ./"${unpacked_tarball_no_minuses}"
        unpacked_tarball="${unpacked_tarball_no_minuses}"
      fi
    fi
    if [ -z "${unpacked_tarball}" ]; then
      # try to deal with package titles not equal to unpacked directory names
      unpacked_tarball_parts_of_name=`echo "${tarball_or_src_name}" | tr -- "-_." "\n\n\n"`
      unpacked_tar_name_part1=`echo "${unpacked_tarball_parts_of_name}" | head -1`
      unpacked_tar_name_part2=`echo "${unpacked_tarball_parts_of_name}"    | sed -n '2p'`
      unpacked_tar_name_part3=`echo "${unpacked_tarball_parts_of_name}"    | sed '3!d'`
      [ -z "${unpacked_tarball}" ] && unpacked_tarball=`ls -t | grep "${unpacked_tar_name_part1}" 2>/dev/null | head -1`
      [ -z "${unpacked_tarball}" ] && unpacked_tarball=`ls -t | grep "${unpacked_tar_name_part2}" 2>/dev/null | head -1`
      [ -z "${unpacked_tarball}" ] && unpacked_tarball=`ls -t | grep "${unpacked_tar_name_part3}" 2>/dev/null | head -1`
      if [ ! -z "${unpacked_tarball}" ]; then
        unpacked_tarball_new_name="${PKG_TITLE}"-"${unpacked_tarball}"
        rm -rf ./"${unpacked_tarball_new_name}"
        mv ./"${unpacked_tarball}" ./"${unpacked_tarball_new_name}"
        unpacked_tarball="${unpacked_tarball_new_name}"
      fi
    fi
    if [ -z "${unpacked_tarball}" ]; then
      # now, it's time to report fail
      printf "contents of '%s':\n" `pwd`
      ls -la ./
      echo "ERROR: unpacked tarball cannot be found. Abort."
      exit 1
    fi

    srcpkg_path=`echo "${BUILD_FARM}/${unpacked_tarball}" | sed 's/.srcpkg//'`.srcpkg
    if [ -d "${srcpkg_path}" ]; then
      printf "removing previous '%s' tarball directory..." "${srcpkg_path}"
      rm -rf "${srcpkg_path}" && echo " ok"
    fi
    mv "${unpacked_tarball}" "${srcpkg_path}"

    cd "${BUILD_FARM}"
    echo "build directory:" "${srcpkg_path}"
    builddir_size=`du -ms "${srcpkg_path}" 2>/dev/null | awk '{print $1}'`
    [ -z ${builddir_size} ] && builddir_size="0"
    echo "build directory size: ~${builddir_size} MiB"

    if [ ${builddir_size} -le 64 ]; then
      printf "compressing '%s'..." `basename "${srcpkg_path}"`
      tar cjf "${srcpkg_path}".tar.bz2 `basename "${srcpkg_path}"` 1>/dev/null 2>&1
      if [ $? -eq 0 ]; then
        echo " ok"
        echo "raw source package:" `basename "${srcpkg_path}".tar.bz2`
      else
        echo " fail!"
      fi
    else
      echo "skipping raw source package creation (> 64 MiB)"
    fi

    cd "${PKGBS_DIR}"
    rm -rf "${BUILD_FARM}"/.temp-for_extraction
    ;;
  * )
    echo "ERROR: unknown way to get source code ('${pkg_src_type}'). Abort."
    exit 1
    ;;
esac

exit 0
Unless otherwise stated, the content of this page is licensed under GNU Free Documentation License.