pkg_dig is a small sh(1) script that extracts useful information from
installed packages. The reason I decided to implement a script like this
was to provide a missing feature of pkg_delete(1). Let me give you an
example:

Let's say we have 2 packages, Y and Z.

Package: Y
Dependencies: A, B, C

Package: Z
Dependencies: C, D, E

If we decide to install package Y, pkg_add(1) will install A, B and C as
dependencies. If we want to remove this package later on, pkg_delete(1)
will remove only Y leaving A, B and C installed. Let's expand this a
bit.

If we install Y, along with A, B and C as dependencies. Now, let's say
we install Z also. Thus, D and E will be installed additionally as
dependencies. C is already installed as a dependency of Y. At some point
we decide to remove Y. pkg_delete(1) should remove Y, A and B, leaving C
installed as a dependency for Z. Instead, pkg_delete(1) will remove only
Y, leaving A and B as orphan packages. This is of course inefficient and
undesired.

Here is where pkg_dig comes into play. Using the information stored in
/var/db/pkg it will find which of the dependencies of a package can be
safely removed along with the package. Here is a sample output:

[EMAIL PROTECTED] msid $ pkg_dig xpdf

*- Package matched: xpdf-3.01p1
|- Dependencies: ghostscript-fonts-6.0p0 t1lib-5.1.0p0 openmotif-2.1.30.5p1
|- Required by: none
|- You may also delete: openmotif-2.1.30.5p1

[EMAIL PROTECTED] msid $

You may define multiple packages as well. That's all more or less. I
would love to receive feedback on this from whoever is willing to try
it. I know it cannot be merged with pkg_delete(1) in its current form
since the latter is coded in Perl, but, I was merely trying to present a
feature that I consider to be useful. Probably someone more proficient
in Perl than myself can easily implement this feature and merge it with
pkg_delete(1) as an -F option for example. If you find this useless, I
apologize for the noise. You can grab the latest version from:

http://black.daemons.gr/msid/scripts/pkg_dig

-- 
Sideris Michael
http://black.daemons.gr/msid/
#!/bin/sh

# Copyright (c) 2006, Sideris Michael
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the
#     distribution.
#   * Neither the name of the author nor the names of its
#     contributors may be used to endorse or promote products derived
#     from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

if [ $# -eq 0 ]
then
        echo Usage: `echo $0 |sed 's/^\.\///'` [pkg_name ...]
        exit 0
fi

echo

for arg in $*
do
        if [ true ]
        then
                pkgdb=/var/db/pkg
                pkgname=
                search=
                counter=0

                if [ ! -d $pkgdb ]
                then
                        exit 1
                fi

                cd $pkgdb

                if [ -d $arg ]
                then
                        pkgname=$arg
                        counter=1
                else
                        search=`find . -maxdepth 1 -type d -name "$arg*" | sort 
| sed 's/^\.\///'`

                        for x in $search
                        do
                                if [ $arg == `echo $x | sed -e 's/^\.\///' -e 
's/-[0-9].*//'` ]
                                then
                                        counter=`expr $counter+1 |bc`
                                        pkgname=${pkgname}\ $x
                                fi
                        done
                fi

                if [ "$counter" -eq 0 ]
                then
                        echo "*" No package found: $arg
                elif [ "$counter" -gt 1 ]
                then
                        echo \* Ambiguous: $arg could be `echo $pkgname |sed 
's/ /\ or\ /'`
                else
                        echo \*\- Package matched: $pkgname

                        if [ -f $pkgname/+REQUIRING ]
                        then
                                deps=`cat $pkgname/+REQUIRING`
                                echo \|- Dependencies: $deps
                        else
                                deps=
                                echo \|- Dependencies: none
                        fi

                        if [ -f $pkgname/+REQUIRED_BY ]
                        then
                                required_by=`cat $pkgname/+REQUIRED_BY`
                                echo \|- Required by: $required_by
                        else
                                required_by=
                                echo \|- Required by: none
                        fi

                        if [ "$deps" ]
                        then
                                for x in $deps
                                do
                                        if [ -f $x/+REQUIRED_BY ]
                                        then
                                                if [ `grep -cv $pkgname 
$x/+REQUIRED_BY` -gt 0 ]
                                                then
                                                        deps=`echo "$deps" |sed 
-e 's/'$x'//' -e '/^ *$/d'`
                                                fi
                                        fi
                                done
                        fi

                        if [ "$deps" ]
                        then
                                echo \|- You may also delete: $deps
                        else
                                echo \|- You may also delete: none
                        fi
                fi
        fi

        echo
done

Reply via email to