mduigou@1142: #!/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: mduigou@1141: global_opts="" mduigou@1141: status_output="/dev/stdout" mduigou@1141: qflag="false" mduigou@1141: vflag="false" chegar@1143: sflag="false" mduigou@1141: while [ $# -gt 0 ] mduigou@1141: do mduigou@1141: case $1 in mduigou@1141: -q | --quiet ) mduigou@1141: qflag="true" mduigou@1141: global_opts="${global_opts} -q" mduigou@1141: status_output="/dev/null" mduigou@1141: ;; mduigou@1141: mduigou@1141: -v | --verbose ) mduigou@1141: vflag="true" mduigou@1141: global_opts="${global_opts} -v" mduigou@1141: ;; mduigou@1141: chegar@1143: -s | --sequential ) chegar@1143: sflag="true" chegar@1143: ;; chegar@1143: mduigou@1141: '--' ) # no more options mduigou@1141: shift; break mduigou@1141: ;; mduigou@1141: mduigou@1141: -*) # bad option mduigou@1141: usage mduigou@1141: ;; mduigou@1141: mduigou@1141: * ) # non option mduigou@1141: break mduigou@1141: ;; mduigou@1141: esac mduigou@1141: shift mduigou@1141: done mduigou@1141: mduigou@1141: mduigou@1141: command="$1"; shift mduigou@1142: command_args="$@" mduigou@1141: mduigou@1141: usage() { chegar@1143: echo "usage: $0 [-q|--quiet] [-v|--verbose] [-s|--sequential] [--] [commands...]" > ${status_output} mduigou@1141: exit 1 mduigou@1141: } mduigou@1141: mduigou@1141: if [ "x" = "x$command" ] ; then mduigou@1141: echo "ERROR: No command to hg supplied!" mduigou@1141: usage ohrstrom@538: fi ohrstrom@538: mduigou@1144: # Check if we can use fifos for monitoring sub-process completion. mduigou@1144: on_windows=`uname -s | egrep -ic -e 'cygwin|msys'` mduigou@1144: if [ ${on_windows} = "1" ]; then mduigou@1144: # cygwin has (2014-04-18) broken (single writer only) FIFOs mduigou@1144: # msys has (2014-04-18) no FIFOs. mduigou@1144: have_fifos="false" mduigou@1144: else mduigou@1144: have_fifos="true" mduigou@1144: fi mduigou@1144: 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 mduigou@1141: echo "Waiting for processes ( `cat ${tmp}/*.pid | tr '\n' ' '`) to terminate nicely!" > ${status_output} 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 mduigou@1141: echo "Interrupt complete!" > ${status_output} chegar@615: fi mduigou@1141: rm -f -r ${tmp} ohrstrom@538: fi mduigou@1141: exit 130 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 mduigou@1141: rm -f -r ${tmp} ohrstrom@538: fi ohrstrom@538: } ohrstrom@538: ohrstrom@538: trap 'safe_interrupt' INT QUIT ohrstrom@538: trap 'nice_exit' EXIT chegar@615: mduigou@1141: subrepos="corba jaxp jaxws langtools jdk hotspot nashorn" mduigou@1141: 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: ohrstrom@538: # Only look in specific locations for possible forests (avoids long searches) ohrstrom@538: pull_default="" ohrstrom@538: repos="" ohrstrom@538: repos_extra="" mduigou@1142: if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then mduigou@1142: if [ ! -f .hg/hgrc ] ; then mduigou@1141: echo "ERROR: Need initial repository to use this script" > ${status_output} ohrstrom@538: exit 1 ohrstrom@538: fi mduigou@1142: mduigou@1142: pull_default=`hg paths default` mduigou@1142: if [ "${pull_default}" = "" ] ; then mduigou@1142: echo "ERROR: Need initial clone with 'hg paths default' defined" > ${status_output} mduigou@1142: exit 1 mduigou@1142: fi mduigou@1142: 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 mduigou@1142: if [ "${command_args}" != "" ] ; then ohrstrom@538: pull_default_tail=`echo ${pull_default} | sed -e 's@^.*://[^/]*/\(.*\)@\1@'` mduigou@1142: if [ "x${pull_default}" = "x${pull_default_tail}" ] ; then mduigou@1142: echo "ERROR: Need initial clone from non-local source" > ${status_output} mduigou@1142: exit 1 mduigou@1142: fi mduigou@1142: pull_extra="${command_args}/${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 mduigou@1141: echo "No repositories to process." > ${status_output} ohrstrom@538: exit ohrstrom@538: fi ohrstrom@538: else mduigou@1141: for i in . ${subrepos} ${subrepos_extra} ; do mduigou@1141: if [ -d ${i}/.hg ] ; then mduigou@1141: repos="${repos} ${i}" mduigou@1141: fi ohrstrom@538: done mduigou@1141: mduigou@1141: # Any repos to deal with? mduigou@1141: if [ "${repos}" = "" ] ; then mduigou@1141: echo "No repositories to process." > ${status_output} mduigou@1141: exit mduigou@1141: fi mduigou@1141: mduigou@1141: # any of the repos locked? 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 mduigou@1141: if [ "${locked}" != "" ] ; then mduigou@1141: echo "ERROR: These repositories are locked: ${locked}" > ${status_output} mduigou@1141: exit 1 mduigou@1141: fi ohrstrom@538: at_a_time=8 ohrstrom@538: fi ohrstrom@538: ohrstrom@538: # Echo out what repositories we do a command on. mduigou@1141: echo "# Repositories: ${repos} ${repos_extra}" > ${status_output} ohrstrom@538: mduigou@1141: if [ "${command}" = "serve" ] ; then mduigou@1141: # "serve" is run for all the repos. mduigou@1141: ( mduigou@1141: ( mduigou@1141: ( mduigou@1141: echo "[web]" mduigou@1141: echo "description = $(basename $(pwd))" mduigou@1141: echo "allow_push = *" mduigou@1141: echo "push_ssl = False" mduigou@1141: mduigou@1141: echo "[paths]" mduigou@1141: for i in ${repos} ${repos_extra} ; do mduigou@1141: if [ "${i}" != "." ] ; then mduigou@1141: echo "/$(basename $(pwd))/${i} = ${i}" mduigou@1141: else mduigou@1141: echo "/$(basename $(pwd)) = $(pwd)" mduigou@1141: fi mduigou@1141: done mduigou@1141: ) > ${tmp}/serve.web-conf mduigou@1141: mduigou@1141: echo "serving root repo $(basename $(pwd))" mduigou@1141: mduigou@1141: (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: ) 2>&1 | sed -e "s@^@serve: @" > ${status_output} mduigou@1141: ) & mduigou@1141: else mduigou@1141: # Run the supplied command on all repos in parallel. mduigou@1144: mduigou@1144: # n is the number of subprocess started or which might still be running. mduigou@1141: n=0 mduigou@1144: if [ $have_fifos = "true" ]; then mduigou@1144: # if we have fifos use them to detect command completion. mduigou@1144: mkfifo ${tmp}/fifo mduigou@1144: exec 3<>${tmp}/fifo mduigou@1144: if [ "${sflag}" = "true" ] ; then mduigou@1144: # force sequential mduigou@1144: at_a_time=1 mduigou@1144: fi mduigou@1144: fi mduigou@1144: mduigou@1141: for i in ${repos} ${repos_extra} ; do mduigou@1141: n=`expr ${n} '+' 1` mduigou@1141: repopidfile=`echo ${i} | sed -e 's@./@@' -e 's@/@_@g'` mduigou@1141: reponame=`echo ${i} | sed -e :a -e 's/^.\{1,20\}$/ &/;ta'` mduigou@1141: pull_base="${pull_default}" mduigou@1141: for j in $repos_extra ; do ohrstrom@538: if [ "$i" = "$j" ] ; then ohrstrom@538: pull_base="${pull_extra}" ohrstrom@538: fi mduigou@1141: done mduigou@1144: pull_base="`echo ${pull_base} | sed -e 's@[/]*$@@'`" mduigou@1141: ( mduigou@1141: ( mduigou@1142: if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then mduigou@1144: pull_newrepo="${pull_base}/${i}" mduigou@1141: path="`dirname ${i}`" mduigou@1141: if [ "${path}" != "." ] ; then mduigou@1141: times=0 mduigou@1141: while [ ! -d "${path}" ] ## nested repo, ensure containing dir exists mduigou@1141: do mduigou@1141: times=`expr ${times} '+' 1` mduigou@1141: if [ `expr ${times} '%' 10` -eq 0 ] ; then mduigou@1141: echo "${path} still not created, waiting..." > ${status_output} mduigou@1141: fi mduigou@1141: sleep 5 mduigou@1141: done mduigou@1141: fi mduigou@1144: echo "hg${global_opts} clone ${pull_newrepo} ${i}" > ${status_output} mduigou@1141: (PYTHONUNBUFFERED=true hg${global_opts} clone ${pull_newrepo} ${i}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 & mduigou@1141: else mduigou@1142: echo "cd ${i} && hg${global_opts} ${command} ${command_args}" > ${status_output} mduigou@1142: cd ${i} && (PYTHONUNBUFFERED=true hg${global_opts} ${command} ${command_args}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 & mduigou@1141: fi mduigou@1141: mduigou@1141: echo $! > ${tmp}/${repopidfile}.pid mduigou@1141: ) 2>&1 | sed -e "s@^@${reponame}: @" > ${status_output} mduigou@1144: if [ $have_fifos = "true" ]; then mduigou@1144: echo "${reponame}" >&3 mduigou@1144: fi mduigou@1141: ) & mduigou@1141: mduigou@1144: if [ $have_fifos = "true" ]; then mduigou@1144: # check on count of running subprocesses and possibly wait for completion mduigou@1144: if [ ${at_a_time} -lt ${n} ] ; then mduigou@1144: # read will block until there are completed subprocesses mduigou@1144: while read repo_done; do mduigou@1144: n=`expr ${n} '-' 1` mduigou@1144: if [ ${n} -lt ${at_a_time} ] ; then mduigou@1144: # we should start more subprocesses mduigou@1144: break; mduigou@1144: fi mduigou@1144: done <&3 mduigou@1144: fi mduigou@1144: else mduigou@1144: if [ "${sflag}" = "false" ] ; then mduigou@1144: # Compare completions to starts mduigou@1144: completed="`(ls -1 ${tmp}/*.pid.rc 2> /dev/null | wc -l) || echo 0`" mduigou@1144: while [ ${at_a_time} -lt `expr ${n} '-' ${completed}` ] ; do mduigou@1144: # sleep a short time to give time for something to complete mduigou@1144: sleep 1 mduigou@1144: completed="`(ls -1 ${tmp}/*.pid.rc 2> /dev/null | wc -l) || echo 0`" mduigou@1144: done mduigou@1144: else mduigou@1144: # complete this task before starting another. chegar@1143: wait mduigou@1144: fi chegar@1143: fi ohrstrom@538: done mduigou@1141: fi chegar@615: mduigou@1144: # Wait for all subprocesses 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 mduigou@1141: repo="`echo ${rc} | sed -e s@^${tmp}@@ -e 's@/*\([^/]*\)\.pid\.rc$@\1@' -e 's@_@/@g'`" mduigou@1144: echo "WARNING: ${repo} exited abnormally ($exit_code)" > ${status_output} chegar@615: ec=1 chegar@615: fi chegar@615: done chegar@615: fi chegar@615: exit ${ec}