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