Subject: Re: pkg_summary
To: Jeremy C. Reed <reed@reedmedia.net>
From: Aleksey Cheusov <cheusov@tut.by>
List: pkgsrc-users
Date: 06/13/2007 23:38:32
--=-=-=

> On Wed, 13 Jun 2007, Aleksey Cheusov wrote:

 >> > As for updating or rebuilding -- Maybe we can make a script that removes 
 >> > non-existent data from pkg_summary and adds new data. That should be way 
 >> > faster than creating entire pkg_summary each time.

 >> I can write this util if nobody is already doing this.

> Yes, please do it. Can you do it with sh and awk or sh and sed? (I think 
> that will be good enough so we can easily re-use it.)

Please test it. The attached script keeps pkg-summary up-to-date,
not a gzipped file but plain text.


--=-=-=
Content-Type: application/octet-stream
Content-Disposition: attachment; filename=pkg_update_summary
Content-Description: my script

#!/bin/sh -e

# written by Aleksey Cheusov

if test -z "$PKG_INFO"; then
    PKG_INFO=pkg_info
fi

if test $# -ne 2; then
    echo 'usage: pkg_update_summary summary_file bin_pkg_dir' 1>&2
    exit 1
fi

summary_file="$1"
bin_pkg_dir="$2"

cd "$bin_pkg_dir"

pkgs2summary (){
    xargs $PKG_INFO -X
}

get_all_pkgs (){
    ls -t "$bin_pkg_dir" | grep '[.]t[gb]z$'
}

get_updated_pkgs (){
    get_all_pkgs |
    while read f; do
	if test "$f" -nt "$summary_file"; then
	    echo "$f"
	else
	    break
	fi
    done
}

subtract (){
    cat "$@" |
    LC_ALL=C sort |
    LC_ALL=C uniq -c |
    awk '$1 == 1 {print $2}'
}

get_unchanged_pkgs (){
#    echo "updated=$updated"
#    echo "all=$all"
    { echo "$all"; echo "$updated"; } | subtract
}

tmp1="`dirname $summary_file`/`basename $summary_file`.tmp1"
tmp2="`dirname $summary_file`/`basename $summary_file`.tmp2"

trap "rm -f $tmp1 $tmp2" 0 1 2 15

filter_unchanged (){
    awk -v tmp2="$tmp2" '
    BEGIN {
	while (0 < (ret = getline < tmp2)){
	    sub(/[.]t[bg]z$/, "")
	    keep_array [$1] = ""
	}
	if (ret < 0){
	    printf "reading from %s failed\n", tmp2 > "/dev/stderr"
	    exit 2
	}

	FS = "="
    }
    $1 == "PKGNAME", NF == 0 {
	if ($1 == "PKGNAME"){
	    keep = ($2 in keep_array)
	}

	if (keep){
	    print $0
	}
    }
    ' "$@"
}

if ! test -f "$summary_file"; then
    get_all_pkgs |
    pkgs2summary > "$tmp1"
else
    updated="`get_updated_pkgs`"
    all="`get_all_pkgs`"
    get_unchanged_pkgs > "$tmp2"

    filter_unchanged "$summary_file" > "$tmp1"
    echo "$updated" | pkgs2summary >> "$tmp1"
fi

mv "$tmp1" "$summary_file"

--=-=-=


-- 
Best regards, Aleksey Cheusov.

--=-=-=--