Script "pull-all.sh"
#!/bin/sh

# --------------------
# pull-all.sh
# version 2.4.2.2
# --------------------

script_call_name=`basename ${0}`
usage_str=`echo "Usage:" ./"${script_call_name}" "[ --pull-bigsystem ]" "[ --firstrun [ --force-clone ] ]"`

# --------------------------------------------------------------------------------

PULL_BIGSYSTEM_OPT=""
FIRSTRUN_OPT=""
FORCE_CLONE_OPT=""
for arg in $@
do
  [ $arg = "--pull-bigsystem" ] && PULL_BIGSYSTEM_OPT="yes" && continue
  [ $arg = "--firstrun" ] && FIRSTRUN_OPT="yes" && continue
  [ $arg = "--force-clone" ] && FORCE_CLONE_OPT="yes" && continue
  if [ $arg = "--help" ] || [ $arg = "--usage" ]; then
    echo "${usage_str}"
    exit 0
  fi
  echo "${script_call_name}: unknown option '${arg}'"
done

if [ -z "${FIRSTRUN_OPT}" ] && [ ! -z "${FORCE_CLONE_OPT}" ]; then
  printf "ignored '--force-clone' option: it is valid only with '--firstrun'\n"
  FORCE_CLONE_OPT="ignored"
fi

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

current_date=`date`
echo "date:" "${current_date}"

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

mkdir -p "${BUILD_FARM}"/pull-results-last

# --------------------

git_pull_it()
# requires 1 parameter: name of directory with .git
{
  if [ ! $# -eq 1 ]; then
    echo "*INTERNAL ERROR*"
  else
    printf "git pull %s" "${1}"
    printf "... "

    if [ -d ./${1} ]; then
      cd ./${1}
      if [ -d .git ]; then
        echo "${current_date}" >"${BUILD_FARM}"/pull-results-last/.tempfile

        prev_desc_version=`git describe 2>/dev/null | sed 's|/|-|'`
        [ -z "${prev_desc_version}" ] && prev_desc_version=`git describe --tags 2>/dev/null`
        [ -z "${prev_desc_version}" ] && prev_desc_version=`git describe --always 2>/dev/null`
        [ -z "${prev_desc_version}" ] && prev_desc_version="version unavailable"
        echo "(<-) ${prev_desc_version}" >>"${BUILD_FARM}"/pull-results-last/.tempfile
        echo "========================================" >>"${BUILD_FARM}"/pull-results-last/.tempfile

        git_pull_failed=""
        git pull >>"${BUILD_FARM}"/pull-results-last/.tempfile 2>/dev/null || git_pull_failed="yes"
        pull_output=`head "${BUILD_FARM}"/pull-results-last/.tempfile | sed '1,3d'`
        desc_version=`git describe 2>/dev/null | sed 's|/|-|'`
        [ -z "${desc_version}" ] && desc_version=`git describe --tags 2>/dev/null`
        [ -z "${desc_version}" ] && desc_version=`git describe --always 2>/dev/null`
        [ -z "${desc_version}" ] && desc_version="version unavailable"

        if [ -z "${git_pull_failed}" ]; then
          branch_title=`git branch | grep '\*' | sed 's/* //'`
          printf "branch: %s - " "${branch_title}"

          if [ "${pull_output}" = "Already up-to-date." ] || [ "${desc_version}" = "${prev_desc_version}" ]
          then
            echo "already up-to-date (${desc_version})"
          else
            echo "========================================" >>"${BUILD_FARM}"/pull-results-last/.tempfile
            echo "(->) ${desc_version}" >>"${BUILD_FARM}"/pull-results-last/.tempfile
            mv "${BUILD_FARM}"/pull-results-last/.tempfile "${BUILD_FARM}"/pull-results-last/${1}
            echo "done: ${prev_desc_version} --> ${desc_version}"
          fi
        else
          stderr_of_pull=`git pull 2>&1 | head -1 | cut -d':' -f2- | cut -d' ' -f2-`
          if [ "${stderr_of_pull}" != "up-to-date." ]; then
            printf "\033[1;47;1m\033[1mERROR\033[0m: %s\n" "${stderr_of_pull}"
          else
            branch_title=`git branch | grep '\*' | sed 's/* //'`
            printf "branch: %s - " "${branch_title}"
            echo "already up-to-date! (${desc_version})"
          fi
        fi
        rm -f "${BUILD_FARM}"/pull-results-last/.tempfile
      else
        echo ".git not found"
      fi
    else
      echo "disabled (no such directory)"
    fi
  fi
}

hg_pull_it()
# requires 1 parameter: name of directory with .hg
{
  if [ ! $# -eq 1 ]
  then
    echo "*INTERNAL ERROR*"
  else
    printf "hg pull %s... " "${1}"

    if [ -d ./${1} ]
    then
      cd ./${1}
      if [ -d .hg ]
      then
        echo "${current_date}" >"${BUILD_FARM}"/pull-results-last/.tempfile
        prev_desc_version=`hg parent --template '{latesttag}-{latesttagdistance}-{node|short}\n'`
        [ -z "${prev_desc_version}" ] && prev_desc_version="version unavailable"
        echo "(<-) ${prev_desc_version}" >>"${BUILD_FARM}"/pull-results-last/.tempfile
        echo "========================================" >>"${BUILD_FARM}"/pull-results-last/.tempfile

        hg pull >>"${BUILD_FARM}"/pull-results-last/.tempfile 2>/dev/null
        pull_output=`head "${BUILD_FARM}"/pull-results-last/.tempfile | sed '1,3d'`
        ##desc_version=`hg summary 2>/dev/null | head -1 | awk '{print $2}'`
        desc_version=`hg parent --template '{latesttag}-{latesttagdistance}-{node|short}\n'`
        [ -z "${desc_version}" ] && desc_version="version unavailable"

        if [ "${pull_output}" = "no changes found" ] || [ "${desc_version}" = "${prev_desc_version}" ]
        then
          echo "already up-to-date (${desc_version})"
          rm "${BUILD_FARM}"/pull-results-last/.tempfile
        else
          hg update >>"${BUILD_FARM}"/pull-results-last/.tempfile
          echo "========================================" >>"${BUILD_FARM}"/pull-results-last/.tempfile
          echo "(->) ${desc_version}" >>"${BUILD_FARM}"/pull-results-last/.tempfile
          mv "${BUILD_FARM}"/pull-results-last/.tempfile "${BUILD_FARM}"/pull-results-last/${1}
          echo "done: ${desc_version}"
        fi
      else
        echo ".hg not found"
      fi
    else
      echo "disabled (no such directory)"
    fi
  fi
}

# --------------------

pull_all_in_dir()
# requires 1 parameter: name of directory
{
  cd "${1}"
  num_entries=0

  if [ ! $# -eq 1 ]
  then
    echo "*INTERNAL ERROR*"
  else
    for curr_entry in *
    do
      if [ -d ./"${curr_entry}" ]; then
        if [ -z `echo "${curr_entry}" | grep -- "-build"` ] &&
            [ -z `echo "${curr_entry}" | grep -- "-disabled"` ]; then
          if [ ! -z `echo "${curr_entry}" | grep -- "-git"` ]; then
            git_pull_it `basename "${curr_entry}"`
            num_entries=`expr $num_entries '+' 1`
          elif [ ! -z `echo "${curr_entry}" | grep -- "-hg"` ] ||
                [ ! -z `echo "${curr_entry}" | grep -- "-mercurial"` ]; then
            hg_pull_it `basename "${curr_entry}"`
            num_entries=`expr $num_entries '+' 1`
          fi
        fi
      fi
      cd "${1}"
    done
  fi

  if [ $num_entries -eq 0 ]; then
    echo "No repositiries were found to pull." "Try the --firstrun option."
  else
    echo "Done."
  fi
}

# --------------------

if [ -z "${FIRSTRUN_OPT}" ]; then
  pull_all_in_dir "${BUILD_FARM}"

  if [ "${PULL_BIGSYSTEM_OPT}" = "yes" ]; then
    if [ -d "${BUILD_FARM}"/_BIGSYSTEM ]; then
      echo "(Big system's stuff)"
      pull_all_in_dir "${BUILD_FARM}"/_BIGSYSTEM
    fi
  fi
else
  # "first run" mode
  if [ -d "${BUILD_FARM}"/BUILD-SCRIPTS/SRCINFO ]; then
    echo "(First run mode)"
    [ "${PULL_BIGSYSTEM_OPT}" = "yes" ] && echo "--pull-bigsystem option is ignored"

    cd "${BUILD_FARM}"
    srcinfo_gits=`find ./BUILD-SCRIPTS/SRCINFO -type f -print0 | xargs -0 grep git | grep -v '#' | sed -e 's;/srcinfo-;|;' -e 's;|\([^:]*\):;|\1|;'`
    srcinfo_hgs=`find ./BUILD-SCRIPTS/SRCINFO -type f -print0 | xargs -0 grep hg | grep -v '#' | sed -e 's;/srcinfo-;|;' -e 's;|\([^:]*\):;|\1|;'`
    if [ -z "${srcinfo_gits}" ] && [ -z "${srcinfo_hgs}" ]; then
      echo "Source information files do not contain required entries."
    else
      # clone git repositories
      srcinfo_gits=`echo "${srcinfo_gits}" | sed 's/|/ /g' | cut -d' ' -f2- | sed 's/ git / /' | sort`
      echo "${srcinfo_gits}" | while read git_repo_to_clone; do
        pkg_title=`echo "${git_repo_to_clone}" | cut -d' ' -f1`
        pkg_src_location=`echo "${git_repo_to_clone}" | cut -d' ' -f2`
        if [ ! -z "${pkg_title}" ]; then
          printf "\033[1;47;1m\033[1m%s\033[0m - " "${pkg_title}"
          perform_cloning="yes"
          if [ "${pkg_title}" = "gcc_snapshot" ]; then
            echo "gcc_snapshot is huge! if you do really want, use --force-clone to clone it"
            perform_cloning="no"
          fi
          if [ -d ./"${pkg_title}-git" ]; then
            perform_cloning="no"
            if [ "${FORCE_CLONE_OPT}" = "yes" ]; then
              printf "removing existing directory './%s' - " "${pkg_title}-git"
              cd "${BUILD_FARM}" && rm -rf ./"${pkg_title}-git"
              perform_cloning="yes"
            else
              cd ./"${pkg_title}-git"
              if [ -d .git ]; then
                desc_version=`git describe 2>/dev/null | sed 's|/|-|'`
                [ -z "${desc_version}" ] && desc_version=`git describe --tags 2>/dev/null`
                [ -z "${desc_version}" ] && desc_version=`git describe --always 2>/dev/null`
                [ -z "$desc_version}" ] && desc_version="version unavailable"
                cd "${BUILD_FARM}" && printf "already cloned (%s)\n" "${desc_version}"
              else
                printf "removing invalid directory './%s' - " "${pkg_title}-git"
                cd "${BUILD_FARM}" && rm -rf ./"${pkg_title}-git"
                perform_cloning="yes"
              fi
            fi
          fi
          if [ "${perform_cloning}" = "yes" ]; then
            printf "git clone %s... " "${pkg_src_location}"
            cd "${BUILD_FARM}"
            git clone "${pkg_src_location}" ./"${pkg_title}-git" 1>/dev/null 2>&1 ## 1>"${BUILD_FARM}"/pull-results-last/"${pkg_title}-git-clone"
            if [ $? -eq 0 ]; then
              printf "ok"
              curr_wd=`pwd`
              cd ./"${pkg_title}-git"
              desc_version=`git describe 2>/dev/null | sed 's|/|-|'`
              [ -z "${desc_version}" ] && desc_version=`git describe --tags 2>/dev/null`
              [ -z "${desc_version}" ] && desc_version=`git describe --always 2>/dev/null`
              [ -z "$desc_version}" ] && desc_version="version unavailable"
              printf " (%s)\n" "${desc_version}"
              cd "${curr_wd}"
            else
              printf "\033[37;41;1m\033[1mERROR\033[0m\n"
              rmdir ./"${pkg_title}-git" 2>/dev/null
            fi
          fi
        fi
      done

      # tell to clone hg repositories manually
      srcinfo_hgs=`echo "${srcinfo_hgs}" | sed 's/|/ /g' | cut -d' ' -f2- | sed 's/ hg / /' | sort`
      echo "${srcinfo_hgs}" | while read hg_repo_to_clone; do
        pkg_title=`echo "${hg_repo_to_clone}" | cut -d' ' -f1`
        pkg_src_location=`echo "${hg_repo_to_clone}" | cut -d' ' -f2`
        if [ ! -z "${pkg_title}" ]; then
          printf "\033[1;47;1m\033[1m%s\033[0m - " "${pkg_title}"
          if [ -d ./"${pkg_title}-git" ]; then
            printf "removing existing directory './%s' - " "${pkg_title}-hg"
            rm -rf ./"${pkg_title}-hg"
          fi
          printf "mercurial repository '%s'; please clone it manually\n" "${pkg_src_location}"
        fi
      done

      echo "Done (first run)."
    fi
  else
    printf "Source information files cannot be found; '%s' directory is missing.\n" \
           "${BUILD_FARM}"/BUILD-SCRIPTS/SRCINFO
  fi
fi

# --------------------

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