hgforest.sh 4.3 KB
Newer Older
1 2 3
#!/bin/sh

#
4
# Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#

# Shell script for a fast parallel forest command
27 28
command="$1"
pull_extra_base="$2"
29 30 31 32 33 34 35 36 37 38 39

tmp=/tmp/forest.$$
rm -f -r ${tmp}
mkdir -p ${tmp}

# Remove tmp area on A. B. Normal termination
trap 'rm -f -r ${tmp}' KILL
trap 'rm -f -r ${tmp}' EXIT

# Only look in specific locations for possible forests (avoids long searches)
pull_default=""
40 41 42
repos=""
repos_extra=""
if [ "${command}" = "clone" -o "${command}" = "fclone" ] ; then
43 44 45
  subrepos="corba jaxp jaxws langtools jdk hotspot"
  if [ -f .hg/hgrc ] ; then
    pull_default=`hg paths default`
46 47 48 49
    if [ "${pull_default}" = "" ] ; then
      echo "ERROR: Need initial clone with 'hg paths default' defined"
      exit 1
    fi
50 51
  fi
  if [ "${pull_default}" = "" ] ; then
52
    echo "ERROR: Need initial repository to use this script"
53 54 55 56 57 58 59
    exit 1
  fi
  for i in ${subrepos} ; do
    if [ ! -f ${i}/.hg/hgrc ] ; then
      repos="${repos} ${i}"
    fi
  done
60 61
  if [ "${pull_extra_base}" != "" ] ; then
    subrepos_extra="jdk/src/closed jdk/make/closed jdk/test/closed hotspot/src/closed hotspot/test/closed deploy install sponsors pubs"
O
ohair 已提交
62 63
    pull_default_tail=`echo ${pull_default} | sed -e 's@^.*://[^/]*/\(.*\)@\1@'`
    pull_extra="${pull_extra_base}/${pull_default_tail}"
64 65 66 67 68 69
    for i in ${subrepos_extra} ; do
      if [ ! -f ${i}/.hg/hgrc ] ; then
        repos_extra="${repos_extra} ${i}"
      fi
    done
  fi
70
  at_a_time=2
71 72 73 74 75
  # Any repos to deal with?
  if [ "${repos}" = "" -a "${repos_extra}" = "" ] ; then
    echo "No repositories to clone."
    exit
  fi
76 77 78 79 80 81 82
else
  hgdirs=`ls -d ./.hg ./*/.hg ./*/*/.hg ./*/*/*/.hg ./*/*/*/*/.hg 2>/dev/null`
  # Derive repository names from the .hg directory locations
  for i in ${hgdirs} ; do
    repos="${repos} `echo ${i} | sed -e 's@/.hg$@@'`"
  done
  at_a_time=8
83 84 85 86 87
  # Any repos to deal with?
  if [ "${repos}" = "" ] ; then
    echo "No repositories to process."
    exit
  fi
88 89
fi

90 91
# Echo out what repositories we will clone
echo "# Repos: ${repos} ${repos_extra}"
92 93 94 95 96 97 98 99

# Run the supplied command on all repos in parallel, save output until end
n=0
for i in ${repos} ; do
  echo "Starting on ${i}"
  n=`expr ${n} '+' 1`
  (
    (
100
      if [ "${command}" = "clone" -o "${command}" = "fclone" ] ; then
101 102
        pull_newrepo="`echo ${pull_default}/${i} | sed -e 's@\([^:]/\)//*@\1@g'`"
        cline="hg clone ${pull_newrepo} ${i}"
103 104 105 106 107 108 109 110 111 112 113 114 115
        echo "# ${cline}"
        ( eval "${cline}" )
      else
        cline="hg $*"
        echo "# cd ${i} && ${cline}"
        ( cd ${i} && eval "${cline}" )
      fi
      echo "# exit code $?"
    ) > ${tmp}/repo.${n} 2>&1 ; cat ${tmp}/repo.${n} ) &
  if [ `expr ${n} '%' ${at_a_time}` -eq 0 ] ; then
    sleep 5
  fi
done
116 117 118
# Wait for all hg commands to complete
wait

119 120 121 122 123 124
if [ "${repos_extra}" != "" ] ; then
  for i in ${repos_extra} ; do
    echo "Starting on ${i}"
    n=`expr ${n} '+' 1`
    (
      (
125 126
          pull_newextrarepo="`echo ${pull_extra}/${i} | sed -e 's@\([^:]/\)//*@\1@g'`"
          cline="hg clone ${pull_newextrarepo} ${i}"
127 128 129 130 131 132 133 134
          echo "# ${cline}"
          ( eval "${cline}" )
        echo "# exit code $?"
      ) > ${tmp}/repo.${n} 2>&1 ; cat ${tmp}/repo.${n} ) &
    if [ `expr ${n} '%' ${at_a_time}` -eq 0 ] ; then
      sleep 5
    fi
  done
135 136
  # Wait for all hg commands to complete
  wait
137
fi
138 139 140 141 142 143 144

# Cleanup
rm -f -r ${tmp}

# Terminate with exit 0 all the time (hard to know when to say "failed")
exit 0