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