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