Annotation of pkgsrc/mk/scripts/binpkg-cache, revision 1.3

1.1       dmcmahil    1: #!/bin/sh
                      2: #
1.3     ! dmcmahil    3: # $NetBSD: binpkg-cache,v 1.2 2005/05/19 03:58:40 dmcmahill Exp $
1.1       dmcmahil    4: #
                      5: # Script for generating a cache file with information about
                      6: # all binary packages contained in a directory.
                      7: #
                      8: # Copyright (c) 2005 The NetBSD Foundation, Inc.
                      9: # All rights reserved.
                     10: #
                     11: # This code is derived from software contributed to The NetBSD Foundation
                     12: # by Dan McMahill.
                     13: #
                     14: # Redistribution and use in source and binary forms, with or without
                     15: # modification, are permitted provided that the following conditions
                     16: # are met:
                     17: # 1. Redistributions of source code must retain the above copyright
                     18: #    notice, this list of conditions and the following disclaimer.
                     19: # 2. Redistributions in binary form must reproduce the above copyright
                     20: #    notice, this list of conditions and the following disclaimer in the
                     21: #    documentation and/or other materials provided with the distribution.
                     22: # 3. All advertising materials mentioning features or use of this software
                     23: #    must display the following acknowledgement:
                     24: #        This product includes software developed by the NetBSD
                     25: #        Foundation, Inc. and its contributors.
                     26: # 4. Neither the name of The NetBSD Foundation nor the names of its
                     27: #    contributors may be used to endorse or promote products derived
                     28: #    from this software without specific prior written permission.
                     29: #
                     30: # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     31: # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     32: # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     33: # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     34: # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     35: # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     36: # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     37: # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     38: # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     39: # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     40: # POSSIBILITY OF SUCH DAMAGE.
                     41: #
                     42: 
                     43: 
                     44: TMPDIR=${TMPDIR:-/tmp}
                     45: PACKAGES=${PACKAGES:-/usr/pkgsrc/packages/}
                     46: AWK=${AWK:-awk}
                     47: CMP=${CMP:-cmp}
1.3     ! dmcmahil   48: FIND=${FIND:-find}
1.1       dmcmahil   49: GREP=${GREP:-grep}
                     50: PKG_INFO=${PKG_INFO:-pkg_info}
                     51: PKG_SUFX=${PKG_SUFX:-.tgz}
                     52: SED=${SED:-sed}
                     53: SORT=${SORT:-sort}
                     54: STAT=${STAT:-stat}
                     55: 
                     56: cachefile=.pkgcache
                     57: cacheversion=20050428
                     58: 
                     59: prompt="----> "
                     60: tab="      "
                     61: 
                     62: tmpd=${TMPDIR}/pkg-cache.$$
                     63: mkdir -m 0700 ${tmpd}
                     64: if test $? -ne 0 ; then
                     65:        echo "ERROR:  Could not create temporary directory ${tmpd}"
                     66:        echo "Either you do not have write permission to ${tmpd} or"
                     67:        echo "${tmpd} already exists"
                     68:        exit 1
                     69: fi
                     70: 
                     71: prog=$0
                     72: 
                     73: usage(){
                     74:        echo "$prog - Generates cache files for each directory containing binary"
                     75:        echo "        packages.  This cache file can then be used by the README.html"
                     76:        echo "        generation code to avoid having to call pkg_info(1) over and over"
                     77:        echo "        on the same binary package."
                     78:        echo " "
                     79:        echo "Usage:      $prog [-d|--debug] [-v|--verbose] [-p|--packages <dir>]"
                     80:        echo " "
                     81:        echo "            $prog -h|--help"
                     82:        echo " "
                     83:        echo "            $prog -V|--version"
                     84:        echo " "
                     85:        echo "The options supported by $prog are: "
                     86:        echo " "
                     87:        echo "  -d|--debug            Enables debugging output"
                     88:        echo " "
                     89:        echo "  -f|--database <file>  Writes the database into file specified by <file>"
                     90:        echo " "
                     91:        echo "  -h|--help             Displays this help message"
                     92:        echo " "
                     93:        echo "  -p|--packages <dir>   Specifies the top level directory to be searched"
                     94:        echo "                        for binary packages."
                     95:        echo " "
                     96:        echo "  -v|--version          Displays the version of this script and exits."
                     97:        echo " "
                     98:        echo "Example:    $prog -v --packages /usr/pkgsrc/packages"
                     99:        echo " "
                    100: }
                    101: 
                    102: clean_and_exit0(){
                    103:        rm -fr ${tmpd}
                    104:        exit 0
                    105: }
                    106: 
                    107: clean_and_exit1(){
                    108:        rm -fr ${tmpd}
                    109:        exit 1
                    110: }
                    111: 
                    112: all_cache_files=""
                    113: 
                    114: process_binpkg_dir(){
                    115:        all_cache_files="${all_cache_files} ${d}/${cachefile}"
                    116: 
                    117:        need_update=no
                    118:        if test -f ${d}/${cachefile} ; then
1.3     ! dmcmahil  119:                stale_entries=`${FIND} ${d} -type f -name \*${PKG_SUFX} -newer ${d}/${cachefile} -print`
1.1       dmcmahil  120: 
                    121:                # FIX_ME
                    122:                #
                    123:                # We also should find cache entries for files which no longer exist
                    124:                # and nuke them.  Right now we simply declare the entire cache out
                    125:                # of date.  Once we implement incremental updates to the cache,
                    126:                # we need to remove the entries but not mark the entire cache as
                    127:                # bad.
                    128:                if test "X${DEBUG}" = "Xyes" ; then
                    129:                        echo "      Checking for cache entries with no corresponding pkg."
                    130:                fi
                    131:                # get the list of what pkgs belong in the cache
                    132:                rm -f ${tmpd}/pkg_list ${tmpd}/cache_pkg_list
1.3     ! dmcmahil  133:                ${FIND} ${d}/ -name \*${PKG_SUFX} -print | \
        !           134:                        ${SED} -e "s;^${PACKAGES}/*;;g" -e 's;//;/;g' | \
        !           135:                        ${SORT} > ${tmpd}/pkg_list
1.1       dmcmahil  136: 
                    137:                # and get the list of what is in the cache
                    138:                ${AWK} '/pkgcache_begin/ {gsub(/pkgcache_begin[ \t]*/, ""); print}' \
                    139:                        ${d}/${cachefile} | ${SORT} > ${tmpd}/cache_pkg_list
                    140: 
                    141:                if ${CMP} -s ${tmpd}/pkg_list ${tmpd}/cache_pkg_list ; then
                    142:                        if test "X${DEBUG}" = "Xyes" ; then
                    143:                                echo "      No extra cache entries in ${d}/${cachefile}"
                    144:                        fi
                    145:                else
                    146:                        echo "      Entries found in ${d}/${cachefile} but no packages found"
                    147:                        need_update=yes
                    148:                fi
                    149:        else
                    150:                stale_entries=""
                    151:        fi
                    152: 
                    153:        if test "X${force}" = "Xyes" -o "X${need_update}" = "Xyes" ; then
                    154:                need_update=yes
                    155:                echo "${tab}Forcing rebuild of cache ${d}/${cachefile}."
                    156:        elif test ! -f ${d}/${cachefile} ; then
                    157:                need_update=yes
                    158:                echo "${tab}Missing cache file.  ${d}/${cachefile} will be generated."
                    159:        elif test -n "${stale_entries}" ; then
                    160:                need_update=yes
                    161:                echo "${tab}Stale cache file.  ${d}/${cachefile} will be regenerated."
                    162:        else
                    163:                ${GREP} "pkgcache_version ${cacheversion}" ${d}/${cachefile} >/dev/null 2>&1
                    164:                if test $? -ne 0 ; then
                    165:                        need_update=yes
                    166:                        echo "${tab}Old version cache file.  ${d}/${cachefile} will be regenerated."
                    167:                else
                    168:                        echo "${tab}Cache file ${d}/${cachefile} is up to date."
                    169:                fi
                    170:        fi
                    171: 
                    172:        # FIX_ME
                    173:        # We should use stale_entries in a way where we only update the 
                    174:        # cache file entries corresponding to these if we're rebuilding
                    175:        # due to stale entries.  That should save a good bit of time.
                    176:        # 
                    177:        if test "X${need_update}" = "Xyes" ; then
                    178:                echo "pkgcache_version ${cacheversion}" > ${tmpd}/${cachefile}
                    179:                for f in ${d}/*${PKG_SUFX} ; do
                    180:                        fn=`echo $f | ${SED} "s;^${PACKAGES}/*;;g"`
                    181:                        if test "X${DEBUG}" = "Xyes" ; then
                    182:                                echo "     Adding ${fn} (${f}) to the cache"
                    183:                        fi
                    184:                        echo " " >> ${tmpd}/${cachefile}
                    185:                        # stat(1) needs to be added to the bootstrap kit
                    186:                        # first if we want to use it here
                    187:                        #eval $(${STAT} -s ${f} 2>/dev/null)
                    188:                        echo "pkgcache_begin ${fn}" >> ${tmpd}/${cachefile}
                    189:                        #echo "pkgcache_mtime=${st_mtime}" >> ${tmpd}/${cachefile}
                    190:                        if test "X${DEBUG}" = "Xyes" ; then
                    191:                                echo "${PKG_INFO} -q -B ${f}"
                    192:                        fi
                    193:                        ${PKG_INFO} -q -B ${f} >> ${tmpd}/${cachefile}
                    194:                        echo "pkgcache_end ${fn}" >> ${tmpd}/${cachefile}
                    195:                done
                    196:                mv ${tmpd}/${cachefile} ${d}/${cachefile}
1.2       dmcmahil  197:                if test $? -ne 0 ; then
                    198:                        echo "move of ${tmpd}/${cachefile} to ${d}/${cachefile} failed!"
                    199:                        echo "Perhaps you do not have write permissions to ${d}?"
                    200:                        clean_and_exit1
                    201:                fi
1.1       dmcmahil  202:        fi
                    203: 
                    204: 
                    205: }
                    206: 
                    207: process_cache_files(){
                    208:        echo "${prompt}Checking master cache file ${PACKAGES}/${cachefile}"
                    209:        echo "pkgcache_version ${cacheversion}" > ${tmpd}/${cachefile}
                    210:        if test -n "${all_cache_files}" ; then
                    211:                for c in ${all_cache_files} ; do
                    212:                        echo "pkgcache_cachefile ${c}" >> ${tmpd}/${cachefile}
                    213:                done
                    214:        fi
                    215:        if test ! -f ${PACKAGES}/${cachefile} ; then
                    216:                echo "${tab}Creating master cache file ${PACKAGES}/${cachefile}"
1.2       dmcmahil  217:                mv ${tmpd}/${cachefile} ${PACKAGES}/${cachefile}
                    218:                if test $? -ne 0 ; then
                    219:                        echo "move of ${tmpd}/${cachefile} to ${PACKAGES}/${cachefile} failed!"
                    220:                        echo "Perhaps you do not have write permissions to ${PACKAGES}?"
                    221:                        clean_and_exit1
                    222:                fi
                    223:        elif ${CMP} -s ${tmpd}/${cachefile} ${PACKAGES}/${cachefile} ; then
1.1       dmcmahil  224:                echo "${tab}Master cache file ${PACKAGES}/${cachefile} is up to date"
                    225:        else
                    226:                echo "${tab}Updating master cache file ${PACKAGES}/${cachefile}"
                    227:                mv ${tmpd}/${cachefile} ${PACKAGES}/${cachefile}
1.2       dmcmahil  228:                if test $? -ne 0 ; then
                    229:                        echo "move of ${tmpd}/${cachefile} to ${PACKAGES}/${cachefile} failed!"
                    230:                        echo "Perhaps you do not have write permissions to ${PACKAGES}?"
                    231:                        clean_and_exit1
                    232:                fi
1.1       dmcmahil  233:        fi
                    234: }
                    235: 
                    236: ######################################################################
                    237: #
                    238: #  Handle command line options
                    239: #
                    240: ######################################################################
                    241: 
                    242: DEBUG=no
                    243: verbose=no
                    244: force=no
                    245: 
                    246: while
                    247:        test -n "$1"
                    248: do
                    249:        case "$1" in
                    250: 
                    251:                # Turn on debugging
                    252:                -d|--debug)
                    253:                        DEBUG=yes
                    254:                        verbose=yes
                    255:                        shift
                    256:                        ;;
                    257: 
                    258:                # Force a rebuilde of the cache
                    259:                -F|--force)
                    260:                        force=yes
                    261:                        shift
                    262:                        ;;
                    263:                # Help
                    264:                -h|--help)
                    265:                        usage
                    266:                        exit 0
                    267:                        ;;
                    268: 
                    269:                # Use the specified packages directory
                    270:                -p|--packages)
                    271:                        PACKAGES=$2
                    272:                        shift 2
                    273:                        ;;
                    274: 
                    275:                # Version
                    276:                -V|--version)
                    277:                        ${AWK} '/^#[ \t]*\$NetBSD/ {gsub(/,v/,"",$3);printf("%s:  Version %s, %s\n",$3,$4,$5); exit 0;}' $prog
                    278:                        exit 0
                    279:                        ;;
                    280: 
                    281:                # Turn on verbose output, but not as noisy as DEBUG
                    282:                -v|--verbose)
                    283:                        verbose=yes
                    284:                        shift
                    285:                        ;;
                    286: 
                    287:                -*) echo "$prog:  ERROR:  $1 is not a valid option"
                    288:                        usage
                    289:                        clean_and_exit1
                    290:                        ;;
                    291: 
                    292:                *) 
                    293:                        break
                    294:                        ;;
                    295: 
                    296:        esac
                    297: done
                    298: 
                    299: # put a trailing / after ${PACKAGES} in case ${PACKAGES} is 
                    300: # a link.
1.3     ! dmcmahil  301: for d in `${FIND} ${PACKAGES}/ -type d -print` ; do
1.1       dmcmahil  302:        if test "X${DEBUG}" = "Xyes" ; then
                    303:                echo "${prompt}Processing directory ${d}"
                    304:        fi
                    305:        is_pkg_dir=no
                    306:        for f in ${d}/*${PKG_SUFX} ; do
                    307:                if test -f "${f}" -a ! -h "${f}" ; then
                    308:                        ${PKG_INFO} ${f} >/dev/null 2>&1
                    309:                        if test $? -eq 0 ; then
                    310:                                is_pkg_dir=yes
                    311:                                break
                    312:                        fi
                    313:                fi
                    314:        done
                    315:        if test "X${is_pkg_dir}" = "Xyes" ; then
                    316:                echo "${prompt}Checking cache in ${d}"
                    317:                process_binpkg_dir
                    318:        else
                    319:                if test "X${DEBUG}" = "Xyes" ; then
                    320:                        echo "${prompt}no binary packages in ${d}"
                    321:                fi
                    322:        fi
                    323:                
                    324: done
                    325: 
                    326: process_cache_files
                    327: 
                    328: clean_and_exit0
                    329: 

CVSweb <webmaster@jp.NetBSD.org>