Annotation of pkgsrc/pkgtools/pkg_rolling-replace/files/pkg_rolling-replace.sh, revision 1.6

1.1       gdt         1: #!/bin/sh
                      2: 
1.6     ! tnn         3: # $NetBSD: pkg_rolling-replace.sh,v 1.5 2007/05/20 13:35:56 tnn Exp $
1.1       gdt         4: #<license>
                      5: # Copyright (c) 2006 BBN Technologies Corp.  All rights reserved.
                      6: #
                      7: # Redistribution and use in source and binary forms, with or without
                      8: # modification, are permitted provided that the following conditions
                      9: # are met:
                     10: # 1. Redistributions of source code must retain the above copyright
                     11: #    notice, this list of conditions and the following disclaimer.
                     12: # 2. Redistributions in binary form must reproduce the above copyright
                     13: #    notice, this list of conditions and the following disclaimer in the
                     14: #    documentation and/or other materials provided with the distribution.
                     15: # 3. Neither the name of BBN Technologies nor the names of its contributors
                     16: #    may be used to endorse or promote products derived from this software
                     17: #    without specific prior written permission.
                     18: #
                     19: # THIS SOFTWARE IS PROVIDED BY BBN TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
                     20: # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21: # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22: # ARE DISCLAIMED.  IN NO EVENT SHALL BBN TECHNOLOGIES OR CONTRIBUTORS BE
                     23: # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     24: # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     25: # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     26: # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     27: # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     28: # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     29: # POSSIBILITY OF SUCH DAMAGE.
                     30: #
                     31: # Effort sponsored in part by the Defense Advanced Research Projects
                     32: # Agency (DARPA) and the Department of the Interior National Business
                     33: # Center under agreement number NBCHC050166.
                     34: #</license>
                     35: 
                     36: # REVIEW NOTATION LEGEND
                     37: #   XXX marks things that are broken/kludgy
                     38: #   TODO marks things that need to be done
                     39: #   FIXED marks items fixed, needs review and deletion of FIXED comment
                     40: 
                     41: # Items to be completed for initial release:
                     42: 
                     43: # TODO: move bulk of help into a real man page.
                     44: 
                     45: # Items for future work (some imply changes in pkgsrc, not necessarily here)
                     46: #
                     47: #   Make a pass over all packages for build depends, and rememeber
                     48: # which packages have been so checked.  Check newly unsafe packages.
                     49: # The current lazy evaluation can perhaps fail to do things in the
                     50: # right order.  Alternatively, explain why the current scheme is safe
                     51: # and preferred.
                     52: #
                     53: #   Consider expansion to update via binary packages.  This raises
                     54: # interesting questions about ABIs and binary builds
                     55: #
                     56: #   Deal with conflicts in a smarter way, perhaps more automatically.
                     57: # This is hard because just removing packages is not ok.
                     58: #
                     59: #   Handle foo being split into foo and foo-share.  Now, make replace
                     60: # of foo pulls in foo-share which conflicts with foo.  This needlessly
                     61: # loses.
                     62: #
                     63: 
1.3       tnn        64: # Substituted by pkgsrc at pre-configure time.
1.4       tnn        65: MAKE="@MAKE@"
1.3       tnn        66: 
                     67: test -z "$PKG_DBDIR" && PKG_DBDIR=/var/db/pkg
1.4       tnn        68: test -z "$MAKECONF" && MAKECONF="@MAKECONF@"
                     69: test -f "$MAKECONF" && test -z "$PKGSRCDIR" && \
1.6     ! tnn        70:     PKGSRCDIR="`(cat "$MAKECONF"; \
        !            71:     printf '\n_print_pkgsrcdir:\n\t@echo "${PKGSRCDIR}"\n') | \
        !            72:     "$MAKE" BSD_PKG_MK=1 -f - _print_pkgsrcdir`"
1.3       tnn        73: test -z "$PKGSRCDIR" && PKGSRCDIR=/usr/pkgsrc
1.1       gdt        74: 
                     75: unset PKG_PATH || true  #or pkgsrc makefiles will complain
                     76: 
                     77: usage()
                     78: {
                     79:     echo "Usage: pkg_rolling-replace [opts]
                     80:         -h         This help
                     81:         -n         Don't actually do make replace
                     82:         -u         Update mismatched packages
                     83:         -v         Verbose
                     84:         -x <pkg>   exclude <pkg> from update check
                     85: 
                     86: pkg_rolling-replace does 'make replace' on one package at a time,
                     87: tsorting the packages being replaced according to their
                     88: interdependencies, which avoids most duplicate rebuilds.
                     89: 
                     90: pkg_rolling-replace can be used in one of two ways:
                     91: 
                     92:     - 'make replace' is unsafe in that, if the replaced package's ABI
                     93:       changes, its dependent packages may break.  If this happens, run
                     94:       'pkg_rolling-replace' (no arguments) to rebuild them against the
                     95:       new version.
                     96: 
                     97:     - 'pkg_chk -u' will delete all your mismatched (outdated)
                     98:       packages, then reinstall them one at a time, leaving you without
                     99:       those packages in the meantime.  'pkg_rolling-replace -u' will
                    100:       instead upgrade them in place, allowing you to keep using your
                    101:       system in the meantime (maybe...if you're lucky...because
                    102:       pkg_rolling-replace replaces the \"deepest\" dependency first,
                    103:       things could still break if that happens to be a fundamental
                    104:       library whose ABI has changed).
                    105: "
                    106:     exit 1
                    107: }
                    108: 
                    109: # pkg_rolling-replace's output is intermingled with pkgsrc make
                    110: # output.  Thus, we use a different prefix.  pkgsrc uses '===>' for
                    111: # major steps, and '=>' for minor ones.  Thus, we use 'RR>'.
                    112: 
                    113: OPI='RR>'
                    114: OPC='rr>' # continuation
                    115: 
                    116: 
                    117: # Echo the names of packages needing updates, versions stripped.  This
                    118: # has been tested with pkg_chk 1.76.  Older versions are not
                    119: # supported.  Newer versions may or may not work (patches welcome).
                    120: check_packages_mismatched()
                    121: {
                    122:     pkg_chk -u -q | while read line; do
                    123:         # duplicate output of pkg_chk to stderr (bypass $(...) or `...`)
                    124:         echo "${OPC} $line" > /dev/stderr
                    125:        # Look for the first thing that looks like pkg-version rather
                    126:        # than category/pkg and remove the version.
                    127:         for word in $line; do
1.5       tnn       128:             if [ "$(echo $word | egrep '^[^/]+-[0-9][^-/]*$')" ]; then
                    129:                 echo $word | sed 's/-[0-9][^-]*$//'
1.1       gdt       130:                 break  #done with this line
                    131:             fi
                    132:         done
                    133:     done
                    134: }
                    135: 
                    136: # usage: check_packages_w_flag flag
                    137: # echo packages with flag=YES
                    138: check_packages_w_flag()
                    139: {
                    140:     _flag=$1; shift
                    141:     for pkgver in $(pkg_info -e '*'); do
                    142:         if pkg_info -Bq $pkgver \
                    143:                 | egrep "^$_flag=[Yy][Ee][Ss]" > /dev/null; then
1.5       tnn       144:             echo $pkgver | sed 's/-[0-9][^-]*$//'
1.1       gdt       145:         fi
                    146:     done
                    147: }
                    148: 
                    149: # echo dep->pkg edges for all installed packages
                    150: depgraph_installed()
                    151: {
                    152:     for pkgver in $(pkg_info -e '*'); do
1.5       tnn       153:         pkg=$(echo $pkgver | sed 's/-[0-9][^-]*$//')
1.1       gdt       154:        # Include $pkg as a node without dependencies in case it has none.
                    155:         echo $pkg $pkg
                    156:         for depver in $(pkg_info -Nq $pkg); do
1.5       tnn       157:             dep=$(echo $depver | sed 's/-[0-9][^-]*$//')
1.1       gdt       158:             echo $dep $pkg
                    159:         done
                    160:     done
                    161: }
                    162: 
                    163: # usage: who_requires pkg --in-graph DEPGRAPH
                    164: who_requires()
                    165: {
                    166:     _target=$1; shift
                    167:     test "$1" = '--in-graph' && shift || return $((false))
                    168: 
                    169:     while [ $# -gt 0 ]; do
                    170:         _dep=$1; _pkg=$2; shift 2;
                    171:         if [ $_dep = $_target -a $_dep != $_pkg ]; then
                    172:             echo $_pkg
                    173:         fi
                    174:     done
                    175: }
                    176: 
                    177: # usage: is_member x list
                    178: # return true (0) if x appears in list, false (1) otherwise
                    179: is_member()
                    180: {
                    181:     _x="$1"; shift;
                    182:     while [ $# -gt 0 -a "$1" != "$_x" ]; do
                    183:         shift
                    184:     done
                    185: 
                    186:     test $# -gt 0
                    187: }
                    188: 
                    189: # usage: exclude list1 --from list2
                    190: # echo list2 with items from list1 excluded
                    191: exclude()
                    192: {
                    193:     _excl=
                    194:     while [ $# -gt 0 -a $1 != "--from" ]; do
                    195:         _excl="$_excl $1"
                    196:         shift
                    197:     done
                    198:     if [ $# -gt 0 ]; then
                    199:         shift  #discard '--from'
                    200:     fi
                    201:     while [ $# -gt 0 ]; do
                    202:         is_member $1 $_excl || echo $1
                    203:         shift
                    204:     done
                    205: }
                    206: 
                    207: # usage: uniquify list
                    208: # just does sort -u on list (equiv. to sort | uniq)
                    209: # CAUTION: reorders list
                    210: uniqify()
                    211: {
                    212:     for _x in $*; do echo $_x; done | sort -u
                    213: }
                    214: 
                    215: verbose()
                    216: {
                    217:     if [ -n "$opt_v" ]; then
                    218:         echo "$@"
                    219:     fi
                    220: }
                    221: 
                    222: vsleep()
                    223: {
                    224:     if [ -n "$opt_v" ]; then
                    225:         sleep $1
                    226:     fi
                    227: }
                    228: 
                    229: ######################################################################
                    230: ##
                    231: ## main()
                    232: ##
                    233: 
                    234: EXCLUDE=
                    235: 
                    236: args=$(getopt hnuvx: $*)
                    237: if [ $? -ne 0 ]; then
                    238:     opt_h=1
                    239: fi
                    240: set -- $args
                    241: while [ $# -gt 0 ]; do
                    242:     case "$1" in
                    243:         -h) opt_h=1 ;;
                    244:         -n) opt_n=1 ;;
                    245:         -u) opt_u=1 ;;
                    246:         -v) opt_v=1 ;;
                    247:         -x) EXCLUDE="$EXCLUDE $(echo $2 | sed 's/,/ /g')" ; shift ;;
                    248:         --) shift; break ;;
                    249:     esac
                    250:     shift
                    251: done
                    252: 
                    253: if [ -n "$opt_h" ]; then
                    254:     usage
                    255: fi
                    256: 
                    257: MISMATCH_TODO=
                    258: if [ -n "$opt_u" ]; then
                    259:     echo "${OPI} Checking for mismatched installed packages using pkg_chk"
                    260:     MISMATCH_TODO=$(check_packages_mismatched)
                    261:     echo "${OPI} Excluding the following mismatched packages:"
                    262:     echo "${OPC} EXCLUDE=[$EXCLUDE]"
                    263:     MISMATCH_TODO=$(exclude $EXCLUDE --from $MISMATCH_TODO)
                    264: fi
                    265: 
                    266: echo "${OPI} Checking for rebuild-requested installed packages (rebuild=YES)"
                    267: REBUILD_TODO=$(check_packages_w_flag 'rebuild')
                    268: 
                    269: echo "${OPI} Checking for unsafe installed packages (unsafe_depends=YES)"
                    270: UNSAFE_TODO=$(check_packages_w_flag 'unsafe_depends')
                    271: 
                    272: # DEPGRAPH_INSTALLED is rebuilt each round.  DEPGRAPH_SRC will collect
                    273: # edges that we discover using 'make show-depends', but that weren't
                    274: # listed as depends by the installed version of a package, and
                    275: # DEPENDS_CHECKED lists packages for which we've already done that
                    276: # check.
                    277: DEPGRAPH_INSTALLED=
                    278: DEPGRAPH_SRC=
                    279: DEPENDS_CHECKED=
                    280: 
                    281: verbose "${OPI} Packages to rebuild:"
                    282: verbose "${OPC} MISMATCH_TODO=[$(echo $MISMATCH_TODO)]"  #strip newlines
                    283: verbose "${OPC} REBUILD_TODO=[$(echo $REBUILD_TODO)]"
                    284: verbose "${OPC} UNSAFE_TODO=[$(echo $UNSAFE_TODO)]"
                    285: vsleep 2
                    286: 
                    287: REPLACE_TODO=$(uniqify $MISMATCH_TODO $REBUILD_TODO $UNSAFE_TODO)
                    288: depgraph_built=0
                    289: 
                    290: while [ -n "$REPLACE_TODO" ]; do
                    291:     # don't rebuild depgraph if we continued from new-depends step below
                    292:     if [ "$depgraph_built" -eq 0 ]; then
                    293:         echo "${OPI} Building dependency graph for installed packages"
                    294:         DEPGRAPH_INSTALLED=$(depgraph_installed)
                    295:         depgraph_built=1
                    296:     fi
                    297: 
                    298:     echo "${OPI} Tsorting dependency graph"
                    299:     TSORTED=$(echo $DEPGRAPH_INSTALLED $DEPGRAPH_SRC | tsort)
                    300:     for pkg in $TSORTED; do
                    301:         if is_member $pkg $REPLACE_TODO; then
                    302:             break;
                    303:         fi
                    304:     done
                    305:     pkgdir=$(pkg_info -Bq $pkg | awk -F= '/PKGPATH=/{print $2}')
                    306:     echo "${OPI} Selecting $pkg ($pkgdir) as next package to replace"
                    307:     sleep 1
                    308: 
                    309:     # Newer versions in pkgsrc sometimes have more depends than are
                    310:     # recorded for the installed version (this is entirely to be
                    311:     # expected as packages are split, renamed, and add new features).
                    312:     # When this happens, add the new edges to the graph and re-tsort.
                    313:     # Don't re-tsort if the new edges were already installed once
                    314:     # (otherwise we'll go into a loop).
                    315: 
                    316:     # XXX I'm not sure that DEPENDS_CHECKED is necessary, or whether
                    317:     # simply ignoring new deps that aren't in $REPLACE_TODO would have
                    318:     # been sufficient.  The DEPENDS_CHECKED approach causes one extra
                    319:     # tsort in some cases, which isn't overly expensive.
                    320: 
                    321:     # XXX After tsorting and choosing what to 'make replace', we
                    322:     # filter out packages that aren't already installed and marked for
                    323:     # replacement.  This is safe, since uninstalled depends will be
                    324:     # pulled in automatically by 'make replace'; but in rare cases it
                    325:     # might be non-optimal, leading to a duplicate rebuild that we
                    326:     # would have avoided had we selected the uninstalled package in
                    327:     # tsorted order and run 'make install' on it.  This seems like
                    328:     # such a rare case that the added complexity isn't worth it.
                    329: 
                    330:     if ! is_member $pkg $DEPENDS_CHECKED; then
                    331:        echo "${OPI} Checking if $pkg has new depends..."
1.5       tnn       332:        OLD_DEPENDS=$(pkg_info -Nq $pkg | sed 's/-[0-9][^-]*$//')
1.1       gdt       333:        NEW_DEPENDS=
                    334:        cd "$PKGSRCDIR/$pkgdir"
                    335:        bdeps=$(${MAKE} show-depends VARNAME=BUILD_DEPENDS)
                    336:        rdeps=$(${MAKE} show-depends)
                    337:        for depver in $bdeps $rdeps; do
1.5       tnn       338:            dep=$(echo $depver | sed -E -e 's/[:[].*$/0/' \
                    339:                -e 's/(>=|<=|-)[0-9][^-]*$//')
1.1       gdt       340:            if ! is_member $dep $OLD_DEPENDS $NEW_DEPENDS; then
                    341:                NEW_DEPENDS="$NEW_DEPENDS $dep"
                    342:                DEPGRAPH_SRC="$DEPGRAPH_SRC $dep $pkg"
                    343:            fi
                    344:        done
                    345:        DEPENDS_CHECKED="$DEPENDS_CHECKED $pkg"
                    346:        if [ -n "$NEW_DEPENDS" ]; then
                    347:            echo "${OPI} $pkg has the following new depends (need to re-tsort):"
                    348:            echo "${OPC} [$(echo $NEW_DEPENDS)]"
                    349:            sleep 2
                    350:            continue
                    351:        fi
                    352:     fi
                    353: 
                    354:     # Do make replace, with clean before, and package and clean afterwards.
                    355:     echo "${OPI} Replacing $(pkg_info -e $pkg)"
                    356:     FAIL=
                    357:     if [ -z "$opt_n" ]; then
                    358:        cd "$PKGSRCDIR/$pkgdir" \
                    359:            && ${MAKE} clean && ${MAKE} replace && ${MAKE} package && ${MAKE} clean \
                    360:            || FAIL=1
                    361:     else
                    362:        echo "cd $PKGSRCDIR/$pkgdir && ${MAKE} clean && ${MAKE} replace && ${MAKE} package && ${MAKE} clean"
                    363:     fi
                    364:     if [ -n "$FAIL" ]; then
                    365:         echo "*** 'make replace' failed for package $pkg."
                    366:         echo "*** Please read the errors listed above, fix the problem,"
                    367:         echo "*** then re-run pkg_rolling-replace to continue."
                    368:         exit 1
                    369:     fi
                    370:     sleep 1
                    371: 
                    372:     # remove just-replaced package from all *_TODO lists
                    373:     MISMATCH_TODO=$(exclude $pkg --from $MISMATCH_TODO)
                    374:     REBUILD_TODO=$(exclude $pkg --from $REBUILD_TODO)
                    375:     UNSAFE_TODO=$(exclude $pkg --from $UNSAFE_TODO)
                    376: 
                    377:     echo "${OPI} Re-checking for unsafe installed packages (unsafe_depends=YES)"
                    378:     if [ -n "$opt_n" ]; then
                    379:        # With -n, the replace didn't happen, and thus the packages that would
                    380:        # have been marked unsafe_depends=YES were not.  Add the set that
                    381:        # would have been marked so we can watch what pkg_rolling-replace
                    382:        # would have done (approximately).
                    383:         UNSAFE_TODO=$(uniqify $UNSAFE_TODO \
                    384:             $(who_requires $pkg --in-graph $DEPGRAPH_INSTALLED))
                    385:         sleep 1
                    386:     else
                    387:         UNSAFE_TODO=$(check_packages_w_flag 'unsafe_depends')
                    388:     fi
                    389: 
                    390:     verbose "${OPI} Packages to rebuild:"
                    391:     verbose "${OPC} MISMATCH_TODO=[$(echo $MISMATCH_TODO)]"  #strips newlines
                    392:     verbose "${OPC} REBUILD_TODO=[$(echo $REBUILD_TODO)]"
                    393:     verbose "${OPC} UNSAFE_TODO=[$(echo $UNSAFE_TODO)]"
                    394:     vsleep 4
                    395: 
                    396:     REPLACE_TODO=$(uniqify $MISMATCH_TODO $REBUILD_TODO $UNSAFE_TODO)
                    397:     depgraph_built=0
                    398: done
                    399: 
                    400: echo "${OPI} No more packages to replace; done."

CVSweb <webmaster@jp.NetBSD.org>