ohrstrom@538: #!/bin/sh ohrstrom@538: ohrstrom@538: # ohrstrom@538: # Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. ohrstrom@538: # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohrstrom@538: # ohrstrom@538: # This code is free software; you can redistribute it and/or modify it ohrstrom@538: # under the terms of the GNU General Public License version 2 only, as ohrstrom@538: # published by the Free Software Foundation. ohrstrom@538: # ohrstrom@538: # This code is distributed in the hope that it will be useful, but WITHOUT ohrstrom@538: # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohrstrom@538: # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohrstrom@538: # version 2 for more details (a copy is included in the LICENSE file that ohrstrom@538: # accompanied this code). ohrstrom@538: # ohrstrom@538: # You should have received a copy of the GNU General Public License version ohrstrom@538: # 2 along with this work; if not, write to the Free Software Foundation, ohrstrom@538: # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohrstrom@538: # ohrstrom@538: # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohrstrom@538: # or visit www.oracle.com if you need additional information or have any ohrstrom@538: # questions. ohrstrom@538: # ohrstrom@538: ohrstrom@538: # Shell script for a fast parallel forest command ohrstrom@538: command="$1" ohrstrom@538: pull_extra_base="$2" ohrstrom@538: ohrstrom@538: # Python always buffers stdout significantly, thus we will not see any output from hg clone jdk, ohrstrom@538: # until a lot of time has passed! By passing -u to python, we get incremental updates ohrstrom@538: # on stdout. Much nicer. ohrstrom@538: whichhg="`which hg`" ohrstrom@538: ohrstrom@538: if [ "${whichhg}" = "" ] ; then ohrstrom@538: echo Cannot find hg! ohrstrom@538: exit 1 ohrstrom@538: fi ohrstrom@538: ohrstrom@538: if [ "" = "$command" ] ; then ohrstrom@538: echo No command to hg supplied! ohrstrom@538: exit 1 ohrstrom@538: fi ohrstrom@538: ohrstrom@538: has_hash_bang="`head -n 1 "${whichhg}" | cut -b 1-2`" ohrstrom@538: python="" ohrstrom@538: bpython="" ohrstrom@538: ohrstrom@538: if [ "#!" = "$has_hash_bang" ] ; then ohrstrom@538: python="`head -n 1 ${whichhg} | cut -b 3-`" ohrstrom@538: bpython="`basename "$python"`" ohrstrom@538: fi ohrstrom@538: ohrstrom@538: if [ "python" = "$bpython" -a -x "$python" ] ; then ohrstrom@538: hg="${python} -u ${whichhg}" ohrstrom@538: else ohrstrom@538: echo Cannot find python from hg launcher. Running plain hg, which probably has buffered stdout. ohrstrom@538: hg="hg" ohrstrom@538: fi ohrstrom@538: ohrstrom@538: # Clean out the temporary directory that stores the pid files. ohrstrom@538: tmp=/tmp/forest.$$ ohrstrom@538: rm -f -r ${tmp} ohrstrom@538: mkdir -p ${tmp} ohrstrom@538: ohrstrom@538: safe_interrupt () { ohrstrom@538: if [ -d ${tmp} ]; then ohrstrom@538: if [ "`ls ${tmp}`" != "" ]; then ohrstrom@538: echo "Waiting for processes ( `cat ${tmp}/* | tr '\n' ' '`) to terminate nicely!" ohrstrom@538: sleep 1 ohrstrom@538: # Pipe stderr to dev/null to silence kill, that complains when trying to kill ohrstrom@538: # a subprocess that has already exited. ohrstrom@538: kill -TERM `cat ${tmp}/* | tr '\n' ' '` 2> /dev/null ohrstrom@538: wait ohrstrom@538: echo Interrupt complete! ohrstrom@538: fi ohrstrom@538: fi ohrstrom@538: rm -f -r ${tmp} ohrstrom@538: exit 1 ohrstrom@538: } ohrstrom@538: ohrstrom@538: nice_exit () { ohrstrom@538: if [ -d ${tmp} ]; then ohrstrom@538: if [ "`ls ${tmp}`" != "" ]; then ohrstrom@538: wait ohrstrom@538: fi ohrstrom@538: fi ohrstrom@538: rm -f -r ${tmp} ohrstrom@538: } ohrstrom@538: ohrstrom@538: trap 'safe_interrupt' INT QUIT ohrstrom@538: trap 'nice_exit' EXIT ohrstrom@538: ohrstrom@538: # Only look in specific locations for possible forests (avoids long searches) ohrstrom@538: pull_default="" ohrstrom@538: repos="" ohrstrom@538: repos_extra="" ohrstrom@538: if [ "${command}" = "clone" -o "${command}" = "fclone" ] ; then ohrstrom@538: subrepos="corba jaxp jaxws langtools jdk hotspot" ohrstrom@538: if [ -f .hg/hgrc ] ; then ohrstrom@538: pull_default=`hg paths default` ohrstrom@538: if [ "${pull_default}" = "" ] ; then ohrstrom@538: echo "ERROR: Need initial clone with 'hg paths default' defined" ohrstrom@538: exit 1 ohrstrom@538: fi ohrstrom@538: fi ohrstrom@538: if [ "${pull_default}" = "" ] ; then ohrstrom@538: echo "ERROR: Need initial repository to use this script" ohrstrom@538: exit 1 ohrstrom@538: fi ohrstrom@538: for i in ${subrepos} ; do ohrstrom@538: if [ ! -f ${i}/.hg/hgrc ] ; then ohrstrom@538: repos="${repos} ${i}" ohrstrom@538: fi ohrstrom@538: done ohrstrom@538: if [ "${pull_extra_base}" != "" ] ; then ohrstrom@538: subrepos_extra="jdk/src/closed jdk/make/closed jdk/test/closed hotspot/make/closed hotspot/src/closed hotspot/test/closed deploy install sponsors pubs" ohrstrom@538: pull_default_tail=`echo ${pull_default} | sed -e 's@^.*://[^/]*/\(.*\)@\1@'` ohrstrom@538: pull_extra="${pull_extra_base}/${pull_default_tail}" ohrstrom@538: for i in ${subrepos_extra} ; do ohrstrom@538: if [ ! -f ${i}/.hg/hgrc ] ; then ohrstrom@538: repos_extra="${repos_extra} ${i}" ohrstrom@538: fi ohrstrom@538: done ohrstrom@538: fi ohrstrom@538: at_a_time=2 ohrstrom@538: # Any repos to deal with? ohrstrom@538: if [ "${repos}" = "" -a "${repos_extra}" = "" ] ; then ohrstrom@538: exit ohrstrom@538: fi ohrstrom@538: else ohrstrom@538: hgdirs=`ls -d ./.hg ./*/.hg ./*/*/.hg ./*/*/*/.hg ./*/*/*/*/.hg 2>/dev/null` ohrstrom@538: # Derive repository names from the .hg directory locations ohrstrom@538: for i in ${hgdirs} ; do ohrstrom@538: repos="${repos} `echo ${i} | sed -e 's@/.hg$@@'`" ohrstrom@538: done ohrstrom@538: for i in ${repos} ; do ohrstrom@538: if [ -h ${i}/.hg/store/lock -o -f ${i}/.hg/store/lock ] ; then ohrstrom@538: locked="${i} ${locked}" ohrstrom@538: fi ohrstrom@538: done ohrstrom@538: at_a_time=8 ohrstrom@538: # Any repos to deal with? ohrstrom@538: if [ "${repos}" = "" ] ; then ohrstrom@538: echo "No repositories to process." ohrstrom@538: exit ohrstrom@538: fi ohrstrom@538: if [ "${locked}" != "" ] ; then ohrstrom@538: echo "These repositories are locked: ${locked}" ohrstrom@538: exit ohrstrom@538: fi ohrstrom@538: fi ohrstrom@538: ohrstrom@538: # Echo out what repositories we do a command on. ohrstrom@538: echo "# Repositories: ${repos} ${repos_extra}" ohrstrom@538: echo ohrstrom@538: ohrstrom@538: # Run the supplied command on all repos in parallel. ohrstrom@538: n=0 ohrstrom@538: for i in ${repos} ${repos_extra} ; do ohrstrom@538: n=`expr ${n} '+' 1` ohrstrom@538: repopidfile=`echo ${i} | sed -e 's@./@@' -e 's@/@_@g'` ohrstrom@538: reponame=`echo ${i} | sed -e :a -e 's/^.\{1,20\}$/ &/;ta'` ohrstrom@538: pull_base="${pull_default}" ohrstrom@538: for j in $repos_extra ; do ohrstrom@538: if [ "$i" = "$j" ] ; then ohrstrom@538: pull_base="${pull_extra}" ohrstrom@538: fi ohrstrom@538: done ohrstrom@538: ( ohrstrom@538: ( ohrstrom@538: if [ "${command}" = "clone" -o "${command}" = "fclone" ] ; then ohrstrom@538: pull_newrepo="`echo ${pull_base}/${i} | sed -e 's@\([^:]/\)//*@\1@g'`" ohrstrom@538: echo ${hg} clone ${pull_newrepo} ${i} ohrstrom@538: ${hg} clone ${pull_newrepo} ${i} & ohrstrom@538: else ohrstrom@538: echo "cd ${i} && ${hg} $*" ohrstrom@538: cd ${i} && ${hg} "$@" & ohrstrom@538: fi ohrstrom@538: echo $! > ${tmp}/${repopidfile}.pid ohrstrom@538: ) 2>&1 | sed -e "s@^@${reponame}: @") & ohrstrom@538: ohrstrom@538: if [ `expr ${n} '%' ${at_a_time}` -eq 0 ] ; then ohrstrom@538: sleep 2 ohrstrom@538: echo Waiting 5 secs before spawning next background command. ohrstrom@538: sleep 3 ohrstrom@538: fi ohrstrom@538: done ohrstrom@538: # Wait for all hg commands to complete ohrstrom@538: wait ohrstrom@538: ohrstrom@538: # Terminate with exit 0 all the time (hard to know when to say "failed") ohrstrom@538: exit 0 ohrstrom@538: