common/bin/hgforest.sh

Thu, 31 Aug 2017 15:40:18 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:40:18 +0800
changeset 1133
50aaf272884f
parent 902
d832f6171acd
parent 1
2bcf2a10b799
child 1288
7801df19942d
permissions
-rw-r--r--

merge

     1 #!/bin/sh
     3 #
     4 # Copyright (c) 2009, 2013, 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 #
    26 #
    27 # This file has been modified by Loongson Technology in 2013. These
    28 # modifications are Copyright (c) 2013 Loongson Technology, and are made
    29 # available on the same license terms set forth above.
    30 #
    32 # Shell script for a fast parallel forest command
    33 command="$1"
    34 pull_extra_base="$2"
    36 if [ "" = "$command" ] ; then
    37   echo No command to hg supplied!
    38   exit 1
    39 fi
    41 # Clean out the temporary directory that stores the pid files.
    42 tmp=/tmp/forest.$$
    43 rm -f -r ${tmp}
    44 mkdir -p ${tmp}
    46 safe_interrupt () {
    47   if [ -d ${tmp} ]; then
    48     if [ "`ls ${tmp}/*.pid`" != "" ]; then
    49       echo "Waiting for processes ( `cat ${tmp}/*.pid | tr '\n' ' '`) to terminate nicely!"
    50       sleep 1
    51       # Pipe stderr to dev/null to silence kill, that complains when trying to kill
    52       # a subprocess that has already exited.
    53       kill -TERM `cat ${tmp}/*.pid | tr '\n' ' '` 2> /dev/null
    54       wait
    55       echo Interrupt complete!
    56     fi
    57   fi
    58   rm -f -r ${tmp}
    59   exit 1
    60 }
    62 nice_exit () {
    63   if [ -d ${tmp} ]; then
    64     if [ "`ls ${tmp}`" != "" ]; then
    65       wait
    66     fi
    67   fi
    68   rm -f -r ${tmp}
    69 }
    71 trap 'safe_interrupt' INT QUIT
    72 trap 'nice_exit' EXIT
    74 # Only look in specific locations for possible forests (avoids long searches)
    75 pull_default=""
    76 repos=""
    77 repos_extra=""
    78 if [ "${command}" = "clone" -o "${command}" = "fclone" ] ; then
    79   subrepos="corba jaxp jaxws langtools jdk hotspot nashorn"
    80   if [ -f .hg/hgrc ] ; then
    81     pull_default=`hg paths default`
    82     if [ "${pull_default}" = "" ] ; then
    83       echo "ERROR: Need initial clone with 'hg paths default' defined"
    84       exit 1
    85     fi
    86   fi
    87   if [ "${pull_default}" = "" ] ; then
    88     echo "ERROR: Need initial repository to use this script"
    89     exit 1
    90   fi
    91   for i in ${subrepos} ; do
    92     if [ ! -f ${i}/.hg/hgrc ] ; then
    93       repos="${repos} ${i}"
    94     fi
    95   done
    96   if [ "${pull_extra_base}" != "" ] ; then
    97     subrepos_extra="jdk/src/closed jdk/make/closed jdk/test/closed hotspot/make/closed hotspot/src/closed hotspot/test/closed deploy install sponsors pubs"
    98     pull_default_tail=`echo ${pull_default} | sed -e 's@^.*://[^/]*/\(.*\)@\1@'`
    99     pull_extra="${pull_extra_base}/${pull_default_tail}"
   100     for i in ${subrepos_extra} ; do
   101       if [ ! -f ${i}/.hg/hgrc ] ; then
   102         repos_extra="${repos_extra} ${i}"
   103       fi
   104     done
   105   fi
   106   at_a_time=2
   107   # Any repos to deal with?
   108   if [ "${repos}" = "" -a "${repos_extra}" = "" ] ; then
   109     exit
   110   fi
   111 else
   112   hgdirs=`ls -d ./.hg ./*/.hg ./*/*/.hg ./*/*/*/.hg ./*/*/*/*/.hg 2>/dev/null`
   113   # Derive repository names from the .hg directory locations
   114   for i in ${hgdirs} ; do
   115     repos="${repos} `echo ${i} | sed -e 's@/.hg$@@'`"
   116   done
   117   for i in ${repos} ; do
   118     if [ -h ${i}/.hg/store/lock -o -f ${i}/.hg/store/lock ] ; then
   119       locked="${i} ${locked}"
   120     fi
   121   done
   122   at_a_time=8
   123   # Any repos to deal with?
   124   if [ "${repos}" = "" ] ; then
   125     echo "No repositories to process."
   126     exit
   127   fi
   128   if [ "${locked}" != "" ] ; then
   129     echo "These repositories are locked: ${locked}"
   130     exit
   131   fi
   132 fi
   134 # Echo out what repositories we do a command on.
   135 echo "# Repositories: ${repos} ${repos_extra}"
   136 echo
   138 # Run the supplied command on all repos in parallel.
   139 n=0
   140 for i in ${repos} ${repos_extra} ; do
   141   n=`expr ${n} '+' 1`
   142   repopidfile=`echo ${i} | sed -e 's@./@@' -e 's@/@_@g'`
   143   reponame=`echo ${i} | sed -e :a -e 's/^.\{1,20\}$/ &/;ta'`
   144   pull_base="${pull_default}"
   145   for j in $repos_extra ; do
   146       if [ "$i" = "$j" ] ; then
   147           pull_base="${pull_extra}"
   148       fi
   149   done
   150   (
   151     (
   152       if [ "${command}" = "clone" -o "${command}" = "fclone" ] ; then
   153         # 2013.2.27 Jin: fix the repo url for ssh
   154         #pull_newrepo="`echo ${pull_base}/${i} | sed -e 's@\([^:]/\)//*@\1@g'`"
   155         pull_newrepo=${pull_base}/$i
   156         echo hg clone ${pull_newrepo} ${i}
   157         path="`dirname ${i}`"
   158         if [ "${path}" != "." ] ; then
   159           times=0
   160           while [ ! -d "${path}" ]   ## nested repo, ensure containing dir exists
   161           do
   162             times=`expr ${times} '+' 1`
   163             if [ `expr ${times} '%' 10` -eq 0 ] ; then
   164               echo ${path} still not created, waiting...
   165             fi
   166             sleep 5
   167           done
   168         fi
   169         (PYTHONUNBUFFERED=true hg clone ${pull_newrepo} ${i}; echo "$?" > ${tmp}/${repopidfile}.pid.rc )
   170       else
   171         echo "cd ${i} && hg $*"
   172         cd ${i} && (PYTHONUNBUFFERED=true hg "$@"; echo "$?" > ${tmp}/${repopidfile}.pid.rc )
   173       fi
   174       echo $! > ${tmp}/${repopidfile}.pid
   175     ) 2>&1 | sed -e "s@^@${reponame}:   @") 
   177   if [ `expr ${n} '%' ${at_a_time}` -eq 0 ] ; then
   178     sleep 2
   179     echo Waiting 5 secs before spawning next background command.
   180     sleep 3
   181   fi
   182 done
   183 # Wait for all hg commands to complete
   184 wait
   186 # Terminate with exit 0 only if all subprocesses were successful
   187 ec=0
   188 if [ -d ${tmp} ]; then
   189   for rc in ${tmp}/*.pid.rc ; do
   190     exit_code=`cat ${rc} | tr -d ' \n\r'`
   191     if [ "${exit_code}" != "0" ] ; then
   192       echo "WARNING: ${rc} exited abnormally."
   193       ec=1
   194     fi
   195   done
   196 fi
   197 exit ${ec}

mercurial