hgforest.sh revision 1041:d6f66566d8b3
1#!/bin/sh
2
3#
4# Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
5# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6#
7# This code is free software; you can redistribute it and/or modify it
8# under the terms of the GNU General Public License version 2 only, as
9# published by the Free Software Foundation.
10#
11# This code is distributed in the hope that it will be useful, but WITHOUT
12# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14# version 2 for more details (a copy is included in the LICENSE file that
15# accompanied this code).
16#
17# You should have received a copy of the GNU General Public License version
18# 2 along with this work; if not, write to the Free Software Foundation,
19# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20#
21# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22# or visit www.oracle.com if you need additional information or have any
23# questions.
24#
25
26# Shell script for a fast parallel forest command
27
28global_opts=""
29status_output="/dev/stdout"
30qflag="false"
31vflag="false"
32sflag="false"
33while [ $# -gt 0 ]
34do
35  case $1 in
36    -q | --quiet )
37      qflag="true"
38      global_opts="${global_opts} -q"
39      status_output="/dev/null"
40      ;;
41
42    -v | --verbose )
43      vflag="true"
44      global_opts="${global_opts} -v"
45      ;;
46
47    -s | --sequential )
48      sflag="true"
49      ;;
50
51    '--' ) # no more options
52      shift; break
53      ;;
54
55    -*)  # bad option
56      usage
57      ;;
58
59     * )  # non option
60      break
61      ;;
62  esac
63  shift
64done
65
66
67command="$1"; shift
68command_args="$@"
69
70usage() {
71      echo "usage: $0 [-q|--quiet] [-v|--verbose] [-s|--sequential] [--] <command> [commands...]" > ${status_output}
72      exit 1
73}
74
75if [ "x" = "x$command" ] ; then
76  echo "ERROR: No command to hg supplied!"
77  usage
78fi
79
80# Check if we can use fifos for monitoring sub-process completion.
81on_windows=`uname -s | egrep -ic -e 'cygwin|msys'`
82if [ ${on_windows} = "1" ]; then
83  # cygwin has (2014-04-18) broken (single writer only) FIFOs
84  # msys has (2014-04-18) no FIFOs.
85  have_fifos="false"
86else
87  have_fifos="true"
88fi
89
90# Clean out the temporary directory that stores the pid files.
91tmp=/tmp/forest.$$
92rm -f -r ${tmp}
93mkdir -p ${tmp}
94
95safe_interrupt () {
96  if [ -d ${tmp} ]; then
97    if [ "`ls ${tmp}/*.pid`" != "" ]; then
98      echo "Waiting for processes ( `cat ${tmp}/*.pid | tr '\n' ' '`) to terminate nicely!" > ${status_output}
99      sleep 1
100      # Pipe stderr to dev/null to silence kill, that complains when trying to kill
101      # a subprocess that has already exited.
102      kill -TERM `cat ${tmp}/*.pid | tr '\n' ' '` 2> /dev/null
103      wait
104      echo "Interrupt complete!" > ${status_output}
105    fi
106    rm -f -r ${tmp}
107  fi
108  exit 130
109}
110
111nice_exit () {
112  if [ -d ${tmp} ]; then
113    if [ "`ls ${tmp}`" != "" ]; then
114      wait
115    fi
116    rm -f -r ${tmp}
117  fi
118}
119
120trap 'safe_interrupt' INT QUIT
121trap 'nice_exit' EXIT
122
123subrepos="corba jaxp jaxws langtools jdk hotspot nashorn"
124subrepos_extra="closed jdk/src/closed jdk/make/closed jdk/test/closed hotspot/make/closed hotspot/src/closed hotspot/test/closed deploy install sponsors pubs"
125
126# Only look in specific locations for possible forests (avoids long searches)
127pull_default=""
128repos=""
129repos_extra=""
130if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then
131  if [ ! -f .hg/hgrc ] ; then
132    echo "ERROR: Need initial repository to use this script" > ${status_output}
133    exit 1
134  fi
135
136  pull_default=`hg paths default`
137  if [ "${pull_default}" = "" ] ; then
138    echo "ERROR: Need initial clone with 'hg paths default' defined" > ${status_output}
139    exit 1
140  fi
141
142  for i in ${subrepos} ; do
143    if [ ! -f ${i}/.hg/hgrc ] ; then
144      repos="${repos} ${i}"
145    fi
146  done
147
148  pull_default_tail=`echo ${pull_default} | sed -e 's@^.*://[^/]*/\(.*\)@\1@'`
149
150  if [ "${command_args}" != "" ] ; then
151    if [ "x${pull_default}" = "x${pull_default_tail}" ] ; then
152      echo "ERROR: Need initial clone from non-local source" > ${status_output}
153      exit 1
154    fi
155    pull_extra="${command_args}/${pull_default_tail}"
156    for i in ${subrepos_extra} ; do
157      if [ ! -f ${i}/.hg/hgrc ] ; then
158        repos_extra="${repos_extra} ${i}"
159      fi
160    done
161  else
162    if [ "x${pull_default}" = "x${pull_default_tail}" ] ; then
163      # local source repo. Copy the extras ones that exist there.
164      for i in ${subrepos_extra} ; do
165        if [ -f ${pull_default}/${i}/.hg/hgrc -a ! -f ${i}/.hg/hgrc ] ; then
166          # sub-repo there in source but not here
167          repos_extra="${repos_extra} ${i}"
168        fi
169      done
170    fi
171  fi
172  at_a_time=2
173  # Any repos to deal with?
174  if [ "${repos}" = "" -a "${repos_extra}" = "" ] ; then
175    echo "No repositories to process." > ${status_output}
176    exit
177  fi
178else
179  for i in . ${subrepos} ${subrepos_extra} ; do
180    if [ -d ${i}/.hg ] ; then
181      repos="${repos} ${i}"
182    fi
183  done
184
185  # Any repos to deal with?
186  if [ "${repos}" = "" ] ; then
187    echo "No repositories to process." > ${status_output}
188    exit
189  fi
190
191  # any of the repos locked?
192  for i in ${repos} ; do
193    if [ -h ${i}/.hg/store/lock -o -f ${i}/.hg/store/lock ] ; then
194      locked="${i} ${locked}"
195    fi
196  done
197  if [ "${locked}" != "" ] ; then
198    echo "ERROR: These repositories are locked: ${locked}" > ${status_output}
199    exit 1
200  fi
201  at_a_time=8
202fi
203
204# Echo out what repositories we do a command on.
205echo "# Repositories: ${repos} ${repos_extra}" > ${status_output}
206
207if [ "${command}" = "serve" ] ; then
208  # "serve" is run for all the repos.
209  (
210    (
211      (
212        echo "[web]"
213        echo "description = $(basename $(pwd))"
214        echo "allow_push = *"
215        echo "push_ssl = False"
216
217        echo "[paths]"
218        for i in ${repos} ${repos_extra} ; do
219          if [ "${i}" != "." ] ; then
220            echo "/$(basename $(pwd))/${i} = ${i}"
221          else
222            echo "/$(basename $(pwd)) = $(pwd)"
223          fi
224        done
225      ) > ${tmp}/serve.web-conf
226
227      echo "serving root repo $(basename $(pwd))"
228
229      (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 &
230    ) 2>&1 | sed -e "s@^@serve:   @" > ${status_output}
231  ) &
232else
233  # Run the supplied command on all repos in parallel.
234
235  # n is the number of subprocess started or which might still be running.
236  n=0
237  if [ $have_fifos = "true" ]; then
238    # if we have fifos use them to detect command completion.
239    mkfifo ${tmp}/fifo
240    exec 3<>${tmp}/fifo
241    if [ "${sflag}" = "true" ] ; then
242      # force sequential
243      at_a_time=1
244    fi
245  fi
246
247  for i in ${repos} ${repos_extra} ; do
248    n=`expr ${n} '+' 1`
249    repopidfile=`echo ${i} | sed -e 's@./@@' -e 's@/@_@g'`
250    reponame=`echo ${i} | sed -e :a -e 's/^.\{1,20\}$/ &/;ta'`
251    pull_base="${pull_default}"
252    for j in $repos_extra ; do
253      if [ "$i" = "$j" ] ; then
254          pull_base="${pull_extra}"
255      fi
256    done
257    pull_base="`echo ${pull_base} | sed -e 's@[/]*$@@'`"
258    (
259      (
260        if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then
261          pull_newrepo="${pull_base}/${i}"
262          path="`dirname ${i}`"
263          if [ "${path}" != "." ] ; then
264            times=0
265            while [ ! -d "${path}" ]   ## nested repo, ensure containing dir exists
266            do
267              times=`expr ${times} '+' 1`
268              if [ `expr ${times} '%' 10` -eq 0 ] ; then
269                echo "${path} still not created, waiting..." > ${status_output}
270              fi
271              sleep 5
272            done
273          fi
274          echo "hg${global_opts} clone ${pull_newrepo} ${i}" > ${status_output}
275          (PYTHONUNBUFFERED=true hg${global_opts} clone ${pull_newrepo} ${i}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &
276        else
277          echo "cd ${i} && hg${global_opts} ${command} ${command_args}" > ${status_output}
278          cd ${i} && (PYTHONUNBUFFERED=true hg${global_opts} ${command} ${command_args}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &
279        fi
280
281        echo $! > ${tmp}/${repopidfile}.pid
282      ) 2>&1 | sed -e "s@^@${reponame}:   @" > ${status_output}
283      if [ $have_fifos = "true" ]; then
284        echo "${reponame}" >&3
285      fi
286    ) &
287
288    if [ $have_fifos = "true" ]; then
289      # check on count of running subprocesses and possibly wait for completion
290      if [ ${at_a_time} -lt ${n} ] ; then
291        # read will block until there are completed subprocesses
292        while read repo_done; do
293          n=`expr ${n} '-' 1`
294          if [ ${n} -lt ${at_a_time} ] ; then
295            # we should start more subprocesses
296            break;
297          fi
298        done <&3
299      fi
300    else
301      if [ "${sflag}" = "false" ] ; then
302        # Compare completions to starts
303        completed="`(ls -1 ${tmp}/*.pid.rc 2> /dev/null | wc -l) || echo 0`"
304        while [ ${at_a_time} -lt `expr ${n} '-' ${completed}` ] ; do
305          # sleep a short time to give time for something to complete
306          sleep 1
307          completed="`(ls -1 ${tmp}/*.pid.rc 2> /dev/null | wc -l) || echo 0`"
308        done
309      else
310        # complete this task before starting another.
311        wait
312      fi
313    fi
314  done
315fi
316
317# Wait for all subprocesses to complete
318wait
319
320# Terminate with exit 0 only if all subprocesses were successful
321ec=0
322if [ -d ${tmp} ]; then
323  for rc in ${tmp}/*.pid.rc ; do
324    exit_code=`cat ${rc} | tr -d ' \n\r'`
325    if [ "${exit_code}" != "0" ] ; then
326      repo="`echo ${rc} | sed -e s@^${tmp}@@ -e 's@/*\([^/]*\)\.pid\.rc$@\1@' -e 's@_@/@g'`"
327      echo "WARNING: ${repo} exited abnormally ($exit_code)" > ${status_output}
328      ec=1
329    fi
330  done
331fi
332exit ${ec}
333