Path: blob/aarch64-shenandoah-jdk8u272-b10/common/bin/hgforest.sh
32278 views
#!/bin/sh1#2# Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4#5# This code is free software; you can redistribute it and/or modify it6# under the terms of the GNU General Public License version 2 only, as7# published by the Free Software Foundation.8#9# This code is distributed in the hope that it will be useful, but WITHOUT10# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12# version 2 for more details (a copy is included in the LICENSE file that13# accompanied this code).14#15# You should have received a copy of the GNU General Public License version16# 2 along with this work; if not, write to the Free Software Foundation,17# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18#19# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20# or visit www.oracle.com if you need additional information or have any21# questions.22#2324# Shell script for a fast parallel forest/trees command2526usage() {27echo "usage: $0 [-h|--help] [-q|--quiet] [-v|--verbose] [-s|--sequential] [--] <command> [commands...]" > ${status_output}28echo "Environment variables which modify behaviour:"29echo " HGFOREST_QUIET : (boolean) If 'true' then standard output is redirected to /dev/null"30echo " HGFOREST_VERBOSE : (boolean) If 'true' then Mercurial asked to produce verbose output"31echo " HGFOREST_SEQUENTIAL : (boolean) If 'true' then repos are processed sequentially. Disables concurrency"32echo " HGFOREST_GLOBALOPTS : (string, must begin with space) Additional Mercurial global options"33echo " HGFOREST_REDIRECT : (file path) Redirect standard output to specified file"34echo " HGFOREST_FIFOS : (boolean) Default behaviour for FIFO detection. Does not override FIFOs disabled"35echo " HGFOREST_CONCURRENCY: (positive integer) Number of repos to process concurrently"36echo " HGFOREST_DEBUG : (boolean) If 'true' then temp files are retained"37exit 138}3940global_opts="${HGFOREST_GLOBALOPTS:-}"41status_output="${HGFOREST_REDIRECT:-/dev/stdout}"42qflag="${HGFOREST_QUIET:-false}"43vflag="${HGFOREST_VERBOSE:-false}"44sflag="${HGFOREST_SEQUENTIAL:-false}"45while [ $# -gt 0 ]46do47case $1 in48-h | --help )49usage50;;5152-q | --quiet )53qflag="true"54;;5556-v | --verbose )57vflag="true"58;;5960-s | --sequential )61sflag="true"62;;6364'--' ) # no more options65shift; break66;;6768-*) # bad option69usage70;;7172* ) # non option73break74;;75esac76shift77done7879# silence standard output?80if [ ${qflag} = "true" ] ; then81global_opts="${global_opts} -q"82status_output="/dev/null"83fi8485# verbose output?86if [ ${vflag} = "true" ] ; then87global_opts="${global_opts} -v"88fi8990# Make sure we have a command.91if [ $# -lt 1 -o -z "${1:-}" ] ; then92echo "ERROR: No command to hg supplied!"93usage94fi9596command="$1"; shift97command_args="${@:-}"9899# Clean out the temporary directory that stores the pid files.100tmp=/tmp/forest.$$101rm -f -r ${tmp}102mkdir -p ${tmp}103104105if [ "${HGFOREST_DEBUG:-false}" = "true" ] ; then106echo "DEBUG: temp files are in: ${tmp}"107fi108109# Check if we can use fifos for monitoring sub-process completion.110echo "1" > ${tmp}/read111while_subshell=1112while read line; do113while_subshell=0114break;115done < ${tmp}/read116rm ${tmp}/read117118on_windows=`uname -s | egrep -ic -e 'cygwin|msys'`119120if [ ${while_subshell} = "1" -o ${on_windows} = "1" ]; then121# cygwin has (2014-04-18) broken (single writer only) FIFOs122# msys has (2014-04-18) no FIFOs.123# older shells create a sub-shell for redirect to while124have_fifos="false"125else126have_fifos="${HGFOREST_FIFOS:-true}"127fi128129safe_interrupt () {130if [ -d ${tmp} ]; then131if [ "`ls ${tmp}/*.pid`" != "" ]; then132echo "Waiting for processes ( `cat ${tmp}/.*.pid ${tmp}/*.pid 2> /dev/null | tr '\n' ' '`) to terminate nicely!" > ${status_output}133sleep 1134# Pipe stderr to dev/null to silence kill, that complains when trying to kill135# a subprocess that has already exited.136kill -TERM `cat ${tmp}/*.pid | tr '\n' ' '` 2> /dev/null137wait138echo "Interrupt complete!" > ${status_output}139fi140rm -f -r ${tmp}141fi142exit 130143}144145nice_exit () {146if [ -d ${tmp} ]; then147if [ "`ls -A ${tmp} 2> /dev/null`" != "" ]; then148wait149fi150if [ "${HGFOREST_DEBUG:-false}" != "true" ] ; then151rm -f -r ${tmp}152fi153fi154}155156trap 'safe_interrupt' INT QUIT157trap 'nice_exit' EXIT158159subrepos="corba jaxp jaxws langtools jdk hotspot nashorn"160subrepos_extra="jdk/src/closed jdk/make/closed jdk/test/closed hotspot/make/closed hotspot/src/closed hotspot/test/closed deploy install sponsors pubs"161162# Only look in specific locations for possible forests (avoids long searches)163pull_default=""164repos=""165repos_extra=""166if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then167# we must be a clone168if [ ! -f .hg/hgrc ] ; then169echo "ERROR: Need initial repository to use this script" > ${status_output}170exit 1171fi172173# the clone must know where it came from (have a default pull path).174pull_default=`hg paths default`175if [ "${pull_default}" = "" ] ; then176echo "ERROR: Need initial clone with 'hg paths default' defined" > ${status_output}177exit 1178fi179180# determine which sub repos need to be cloned.181for i in ${subrepos} ; do182if [ ! -f ${i}/.hg/hgrc ] ; then183repos="${repos} ${i}"184fi185done186187pull_default_tail=`echo ${pull_default} | sed -e 's@^.*://[^/]*/\(.*\)@\1@'`188189if [ -n "${command_args}" ] ; then190# if there is an "extra sources" path then reparent "extra" repos to that path191if [ "x${pull_default}" = "x${pull_default_tail}" ] ; then192echo "ERROR: Need initial clone from non-local source" > ${status_output}193exit 1194fi195pull_extra="${command_args}/${pull_default_tail}"196197# determine which extra subrepos need to be cloned.198for i in ${subrepos_extra} ; do199if [ ! -f ${i}/.hg/hgrc ] ; then200repos_extra="${repos_extra} ${i}"201fi202done203else204if [ "x${pull_default}" = "x${pull_default_tail}" ] ; then205# local source repo. Clone the "extra" subrepos that exist there.206for i in ${subrepos_extra} ; do207if [ -f ${pull_default}/${i}/.hg/hgrc -a ! -f ${i}/.hg/hgrc ] ; then208# sub-repo there in source but not here209repos_extra="${repos_extra} ${i}"210fi211done212fi213fi214215# Any repos to deal with?216if [ "${repos}" = "" -a "${repos_extra}" = "" ] ; then217echo "No repositories to process." > ${status_output}218exit219fi220221# Repos to process concurrently. Clone does better with low concurrency.222at_a_time="${HGFOREST_CONCURRENCY:-2}"223else224# Process command for all of the present repos225for i in . ${subrepos} ${subrepos_extra} ; do226if [ -d ${i}/.hg ] ; then227repos="${repos} ${i}"228fi229done230231# Any repos to deal with?232if [ "${repos}" = "" ] ; then233echo "No repositories to process." > ${status_output}234exit235fi236237# any of the repos locked?238locked=""239for i in ${repos} ; do240if [ -h ${i}/.hg/store/lock -o -f ${i}/.hg/store/lock ] ; then241locked="${i} ${locked}"242fi243done244if [ "${locked}" != "" ] ; then245echo "ERROR: These repositories are locked: ${locked}" > ${status_output}246exit 1247fi248249# Repos to process concurrently.250at_a_time="${HGFOREST_CONCURRENCY:-8}"251fi252253# Echo out what repositories we do a command on.254echo "# Repositories: ${repos} ${repos_extra}" > ${status_output}255256if [ "${command}" = "serve" ] ; then257# "serve" is run for all the repos as one command.258(259(260cwd=`pwd`261serving=`basename ${cwd}`262(263echo "[web]"264echo "description = ${serving}"265echo "allow_push = *"266echo "push_ssl = False"267268echo "[paths]"269for i in ${repos} ; do270if [ "${i}" != "." ] ; then271echo "/${serving}/${i} = ${i}"272else273echo "/${serving} = ${cwd}"274fi275done276) > ${tmp}/serve.web-conf277278echo "serving root repo ${serving}" > ${status_output}279280echo "hg${global_opts} serve" > ${status_output}281(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 &282) 2>&1 | sed -e "s@^@serve: @" > ${status_output}283) &284else285# Run the supplied command on all repos in parallel.286287# n is the number of subprocess started or which might still be running.288n=0289if [ ${have_fifos} = "true" ]; then290# if we have fifos use them to detect command completion.291mkfifo ${tmp}/fifo292exec 3<>${tmp}/fifo293fi294295# iterate over all of the subrepos.296for i in ${repos} ${repos_extra} ; do297n=`expr ${n} '+' 1`298repopidfile=`echo ${i} | sed -e 's@./@@' -e 's@/@_@g'`299reponame=`echo ${i} | sed -e :a -e 's/^.\{1,20\}$/ &/;ta'`300pull_base="${pull_default}"301302# regular repo or "extra" repo?303for j in ${repos_extra} ; do304if [ "${i}" = "${j}" ] ; then305# it's an "extra"306pull_base="${pull_extra}"307fi308done309310# remove trailing slash311pull_base="`echo ${pull_base} | sed -e 's@[/]*$@@'`"312313# execute the command on the subrepo314(315(316if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then317# some form of clone318clone_newrepo="${pull_base}/${i}"319parent_path="`dirname ${i}`"320if [ "${parent_path}" != "." ] ; then321times=0322while [ ! -d "${parent_path}" ] ; do ## nested repo, ensure containing dir exists323if [ "${sflag}" = "true" ] ; then324# Missing parent is fatal during sequential operation.325echo "ERROR: Missing parent path: ${parent_path}" > ${status_output}326exit 1327fi328times=`expr ${times} '+' 1`329if [ `expr ${times} '%' 10` -eq 0 ] ; then330echo "${parent_path} still not created, waiting..." > ${status_output}331fi332sleep 5333done334fi335# run the clone command.336echo "hg${global_opts} clone ${clone_newrepo} ${i}" > ${status_output}337(PYTHONUNBUFFERED=true hg${global_opts} clone ${clone_newrepo} ${i}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &338else339# run the command.340echo "cd ${i} && hg${global_opts} ${command} ${command_args}" > ${status_output}341cd ${i} && (PYTHONUNBUFFERED=true hg${global_opts} ${command} ${command_args}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &342fi343344echo $! > ${tmp}/${repopidfile}.pid345) 2>&1 | sed -e "s@^@${reponame}: @" > ${status_output}346# tell the fifo waiter that this subprocess is done.347if [ ${have_fifos} = "true" ]; then348echo "${i}" >&3349fi350) &351352if [ "${sflag}" = "true" ] ; then353# complete this task before starting another.354wait355else356if [ "${have_fifos}" = "true" ]; then357# check on count of running subprocesses and possibly wait for completion358if [ ${n} -ge ${at_a_time} ] ; then359# read will block until there are completed subprocesses360while read repo_done; do361n=`expr ${n} '-' 1`362if [ ${n} -lt ${at_a_time} ] ; then363# we should start more subprocesses364break;365fi366done <&3367fi368else369# Compare completions to starts370completed="`(ls -a1 ${tmp}/*.pid.rc 2> /dev/null | wc -l) || echo 0`"371while [ `expr ${n} '-' ${completed}` -ge ${at_a_time} ] ; do372# sleep a short time to give time for something to complete373sleep 1374completed="`(ls -a1 ${tmp}/*.pid.rc 2> /dev/null | wc -l) || echo 0`"375done376fi377fi378done379fi380381# Wait for all subprocesses to complete382wait383384# Terminate with exit 0 only if all subprocesses were successful385ec=0386if [ -d ${tmp} ]; then387rcfiles="`(ls -a ${tmp}/*.pid.rc 2> /dev/null) || echo ''`"388for rc in ${rcfiles} ; do389exit_code=`cat ${rc} | tr -d ' \n\r'`390if [ "${exit_code}" != "0" ] ; then391repo="`echo ${rc} | sed -e 's@^'${tmp}'@@' -e 's@/*\([^/]*\)\.pid\.rc$@\1@' -e 's@_@/@g'`"392echo "WARNING: ${repo} exited abnormally (${exit_code})" > ${status_output}393ec=1394fi395done396fi397exit ${ec}398399400