ohrstrom@538: #!/bin/sh ohrstrom@538: ohrstrom@538: # mduigou@845: # Copyright (c) 2009, 2013, 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: if [ "" = "$command" ] ; then ohrstrom@538: echo No command to hg supplied! ohrstrom@538: exit 1 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 () { chegar@615: if [ -d ${tmp} ]; then chegar@615: if [ "`ls ${tmp}/*.pid`" != "" ]; then chegar@615: echo "Waiting for processes ( `cat ${tmp}/*.pid | 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. chegar@615: kill -TERM `cat ${tmp}/*.pid | tr '\n' ' '` 2> /dev/null chegar@615: wait chegar@615: echo Interrupt complete! chegar@615: fi ohrstrom@538: fi ohrstrom@538: rm -f -r ${tmp} ohrstrom@538: exit 1 ohrstrom@538: } ohrstrom@538: ohrstrom@538: nice_exit () { chegar@615: if [ -d ${tmp} ]; then chegar@615: if [ "`ls ${tmp}`" != "" ]; then chegar@615: wait chegar@615: 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 chegar@615: 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 jjg@645: subrepos="corba jaxp jaxws langtools jdk hotspot nashorn" 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'`" mduigou@845: echo hg clone ${pull_newrepo} ${i} chegar@615: path="`dirname ${i}`" chegar@615: if [ "${path}" != "." ] ; then chegar@615: times=0 chegar@615: while [ ! -d "${path}" ] ## nested repo, ensure containing dir exists chegar@615: do chegar@615: times=`expr ${times} '+' 1` chegar@615: if [ `expr ${times} '%' 10` -eq 0 ] ; then chegar@615: echo ${path} still not created, waiting... chegar@615: fi chegar@615: sleep 5 chegar@615: done chegar@615: fi mduigou@845: (PYTHONUNBUFFERED=true hg clone ${pull_newrepo} ${i}; echo "$?" > ${tmp}/${repopidfile}.pid.rc )& ohrstrom@538: else mduigou@845: echo "cd ${i} && hg $*" mduigou@845: cd ${i} && (PYTHONUNBUFFERED=true hg "$@"; echo "$?" > ${tmp}/${repopidfile}.pid.rc )& chegar@615: fi ohrstrom@538: echo $! > ${tmp}/${repopidfile}.pid ohrstrom@538: ) 2>&1 | sed -e "s@^@${reponame}: @") & chegar@615: 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: chegar@615: # Terminate with exit 0 only if all subprocesses were successful chegar@615: ec=0 chegar@615: if [ -d ${tmp} ]; then chegar@615: for rc in ${tmp}/*.pid.rc ; do chegar@615: exit_code=`cat ${rc} | tr -d ' \n\r'` chegar@615: if [ "${exit_code}" != "0" ] ; then chegar@615: echo "WARNING: ${rc} exited abnormally." chegar@615: ec=1 chegar@615: fi chegar@615: done chegar@615: fi chegar@615: exit ${ec}