Subject: [PATCH] Improve the parsing of debian/rules to extract the DH_COMPAT information, only extract the first line of debian/compat, just like debhelper does, and warn if the compatibility version either in debian/compat or debian/rules is not a number.

From: Raphael Geissert <[email-masked]>
Date: Sat, 31 Jan 2009 18:53:03 -0600

Signed-off-by: Raphael Geissert <[email-masked]>
---
 checks/debhelper                                |   29 ++++++++++++++++++----
 checks/debhelper.desc                           |   13 +++++++++-
 t/tests/debhelper-compat/debian/debian/compat   |    1 +
 t/tests/debhelper-compat/desc                   |    5 ++++
 t/tests/debhelper-compat/tags                   |    1 +
 t/tests/debhelper-dh-compat/debian/debian/rules |    8 ++++++
 t/tests/debhelper-dh-compat/desc                |    5 ++++
 t/tests/debhelper-dh-compat/tags                |    4 +++
 8 files changed, 59 insertions(+), 7 deletions(-)
 create mode 100644 t/tests/debhelper-compat/debian/debian/compat
 create mode 100644 t/tests/debhelper-compat/desc
 create mode 100644 t/tests/debhelper-compat/tags
 create mode 100755 t/tests/debhelper-dh-compat/debian/debian/rules
 create mode 100644 t/tests/debhelper-dh-compat/desc
 create mode 100644 t/tests/debhelper-dh-compat/tags

diff --git a/checks/debhelper b/checks/debhelper
index 19db213..f761c30 100644
--- a/checks/debhelper
+++ b/checks/debhelper
@@ -77,9 +77,9 @@ my @versioncheck;
 while (<RULES>) {
     if (m/^\s+-?(dh_\w+)/) {
         my $dhcommand = $1;
-    	if ($dhcommand =~ /dh_testversion(?:\s+(.+))?/) {
+	if ($dhcommand =~ /dh_testversion(?:\s+([^\s]+))?/) {
 	    $needversiondepends = $1 if ($1);
-            tag "dh_testversion-is-deprecated", "";
+	    tag "dh_testversion-is-deprecated", "";
 	}
 	if ($dhcommand eq 'dh_dhelp') {
 	    tag "dh_dhelp-is-deprecated", "";
@@ -122,12 +122,15 @@ while (<RULES>) {
 	# a debian/compat file if none was found; that logic is handled later.
 	$dhcompatvalue = $cdbscompat;
 	$usescdbs = 1;
-    } elsif (/^\s*export\s+DH_COMPAT\s*:?=\s*(\d+)/) {
+    } elsif (/^\s*export\s+DH_COMPAT\s*:?=\s*([^\s]+)/) {
 	$needversiondepends = $1;
     } elsif (/^\s*export\s+DH_COMPAT/) {
 	$needversiondepends = $dhcompatvalue if $dhcompatvalue;
-    } elsif (/^\s*DH_COMPAT\s*:?=\s*(\d+)/) {
+    } elsif (/^\s*DH_COMPAT\s*:?=\s*([^\s]+)/) {
 	$dhcompatvalue = $1;
+	# one can export and then set the value:
+	$needversiondepends = $1
+	    if ($needversiondepends);
     }
     if (/^\s+dh_python\s/) {
         $seendhpython = 1;
@@ -163,23 +166,37 @@ for my $binpkg (keys %$pkgs) {
 	   and $pkgs->{$binpkg} eq 'deb';
 }
 
+my $compatnan = 0;
 # Check the compat file.  Do this separately from looping over all of the
 # other files since we use the compat value when checking for brace expansion.
 if (-f 'debfiles/compat') {
-    $compat = slurp_entire_file('debfiles/compat');
+    my $compat_file = slurp_entire_file('debfiles/compat');
+    ($compat) = split(/\n/, $compat_file);
     $compat =~ s/^\s+$//;
     if ($compat) {
-	$compat =~ s/\s+$//;
+	chomp $compat;
+	if ($compat !~ m/^\d+$/) {
+	    tag 'debhelper-compat-is-not-a-number', $compat;
+	    $compat =~ s/[^\d]//g;
+	    $compatnan = 1;
+	}
 	if ($needversiondepends) {
 	    tag 'declares-possibly-conflicting-debhelper-compat-versions',
 		"rules=$needversiondepends compat=$compat";
 	} else {
+	    # this is not just to fill in the gap, but because debhelper
+	    # prefers DH_COMPAT over debian/compat
 	    $needversiondepends = $compat;
 	}
     } else {
 	tag 'debhelper-compat-file-is-empty';
     }
 }
+if ($needversiondepends !~ m/^\d+$/ and not $compatnan) {
+    tag 'debhelper-compatibility-version-is-not-a-number', $needversiondepends;
+    $needversiondepends =~ s/[^\d]//g;
+    $compatnan = 1;
+}
 
 # Check the files in the debian directory for various debhelper-related
 # things.
diff --git a/checks/debhelper.desc b/checks/debhelper.desc
index 9f42dfa..8688396 100644
--- a/checks/debhelper.desc
+++ b/checks/debhelper.desc
@@ -82,7 +82,8 @@ Severity: important
 Certainty: certain
 Ref: debhelper(7)
 Info: The source package has an empty debian/compat file. This is an error,
- the compat level of debhelper should be in there.
+ the compat level of debhelper should be in there. Note that only the first
+ line of the file is relevant.
 
 Tag: declares-possibly-conflicting-debhelper-compat-versions
 Severity: important
@@ -160,3 +161,13 @@ Info: The source package declares a weak dependecy on ${misc:Depends} in
  the given binary package's debian/control entry.  A stronger dependency, that
  is one that ensures the package's installation, is required so that the
  additional commands are available to the maintainer scripts when they are run.
+
+Tag: debhelper-compat-is-not-a-number
+Severity: serious
+Certainty: possible
+Info: The compat version specified in <tt>debian/compat</tt> is not a number.
+
+Tag: debhelper-compatibility-version-is-not-a-number
+Severity: serious
+Certainty: possible
+Info: The compatibility version specified in <tt>debian/rules</tt> is not a number.
diff --git a/t/tests/debhelper-compat/debian/debian/compat b/t/tests/debhelper-compat/debian/debian/compat
new file mode 100644
index 0000000..59b4382
--- /dev/null
+++ b/t/tests/debhelper-compat/debian/debian/compat
@@ -0,0 +1 @@
+[7-
diff --git a/t/tests/debhelper-compat/desc b/t/tests/debhelper-compat/desc
new file mode 100644
index 0000000..503ee69
--- /dev/null
+++ b/t/tests/debhelper-compat/desc
@@ -0,0 +1,5 @@
+Testname: debhelper-compat
+Sequence: 6000
+Version: 1.0
+Description: Test the content of debian/compat
+Test-For: debhelper-compat-is-not-a-number
diff --git a/t/tests/debhelper-compat/tags b/t/tests/debhelper-compat/tags
new file mode 100644
index 0000000..283d75a
--- /dev/null
+++ b/t/tests/debhelper-compat/tags
@@ -0,0 +1 @@
+E: debhelper-compat source: debhelper-compat-is-not-a-number [7-
diff --git a/t/tests/debhelper-dh-compat/debian/debian/rules b/t/tests/debhelper-dh-compat/debian/debian/rules
new file mode 100755
index 0000000..a36f8fd
--- /dev/null
+++ b/t/tests/debhelper-dh-compat/debian/debian/rules
@@ -0,0 +1,8 @@
+#!/usr/bin/make -f
+
+export DH_COMPAT=7
+
+%:
+	dh $@
+
+DH_COMPAT='{3'
diff --git a/t/tests/debhelper-dh-compat/desc b/t/tests/debhelper-dh-compat/desc
new file mode 100644
index 0000000..678c159
--- /dev/null
+++ b/t/tests/debhelper-dh-compat/desc
@@ -0,0 +1,5 @@
+Testname: debhelper-dh-compat
+Sequence: 6000
+Version: 1.0
+Description: Test the value of DH_COMPAT
+Test-For: debhelper-compatibility-version-is-not-a-number
diff --git a/t/tests/debhelper-dh-compat/tags b/t/tests/debhelper-dh-compat/tags
new file mode 100644
index 0000000..bac2af5
--- /dev/null
+++ b/t/tests/debhelper-dh-compat/tags
@@ -0,0 +1,4 @@
+E: debhelper-dh-compat source: debhelper-compatibility-version-is-not-a-number '{3'
+E: debhelper-dh-compat source: declares-possibly-conflicting-debhelper-compat-versions rules='{3' compat=7
+W: debhelper-dh-compat source: debian-rules-sets-DH_COMPAT line 3
+W: debhelper-dh-compat source: package-uses-deprecated-debhelper-compat-version 3