mduigou@1142: #!/bin/sh ohrstrom@538: # mduigou@1149: # Copyright (c) 2009, 2014, 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: mduigou@1148: # Shell script for a fast parallel forest/trees command ohrstrom@538: mduigou@1148: usage() { mduigou@1148: echo "usage: $0 [-h|--help] [-q|--quiet] [-v|--verbose] [-s|--sequential] [--] [commands...]" > ${status_output} mduigou@1148: echo "Environment variables which modify behaviour:" mduigou@1148: echo " HGFOREST_QUIET : (boolean) If 'true' then standard output is redirected to /dev/null" mduigou@1148: echo " HGFOREST_VERBOSE : (boolean) If 'true' then Mercurial asked to produce verbose output" mduigou@1148: echo " HGFOREST_SEQUENTIAL : (boolean) If 'true' then repos are processed sequentially. Disables concurrency" mduigou@1148: echo " HGFOREST_GLOBALOPTS : (string, must begin with space) Additional Mercurial global options" mduigou@1148: echo " HGFOREST_REDIRECT : (file path) Redirect standard output to specified file" mduigou@1148: echo " HGFOREST_FIFOS : (boolean) Default behaviour for FIFO detection. Does not override FIFOs disabled" mduigou@1148: echo " HGFOREST_CONCURRENCY: (positive integer) Number of repos to process concurrently" mduigou@1148: echo " HGFOREST_DEBUG : (boolean) If 'true' then temp files are retained" mduigou@1148: exit 1 mduigou@1148: } mduigou@1148: mduigou@1148: global_opts="${HGFOREST_GLOBALOPTS:-}" mduigou@1148: status_output="${HGFOREST_REDIRECT:-/dev/stdout}" mduigou@1148: qflag="${HGFOREST_QUIET:-false}" mduigou@1148: vflag="${HGFOREST_VERBOSE:-false}" mduigou@1148: sflag="${HGFOREST_SEQUENTIAL:-false}" mduigou@1141: while [ $# -gt 0 ] mduigou@1141: do mduigou@1141: case $1 in mduigou@1148: -h | --help ) mduigou@1148: usage mduigou@1148: ;; mduigou@1148: mduigou@1141: -q | --quiet ) mduigou@1141: qflag="true" mduigou@1141: ;; mduigou@1141: mduigou@1141: -v | --verbose ) mduigou@1141: vflag="true" 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@1148: # silence standard output? mduigou@1148: if [ ${qflag} = "true" ] ; then mduigou@1148: global_opts="${global_opts} -q" mduigou@1148: status_output="/dev/null" mduigou@1148: fi mduigou@1141: mduigou@1148: # verbose output? mduigou@1148: if [ ${vflag} = "true" ] ; then mduigou@1148: global_opts="${global_opts} -v" mduigou@1148: fi mduigou@1141: mduigou@1148: # Make sure we have a command. mduigou@1148: if [ $# -lt 1 -o -z "${1:-}" ] ; then mduigou@1141: echo "ERROR: No command to hg supplied!" mduigou@1141: usage ohrstrom@538: fi ohrstrom@538: mduigou@1148: command="$1"; shift mduigou@1148: command_args="${@:-}" 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: mduigou@1148: mduigou@1148: if [ "${HGFOREST_DEBUG:-false}" = "true" ] ; then mduigou@1148: echo "DEBUG: temp files are in: ${tmp}" mduigou@1148: fi mduigou@1148: mduigou@1148: # Check if we can use fifos for monitoring sub-process completion. mduigou@1148: echo "1" > ${tmp}/read mduigou@1148: while_subshell=1 mduigou@1148: while read line; do mduigou@1148: while_subshell=0 mduigou@1148: break; mduigou@1148: done < ${tmp}/read mduigou@1148: rm ${tmp}/read mduigou@1148: mduigou@1148: on_windows=`uname -s | egrep -ic -e 'cygwin|msys'` mduigou@1148: mduigou@1148: if [ ${while_subshell} = "1" -o ${on_windows} = "1" ]; then mduigou@1148: # cygwin has (2014-04-18) broken (single writer only) FIFOs mduigou@1148: # msys has (2014-04-18) no FIFOs. mduigou@1148: # older shells create a sub-shell for redirect to while mduigou@1148: have_fifos="false" mduigou@1148: else mduigou@1148: have_fifos="${HGFOREST_FIFOS:-true}" mduigou@1148: fi mduigou@1148: ohrstrom@538: safe_interrupt () { chegar@615: if [ -d ${tmp} ]; then chegar@615: if [ "`ls ${tmp}/*.pid`" != "" ]; then mduigou@1148: echo "Waiting for processes ( `cat ${tmp}/.*.pid ${tmp}/*.pid 2> /dev/null | 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 mduigou@1148: if [ "`ls -A ${tmp} 2> /dev/null`" != "" ]; then chegar@615: wait chegar@615: fi mduigou@1148: if [ "${HGFOREST_DEBUG:-false}" != "true" ] ; then mduigou@1148: rm -f -r ${tmp} mduigou@1148: fi 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@1148: # we must be a clone 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@1148: # the clone must know where it came from (have a default pull path). 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: mduigou@1148: # determine which sub repos need to be cloned. 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@1145: mduigou@1145: pull_default_tail=`echo ${pull_default} | sed -e 's@^.*://[^/]*/\(.*\)@\1@'` mduigou@1145: mduigou@1148: if [ -n "${command_args}" ] ; then mduigou@1148: # if there is an "extra sources" path then reparent "extra" repos to that path 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}" mduigou@1148: mduigou@1148: # determine which extra subrepos need to be cloned. 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 mduigou@1145: else mduigou@1145: if [ "x${pull_default}" = "x${pull_default_tail}" ] ; then mduigou@1148: # local source repo. Clone the "extra" subrepos that exist there. mduigou@1145: for i in ${subrepos_extra} ; do mduigou@1145: if [ -f ${pull_default}/${i}/.hg/hgrc -a ! -f ${i}/.hg/hgrc ] ; then mduigou@1145: # sub-repo there in source but not here mduigou@1145: repos_extra="${repos_extra} ${i}" mduigou@1145: fi mduigou@1145: done mduigou@1145: fi ohrstrom@538: fi mduigou@1148: 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 mduigou@1148: mduigou@1148: # Repos to process concurrently. Clone does better with low concurrency. mduigou@1148: at_a_time="${HGFOREST_CONCURRENCY:-2}" ohrstrom@538: else mduigou@1148: # Process command for all of the present repos 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? mduigou@1148: 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 mduigou@1148: mduigou@1148: # Repos to process concurrently. mduigou@1148: at_a_time="${HGFOREST_CONCURRENCY:-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@1148: # "serve" is run for all the repos as one command. mduigou@1141: ( mduigou@1141: ( mduigou@1148: cwd=`pwd` mduigou@1148: serving=`basename ${cwd}` mduigou@1141: ( mduigou@1141: echo "[web]" mduigou@1148: echo "description = ${serving}" mduigou@1141: echo "allow_push = *" mduigou@1141: echo "push_ssl = False" mduigou@1141: mduigou@1141: echo "[paths]" mduigou@1148: for i in ${repos} ; do mduigou@1141: if [ "${i}" != "." ] ; then mduigou@1148: echo "/${serving}/${i} = ${i}" mduigou@1141: else mduigou@1148: echo "/${serving} = ${cwd}" mduigou@1141: fi mduigou@1141: done mduigou@1141: ) > ${tmp}/serve.web-conf mduigou@1141: mduigou@1148: echo "serving root repo ${serving}" > ${status_output} mduigou@1141: mduigou@1148: echo "hg${global_opts} serve" > ${status_output} 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@1148: 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: fi mduigou@1144: mduigou@1148: # iterate over all of the subrepos. 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@1148: mduigou@1148: # regular repo or "extra" repo? mduigou@1148: for j in ${repos_extra} ; do mduigou@1148: if [ "${i}" = "${j}" ] ; then mduigou@1148: # it's an "extra" mduigou@1148: pull_base="${pull_extra}" ohrstrom@538: fi mduigou@1141: done mduigou@1148: mduigou@1148: # remove trailing slash mduigou@1144: pull_base="`echo ${pull_base} | sed -e 's@[/]*$@@'`" mduigou@1148: mduigou@1148: # execute the command on the subrepo mduigou@1141: ( mduigou@1141: ( mduigou@1142: if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then mduigou@1148: # some form of clone mduigou@1148: clone_newrepo="${pull_base}/${i}" mduigou@1148: parent_path="`dirname ${i}`" mduigou@1148: if [ "${parent_path}" != "." ] ; then mduigou@1141: times=0 mduigou@1148: while [ ! -d "${parent_path}" ] ; do ## nested repo, ensure containing dir exists mduigou@1148: if [ "${sflag}" = "true" ] ; then mduigou@1148: # Missing parent is fatal during sequential operation. mduigou@1148: echo "ERROR: Missing parent path: ${parent_path}" > ${status_output} mduigou@1148: exit 1 mduigou@1148: fi mduigou@1149: times=`expr ${times} '+' 1` mduigou@1141: if [ `expr ${times} '%' 10` -eq 0 ] ; then mduigou@1148: echo "${parent_path} still not created, waiting..." > ${status_output} mduigou@1141: fi mduigou@1141: sleep 5 mduigou@1141: done mduigou@1141: fi mduigou@1148: # run the clone command. mduigou@1148: echo "hg${global_opts} clone ${clone_newrepo} ${i}" > ${status_output} mduigou@1148: (PYTHONUNBUFFERED=true hg${global_opts} clone ${clone_newrepo} ${i}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 & mduigou@1141: else mduigou@1148: # run the command. 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@1148: # tell the fifo waiter that this subprocess is done. mduigou@1148: if [ ${have_fifos} = "true" ]; then mduigou@1148: echo "${i}" >&3 mduigou@1144: fi mduigou@1141: ) & mduigou@1141: mduigou@1148: if [ "${sflag}" = "true" ] ; then mduigou@1148: # complete this task before starting another. mduigou@1148: wait mduigou@1144: else mduigou@1148: if [ "${have_fifos}" = "true" ]; then mduigou@1148: # check on count of running subprocesses and possibly wait for completion mduigou@1148: if [ ${n} -ge ${at_a_time} ] ; then mduigou@1148: # read will block until there are completed subprocesses mduigou@1148: while read repo_done; do mduigou@1148: n=`expr ${n} '-' 1` mduigou@1148: if [ ${n} -lt ${at_a_time} ] ; then mduigou@1148: # we should start more subprocesses mduigou@1148: break; mduigou@1148: fi mduigou@1148: done <&3 mduigou@1148: fi mduigou@1148: else mduigou@1144: # Compare completions to starts mduigou@1148: completed="`(ls -a1 ${tmp}/*.pid.rc 2> /dev/null | wc -l) || echo 0`" mduigou@1148: while [ `expr ${n} '-' ${completed}` -ge ${at_a_time} ] ; do mduigou@1144: # sleep a short time to give time for something to complete mduigou@1144: sleep 1 mduigou@1148: completed="`(ls -a1 ${tmp}/*.pid.rc 2> /dev/null | wc -l) || echo 0`" mduigou@1144: done 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 mduigou@1148: rcfiles="`(ls -a ${tmp}/*.pid.rc 2> /dev/null) || echo ''`" mduigou@1148: for rc in ${rcfiles} ; do chegar@615: exit_code=`cat ${rc} | tr -d ' \n\r'` chegar@615: if [ "${exit_code}" != "0" ] ; then mduigou@1148: repo="`echo ${rc} | sed -e 's@^'${tmp}'@@' -e 's@/*\([^/]*\)\.pid\.rc$@\1@' -e 's@_@/@g'`" mduigou@1148: 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}