Annotation of pkgsrc/mk/scripts/binpkg-cache, revision 1.7
1.1 dmcmahil 1: #!/bin/sh
2: #
1.7 ! dmcmahil 3: # $NetBSD: binpkg-cache,v 1.6 2005/05/24 21:48:22 dmcmahill Exp $
1.1 dmcmahil 4: #
5: # Script for generating a cache file with information about
6: # all binary packages contained in a directory.
7: #
1.7 ! dmcmahil 8: # Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
1.1 dmcmahil 9: # All rights reserved.
10: #
11: # This code is derived from software contributed to The NetBSD Foundation
12: # by Dan McMahill.
13: #
14: # Redistribution and use in source and binary forms, with or without
15: # modification, are permitted provided that the following conditions
16: # are met:
17: # 1. Redistributions of source code must retain the above copyright
18: # notice, this list of conditions and the following disclaimer.
19: # 2. Redistributions in binary form must reproduce the above copyright
20: # notice, this list of conditions and the following disclaimer in the
21: # documentation and/or other materials provided with the distribution.
22: # 3. All advertising materials mentioning features or use of this software
23: # must display the following acknowledgement:
24: # This product includes software developed by the NetBSD
25: # Foundation, Inc. and its contributors.
26: # 4. Neither the name of The NetBSD Foundation nor the names of its
27: # contributors may be used to endorse or promote products derived
28: # from this software without specific prior written permission.
29: #
30: # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31: # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32: # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33: # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34: # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35: # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36: # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37: # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38: # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39: # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40: # POSSIBILITY OF SUCH DAMAGE.
41: #
42:
43:
44: TMPDIR=${TMPDIR:-/tmp}
45: PACKAGES=${PACKAGES:-/usr/pkgsrc/packages/}
46: AWK=${AWK:-awk}
47: CMP=${CMP:-cmp}
1.3 dmcmahil 48: FIND=${FIND:-find}
1.1 dmcmahil 49: GREP=${GREP:-grep}
50: PKG_INFO=${PKG_INFO:-pkg_info}
51: PKG_SUFX=${PKG_SUFX:-.tgz}
52: SED=${SED:-sed}
53: SORT=${SORT:-sort}
54: STAT=${STAT:-stat}
55:
56: cachefile=.pkgcache
57: cacheversion=20050428
58:
59: prompt="----> "
60: tab=" "
61:
62: tmpd=${TMPDIR}/pkg-cache.$$
63: mkdir -m 0700 ${tmpd}
64: if test $? -ne 0 ; then
65: echo "ERROR: Could not create temporary directory ${tmpd}"
66: echo "Either you do not have write permission to ${tmpd} or"
67: echo "${tmpd} already exists"
68: exit 1
69: fi
70:
71: prog=$0
72:
73: usage(){
74: echo "$prog - Generates cache files for each directory containing binary"
75: echo " packages. This cache file can then be used by the README.html"
76: echo " generation code to avoid having to call pkg_info(1) over and over"
77: echo " on the same binary package."
78: echo " "
79: echo "Usage: $prog [-d|--debug] [-v|--verbose] [-p|--packages <dir>]"
80: echo " "
81: echo " $prog -h|--help"
82: echo " "
83: echo " $prog -V|--version"
84: echo " "
85: echo "The options supported by $prog are: "
86: echo " "
87: echo " -d|--debug Enables debugging output"
88: echo " "
89: echo " -f|--database <file> Writes the database into file specified by <file>"
90: echo " "
91: echo " -h|--help Displays this help message"
92: echo " "
93: echo " -p|--packages <dir> Specifies the top level directory to be searched"
94: echo " for binary packages."
95: echo " "
96: echo " -v|--version Displays the version of this script and exits."
97: echo " "
1.7 ! dmcmahil 98: echo "Returns 0 on success, 1 on errors, 2 if the packages"
! 99: echo "directory does not exist."
! 100: echo " "
1.1 dmcmahil 101: echo "Example: $prog -v --packages /usr/pkgsrc/packages"
102: echo " "
103: }
104:
105: clean_and_exit0(){
106: rm -fr ${tmpd}
107: exit 0
108: }
109:
110: clean_and_exit1(){
111: rm -fr ${tmpd}
112: exit 1
113: }
114:
1.7 ! dmcmahil 115: clean_and_exit2(){
! 116: rm -fr ${tmpd}
! 117: exit 2
! 118: }
! 119:
1.1 dmcmahil 120: all_cache_files=""
121:
1.4 dmcmahil 122: #############################################################################
123: #
124: # process_binpkg_dir()
125: #
126: # Process a directory by checking to see if a cache file exists. If the
127: # cache file exists, make sure it is up to date. If the file does not
128: # exist, create one.
129: #
130: # also keep track of this directory so it can be added to the master
131: # cache.
132: #
133:
1.1 dmcmahil 134: process_binpkg_dir(){
135: need_update=no
136: if test -f ${d}/${cachefile} ; then
1.3 dmcmahil 137: stale_entries=`${FIND} ${d} -type f -name \*${PKG_SUFX} -newer ${d}/${cachefile} -print`
1.1 dmcmahil 138:
139: # FIX_ME
140: #
141: # We also should find cache entries for files which no longer exist
142: # and nuke them. Right now we simply declare the entire cache out
143: # of date. Once we implement incremental updates to the cache,
144: # we need to remove the entries but not mark the entire cache as
145: # bad.
146: if test "X${DEBUG}" = "Xyes" ; then
147: echo " Checking for cache entries with no corresponding pkg."
148: fi
149: # get the list of what pkgs belong in the cache
150: rm -f ${tmpd}/pkg_list ${tmpd}/cache_pkg_list
1.3 dmcmahil 151: ${FIND} ${d}/ -name \*${PKG_SUFX} -print | \
152: ${SED} -e "s;^${PACKAGES}/*;;g" -e 's;//;/;g' | \
153: ${SORT} > ${tmpd}/pkg_list
1.1 dmcmahil 154:
155: # and get the list of what is in the cache
156: ${AWK} '/pkgcache_begin/ {gsub(/pkgcache_begin[ \t]*/, ""); print}' \
157: ${d}/${cachefile} | ${SORT} > ${tmpd}/cache_pkg_list
158:
159: if ${CMP} -s ${tmpd}/pkg_list ${tmpd}/cache_pkg_list ; then
160: if test "X${DEBUG}" = "Xyes" ; then
161: echo " No extra cache entries in ${d}/${cachefile}"
162: fi
163: else
164: echo " Entries found in ${d}/${cachefile} but no packages found"
165: need_update=yes
166: fi
167: else
168: stale_entries=""
169: fi
170:
171: if test "X${force}" = "Xyes" -o "X${need_update}" = "Xyes" ; then
172: need_update=yes
173: echo "${tab}Forcing rebuild of cache ${d}/${cachefile}."
174: elif test ! -f ${d}/${cachefile} ; then
175: need_update=yes
176: echo "${tab}Missing cache file. ${d}/${cachefile} will be generated."
177: elif test -n "${stale_entries}" ; then
178: need_update=yes
179: echo "${tab}Stale cache file. ${d}/${cachefile} will be regenerated."
180: else
181: ${GREP} "pkgcache_version ${cacheversion}" ${d}/${cachefile} >/dev/null 2>&1
182: if test $? -ne 0 ; then
183: need_update=yes
1.5 dmcmahil 184: echo "${tab}Invalid version cache file. ${d}/${cachefile} will be regenerated."
185: echo "Need version ${cacheversion} but the file has"
186: ${GREP} "^pkgcache_version " ${d}/${cachefile}
1.1 dmcmahil 187: else
188: echo "${tab}Cache file ${d}/${cachefile} is up to date."
189: fi
190: fi
191:
192: # FIX_ME
193: # We should use stale_entries in a way where we only update the
194: # cache file entries corresponding to these if we're rebuilding
195: # due to stale entries. That should save a good bit of time.
196: #
197: if test "X${need_update}" = "Xyes" ; then
198: echo "pkgcache_version ${cacheversion}" > ${tmpd}/${cachefile}
199: for f in ${d}/*${PKG_SUFX} ; do
200: fn=`echo $f | ${SED} "s;^${PACKAGES}/*;;g"`
201: if test "X${DEBUG}" = "Xyes" ; then
202: echo " Adding ${fn} (${f}) to the cache"
203: fi
204: echo " " >> ${tmpd}/${cachefile}
205: # stat(1) needs to be added to the bootstrap kit
206: # first if we want to use it here
207: #eval $(${STAT} -s ${f} 2>/dev/null)
208: echo "pkgcache_begin ${fn}" >> ${tmpd}/${cachefile}
209: #echo "pkgcache_mtime=${st_mtime}" >> ${tmpd}/${cachefile}
210: if test "X${DEBUG}" = "Xyes" ; then
211: echo "${PKG_INFO} -q -B ${f}"
212: fi
213: ${PKG_INFO} -q -B ${f} >> ${tmpd}/${cachefile}
214: echo "pkgcache_end ${fn}" >> ${tmpd}/${cachefile}
215: done
1.4 dmcmahil 216: mv -f ${tmpd}/${cachefile} ${d}/${cachefile}
1.2 dmcmahil 217: if test $? -ne 0 ; then
1.4 dmcmahil 218: echo "********** WARNING **********"
1.2 dmcmahil 219: echo "move of ${tmpd}/${cachefile} to ${d}/${cachefile} failed!"
220: echo "Perhaps you do not have write permissions to ${d}?"
1.4 dmcmahil 221: echo "This directory will be dropped from the master cache file."
222: echo "********** WARNING **********"
223: return
1.2 dmcmahil 224: fi
1.1 dmcmahil 225: fi
226:
1.4 dmcmahil 227: # if we got here, then this directory should have a good cache file in
228: # it and we should be able to add it to the master cache file
229: all_cache_files="${all_cache_files} ${d}/${cachefile}"
1.1 dmcmahil 230:
231: }
232:
233: process_cache_files(){
234: echo "${prompt}Checking master cache file ${PACKAGES}/${cachefile}"
235: echo "pkgcache_version ${cacheversion}" > ${tmpd}/${cachefile}
236: if test -n "${all_cache_files}" ; then
237: for c in ${all_cache_files} ; do
238: echo "pkgcache_cachefile ${c}" >> ${tmpd}/${cachefile}
239: done
240: fi
241: if test ! -f ${PACKAGES}/${cachefile} ; then
242: echo "${tab}Creating master cache file ${PACKAGES}/${cachefile}"
1.4 dmcmahil 243: mv -f ${tmpd}/${cachefile} ${PACKAGES}/${cachefile}
1.2 dmcmahil 244: if test $? -ne 0 ; then
1.4 dmcmahil 245: echo "********** ERROR **********"
1.2 dmcmahil 246: echo "move of ${tmpd}/${cachefile} to ${PACKAGES}/${cachefile} failed!"
247: echo "Perhaps you do not have write permissions to ${PACKAGES}?"
1.4 dmcmahil 248: echo "********** ERROR **********"
1.2 dmcmahil 249: clean_and_exit1
250: fi
251: elif ${CMP} -s ${tmpd}/${cachefile} ${PACKAGES}/${cachefile} ; then
1.1 dmcmahil 252: echo "${tab}Master cache file ${PACKAGES}/${cachefile} is up to date"
253: else
254: echo "${tab}Updating master cache file ${PACKAGES}/${cachefile}"
1.4 dmcmahil 255: mv -f ${tmpd}/${cachefile} ${PACKAGES}/${cachefile}
1.2 dmcmahil 256: if test $? -ne 0 ; then
1.4 dmcmahil 257: echo "********** ERROR **********"
1.2 dmcmahil 258: echo "move of ${tmpd}/${cachefile} to ${PACKAGES}/${cachefile} failed!"
259: echo "Perhaps you do not have write permissions to ${PACKAGES}?"
1.4 dmcmahil 260: echo "********** ERROR **********"
1.2 dmcmahil 261: clean_and_exit1
262: fi
1.1 dmcmahil 263: fi
264: }
265:
266: ######################################################################
267: #
268: # Handle command line options
269: #
270: ######################################################################
271:
272: DEBUG=no
273: verbose=no
274: force=no
275:
276: while
277: test -n "$1"
278: do
279: case "$1" in
280:
281: # Turn on debugging
282: -d|--debug)
283: DEBUG=yes
284: verbose=yes
285: shift
286: ;;
287:
288: # Force a rebuilde of the cache
289: -F|--force)
290: force=yes
291: shift
292: ;;
293: # Help
294: -h|--help)
295: usage
296: exit 0
297: ;;
298:
299: # Use the specified packages directory
300: -p|--packages)
301: PACKAGES=$2
302: shift 2
303: ;;
304:
305: # Version
306: -V|--version)
307: ${AWK} '/^#[ \t]*\$NetBSD/ {gsub(/,v/,"",$3);printf("%s: Version %s, %s\n",$3,$4,$5); exit 0;}' $prog
308: exit 0
309: ;;
310:
311: # Turn on verbose output, but not as noisy as DEBUG
312: -v|--verbose)
313: verbose=yes
314: shift
315: ;;
316:
317: -*) echo "$prog: ERROR: $1 is not a valid option"
318: usage
319: clean_and_exit1
320: ;;
321:
322: *)
323: break
324: ;;
325:
326: esac
327: done
328:
1.6 dmcmahil 329: if test $# -ne 0 ; then
330: echo "$0: $* is invalid"
331: usage
332: clean_and_exit1
333: fi
334:
335: if test ! -d ${PACKAGES} ; then
336: echo "Packages directory ${PACKAGES} seems to be missing"
1.7 ! dmcmahil 337: clean_and_exit2
1.6 dmcmahil 338: fi
339:
1.1 dmcmahil 340: # put a trailing / after ${PACKAGES} in case ${PACKAGES} is
341: # a link.
1.3 dmcmahil 342: for d in `${FIND} ${PACKAGES}/ -type d -print` ; do
1.1 dmcmahil 343: if test "X${DEBUG}" = "Xyes" ; then
344: echo "${prompt}Processing directory ${d}"
345: fi
346: is_pkg_dir=no
347: for f in ${d}/*${PKG_SUFX} ; do
348: if test -f "${f}" -a ! -h "${f}" ; then
349: ${PKG_INFO} ${f} >/dev/null 2>&1
350: if test $? -eq 0 ; then
351: is_pkg_dir=yes
352: break
353: fi
354: fi
355: done
356: if test "X${is_pkg_dir}" = "Xyes" ; then
357: echo "${prompt}Checking cache in ${d}"
358: process_binpkg_dir
359: else
360: if test "X${DEBUG}" = "Xyes" ; then
361: echo "${prompt}no binary packages in ${d}"
362: fi
363: fi
364:
365: done
366:
367: process_cache_files
368:
369: clean_and_exit0
370:
CVSweb <webmaster@jp.NetBSD.org>