Annotation of pkgsrc/mk/scripts/mkdatabase, revision 1.11

1.1       dmcmahil    1: #!/bin/sh
1.11    ! maya        2: # $NetBSD: mkdatabase,v 1.10 2015/10/03 13:17:57 bsiegert Exp $
1.1       dmcmahil    3: #
                      4: # Script for generating a database with complete dependency information
                      5: # for a particular package
                      6: #
                      7: # Copyright (c) 2003 The NetBSD Foundation, Inc.
                      8: # All rights reserved.
                      9: #
                     10: # This code is derived from software contributed to The NetBSD Foundation
                     11: # by Dan McMahill.
                     12: #
                     13: # Redistribution and use in source and binary forms, with or without
                     14: # modification, are permitted provided that the following conditions
                     15: # are met:
                     16: # 1. Redistributions of source code must retain the above copyright
                     17: #    notice, this list of conditions and the following disclaimer.
                     18: # 2. Redistributions in binary form must reproduce the above copyright
                     19: #    notice, this list of conditions and the following disclaimer in the
                     20: #    documentation and/or other materials provided with the distribution.
                     21: #
                     22: # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     23: # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     24: # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     25: # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     26: # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     27: # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     28: # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     29: # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     30: # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     31: # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     32: # POSSIBILITY OF SUCH DAMAGE.
                     33: #
                     34: 
1.6       rillig     35: #
                     36: # Global variables, based on environment variables
                     37: #
1.1       dmcmahil   38: 
                     39: TMPDIR=${TMPDIR:-/tmp}
                     40: BMAKE=${BMAKE:-make}
                     41: AWK=${AWK:-/usr/bin/awk}
                     42: DATABASE=${DATABASE:-${TMPDIR}/pkgdb.$$}
1.2       dmcmahil   43: EXPR=${EXPR:-expr}
1.1       dmcmahil   44: # as of 2003-01-04, metapkgs/gnome gets to pass #6 so
                     45: # it is very likely that if you reach 25, something is broken
                     46: MAX_PASS=${MAX_PASS:-25}
                     47: 
1.6       rillig     48: #
                     49: # Global variables
                     50: #
                     51: 
1.1       dmcmahil   52: prog=$0
1.8       rillig     53: append_flag=no
1.6       rillig     54: debug_flag=""          # meaning "no"
                     55: 
                     56: #
                     57: # Helper functions
                     58: #
1.1       dmcmahil   59: 
1.6       rillig     60: usage() {
1.1       dmcmahil   61:     echo "$prog - Generates a complete dependency tree for a particular package"
1.3       dmcmahil   62:     echo "Usage:      $prog [-a|--append] [-d|--debug] [-f|--database database]"
1.6       rillig     63:     echo ""
1.1       dmcmahil   64:     echo "            $prog -h|--help"
1.6       rillig     65:     echo ""
1.1       dmcmahil   66:     echo "            $prog -v|--version"
1.6       rillig     67:     echo ""
1.1       dmcmahil   68:     echo "The options supported by $prog are: "
1.6       rillig     69:     echo ""
1.3       dmcmahil   70:     echo "  -a|--append           Append to the database rather than overwriting it"
1.6       rillig     71:     echo ""
1.1       dmcmahil   72:     echo "  -d|--debug            Enables debugging output"
1.6       rillig     73:     echo ""
1.1       dmcmahil   74:     echo "  -f|--database <file>  Writes the database into file specified by <file>"
1.6       rillig     75:     echo ""
1.1       dmcmahil   76:     echo "  -h|--help             Displays this help message"
1.6       rillig     77:     echo ""
1.1       dmcmahil   78:     echo "  -v|--version          Displays the version of this script and exits."
1.6       rillig     79:     echo ""
1.1       dmcmahil   80:     echo "Example:    cd /usr/pkgsrc/graphics/gimp && $prog -d /tmp/gimp_database"
1.6       rillig     81:     echo ""
1.1       dmcmahil   82: }
                     83: 
                     84: ######################################################################
                     85: #
                     86: #  Handle command line options
                     87: #
                     88: ######################################################################
                     89: 
1.5       rillig     90: while test $# -gt 0; do
                     91:     case $1 in
1.1       dmcmahil   92: 
1.3       dmcmahil   93:     # Append to the database
                     94:     -a|--append)
1.8       rillig     95:        append_flag=yes
1.3       dmcmahil   96:        shift
                     97:        ;;
                     98: 
1.1       dmcmahil   99:     # Turn on debugging
                    100:     -d|--debug)
1.6       rillig    101:        debug_flag=yes
1.1       dmcmahil  102:        shift
                    103:        ;;
                    104: 
                    105:     # Use the specified database file
                    106:     -f|--database)
                    107:        DATABASE=$2
                    108:        shift 2
                    109:        ;;
                    110: 
                    111:     # Help
                    112:     -h|--help)
                    113:        usage
                    114:        exit 0
                    115:        ;;
                    116: 
                    117:     # Version
1.4       jmmv      118:     -v|--version)
1.1       dmcmahil  119:        ${AWK} '/^#[ \t]*\$NetBSD/ {gsub(/,v/,"",$3);printf("%s:  Version %s, %s\n",$3,$4,$5); exit 0;}' $prog
                    120:        exit 0
                    121:         ;;
                    122: 
1.7       rillig    123:     *) echo "$prog:  ERROR:  $1 is not a valid option"
1.1       dmcmahil  124:        usage
1.6       rillig    125:        exit 1
1.1       dmcmahil  126:        ;;
                    127:     esac
                    128: done
                    129: 
1.6       rillig    130: case $debug_flag in
                    131: yes)   set -v;;
                    132: esac
1.1       dmcmahil  133: 
1.5       rillig    134: if [ ! -d "$TMPDIR" ]; then
1.9       rillig    135:        # FIXME: wouldn't it be better to fail in this case?
1.5       rillig    136:        mkdir -p "$TMPDIR"
1.1       dmcmahil  137: fi
                    138: 
                    139: prompt="----> "
                    140: 
1.4       jmmv      141: case ${DATABASE} in
1.1       dmcmahil  142:     /*)
                    143:        # we already have the absolute path to the database file
                    144:        # so do nothing
                    145:        ;;
                    146: 
                    147:     *)
1.9       rillig    148:        # FIXME: wouldn't it be better to fail in this case?
                    149: 
1.1       dmcmahil  150:        # make sure we have the full path to the database file
                    151:        DATABASE=`pwd`/${DATABASE}
                    152:        ;;
                    153: esac
                    154: 
                    155: 
1.10      bsiegert  156: here=`pwd`
                    157: tmp1=`dirname "$here"`
                    158: pkgcat=`basename "$tmp1"`
                    159: pkg=`basename "$here"`
                    160: pkgpath=$pkgcat/$pkg
1.8       rillig    161: if [ $append_flag = yes ]; then
1.3       dmcmahil  162:     echo "$prompt Appending to database in ${DATABASE}"
1.5       rillig    163:     if [ ! -f "${DATABASE}" ]; then
                    164:        touch "${DATABASE}"
1.3       dmcmahil  165:     fi
                    166:     # make sure we haven't already been listed before
                    167:     # appending ourselves.
1.6       rillig    168:     case $debug_flag in
                    169:     yes)       echo "Looking for $pkgpath before appending";;
                    170:     esac
1.9       rillig    171:     # FIXME: $pkgpath may contain special regex characters.
1.5       rillig    172:     if grep "^index $pkgpath " "${DATABASE}" >/dev/null 2>&1 ; then
1.3       dmcmahil  173:        echo "$prompt $pkgpath has already been depended.  Skipping..."
                    174:        exit 0
                    175:     else
1.5       rillig    176:        ${BMAKE} print-summary-data >> "${DATABASE}" || exit 1
1.3       dmcmahil  177:     fi
                    178: else
                    179:     echo "$prompt Creating new database in ${DATABASE}"
1.10      bsiegert  180:     o=`${BMAKE} show-options | ${AWK} -f ../../mk/scripts/htmloptions.awk`
                    181:     echo "htmloptions ${pkgpath} $o" > ${DATABASE}
                    182:     ${BMAKE} print-summary-data >> "${DATABASE}" || exit 1
1.3       dmcmahil  183: fi
1.1       dmcmahil  184: here=`pwd`
                    185: echo "$prompt Depending in $here (pass #1)"
1.6       rillig    186: dirs=`${AWK} -f ../../mk/scripts/chkdatabase.awk debug=${debug_flag} "${DATABASE}"`
1.1       dmcmahil  187: pass=2
1.5       rillig    188: while [ ! -z "$dirs" -a $pass -lt "${MAX_PASS}" ]; do
1.4       jmmv      189:        for d in $dirs ; do
1.1       dmcmahil  190:                echo "$prompt Depending in ../../$d (pass #$pass)" ;\
1.5       rillig    191:                cd "../../$d" && ${BMAKE} print-summary-data >> "${DATABASE}" || exit 1
                    192:                cd "$here"
1.1       dmcmahil  193:        done
1.6       rillig    194:        dirs=`${AWK} -f ../../mk/scripts/chkdatabase.awk debug=${debug_flag} ${DATABASE}`
1.2       dmcmahil  195:        pass=`${EXPR} $pass + 1`
1.1       dmcmahil  196: done
                    197: 
                    198: if [ $pass -eq ${MAX_PASS} ]; then
                    199:        echo "ERROR:  You have reached $pass times through the dependency tree"
                    200:        echo "        and _still_ not finished.  This is probably due to a broken"
                    201:        echo "        set of dependencies.  You may wish to examine the partial"
                    202:        echo "        database left in ${DATABASE}"
                    203:        exit 1
                    204: else
                    205:        echo "Complete dependency database left in ${DATABASE}"
                    206: fi

CVSweb <webmaster@jp.NetBSD.org>