Sometimes I come to a machine where another admin installed some CSW packages which pulled in a lot of dependencies. Is there a way to list the packages that where meant to be installed?
asked: 2012-03-26 by: Dagobert
Dagobert answers:
This question seems rather trivial – just use pkginfo
and be done with it. However, this gives you the complete list of installed CSW packages. When trying to find out what the initial cause for installation was I wrote this tiny script that prints the minimal set of CSW packages needed to be installed to result in the current set of CSW packages installed. No magic in there, but sometimes these little helpers come in handy
#!/opt/csw/bin/perl
my @allpkgs = grep { /^CSW/ }
map { (split( /\s+/ ))[1] }
`/usr/bin/pkginfo`;
my %minimal;
$minimal{$_} = 1 foreach (@allpkgs);
# Remove dependencies
foreach (@allpkgs) {
open D, "/var/sadm/pkg/$_/install/depend" or next;
while( <D> ) {
my ($type, $pkg) = split( /\s+/ );
next if( $type ne "P" );
delete $minimal{$pkg};
}
close D;
}
print "$_\n" foreach (keys %minimal);
cgrzemba answers:
mental exercise: Where is the mistake:
This should deliver the same result, but it doesn't:
#!/usr/bin/env python
from subprocess import Popen, PIPE
import re
allpkg = []
f = Popen("/usr/bin/pkginfo", stdout=PIPE).stdout
allpkg = [ pkg.split()[1] for pkg in f if re.match('^CSW\w+',pkg.split()[1]) ]
f.close()
minimal = allpkg
for pkg in allpkg:
try:
f = open('/var/sadm/pkg/%s/install/depend' % pkg)
for line in f:
if len(line)>3 and line.split()[0] == 'P':
try:
minimal.remove(line.split()[1])
except ValueError:
pass
f.close()
except IOError:
pass
for pkg in minimal:
print pkg