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

1.1       gdt         1: #!/bin/sh
                      2: 
1.12    ! tnn         3: # $NetBSD: pkg_rolling-replace.sh,v 1.11 2007/08/03 00:55:09 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: 
1.4       tnn        67: test -z "$MAKECONF" && MAKECONF="@MAKECONF@"
1.7       tnn        68: test -f "$MAKECONF" && test -z "$PKGSRCDIR" && PKGSRCDIR="` \
                     69:     printf '.include "%s"\n_print_pkgsrcdir:\n\t@echo "${PKGSRCDIR}"\n' \
1.8       tnn        70:     "$MAKECONF" | "$MAKE" -f - BSD_PKG_MK=1 _print_pkgsrcdir`"
1.3       tnn        71: test -z "$PKGSRCDIR" && PKGSRCDIR=/usr/pkgsrc
1.12    ! tnn        72: test -z "$PKG_CHK" && PKG_CHK="@PKG_CHK@"
        !            73: test -z "$PKG_INFO" && PKG_INFO="@PKG_INFO_CMD@"
        !            74: test -z "$PKG_ADMIN" && PKG_ADMIN="@PKG_ADMIN@"
1.1       gdt        75: 
                     76: unset PKG_PATH || true  #or pkgsrc makefiles will complain
                     77: 
                     78: usage()
                     79: {
                     80:     echo "Usage: pkg_rolling-replace [opts]
                     81:         -h         This help
                     82:         -n         Don't actually do make replace
                     83:         -u         Update mismatched packages
                     84:         -v         Verbose
                     85:         -x <pkg>   exclude <pkg> from update check
                     86: 
                     87: pkg_rolling-replace does 'make replace' on one package at a time,
                     88: tsorting the packages being replaced according to their
                     89: interdependencies, which avoids most duplicate rebuilds.
                     90: 
                     91: pkg_rolling-replace can be used in one of two ways:
                     92: 
                     93:     - 'make replace' is unsafe in that, if the replaced package's ABI
                     94:       changes, its dependent packages may break.  If this happens, run
                     95:       'pkg_rolling-replace' (no arguments) to rebuild them against the
                     96:       new version.
                     97: 
                     98:     - 'pkg_chk -u' will delete all your mismatched (outdated)
                     99:       packages, then reinstall them one at a time, leaving you without
                    100:       those packages in the meantime.  'pkg_rolling-replace -u' will
                    101:       instead upgrade them in place, allowing you to keep using your
                    102:       system in the meantime (maybe...if you're lucky...because
                    103:       pkg_rolling-replace replaces the \"deepest\" dependency first,
                    104:       things could still break if that happens to be a fundamental
                    105:       library whose ABI has changed).
                    106: "
                    107:     exit 1
                    108: }
                    109: 
                    110: # pkg_rolling-replace's output is intermingled with pkgsrc make
                    111: # output.  Thus, we use a different prefix.  pkgsrc uses '===>' for
                    112: # major steps, and '=>' for minor ones.  Thus, we use 'RR>'.
                    113: 
                    114: OPI='RR>'
                    115: OPC='rr>' # continuation
                    116: 
                    117: 
                    118: # Echo the names of packages needing updates, versions stripped.  This
                    119: # has been tested with pkg_chk 1.76.  Older versions are not
                    120: # supported.  Newer versions may or may not work (patches welcome).
                    121: check_packages_mismatched()
                    122: {
1.12    ! tnn       123:     ${PKG_CHK} -u -q | while read line; do
1.1       gdt       124:         # duplicate output of pkg_chk to stderr (bypass $(...) or `...`)
1.11      tnn       125:         echo "${OPC} $line" 1>&2
1.1       gdt       126:        # Look for the first thing that looks like pkg-version rather
                    127:        # than category/pkg and remove the version.
                    128:         for word in $line; do
1.5       tnn       129:             if [ "$(echo $word | egrep '^[^/]+-[0-9][^-/]*$')" ]; then
                    130:                 echo $word | sed 's/-[0-9][^-]*$//'
1.1       gdt       131:                 break  #done with this line
                    132:             fi
                    133:         done
                    134:     done
                    135: }
                    136: 
                    137: # usage: check_packages_w_flag flag
                    138: # echo packages with flag=YES
                    139: check_packages_w_flag()
                    140: {
                    141:     _flag=$1; shift
1.12    ! tnn       142:     for pkgver in $(${PKG_INFO} -e '*'); do
        !           143:         if ${PKG_INFO} -Bq $pkgver \
1.1       gdt       144:                 | egrep "^$_flag=[Yy][Ee][Ss]" > /dev/null; then
1.5       tnn       145:             echo $pkgver | sed 's/-[0-9][^-]*$//'
1.1       gdt       146:         fi
                    147:     done
                    148: }
                    149: 
                    150: # echo dep->pkg edges for all installed packages
                    151: depgraph_installed()
                    152: {
1.12    ! tnn       153:     for pkgver in $(${PKG_INFO} -e '*'); do
1.5       tnn       154:         pkg=$(echo $pkgver | sed 's/-[0-9][^-]*$//')
1.1       gdt       155:        # Include $pkg as a node without dependencies in case it has none.
                    156:         echo $pkg $pkg
1.12    ! tnn       157:         for depver in $(${PKG_INFO} -Nq $pkg); do
1.5       tnn       158:             dep=$(echo $depver | sed 's/-[0-9][^-]*$//')
1.1       gdt       159:             echo $dep $pkg
                    160:         done
                    161:     done
                    162: }
                    163: 
                    164: # usage: who_requires pkg --in-graph DEPGRAPH
                    165: who_requires()
                    166: {
                    167:     _target=$1; shift
                    168:     test "$1" = '--in-graph' && shift || return $((false))
                    169: 
                    170:     while [ $# -gt 0 ]; do
                    171:         _dep=$1; _pkg=$2; shift 2;
                    172:         if [ $_dep = $_target -a $_dep != $_pkg ]; then
                    173:             echo $_pkg
                    174:         fi
                    175:     done
                    176: }
                    177: 
                    178: # usage: is_member x list
                    179: # return true (0) if x appears in list, false (1) otherwise
                    180: is_member()
                    181: {
                    182:     _x="$1"; shift;
                    183:     while [ $# -gt 0 -a "$1" != "$_x" ]; do
                    184:         shift
                    185:     done
                    186: 
                    187:     test $# -gt 0
                    188: }
                    189: 
                    190: # usage: exclude list1 --from list2
                    191: # echo list2 with items from list1 excluded
                    192: exclude()
                    193: {
                    194:     _excl=
                    195:     while [ $# -gt 0 -a $1 != "--from" ]; do
                    196:         _excl="$_excl $1"
                    197:         shift
                    198:     done
                    199:     if [ $# -gt 0 ]; then
                    200:         shift  #discard '--from'
                    201:     fi
                    202:     while [ $# -gt 0 ]; do
                    203:         is_member $1 $_excl || echo $1
                    204:         shift
                    205:     done
                    206: }
                    207: 
                    208: # usage: uniquify list
                    209: # just does sort -u on list (equiv. to sort | uniq)
                    210: # CAUTION: reorders list
                    211: uniqify()
                    212: {
                    213:     for _x in $*; do echo $_x; done | sort -u
                    214: }
                    215: 
                    216: verbose()
                    217: {
                    218:     if [ -n "$opt_v" ]; then
                    219:         echo "$@"
                    220:     fi
                    221: }
                    222: 
                    223: vsleep()
                    224: {
                    225:     if [ -n "$opt_v" ]; then
                    226:         sleep $1
                    227:     fi
                    228: }
                    229: 
                    230: ######################################################################
                    231: ##
                    232: ## main()
                    233: ##
                    234: 
                    235: EXCLUDE=
                    236: 
                    237: args=$(getopt hnuvx: $*)
                    238: if [ $? -ne 0 ]; then
                    239:     opt_h=1
                    240: fi
                    241: set -- $args
                    242: while [ $# -gt 0 ]; do
                    243:     case "$1" in
                    244:         -h) opt_h=1 ;;
                    245:         -n) opt_n=1 ;;
                    246:         -u) opt_u=1 ;;
                    247:         -v) opt_v=1 ;;
                    248:         -x) EXCLUDE="$EXCLUDE $(echo $2 | sed 's/,/ /g')" ; shift ;;
                    249:         --) shift; break ;;
                    250:     esac
                    251:     shift
                    252: done
                    253: 
                    254: if [ -n "$opt_h" ]; then
                    255:     usage
                    256: fi
                    257: 
                    258: MISMATCH_TODO=
                    259: if [ -n "$opt_u" ]; then
                    260:     echo "${OPI} Checking for mismatched installed packages using pkg_chk"
                    261:     MISMATCH_TODO=$(check_packages_mismatched)
                    262:     echo "${OPI} Excluding the following mismatched packages:"
                    263:     echo "${OPC} EXCLUDE=[$EXCLUDE]"
                    264:     MISMATCH_TODO=$(exclude $EXCLUDE --from $MISMATCH_TODO)
                    265: fi
                    266: 
                    267: echo "${OPI} Checking for rebuild-requested installed packages (rebuild=YES)"
                    268: REBUILD_TODO=$(check_packages_w_flag 'rebuild')
                    269: 
                    270: echo "${OPI} Checking for unsafe installed packages (unsafe_depends=YES)"
                    271: UNSAFE_TODO=$(check_packages_w_flag 'unsafe_depends')
                    272: 
                    273: # DEPGRAPH_INSTALLED is rebuilt each round.  DEPGRAPH_SRC will collect
                    274: # edges that we discover using 'make show-depends', but that weren't
                    275: # listed as depends by the installed version of a package, and
                    276: # DEPENDS_CHECKED lists packages for which we've already done that
                    277: # check.
                    278: DEPGRAPH_INSTALLED=
                    279: DEPGRAPH_SRC=
                    280: DEPENDS_CHECKED=
                    281: 
                    282: verbose "${OPI} Packages to rebuild:"
                    283: verbose "${OPC} MISMATCH_TODO=[$(echo $MISMATCH_TODO)]"  #strip newlines
                    284: verbose "${OPC} REBUILD_TODO=[$(echo $REBUILD_TODO)]"
                    285: verbose "${OPC} UNSAFE_TODO=[$(echo $UNSAFE_TODO)]"
                    286: vsleep 2
                    287: 
                    288: REPLACE_TODO=$(uniqify $MISMATCH_TODO $REBUILD_TODO $UNSAFE_TODO)
                    289: depgraph_built=0
                    290: 
                    291: while [ -n "$REPLACE_TODO" ]; do
                    292:     # don't rebuild depgraph if we continued from new-depends step below
                    293:     if [ "$depgraph_built" -eq 0 ]; then
                    294:         echo "${OPI} Building dependency graph for installed packages"
                    295:         DEPGRAPH_INSTALLED=$(depgraph_installed)
                    296:         depgraph_built=1
                    297:     fi
                    298: 
                    299:     echo "${OPI} Tsorting dependency graph"
                    300:     TSORTED=$(echo $DEPGRAPH_INSTALLED $DEPGRAPH_SRC | tsort)
                    301:     for pkg in $TSORTED; do
                    302:         if is_member $pkg $REPLACE_TODO; then
                    303:             break;
                    304:         fi
                    305:     done
1.12    ! tnn       306:     pkgdir=$(${PKG_INFO} -Bq $pkg | awk -F= '/PKGPATH=/{print $2}')
1.1       gdt       307:     echo "${OPI} Selecting $pkg ($pkgdir) as next package to replace"
                    308:     sleep 1
                    309: 
                    310:     # Newer versions in pkgsrc sometimes have more depends than are
                    311:     # recorded for the installed version (this is entirely to be
                    312:     # expected as packages are split, renamed, and add new features).
                    313:     # When this happens, add the new edges to the graph and re-tsort.
                    314:     # Don't re-tsort if the new edges were already installed once
                    315:     # (otherwise we'll go into a loop).
                    316: 
                    317:     # XXX I'm not sure that DEPENDS_CHECKED is necessary, or whether
                    318:     # simply ignoring new deps that aren't in $REPLACE_TODO would have
                    319:     # been sufficient.  The DEPENDS_CHECKED approach causes one extra
                    320:     # tsort in some cases, which isn't overly expensive.
                    321: 
                    322:     # XXX After tsorting and choosing what to 'make replace', we
                    323:     # filter out packages that aren't already installed and marked for
                    324:     # replacement.  This is safe, since uninstalled depends will be
                    325:     # pulled in automatically by 'make replace'; but in rare cases it
                    326:     # might be non-optimal, leading to a duplicate rebuild that we
                    327:     # would have avoided had we selected the uninstalled package in
                    328:     # tsorted order and run 'make install' on it.  This seems like
                    329:     # such a rare case that the added complexity isn't worth it.
                    330: 
                    331:     if ! is_member $pkg $DEPENDS_CHECKED; then
                    332:        echo "${OPI} Checking if $pkg has new depends..."
1.12    ! tnn       333:        OLD_DEPENDS=$(${PKG_INFO} -Nq $pkg | sed 's/-[0-9][^-]*$//')
1.1       gdt       334:        NEW_DEPENDS=
                    335:        cd "$PKGSRCDIR/$pkgdir"
                    336:        bdeps=$(${MAKE} show-depends VARNAME=BUILD_DEPENDS)
                    337:        rdeps=$(${MAKE} show-depends)
                    338:        for depver in $bdeps $rdeps; do
1.10      tnn       339:            dep=$(echo $depver | sed -e 's/[:[].*$/0/' -e 's/[<>]=/-/' \
                    340:                -e 's/-[0-9][^-]*$//')
1.1       gdt       341:            if ! is_member $dep $OLD_DEPENDS $NEW_DEPENDS; then
                    342:                NEW_DEPENDS="$NEW_DEPENDS $dep"
                    343:                DEPGRAPH_SRC="$DEPGRAPH_SRC $dep $pkg"
                    344:            fi
                    345:        done
                    346:        DEPENDS_CHECKED="$DEPENDS_CHECKED $pkg"
                    347:        if [ -n "$NEW_DEPENDS" ]; then
                    348:            echo "${OPI} $pkg has the following new depends (need to re-tsort):"
                    349:            echo "${OPC} [$(echo $NEW_DEPENDS)]"
                    350:            sleep 2
                    351:            continue
                    352:        fi
                    353:     fi
                    354: 
                    355:     # Do make replace, with clean before, and package and clean afterwards.
1.12    ! tnn       356:     echo "${OPI} Replacing $(${PKG_INFO} -e $pkg)"
1.1       gdt       357:     FAIL=
1.12    ! tnn       358:     cmd="cd \"$PKGSRCDIR/$pkgdir\" \
        !           359:            && ${MAKE} clean && ${MAKE} replace \
        !           360:            && ([ -z \"$(${PKG_INFO} -Q unsafe_depends $pkg)\" ] \
        !           361:                || ${PKG_ADMIN} unset unsafe_depends $pkg) \
        !           362:            && ([ -z \"$(${PKG_INFO} -Q rebuild $pkg)\" ] \
        !           363:                || ${PKG_ADMIN} unset rebuild $pkg) \
        !           364:            && ${MAKE} package && ${MAKE} clean \
        !           365:            || FAIL=1"
1.1       gdt       366:     if [ -z "$opt_n" ]; then
1.12    ! tnn       367:        eval "$cmd"
1.1       gdt       368:     else
1.12    ! tnn       369:        echo "$cmd"
1.1       gdt       370:     fi
                    371:     if [ -n "$FAIL" ]; then
                    372:         echo "*** 'make replace' failed for package $pkg."
                    373:         echo "*** Please read the errors listed above, fix the problem,"
                    374:         echo "*** then re-run pkg_rolling-replace to continue."
                    375:         exit 1
                    376:     fi
                    377:     sleep 1
                    378: 
                    379:     # remove just-replaced package from all *_TODO lists
                    380:     MISMATCH_TODO=$(exclude $pkg --from $MISMATCH_TODO)
                    381:     REBUILD_TODO=$(exclude $pkg --from $REBUILD_TODO)
                    382:     UNSAFE_TODO=$(exclude $pkg --from $UNSAFE_TODO)
                    383: 
                    384:     echo "${OPI} Re-checking for unsafe installed packages (unsafe_depends=YES)"
                    385:     if [ -n "$opt_n" ]; then
                    386:        # With -n, the replace didn't happen, and thus the packages that would
                    387:        # have been marked unsafe_depends=YES were not.  Add the set that
                    388:        # would have been marked so we can watch what pkg_rolling-replace
                    389:        # would have done (approximately).
                    390:         UNSAFE_TODO=$(uniqify $UNSAFE_TODO \
                    391:             $(who_requires $pkg --in-graph $DEPGRAPH_INSTALLED))
                    392:         sleep 1
                    393:     else
                    394:         UNSAFE_TODO=$(check_packages_w_flag 'unsafe_depends')
                    395:     fi
                    396: 
                    397:     verbose "${OPI} Packages to rebuild:"
                    398:     verbose "${OPC} MISMATCH_TODO=[$(echo $MISMATCH_TODO)]"  #strips newlines
                    399:     verbose "${OPC} REBUILD_TODO=[$(echo $REBUILD_TODO)]"
                    400:     verbose "${OPC} UNSAFE_TODO=[$(echo $UNSAFE_TODO)]"
                    401:     vsleep 4
                    402: 
                    403:     REPLACE_TODO=$(uniqify $MISMATCH_TODO $REBUILD_TODO $UNSAFE_TODO)
                    404:     depgraph_built=0
                    405: done
                    406: 
                    407: echo "${OPI} No more packages to replace; done."

CVSweb <webmaster@jp.NetBSD.org>