]> git.pld-linux.org Git - packages/rpm.git/blobdiff - rpm-php-requires.php
- print help screen when no action specified,
[packages/rpm.git] / rpm-php-requires.php
index 9ab527c1306ffac313f0c0e8fb109bf0afb7fed1..ac832081789812cb006a3ad60217051cc308ddf5 100644 (file)
 #!/usr/bin/php
 <?php
 /**
- * Get the Compatibility info for an entire folder (recursive)
  *
+ * Check system dependences between php-pear modules.
+ *
+ * Paweł Gołaszewski <blues@pld-linux.org> (Perl version)
+ * Michał Moskal <malekith@pld-linux.org> (Perl version)
+ * Elan Ruusamäe <glen@pld-linux.org>
+ */
+
+/**
+ * Produce pear(Path/To/File.php) deps
+ * Ported to PHP from Perl version of rpm-php-requires.
+ *
+ * @TODO: use tokenizer to parse php files.
  */
+function peardeps($files) {
+       // files inside php_pear_dir have this prefix
+       $prefix = RPM_BUILD_ROOT. PHP_PEAR_DIR . DIRECTORY_SEPARATOR;
+       $length = strlen($prefix);
 
-require_once 'PHP/CompatInfo.php';
+       $req = array();
+       foreach ($files as $f) {
+               // skip non-php files
+               if (substr($f, -4) != '.php') {
+                       continue;
+               }
 
-$info = new PHP_CompatInfo('null');
+               // subdir inside php_pear_dir
+               if (substr($f, 0, $length) == $prefix) {
+                       $file_dir = dirname(substr($f, $length));
+               } else {
+                       $file_dir = null;
+               }
 
-$folder  = $argv[1];
-$options = array(
-    'file_ext' => array('php'),
-);
+               foreach (explode(PHP_EOL, file_get_contents($f)) as $line) {
+                       // skip comments
+                       if (preg_match('/^\s*(#|\/\/|\*|\/\*)/', $line)) {
+                               continue;
+                       }
 
-$res = $info->parseData($folder);
-if (version_compare($res['version'], '5.0.0', 'ge')) {
-       $epoch = 4;
-       // produce dependencies only for php5
-       $compat = false;
-       // session has always been compiled in
-       // date, spl are internal for php
-       $staticmods = array('session', 'date', 'spl');
-} else {
-       $epoch = 3;
-       // produce dependencies where php4/php5 both are ok
-       $compat = true;
-       // session has always been compiled in
-       $staticmods = array('session');
-}
-echo "Requires:\tphp-common >= ", $epoch, ":", $res['version'], "\n";
+                       while (preg_match("/(\W|^)(require|include)(_once)?
+                                       \s* \(? \s*
+                                       (\"([^\"]*)\"|'([^']*)')
+                                       \s* \)? \s* ;/x", $line, $m)) {
+
+                               if ($m[5] != "") {
+                                       $x = $m[5];
+                               } else if ($m[6] != "") {
+                                       $x = $m[6];
+                               } else {
+                                       continue 2;
+                               }
 
-# process extensions
-foreach ($res['extensions'] as $ext) {
-       if (in_array($ext, $staticmods)) {
-               continue;
+                               if (substr($x, 0, 2) == './' || substr($x, -1) == '$') {
+                                       continue 2;
+                               }
+
+                               if (substr($x, -4) != '.php') {
+                                       continue 2;
+                               }
+                               $req[$x] = 1;
+                               continue 2;
+                       }
+
+                       if (is_null($file_dir)) {
+                               continue;
+                       }
+
+                       while (preg_match("/(\W|^)(require|include)(_once)?
+                                       \s* \(? \s* dirname \s* \( \s* __FILE__ \s* \) \s* \. \s*
+                                       (\"([^\"]*)\"|'([^']*)')
+                                       \s* \)? \s* ;/x", $line, $m)) {
+
+                               if ($m[5] != "") {
+                                       $x = $m[5];
+                               } else if ($m[6] != "") {
+                                       $x = $m[6];
+                               } else {
+                                       continue 2;
+                               }
+
+                               if (substr($x, -1) == '$') {
+                                       continue 2;
+                               }
+                               if (substr($x, -4) != '.php') {
+                                       continue 2;
+                               }
+
+                               $x = "$file_dir/$x";
+                               $req[$x] = 1;
+                               continue;
+                       }
+               }
        }
 
-       if ($compat) {
-               echo "Requires:\tphp(", $ext, ")\n";
+       foreach (array_keys($req) as $f) {
+               // skip self deps
+               if (array_key_exists($f, $files)) {
+                       continue;
+               }
+               print "pear($f)\n";
+       }
+}
+
+/**
+ * Produce dependencies for extensions using PEAR PHP_CompatInfo package.
+ */
+function extdeps($files) {
+       require_once 'PHP/CompatInfo.php';
+
+       $info = new PHP_CompatInfo('null');
+       $res = $info->parseData($files);
+
+       if (version_compare($res['version'], '5.0.0', 'ge')) {
+               $epoch = 4;
+               // session, pcre are statically compiled in
+               // date, SPL, SimpleXML are internal for php
+               // sapi_apache?
+               $staticmods = array('standard', 'ereg', 'session', 'pcre', 'date', 'spl', 'simplexml');
        } else {
-               echo "Requires:\tphp-", $ext, "\n";
+               $epoch = 3;
+               // session has always been compiled in
+               $staticmods = array('standard', 'ereg', 'session');
+       }
+       echo "php-common >= ", $epoch, ":", $res['version'], "\n";
+
+       // process extensions
+       foreach ($res['extensions'] as $ext) {
+               if (in_array($ext, $staticmods)) {
+                       continue;
+               }
+
+               echo "php(", $ext, ")\n";
        }
 }
+
+define('RPM_BUILD_ROOT', getenv('RPM_BUILD_ROOT'));
+define('PHP_PEAR_DIR', '/usr/share/pear');
+
+if ($argc > 1) {
+       $files = array_splice($argv, 1);
+} else {
+       $files = explode(PHP_EOL, trim(file_get_contents('php://stdin')));
+}
+
+peardeps($files);
+extdeps($files);
This page took 0.042877 seconds and 4 git commands to generate.