Imported from CVS.
[autobuildrequires.git] / auto-br-analyze.pl.in
diff --git a/auto-br-analyze.pl.in b/auto-br-analyze.pl.in
new file mode 100755 (executable)
index 0000000..9163f53
--- /dev/null
@@ -0,0 +1,105 @@
+#!@PERL@ -w
+# @configure_input@
+#
+# Automatic generation of BuildRequires dependencies for rpmbuild.
+# Copyright (C) 2008 Red Hat Inc.
+# Written by Richard W.M. Jones <rjones@redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+use strict;
+
+use String::ShellQuote;
+use File::Temp qw(tempfile);
+
+if (@ARGV != 1) {
+    die "auto-br-analyze: logfile parameter missing\n";
+}
+
+my %files;
+
+my $homedir = $ENV{HOME};
+
+open LOG, "<$ARGV[0]" or die ($ARGV[0], ": $!\n");
+
+while (<LOG>) {
+    if (/^(open|execve) (.+)/) {
+       my $file = $2;
+
+       # Ignore files which don't exist, and files located in some
+       # places we ignore (eg. under $HOME).
+       my ($dev, $ino, $mode) = stat $file;
+       next unless defined $dev;
+       next if $homedir && $file =~ /^$homedir/;
+
+       $files{$file} = 1;
+    }
+}
+
+close LOG;
+
+# Work out which RPM packages are used.  Note we don't care about
+# which specific file is owned by which specific package, which allows
+# us to do a lot of nice shell magic for the hard work here.
+my @owners;
+
+my ($fh, $filename) = tempfile ();
+
+my $cmd =
+    "xargs -0 ".
+    "rpm -q --qf '%{name} %{epoch} %{version} %{release} %{arch}\n' -f ".
+    "2>&1 | ".
+    "grep -v '^file.*is not owned by any package\$' | ".
+    "grep -v '^error:' | ".
+    "sort -u > $filename";
+open PIPE, "| $cmd" or die "$cmd: $!";
+print PIPE "$_\0" foreach (sort keys %files);
+close PIPE;
+
+while (<$fh>) {
+    if (/^(\S+) (\d+|\(none\)) (\S+) (\S+) (\S+)$/) {
+       my ($rpm, $epoch, $version, $release, $arch) =
+           ($1, $2, $3, $4, $5);
+       $epoch = "0" if $epoch eq "(none)";
+       push @owners, {
+           rpm => $rpm,
+           epoch => $epoch, version => $version, release => $release,
+           arch => $arch
+       }
+    } else {
+       die "rpm: $_"
+    }
+}
+
+# Print the list of BuildRequires.
+
+foreach (@owners) {
+    my $rpm = $_->{rpm};
+    my $epoch = $_->{epoch};
+    my $version = $_->{version};
+    my $release = $_->{release};
+    my $arch = $_->{arch};
+
+    # Ignore 'rpm', 'rpm-build' and a few others which are introduced
+    # by the rpmbuild system itself.
+    next if $rpm eq "rpm" || $rpm eq "rpm-build" || $rpm eq "python"
+       || $rpm eq "redhat-rpm-config";
+
+    print "BuildRequires: $rpm = ";
+    if ($epoch != 0) {
+       print "$epoch:";
+    }
+    print "$version.$release.$arch\n";
+}