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

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

CVSweb <webmaster@jp.NetBSD.org>