| 1 |
#!/bin/env perl -w |
| 2 |
# |
| 3 |
# $Id: depmaker 6679 2009-10-03 08:47:27Z dmichelsen $ |
| 4 |
# |
| 5 |
# Copyright 2006 Cory Omand <comand@blastwave.org> |
| 6 |
# All rights reserved. Use is subject to license terms. |
| 7 |
# |
| 8 |
# Redistribution and/or use, with or without modification, is |
| 9 |
# permitted. This software is without warranty of any kind. The |
| 10 |
# author(s) shall not be liable in the event that use of the |
| 11 |
# software causes damage. |
| 12 |
# |
| 13 |
# depmaker - find package dependencies for a list of files |
| 14 |
# |
| 15 |
|
| 16 |
use strict; |
| 17 |
|
| 18 |
use File::Basename; |
| 19 |
use Getopt::Long qw/:config no_ignore_case gnu_getopt/; |
| 20 |
use File::Spec::Functions qw/canonpath catfile/; |
| 21 |
use File::Temp qw/tempfile/; |
| 22 |
|
| 23 |
my $self = basename $0; |
| 24 |
my $VERSION = "1.0"; |
| 25 |
|
| 26 |
# Get command line options |
| 27 |
my $do_script = 1; |
| 28 |
my $be_quiet = 0; |
| 29 |
my (@nodep, $rootdir); |
| 30 |
my $text_files = 0; |
| 31 |
GetOptions( |
| 32 |
'script!' => \$do_script, |
| 33 |
'quiet|q' => \$be_quiet, |
| 34 |
'root=s' => \$rootdir, |
| 35 |
'nodep=s' => \@nodep, |
| 36 |
'help|h' => \&usage, |
| 37 |
'text|t' => \$text_files, |
| 38 |
'version|V' => sub { |
| 39 |
print STDERR "$self v$VERSION\n"; |
| 40 |
exit 2; |
| 41 |
}, |
| 42 |
) or usage(); |
| 43 |
|
| 44 |
# Join package patterns |
| 45 |
my $nodep = join "|", @nodep; |
| 46 |
|
| 47 |
sub usage { |
| 48 |
print STDERR "Usage: $self [--text] [--no/script] [--nodep <pkg>] [--root <path>] < path_list > depend\n"; |
| 49 |
exit 1; |
| 50 |
} |
| 51 |
|
| 52 |
sub msg { |
| 53 |
return if $be_quiet; |
| 54 |
print STDERR @_; |
| 55 |
} |
| 56 |
|
| 57 |
sub exclude { |
| 58 |
my $path = shift; |
| 59 |
foreach my $pat (@nodep) { return 1 if $path =~ $pat } |
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
msg "root directory : $rootdir\n" if $rootdir; |
| 64 |
msg "skip depends for : " . join(", ", sort @nodep) . "\n"; |
| 65 |
msg "script dependencies : " . ($do_script ? "enabled" : "disabled") . "\n"; |
| 66 |
msg "\n"; |
| 67 |
|
| 68 |
msg "processing path list...\n"; |
| 69 |
|
| 70 |
# Analyze the incoming path list... |
| 71 |
my %depfiles; |
| 72 |
foreach (<>) { |
| 73 |
chomp; |
| 74 |
|
| 75 |
# Prepend the root directory, if supplied |
| 76 |
my ($filedst,$filesrc) = split /=/, $_; |
| 77 |
$filesrc = $filedst unless $filesrc; |
| 78 |
if ($rootdir) { |
| 79 |
if ($filesrc =~ /^\$basedir/) { |
| 80 |
$filesrc =~ s/^\$basedir/$rootdir/; |
| 81 |
} |
| 82 |
else { |
| 83 |
$filesrc = catfile($rootdir, $filesrc); |
| 84 |
} |
| 85 |
$filesrc = canonpath($filesrc); |
| 86 |
} |
| 87 |
|
| 88 |
local $_ = $filesrc; |
| 89 |
next unless -f; |
| 90 |
|
| 91 |
my ($ftype) = `/bin/file -h '$_'`; |
| 92 |
|
| 93 |
my @depfiles; |
| 94 |
if ($do_script and $ftype =~ /script/) { |
| 95 |
# Extract an interpreter dependency... |
| 96 |
my ($bangpath) = `head -1 $_`; chomp $bangpath; |
| 97 |
$bangpath =~ s/^#!\s*(\S+)(\s.+)?/$1/; |
| 98 |
|
| 99 |
msg "$_ ... $bangpath script"; |
| 100 |
|
| 101 |
# Compensate for /bin -> /usr/bin |
| 102 |
$bangpath = "/usr$bangpath" if $bangpath =~ '^/bin'; |
| 103 |
$depfiles{$bangpath}++; |
| 104 |
|
| 105 |
} |
| 106 |
elsif ($ftype =~ /dynamically linked/) { |
| 107 |
msg "$_\n"; |
| 108 |
|
| 109 |
foreach my $line (`dump -Lv $_`) { |
| 110 |
next unless $line =~ /NEEDED/; chomp $line; |
| 111 |
my (undef, undef, $lib) = split /\s+/, $line; |
| 112 |
#msg " $lib\n"; |
| 113 |
$depfiles{$lib}++ if $lib; |
| 114 |
} |
| 115 |
} |
| 116 |
elsif ($text_files and $ftype =~ /text/) { |
| 117 |
$depfiles{$filesrc}++; |
| 118 |
} |
| 119 |
} |
| 120 |
exit 0 unless scalar keys %depfiles; |
| 121 |
|
| 122 |
# Write dependency paths out to a temp file |
| 123 |
my ($deptmp_fh, $deptmp) = tempfile; |
| 124 |
print $deptmp_fh $_, "\n" foreach (sort keys %depfiles); |
| 125 |
|
| 126 |
msg "searching package database for dependencies...\n"; |
| 127 |
|
| 128 |
# Look for all files that had dependencies in the system install |
| 129 |
# install database, and store the package names... |
| 130 |
$nodep = "-v '$nodep'" if $nodep; |
| 131 |
my $query; |
| 132 |
$query .= $nodep ? "egrep $nodep " : "cat "; |
| 133 |
$query .= "/var/sadm/install/contents "; |
| 134 |
$query .= "| fgrep -f $deptmp "; |
| 135 |
$query .= "| gawk '{print \$NF}' "; |
| 136 |
$query .= "| sort | uniq"; |
| 137 |
|
| 138 |
close $deptmp_fh; |
| 139 |
|
| 140 |
chomp(my @deppkg = `$query`); |
| 141 |
|
| 142 |
foreach (sort @deppkg) { |
| 143 |
my ($desc) = `pkginfo $_` or next; |
| 144 |
chomp $desc; |
| 145 |
my ($class, $pkginst, $pdesc) = split /\s+/, $desc, 3; |
| 146 |
print "P $pkginst $pdesc\n"; |
| 147 |
} |
| 148 |
|
| 149 |
exit 0; |