Subject: [PATCH] Implement and read binary packages' fields from source control files

From: Raphael Geissert <[email-masked]>
Date: Wed, 28 Jan 2009 00:22:17 -0600

Signed-off-by: Raphael Geissert <[email-masked]>
---
 checks/debhelper              |   10 ++--------
 lib/Lintian/Collect/Source.pm |   35 ++++++++++++++++++++++++-----------
 2 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/checks/debhelper b/checks/debhelper
index 5ac54f4..19db213 100644
--- a/checks/debhelper
+++ b/checks/debhelper
@@ -147,16 +147,10 @@ for my $binpkg (keys %$pkgs) {
     local $/;
 
     foreach my $field (qw(pre-depends depends)) {
-	if (open DEPENDS, '<', "control/$binpkg/$field") {
-	    chomp ($strong_depends .= <DEPENDS>);
-	    close(DEPENDS);
-	}
+	$strong_depends .= $info->binary_field($binpkg, $field);
     }
     foreach my $field (qw(recommends suggests)) {
-	if (open DEPENDS, '<', "control/$binpkg/$field") {
-	    chomp ($weak_depends .= <DEPENDS>);
-	    close(DEPENDS);
-	}
+	$weak_depends .= $info->binary_field($binpkg, $field);
     }
     $depends = $weak_depends . $strong_depends;
 
diff --git a/lib/Lintian/Collect/Source.pm b/lib/Lintian/Collect/Source.pm
index 4a7d6c2..bc6980f 100644
--- a/lib/Lintian/Collect/Source.pm
+++ b/lib/Lintian/Collect/Source.pm
@@ -77,17 +77,7 @@ sub binaries {
         or fail("Can't open control directory.");
     while (my $binpkg = readdir(BINPKGS)) {
         next if $binpkg =~ /^\.\.?$/;
-        if (-d "control/$binpkg") {
-            if (open TYPE, "<", "control/$binpkg/xc-package-type") {
-                my $type = <TYPE> || '';
-                chomp $type;
-                $type = lc $type;
-                $pkgs{$binpkg} = $type || 'deb';
-                close TYPE;
-            } else {
-                $pkgs{$binpkg} = 'deb';
-            }
-        }
+        $pkgs{$binpkg} = lc $self->binary_field($binpkg, 'xc-package-type') || 'deb';
     }
     closedir BINPKGS;
 
@@ -96,6 +86,24 @@ sub binaries {
     return $self->{binaries};
 }
 
+# Retrieve a field from a binary package
+sub binary_field {
+    my ($self, $binpkg, $field) = @_;
+    return $self->{"binary-$binpkg"}{$field}
+        if exists $self->{"binary_$binpkg"}{$field};
+
+    my $value = '';
+    if (open FIELD, "<", "control/$binpkg/$field") {
+        local $/;
+        $value = <FIELD>;
+        chomp $value;
+        close FIELD;
+    }
+    $self->{"binary_$binpkg"}{$field} = $value;
+
+    return $value;
+}
+
 =head1 NAME
 
 Lintian::Collect::Source - Lintian interface to source package data collection
@@ -153,6 +161,11 @@ Returns true if the source package is native and false otherwise.
 Returns a hash reference with the binary package names as keys and the
 Package-Type as value (which should be either 'deb' or 'udeb' currently).
 
+=item binary_field(BINARY_PACKAGE, FIELD)
+
+Returns the content of the field FIELD for the BINARY_PACKAGE or an empty
+string if such field doesn't exist.
+
 =back
 
 =head1 AUTHOR