common/bin/hgforest.sh

Fri, 11 Apr 2014 09:35:03 +0100

author
chegar
date
Fri, 11 Apr 2014 09:35:03 +0100
changeset 1143
e3ae43560332
parent 1142
2bb0f1489885
child 1144
e16a393ee5e3
permissions
-rw-r--r--

8039990: Add sequential operation support to hgforest
Reviewed-by: mduigou

mduigou@1142 1 #!/bin/sh
ohrstrom@538 2
ohrstrom@538 3 #
mduigou@845 4 # Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
ohrstrom@538 5 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohrstrom@538 6 #
ohrstrom@538 7 # This code is free software; you can redistribute it and/or modify it
ohrstrom@538 8 # under the terms of the GNU General Public License version 2 only, as
ohrstrom@538 9 # published by the Free Software Foundation.
ohrstrom@538 10 #
ohrstrom@538 11 # This code is distributed in the hope that it will be useful, but WITHOUT
ohrstrom@538 12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohrstrom@538 13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohrstrom@538 14 # version 2 for more details (a copy is included in the LICENSE file that
ohrstrom@538 15 # accompanied this code).
ohrstrom@538 16 #
ohrstrom@538 17 # You should have received a copy of the GNU General Public License version
ohrstrom@538 18 # 2 along with this work; if not, write to the Free Software Foundation,
ohrstrom@538 19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohrstrom@538 20 #
ohrstrom@538 21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohrstrom@538 22 # or visit www.oracle.com if you need additional information or have any
ohrstrom@538 23 # questions.
ohrstrom@538 24 #
ohrstrom@538 25
ohrstrom@538 26 # Shell script for a fast parallel forest command
ohrstrom@538 27
mduigou@1141 28 global_opts=""
mduigou@1141 29 status_output="/dev/stdout"
mduigou@1141 30 qflag="false"
mduigou@1141 31 vflag="false"
chegar@1143 32 sflag="false"
mduigou@1141 33 while [ $# -gt 0 ]
mduigou@1141 34 do
mduigou@1141 35 case $1 in
mduigou@1141 36 -q | --quiet )
mduigou@1141 37 qflag="true"
mduigou@1141 38 global_opts="${global_opts} -q"
mduigou@1141 39 status_output="/dev/null"
mduigou@1141 40 ;;
mduigou@1141 41
mduigou@1141 42 -v | --verbose )
mduigou@1141 43 vflag="true"
mduigou@1141 44 global_opts="${global_opts} -v"
mduigou@1141 45 ;;
mduigou@1141 46
chegar@1143 47 -s | --sequential )
chegar@1143 48 sflag="true"
chegar@1143 49 ;;
chegar@1143 50
mduigou@1141 51 '--' ) # no more options
mduigou@1141 52 shift; break
mduigou@1141 53 ;;
mduigou@1141 54
mduigou@1141 55 -*) # bad option
mduigou@1141 56 usage
mduigou@1141 57 ;;
mduigou@1141 58
mduigou@1141 59 * ) # non option
mduigou@1141 60 break
mduigou@1141 61 ;;
mduigou@1141 62 esac
mduigou@1141 63 shift
mduigou@1141 64 done
mduigou@1141 65
mduigou@1141 66
mduigou@1141 67 command="$1"; shift
mduigou@1142 68 command_args="$@"
mduigou@1141 69
mduigou@1141 70 usage() {
chegar@1143 71 echo "usage: $0 [-q|--quiet] [-v|--verbose] [-s|--sequential] [--] <command> [commands...]" > ${status_output}
mduigou@1141 72 exit 1
mduigou@1141 73 }
mduigou@1141 74
mduigou@1141 75
mduigou@1141 76 if [ "x" = "x$command" ] ; then
mduigou@1141 77 echo "ERROR: No command to hg supplied!"
mduigou@1141 78 usage
ohrstrom@538 79 fi
ohrstrom@538 80
ohrstrom@538 81 # Clean out the temporary directory that stores the pid files.
ohrstrom@538 82 tmp=/tmp/forest.$$
ohrstrom@538 83 rm -f -r ${tmp}
ohrstrom@538 84 mkdir -p ${tmp}
ohrstrom@538 85
ohrstrom@538 86 safe_interrupt () {
chegar@615 87 if [ -d ${tmp} ]; then
chegar@615 88 if [ "`ls ${tmp}/*.pid`" != "" ]; then
mduigou@1141 89 echo "Waiting for processes ( `cat ${tmp}/*.pid | tr '\n' ' '`) to terminate nicely!" > ${status_output}
ohrstrom@538 90 sleep 1
ohrstrom@538 91 # Pipe stderr to dev/null to silence kill, that complains when trying to kill
ohrstrom@538 92 # a subprocess that has already exited.
chegar@615 93 kill -TERM `cat ${tmp}/*.pid | tr '\n' ' '` 2> /dev/null
chegar@615 94 wait
mduigou@1141 95 echo "Interrupt complete!" > ${status_output}
chegar@615 96 fi
mduigou@1141 97 rm -f -r ${tmp}
ohrstrom@538 98 fi
mduigou@1141 99 exit 130
ohrstrom@538 100 }
ohrstrom@538 101
ohrstrom@538 102 nice_exit () {
chegar@615 103 if [ -d ${tmp} ]; then
chegar@615 104 if [ "`ls ${tmp}`" != "" ]; then
chegar@615 105 wait
chegar@615 106 fi
mduigou@1141 107 rm -f -r ${tmp}
ohrstrom@538 108 fi
ohrstrom@538 109 }
ohrstrom@538 110
ohrstrom@538 111 trap 'safe_interrupt' INT QUIT
ohrstrom@538 112 trap 'nice_exit' EXIT
chegar@615 113
mduigou@1141 114 subrepos="corba jaxp jaxws langtools jdk hotspot nashorn"
mduigou@1141 115 subrepos_extra="jdk/src/closed jdk/make/closed jdk/test/closed hotspot/make/closed hotspot/src/closed hotspot/test/closed deploy install sponsors pubs"
mduigou@1141 116
ohrstrom@538 117 # Only look in specific locations for possible forests (avoids long searches)
ohrstrom@538 118 pull_default=""
ohrstrom@538 119 repos=""
ohrstrom@538 120 repos_extra=""
mduigou@1142 121 if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then
mduigou@1142 122 if [ ! -f .hg/hgrc ] ; then
mduigou@1141 123 echo "ERROR: Need initial repository to use this script" > ${status_output}
ohrstrom@538 124 exit 1
ohrstrom@538 125 fi
mduigou@1142 126
mduigou@1142 127 pull_default=`hg paths default`
mduigou@1142 128 if [ "${pull_default}" = "" ] ; then
mduigou@1142 129 echo "ERROR: Need initial clone with 'hg paths default' defined" > ${status_output}
mduigou@1142 130 exit 1
mduigou@1142 131 fi
mduigou@1142 132
ohrstrom@538 133 for i in ${subrepos} ; do
ohrstrom@538 134 if [ ! -f ${i}/.hg/hgrc ] ; then
ohrstrom@538 135 repos="${repos} ${i}"
ohrstrom@538 136 fi
ohrstrom@538 137 done
mduigou@1142 138 if [ "${command_args}" != "" ] ; then
ohrstrom@538 139 pull_default_tail=`echo ${pull_default} | sed -e 's@^.*://[^/]*/\(.*\)@\1@'`
mduigou@1142 140 if [ "x${pull_default}" = "x${pull_default_tail}" ] ; then
mduigou@1142 141 echo "ERROR: Need initial clone from non-local source" > ${status_output}
mduigou@1142 142 exit 1
mduigou@1142 143 fi
mduigou@1142 144 pull_extra="${command_args}/${pull_default_tail}"
ohrstrom@538 145 for i in ${subrepos_extra} ; do
ohrstrom@538 146 if [ ! -f ${i}/.hg/hgrc ] ; then
ohrstrom@538 147 repos_extra="${repos_extra} ${i}"
ohrstrom@538 148 fi
ohrstrom@538 149 done
ohrstrom@538 150 fi
ohrstrom@538 151 at_a_time=2
ohrstrom@538 152 # Any repos to deal with?
ohrstrom@538 153 if [ "${repos}" = "" -a "${repos_extra}" = "" ] ; then
mduigou@1141 154 echo "No repositories to process." > ${status_output}
ohrstrom@538 155 exit
ohrstrom@538 156 fi
ohrstrom@538 157 else
mduigou@1141 158 for i in . ${subrepos} ${subrepos_extra} ; do
mduigou@1141 159 if [ -d ${i}/.hg ] ; then
mduigou@1141 160 repos="${repos} ${i}"
mduigou@1141 161 fi
ohrstrom@538 162 done
mduigou@1141 163
mduigou@1141 164 # Any repos to deal with?
mduigou@1141 165 if [ "${repos}" = "" ] ; then
mduigou@1141 166 echo "No repositories to process." > ${status_output}
mduigou@1141 167 exit
mduigou@1141 168 fi
mduigou@1141 169
mduigou@1141 170 # any of the repos locked?
ohrstrom@538 171 for i in ${repos} ; do
ohrstrom@538 172 if [ -h ${i}/.hg/store/lock -o -f ${i}/.hg/store/lock ] ; then
ohrstrom@538 173 locked="${i} ${locked}"
ohrstrom@538 174 fi
ohrstrom@538 175 done
mduigou@1141 176 if [ "${locked}" != "" ] ; then
mduigou@1141 177 echo "ERROR: These repositories are locked: ${locked}" > ${status_output}
mduigou@1141 178 exit 1
mduigou@1141 179 fi
ohrstrom@538 180 at_a_time=8
ohrstrom@538 181 fi
ohrstrom@538 182
ohrstrom@538 183 # Echo out what repositories we do a command on.
mduigou@1141 184 echo "# Repositories: ${repos} ${repos_extra}" > ${status_output}
ohrstrom@538 185
mduigou@1141 186 if [ "${command}" = "serve" ] ; then
mduigou@1141 187 # "serve" is run for all the repos.
mduigou@1141 188 (
mduigou@1141 189 (
mduigou@1141 190 (
mduigou@1141 191 echo "[web]"
mduigou@1141 192 echo "description = $(basename $(pwd))"
mduigou@1141 193 echo "allow_push = *"
mduigou@1141 194 echo "push_ssl = False"
mduigou@1141 195
mduigou@1141 196 echo "[paths]"
mduigou@1141 197 for i in ${repos} ${repos_extra} ; do
mduigou@1141 198 if [ "${i}" != "." ] ; then
mduigou@1141 199 echo "/$(basename $(pwd))/${i} = ${i}"
mduigou@1141 200 else
mduigou@1141 201 echo "/$(basename $(pwd)) = $(pwd)"
mduigou@1141 202 fi
mduigou@1141 203 done
mduigou@1141 204 ) > ${tmp}/serve.web-conf
mduigou@1141 205
mduigou@1141 206 echo "serving root repo $(basename $(pwd))"
mduigou@1141 207
mduigou@1141 208 (PYTHONUNBUFFERED=true hg${global_opts} serve -A ${status_output} -E ${status_output} --pid-file ${tmp}/serve.pid --web-conf ${tmp}/serve.web-conf; echo "$?" > ${tmp}/serve.pid.rc ) 2>&1 &
mduigou@1141 209 ) 2>&1 | sed -e "s@^@serve: @" > ${status_output}
mduigou@1141 210 ) &
mduigou@1141 211 else
mduigou@1141 212 # Run the supplied command on all repos in parallel.
mduigou@1141 213 n=0
mduigou@1141 214 for i in ${repos} ${repos_extra} ; do
mduigou@1141 215 n=`expr ${n} '+' 1`
mduigou@1141 216 repopidfile=`echo ${i} | sed -e 's@./@@' -e 's@/@_@g'`
mduigou@1141 217 reponame=`echo ${i} | sed -e :a -e 's/^.\{1,20\}$/ &/;ta'`
mduigou@1141 218 pull_base="${pull_default}"
mduigou@1141 219 for j in $repos_extra ; do
ohrstrom@538 220 if [ "$i" = "$j" ] ; then
ohrstrom@538 221 pull_base="${pull_extra}"
ohrstrom@538 222 fi
mduigou@1141 223 done
mduigou@1141 224 (
mduigou@1141 225 (
mduigou@1142 226 if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then
mduigou@1141 227 pull_newrepo="`echo ${pull_base}/${i} | sed -e 's@\([^:]/\)//*@\1@g'`"
mduigou@1141 228 path="`dirname ${i}`"
mduigou@1141 229 if [ "${path}" != "." ] ; then
mduigou@1141 230 times=0
mduigou@1141 231 while [ ! -d "${path}" ] ## nested repo, ensure containing dir exists
mduigou@1141 232 do
mduigou@1141 233 times=`expr ${times} '+' 1`
mduigou@1141 234 if [ `expr ${times} '%' 10` -eq 0 ] ; then
mduigou@1141 235 echo "${path} still not created, waiting..." > ${status_output}
mduigou@1141 236 fi
mduigou@1141 237 sleep 5
mduigou@1141 238 done
mduigou@1141 239 fi
mduigou@1142 240 echo "hg clone ${pull_newrepo} ${i}" > ${status_output}
mduigou@1141 241 (PYTHONUNBUFFERED=true hg${global_opts} clone ${pull_newrepo} ${i}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &
mduigou@1141 242 else
mduigou@1142 243 echo "cd ${i} && hg${global_opts} ${command} ${command_args}" > ${status_output}
mduigou@1142 244 cd ${i} && (PYTHONUNBUFFERED=true hg${global_opts} ${command} ${command_args}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &
mduigou@1141 245 fi
mduigou@1141 246
mduigou@1141 247 echo $! > ${tmp}/${repopidfile}.pid
mduigou@1141 248 ) 2>&1 | sed -e "s@^@${reponame}: @" > ${status_output}
mduigou@1141 249 ) &
mduigou@1141 250
chegar@1143 251 if [ `expr ${n} '%' ${at_a_time}` -eq 0 -a "${sflag}" = "false" ] ; then
mduigou@1141 252 sleep 2
mduigou@1141 253 echo "Waiting 5 secs before spawning next background command." > ${status_output}
mduigou@1141 254 sleep 3
mduigou@1141 255 fi
chegar@1143 256
chegar@1143 257 if [ "${sflag}" = "true" ] ; then
chegar@1143 258 wait
chegar@1143 259 fi
ohrstrom@538 260 done
mduigou@1141 261 fi
chegar@615 262
ohrstrom@538 263 # Wait for all hg commands to complete
ohrstrom@538 264 wait
ohrstrom@538 265
chegar@615 266 # Terminate with exit 0 only if all subprocesses were successful
chegar@615 267 ec=0
chegar@615 268 if [ -d ${tmp} ]; then
chegar@615 269 for rc in ${tmp}/*.pid.rc ; do
chegar@615 270 exit_code=`cat ${rc} | tr -d ' \n\r'`
chegar@615 271 if [ "${exit_code}" != "0" ] ; then
mduigou@1141 272 repo="`echo ${rc} | sed -e s@^${tmp}@@ -e 's@/*\([^/]*\)\.pid\.rc$@\1@' -e 's@_@/@g'`"
mduigou@1141 273 echo "WARNING: ${repo} exited abnormally." > ${status_output}
chegar@615 274 ec=1
chegar@615 275 fi
chegar@615 276 done
chegar@615 277 fi
chegar@615 278 exit ${ec}

mercurial