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

1.1       dmcmahil    1: #!/bin/sh
                      2: #
1.2     ! dmcmahil    3: # $NetBSD: binpkg-cache,v 1.1 2005/05/17 21:46:59 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}
                     48: GREP=${GREP:-grep}
                     49: PKG_INFO=${PKG_INFO:-pkg_info}
                     50: PKG_SUFX=${PKG_SUFX:-.tgz}
                     51: SED=${SED:-sed}
                     52: SORT=${SORT:-sort}
                     53: STAT=${STAT:-stat}
                     54: 
                     55: cachefile=.pkgcache
                     56: cacheversion=20050428
                     57: 
                     58: prompt="----> "
                     59: tab="      "
                     60: 
                     61: tmpd=${TMPDIR}/pkg-cache.$$
                     62: mkdir -m 0700 ${tmpd}
                     63: if test $? -ne 0 ; then
                     64:        echo "ERROR:  Could not create temporary directory ${tmpd}"
                     65:        echo "Either you do not have write permission to ${tmpd} or"
                     66:        echo "${tmpd} already exists"
                     67:        exit 1
                     68: fi
                     69: 
                     70: prog=$0
                     71: 
                     72: usage(){
                     73:        echo "$prog - Generates cache files for each directory containing binary"
                     74:        echo "        packages.  This cache file can then be used by the README.html"
                     75:        echo "        generation code to avoid having to call pkg_info(1) over and over"
                     76:        echo "        on the same binary package."
                     77:        echo " "
                     78:        echo "Usage:      $prog [-d|--debug] [-v|--verbose] [-p|--packages <dir>]"
                     79:        echo " "
                     80:        echo "            $prog -h|--help"
                     81:        echo " "
                     82:        echo "            $prog -V|--version"
                     83:        echo " "
                     84:        echo "The options supported by $prog are: "
                     85:        echo " "
                     86:        echo "  -d|--debug            Enables debugging output"
                     87:        echo " "
                     88:        echo "  -f|--database <file>  Writes the database into file specified by <file>"
                     89:        echo " "
                     90:        echo "  -h|--help             Displays this help message"
                     91:        echo " "
                     92:        echo "  -p|--packages <dir>   Specifies the top level directory to be searched"
                     93:        echo "                        for binary packages."
                     94:        echo " "
                     95:        echo "  -v|--version          Displays the version of this script and exits."
                     96:        echo " "
                     97:        echo "Example:    $prog -v --packages /usr/pkgsrc/packages"
                     98:        echo " "
                     99: }
                    100: 
                    101: clean_and_exit0(){
                    102:        rm -fr ${tmpd}
                    103:        exit 0
                    104: }
                    105: 
                    106: clean_and_exit1(){
                    107:        rm -fr ${tmpd}
                    108:        exit 1
                    109: }
                    110: 
                    111: all_cache_files=""
                    112: 
                    113: process_binpkg_dir(){
                    114:        all_cache_files="${all_cache_files} ${d}/${cachefile}"
                    115: 
                    116:        need_update=no
                    117:        if test -f ${d}/${cachefile} ; then
                    118:                stale_entries=`find ${d} -type f -name \*${PKG_SUFX} -newer ${d}/${cachefile} -print`
                    119: 
                    120:                # FIX_ME
                    121:                #
                    122:                # We also should find cache entries for files which no longer exist
                    123:                # and nuke them.  Right now we simply declare the entire cache out
                    124:                # of date.  Once we implement incremental updates to the cache,
                    125:                # we need to remove the entries but not mark the entire cache as
                    126:                # bad.
                    127:                if test "X${DEBUG}" = "Xyes" ; then
                    128:                        echo "      Checking for cache entries with no corresponding pkg."
                    129:                fi
                    130:                # get the list of what pkgs belong in the cache
                    131:                rm -f ${tmpd}/pkg_list ${tmpd}/cache_pkg_list
                    132:                ls ${d}/*${PKG_SUFX} | ${SED} "s;^${PACKAGES}/*;;g" | ${SORT} > ${tmpd}/pkg_list
                    133: 
                    134:                # and get the list of what is in the cache
                    135:                ${AWK} '/pkgcache_begin/ {gsub(/pkgcache_begin[ \t]*/, ""); print}' \
                    136:                        ${d}/${cachefile} | ${SORT} > ${tmpd}/cache_pkg_list
                    137: 
                    138:                if ${CMP} -s ${tmpd}/pkg_list ${tmpd}/cache_pkg_list ; then
                    139:                        if test "X${DEBUG}" = "Xyes" ; then
                    140:                                echo "      No extra cache entries in ${d}/${cachefile}"
                    141:                        fi
                    142:                else
                    143:                        echo "      Entries found in ${d}/${cachefile} but no packages found"
                    144:                        need_update=yes
                    145:                fi
                    146:        else
                    147:                stale_entries=""
                    148:        fi
                    149: 
                    150:        if test "X${force}" = "Xyes" -o "X${need_update}" = "Xyes" ; then
                    151:                need_update=yes
                    152:                echo "${tab}Forcing rebuild of cache ${d}/${cachefile}."
                    153:        elif test ! -f ${d}/${cachefile} ; then
                    154:                need_update=yes
                    155:                echo "${tab}Missing cache file.  ${d}/${cachefile} will be generated."
                    156:        elif test -n "${stale_entries}" ; then
                    157:                need_update=yes
                    158:                echo "${tab}Stale cache file.  ${d}/${cachefile} will be regenerated."
                    159:        else
                    160:                ${GREP} "pkgcache_version ${cacheversion}" ${d}/${cachefile} >/dev/null 2>&1
                    161:                if test $? -ne 0 ; then
                    162:                        need_update=yes
                    163:                        echo "${tab}Old version cache file.  ${d}/${cachefile} will be regenerated."
                    164:                else
                    165:                        echo "${tab}Cache file ${d}/${cachefile} is up to date."
                    166:                fi
                    167:        fi
                    168: 
                    169:        # FIX_ME
                    170:        # We should use stale_entries in a way where we only update the 
                    171:        # cache file entries corresponding to these if we're rebuilding
                    172:        # due to stale entries.  That should save a good bit of time.
                    173:        # 
                    174:        if test "X${need_update}" = "Xyes" ; then
                    175:                echo "pkgcache_version ${cacheversion}" > ${tmpd}/${cachefile}
                    176:                for f in ${d}/*${PKG_SUFX} ; do
                    177:                        fn=`echo $f | ${SED} "s;^${PACKAGES}/*;;g"`
                    178:                        if test "X${DEBUG}" = "Xyes" ; then
                    179:                                echo "     Adding ${fn} (${f}) to the cache"
                    180:                        fi
                    181:                        echo " " >> ${tmpd}/${cachefile}
                    182:                        # stat(1) needs to be added to the bootstrap kit
                    183:                        # first if we want to use it here
                    184:                        #eval $(${STAT} -s ${f} 2>/dev/null)
                    185:                        echo "pkgcache_begin ${fn}" >> ${tmpd}/${cachefile}
                    186:                        #echo "pkgcache_mtime=${st_mtime}" >> ${tmpd}/${cachefile}
                    187:                        if test "X${DEBUG}" = "Xyes" ; then
                    188:                                echo "${PKG_INFO} -q -B ${f}"
                    189:                        fi
                    190:                        ${PKG_INFO} -q -B ${f} >> ${tmpd}/${cachefile}
                    191:                        echo "pkgcache_end ${fn}" >> ${tmpd}/${cachefile}
                    192:                done
                    193:                mv ${tmpd}/${cachefile} ${d}/${cachefile}
1.2     ! dmcmahil  194:                if test $? -ne 0 ; then
        !           195:                        echo "move of ${tmpd}/${cachefile} to ${d}/${cachefile} failed!"
        !           196:                        echo "Perhaps you do not have write permissions to ${d}?"
        !           197:                        clean_and_exit1
        !           198:                fi
1.1       dmcmahil  199:        fi
                    200: 
                    201: 
                    202: }
                    203: 
                    204: process_cache_files(){
                    205:        echo "${prompt}Checking master cache file ${PACKAGES}/${cachefile}"
                    206:        echo "pkgcache_version ${cacheversion}" > ${tmpd}/${cachefile}
                    207:        if test -n "${all_cache_files}" ; then
                    208:                for c in ${all_cache_files} ; do
                    209:                        echo "pkgcache_cachefile ${c}" >> ${tmpd}/${cachefile}
                    210:                done
                    211:        fi
                    212:        if test ! -f ${PACKAGES}/${cachefile} ; then
                    213:                echo "${tab}Creating master cache file ${PACKAGES}/${cachefile}"
1.2     ! dmcmahil  214:                mv ${tmpd}/${cachefile} ${PACKAGES}/${cachefile}
        !           215:                if test $? -ne 0 ; then
        !           216:                        echo "move of ${tmpd}/${cachefile} to ${PACKAGES}/${cachefile} failed!"
        !           217:                        echo "Perhaps you do not have write permissions to ${PACKAGES}?"
        !           218:                        clean_and_exit1
        !           219:                fi
        !           220:        elif ${CMP} -s ${tmpd}/${cachefile} ${PACKAGES}/${cachefile} ; then
1.1       dmcmahil  221:                echo "${tab}Master cache file ${PACKAGES}/${cachefile} is up to date"
                    222:        else
                    223:                echo "${tab}Updating master cache file ${PACKAGES}/${cachefile}"
                    224:                mv ${tmpd}/${cachefile} ${PACKAGES}/${cachefile}
1.2     ! dmcmahil  225:                if test $? -ne 0 ; then
        !           226:                        echo "move of ${tmpd}/${cachefile} to ${PACKAGES}/${cachefile} failed!"
        !           227:                        echo "Perhaps you do not have write permissions to ${PACKAGES}?"
        !           228:                        clean_and_exit1
        !           229:                fi
1.1       dmcmahil  230:        fi
                    231: }
                    232: 
                    233: ######################################################################
                    234: #
                    235: #  Handle command line options
                    236: #
                    237: ######################################################################
                    238: 
                    239: DEBUG=no
                    240: verbose=no
                    241: force=no
                    242: 
                    243: while
                    244:        test -n "$1"
                    245: do
                    246:        case "$1" in
                    247: 
                    248:                # Turn on debugging
                    249:                -d|--debug)
                    250:                        DEBUG=yes
                    251:                        verbose=yes
                    252:                        shift
                    253:                        ;;
                    254: 
                    255:                # Force a rebuilde of the cache
                    256:                -F|--force)
                    257:                        force=yes
                    258:                        shift
                    259:                        ;;
                    260:                # Help
                    261:                -h|--help)
                    262:                        usage
                    263:                        exit 0
                    264:                        ;;
                    265: 
                    266:                # Use the specified packages directory
                    267:                -p|--packages)
                    268:                        PACKAGES=$2
                    269:                        shift 2
                    270:                        ;;
                    271: 
                    272:                # Version
                    273:                -V|--version)
                    274:                        ${AWK} '/^#[ \t]*\$NetBSD/ {gsub(/,v/,"",$3);printf("%s:  Version %s, %s\n",$3,$4,$5); exit 0;}' $prog
                    275:                        exit 0
                    276:                        ;;
                    277: 
                    278:                # Turn on verbose output, but not as noisy as DEBUG
                    279:                -v|--verbose)
                    280:                        verbose=yes
                    281:                        shift
                    282:                        ;;
                    283: 
                    284:                -*) echo "$prog:  ERROR:  $1 is not a valid option"
                    285:                        usage
                    286:                        clean_and_exit1
                    287:                        ;;
                    288: 
                    289:                *) 
                    290:                        break
                    291:                        ;;
                    292: 
                    293:        esac
                    294: done
                    295: 
                    296: # put a trailing / after ${PACKAGES} in case ${PACKAGES} is 
                    297: # a link.
                    298: for d in `find ${PACKAGES}/ -type d -print` ; do
                    299:        if test "X${DEBUG}" = "Xyes" ; then
                    300:                echo "${prompt}Processing directory ${d}"
                    301:        fi
                    302:        is_pkg_dir=no
                    303:        for f in ${d}/*${PKG_SUFX} ; do
                    304:                if test -f "${f}" -a ! -h "${f}" ; then
                    305:                        ${PKG_INFO} ${f} >/dev/null 2>&1
                    306:                        if test $? -eq 0 ; then
                    307:                                is_pkg_dir=yes
                    308:                                break
                    309:                        fi
                    310:                fi
                    311:        done
                    312:        if test "X${is_pkg_dir}" = "Xyes" ; then
                    313:                echo "${prompt}Checking cache in ${d}"
                    314:                process_binpkg_dir
                    315:        else
                    316:                if test "X${DEBUG}" = "Xyes" ; then
                    317:                        echo "${prompt}no binary packages in ${d}"
                    318:                fi
                    319:        fi
                    320:                
                    321: done
                    322: 
                    323: process_cache_files
                    324: 
                    325: clean_and_exit0
                    326: 

CVSweb <webmaster@jp.NetBSD.org>