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