pkgsrc-wip/pkg_summary-utils/files/pkg_update_src_summary.in
Aleksey Cheusov b40eddbe0b New variable to use in awk_condition: fields.
It works when field to match is `.' .
Man page update, tests.
2010-05-30 16:07:13 +00:00

225 lines
5.1 KiB
Bash
Executable file

#!@SH@
#-*-mode: sh -*-
# Copyright (c) 2007-2008 Aleksey Cheusov <vle@gmx.net>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
set -e
. pipestatus
############################################################
# user settable variables
PKGSRCDIR=${PKGSRCDIR:=@PKGSRCDIR@}
BMAKE=${BMAKE:=@BMAKE@}
############################################################
usage (){
cat 1>&2 <<EOF
pkg_update_src_summary - incrementally updates
package summary from pkgsrc/ tree (pkg_summary(5) format)
usage: pkg_update_src_summary [OPTIONS] summary_file
OPTIONS:
-h|--help display this help message
-f|--file <filename> obtain package names from
a specified file, - for stdin
-i|--fields <filename>
-i|--fields=<filename> list of fields (separated by space character
or comma) to be included to the summary,
full summary is generated by default
EOF
}
pkgs_fn=''
while test $# -ne 0; do
case "$1" in
-h|--help)
usage
exit 0;;
-f=*)
printf '%s\n' '-f= is not allowed' 1>&2
exit 1;;
-i=*)
printf '%s\n' '-f= is not allowed' 1>&2
exit 1;;
-f|--file)
pkgs_fn="$2"
shift;;
--file=*)
pkgs_fn="`printf '%s\n' \"$1\" | sed 's,^[^=]*=,,'`";;
-f*)
pkgs_fn="`printf '%s\n' \"$1\" | cut -b3-`";;
-i|--fields)
fields="-f '$2'"
shift;;
--fields=*)
fields="-f '`echo $1 | cut -f2 -d=`'";;
-i*)
fields="-f '`printf '%s\n' \"$1\" | cut -b 3-`'";;
--)
shift
break;;
-*)
echo "Bad option $1" 1>&2
exit 1;;
*)
break
esac
shift
done
if test $# -ne 1; then
usage
exit 1
fi
summary=$1
shift
############################################################
cd $PKGSRCDIR
new_summary=$summary.new
tmp_dir="/tmp/pkg_update_src_summary.$$"
trap "rm -rf $tmp_dir $new_summary" 0 1 2 15
mkdir -m 700 "$tmp_dir"
allpkgs_fn=$tmp_dir/allpkgs
if test "$pkgs_fn"; then
awk '{sub(/#.*/, "")} NF > 0 {print $1}' $pkgs_fn > $allpkgs_fn
else
pkg_list_all_pkgs > $allpkgs_fn
fi
############################################################
#### process packages
# src_summary from scratch
if ! test -f $summary; then
eval pkg_src_summary $fields < $allpkgs_fn > $new_summary
mv $new_summary $summary
exit $?
fi
# micro summary
micsum_fn=$tmp_dir/micsum
pkg_micro_src_summary < $allpkgs_fn >$micsum_fn
# summary against microsummary
cmp_fn=$tmp_dir/cmp
pkg_cmp_summary -p $summary $micsum_fn > $cmp_fn
# copying summary about unchanged packages
eq_pkgs_fn=$tmp_dir/eqpkgs
awk '$1 ~ /^[=]/ {print $2 ":" $3}' $cmp_fn > $eq_pkgs_fn # pkgpath:pkgbase
copy_eq_pkgs (){
env eq_pkgs_fn="$eq_pkgs_fn" runawk -e '
#use "xgetline.awk"
#use "pkg_grep_summary.awk"
BEGIN {
eq_pkgs_fn = ENVIRON ["eq_pkgs_fn"]
FS = " "
while (xgetline0(eq_pkgs_fn)){
eq_pkgs [$1] = 1
}
FS = "="
# keep all fields in associative array "fields"
keep_fields = 1
}
function grep_summary__condition ( pkgpath, pkgbase){
pkgpath = fields ["PKGPATH"]
pkgbase = fields ["PKGNAME"]
sub(/-[^-]+$/, "", pkgbase)
if (pkgpath == "" || pkgbase == ""){
return -1
}
if (((pkgpath ":" pkgbase) in eq_pkgs) &&
!((pkgpath ":" pkgbase) in processed))
{
processed [pkgpath ":" pkgbase] = 1
return 1
}
return 0
}
' "$@"
}
copy_eq_pkgs $summary > $new_summary
# add new/updated/changed packages
changed_pkgs_fn=$tmp_dir/changed_pkgs
awk '$1 ~ /^[+<>?0-9]/ {print $2 ":" $3}' $cmp_fn > $changed_pkgs_fn
extract_pkgpath (){
runawk -v changed_pkgs_fn=$changed_pkgs_fn -e '
#use "xgetline.awk"
BEGIN {
while (xgetline0(changed_pkgs_fn)){
pkgs [$1] = 1
}
SUBSEP = ":"
FS = "="
}
$1 == "PKGNAME" {
pkgbase = $2
sub(/-[^-]+$/, "", pkgbase)
}
$1 == "PKGPATH" {
pkgpath = $2
}
NF == 0 && ((pkgpath ":" pkgbase) in pkgs) {
print pkgpath
pkgbase = pkgpath = ""
}
' "$@"
}
pkgpath_fn=$tmp_dir/pkgpaths
extract_pkgpath $micsum_fn > $pkgpath_fn
eval pkg_src_summary $fields < $pkgpath_fn >> $new_summary
# result
pkg_uniq_summary $new_summary > $new_summary.uniq
rm $new_summary
mv $new_summary.uniq $summary