From d7c26db0a8a0774395812f3f5cd3674b153d0c55 Mon Sep 17 00:00:00 2001
From: Mathieu Parent <math.parent@gmail.com>
Date: Fri, 12 Jul 2013 14:36:54 +0200
Subject: [PATCH 4/4] Check for debian/rules for PHP packages

---
 checks/phppear.desc                             |   20 ++++++++
 checks/phppear.pm                               |   53 ++++++++++++++++++--
 debian/control                                  |    1 +
 t/tests/phppear-composer/desc                   |    1 +
 t/tests/phppear-composer/tags                   |    1 +
 t/tests/phppear-pear/desc                       |    2 +
 t/tests/phppear-pear/tags                       |    3 ++
 t/tests/phppear-pearok/debian/debian/control.in |   19 +++++++
 t/tests/phppear-pearok/debian/debian/rules      |    3 ++
 t/tests/phppear-pearok/desc                     |   12 +++++
 t/tests/phppear-pearok/upstream/package.xml     |   60 +++++++++++++++++++++++
 11 files changed, 172 insertions(+), 3 deletions(-)
 create mode 100755 t/tests/phppear-pearok/debian/debian/control.in
 create mode 100755 t/tests/phppear-pearok/debian/debian/rules
 create mode 100644 t/tests/phppear-pearok/desc
 create mode 100644 t/tests/phppear-pearok/tags
 create mode 100644 t/tests/phppear-pearok/upstream/MyProject_Packages-0.0.1/empty.php
 create mode 100644 t/tests/phppear-pearok/upstream/package.xml

diff --git a/checks/phppear.desc b/checks/phppear.desc
index c063bcd..975e206 100644
--- a/checks/phppear.desc
+++ b/checks/phppear.desc
@@ -55,3 +55,23 @@ Info: The package contains an composer.json file but doesn't build-depend on
  .
  pkg-php-tools is the recommended tool for building PHP Composer packages. For
  more information, install it and read the included README.Composer.
+
+Tag: missing-pkg-php-tools-buildsystem
+Severity: normal
+Certainty: possible
+Info: The package uses pkg-php-tools but dh command is called without
+ --buildsystem=phppear
+ .
+ pkg-php-tools build system is the recommended tool for building PHP PEAR
+ packages. For more information, install it and read the included README.PEAR.
+
+Tag: missing-pkg-php-tools-addon
+Severity: normal
+Certainty: possible
+Info: The package uses pkg-php-tools but dh command is called without
+ --with phppear or --with phpcomposer. A PECL package should also have
+ --with php5.
+ .
+ pkg-php-tools db addons are the recommended tool for building PHP PEAR or
+ Composer packages. For more information, install it and read the included
+ README.PEAR or README.Composer.
diff --git a/checks/phppear.pm b/checks/phppear.pm
index c1807bc..2623082 100644
--- a/checks/phppear.pm
+++ b/checks/phppear.pm
@@ -32,6 +32,8 @@ sub run {
     my ($pkg, $type, $info) = @_;
 
     my $bdepends = $info->relation('build-depends');
+    my $package_type = 'unknown';
+
     # PEAR or PECL package
     my $package_xml = $info->index('package.xml');
     my $package2_xml = $info->index('package2.xml');
@@ -80,9 +82,8 @@ sub run {
             }
             if (defined($package_xml) && $package_xml->is_regular_file) {
                 # Wild guess package type as in PEAR_PackageFile_v2::getPackageType()
-                my $package_type = 'unknown';
-               open(my $package_xml_fd, '<', $info->unpacked($package_xml));
-               while (<$package_xml_fd>) {
+                open(my $package_xml_fd, '<', $info->unpacked($package_xml));
+                while (<$package_xml_fd>) {
                     if (/^\s*<(php|extsrc|extbin|zendextsrc|zendextbin)release\s*\/?>/ ){
                         $package_type = $1;
                         last;
@@ -128,6 +129,52 @@ sub run {
                 '(>= 1.7~)', 'for Composer package support';
         }
     }
+    # Check rules
+    if (defined($package_xml) || defined($package2_xml) || defined($channel_xml) || defined($composer_json)) {
+        my $rules = $info->debfiles('rules');
+        if (!-l $rules or (-f $rules and is_ancestor_of($info->debfiles, $rules))) {
+            my $has_buildsystem_phppear = 0;
+            my $has_addon_phppear = 0;
+            my $has_addon_phpcomposer= 0;
+            my $has_addon_php5= 0;
+            open(my $rules_fd, '<', $rules);
+            while (<$rules_fd>) {
+                while (s,\\$,, and defined (my $cont = <$rules_fd>)) {
+                    $_ .= $cont;
+                }
+                next if /^\s*\#/;
+                if ( m/^\t\s*dh\s.*--buildsystem(?:=|\s+)(?:\S+,)*phppear\s/ ) {
+                    $has_buildsystem_phppear = 1;
+                }
+                if ( m/^\t\s*dh\s.*--with(?:=|\s+)(?:\S+,)*phppear\s/ ) {
+                    $has_addon_phppear = 1;
+                }
+                if ( m/^\t\s*dh\s.*--with(?:=|\s+)(?:\S+,)*phpcomposer\s/ ) {
+                    $has_addon_phpcomposer = 1;
+                }
+                if ( m/^\t\s*dh\s.*--with(?:=|\s+)(?:\S+,)*php5\s/ ) {
+                    $has_addon_php5 = 1;
+                }
+            }
+            close($rules_fd);
+            if (defined($package_xml) || defined($package2_xml) || defined($channel_xml)) {
+                if (!$has_buildsystem_phppear) {
+                    tag 'missing-pkg-php-tools-buildsystem', 'phppear'
+                }
+                if (!$has_addon_phppear) {
+                    tag 'missing-pkg-php-tools-addon', 'phppear'
+                }
+                if (($package_type eq 'extsrc') and !$has_addon_php5) {
+                    tag 'missing-pkg-php-tools-addon', 'php5'
+                }
+            }
+            if (!defined($package_xml) && !defined($package2_xml) && defined($composer_json)) {
+                if (!$has_addon_phpcomposer) {
+                    tag 'missing-pkg-php-tools-addon', 'phpcomposer'
+                }
+            }
+        }
+    }
     return;
 }
 
diff --git a/debian/control b/debian/control
index bcba816..6aed692 100644
--- a/debian/control
+++ b/debian/control
@@ -47,6 +47,7 @@ Build-Depends: binutils,
                man-db,
                patchutils,
                perl,
+               pkg-php-tools (>= 1.5~),
                python,
                python-all-dev,
                python-numpy,
diff --git a/t/tests/phppear-composer/desc b/t/tests/phppear-composer/desc
index 3153526..6b0534b 100644
--- a/t/tests/phppear-composer/desc
+++ b/t/tests/phppear-composer/desc
@@ -3,5 +3,6 @@ Sequence: 6000
 Version: 1.0-1
 Description: Composer phppear tests
 Test-For:
+ missing-pkg-php-tools-addon
  composer-package-without-pkg-php-tools-builddep
 Type: non-native
diff --git a/t/tests/phppear-composer/tags b/t/tests/phppear-composer/tags
index 3a66263..5586cc4 100644
--- a/t/tests/phppear-composer/tags
+++ b/t/tests/phppear-composer/tags
@@ -1 +1,2 @@
 W: phppear-composer source: composer-package-without-pkg-php-tools-builddep
+W: phppear-composer source: missing-pkg-php-tools-addon phpcomposer
diff --git a/t/tests/phppear-pear/desc b/t/tests/phppear-pear/desc
index 8e72e0b..5669ae6 100644
--- a/t/tests/phppear-pear/desc
+++ b/t/tests/phppear-pear/desc
@@ -3,6 +3,8 @@ Sequence: 6000
 Version: 1.0-1
 Description: phppear tests for PEAR packages
 Test-For:
+ missing-pkg-php-tools-addon
+ missing-pkg-php-tools-buildsystem
  pear-package-but-missing-dependency
  pear-package-feature-requires-newer-pkg-php-tools
  pear-package-not-using-substvar
diff --git a/t/tests/phppear-pear/tags b/t/tests/phppear-pear/tags
index d0b3852..b819015 100644
--- a/t/tests/phppear-pear/tags
+++ b/t/tests/phppear-pear/tags
@@ -1,3 +1,6 @@
+W: phppear-pear source: missing-pkg-php-tools-addon php5
+W: phppear-pear source: missing-pkg-php-tools-addon phppear
+W: phppear-pear source: missing-pkg-php-tools-buildsystem phppear
 W: phppear-pear source: pear-package-but-missing-dependency Breaks
 W: phppear-pear source: pear-package-but-missing-dependency Depends
 W: phppear-pear source: pear-package-but-missing-dependency Recommends
diff --git a/t/tests/phppear-pearok/debian/debian/control.in b/t/tests/phppear-pearok/debian/debian/control.in
new file mode 100755
index 0000000..a1e1543
--- /dev/null
+++ b/t/tests/phppear-pearok/debian/debian/control.in
@@ -0,0 +1,19 @@
+Source: {$srcpkg}
+Priority: extra
+Section: {$section}
+Maintainer: {$author}
+Standards-Version: {$standards_version}
+Build-Depends: debhelper (>= 9), pkg-php-tools, php5-dev, dh-php5
+
+Package: {$srcpkg}
+Architecture: {$architecture}
+Depends: $\{misc:Depends\}, $\{phppear:Debian-Depends\}
+Recommends: $\{phppear:Debian-Recommends\}
+Breaks: $\{phppear:Debian-Breaks\}
+Description: $\{phppear:summary\}
+ This is a test package designed to exercise some feature or tag of
+ Lintian.  It is part of the Lintian test suite and may do very odd
+ things.  It should not be installed like a regular package.  It may
+ be an empty package.
+ .
+ $\{phppear:description\}
diff --git a/t/tests/phppear-pearok/debian/debian/rules b/t/tests/phppear-pearok/debian/debian/rules
new file mode 100755
index 0000000..02621c3
--- /dev/null
+++ b/t/tests/phppear-pearok/debian/debian/rules
@@ -0,0 +1,3 @@
+#!/usr/bin/make -f
+%:
+	dh $@ --with phppear --buildsystem phppear
diff --git a/t/tests/phppear-pearok/desc b/t/tests/phppear-pearok/desc
new file mode 100644
index 0000000..8dabd76
--- /dev/null
+++ b/t/tests/phppear-pearok/desc
@@ -0,0 +1,12 @@
+Testname: phppear-pearok
+Sequence: 6000
+Version: 1.0-1
+Description: phppear tests for PEAR packages
+Test-Against:
+ missing-pkg-php-tools-addon
+ missing-pkg-php-tools-buildsystem
+ pear-package-but-missing-dependency
+ pear-package-feature-requires-newer-pkg-php-tools
+ pear-package-not-using-substvar
+ pecl-package-requires-build-dependency
+Type: non-native
diff --git a/t/tests/phppear-pearok/tags b/t/tests/phppear-pearok/tags
new file mode 100644
index 0000000..e69de29
diff --git a/t/tests/phppear-pearok/upstream/MyProject_Packages-0.0.1/empty.php b/t/tests/phppear-pearok/upstream/MyProject_Packages-0.0.1/empty.php
new file mode 100644
index 0000000..e69de29
diff --git a/t/tests/phppear-pearok/upstream/package.xml b/t/tests/phppear-pearok/upstream/package.xml
new file mode 100644
index 0000000..57af010
--- /dev/null
+++ b/t/tests/phppear-pearok/upstream/package.xml
@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<package packagerversion="1.8.1" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0     http://pear.php.net/dtd/tasks-1.0.xsd     http://pear.php.net/dtd/package-2.0     http://pear.php.net/dtd/package-2.0.xsd">
+ <name>MyProject_Packages</name>
+ <channel>pear.php.net</channel>
+ <summary>this is a wrapper package for installing dependencies for MyProject</summary>
+ <description>&lt;Some random notes&gt;</description>
+ <lead>
+  <name>fred</name>
+  <user>fred</user>
+  <email>fred@example.com</email>
+  <active>yes</active>
+ </lead>
+ <date>2009-05-08</date>
+ <time>00:58:09</time>
+ <version>
+  <release>0.0.1</release>
+  <api>0.0.1</api>
+ </version>
+ <stability>
+  <release>alpha</release>
+  <api>alpha</api>
+ </stability>
+ <license uri="http://www.gnu.org/licenses/lgpl.html">LGPL</license>
+ <notes>
+&lt;none&gt;
+ </notes>
+ <contents>
+  <dir baseinstalldir="Test" name="/">
+   <file baseinstalldir="Test" md5sum="d41d8cd98f00b204e9800998ecf8427e" name="empty.php" role="php"/>
+  </dir>
+ </contents>
+ <dependencies>
+  <required>
+   <php>
+    <min>5.2.1</min>
+   </php>
+   <pearinstaller>
+    <min>1.8.0</min>
+   </pearinstaller>
+  </required>
+ </dependencies>
+ <phprelease/>
+ <changelog>
+  <release>
+   <version>
+    <release>0.0.1</release>
+    <api>0.0.1</api>
+   </version>
+   <stability>
+    <release>alpha</release>
+    <api>alpha</api>
+   </stability>
+   <date>2009-05-08</date>
+   <license uri="http://www.gnu.org/licenses/lgpl.html">LGPL</license>
+   <notes>
+&lt;none&gt;
+   </notes>
+  </release>
+ </changelog>
+</package>
-- 
1.7.10.4

