SOURCES: rpm-php-requires.php - ported rpm-php-requires to php

glen glen at pld-linux.org
Wed Oct 22 17:00:04 CEST 2008


Author: glen                         Date: Wed Oct 22 15:00:04 2008 GMT
Module: SOURCES                       Tag: HEAD
---- Log message:
- ported rpm-php-requires to php

---- Files affected:
SOURCES:
   rpm-php-requires.php (1.6 -> 1.7) 

---- Diffs:

================================================================
Index: SOURCES/rpm-php-requires.php
diff -u SOURCES/rpm-php-requires.php:1.6 SOURCES/rpm-php-requires.php:1.7
--- SOURCES/rpm-php-requires.php:1.6	Wed Oct 22 16:03:44 2008
+++ SOURCES/rpm-php-requires.php	Wed Oct 22 16:59:58 2008
@@ -3,26 +3,104 @@
 /**
  *
  * Check system dependences between php-pear modules.
- * Based on Perl version rpm-php-requires.
  *
- * Paweł Gołaszewski <blues at pld-linux.org>
- * Michał Moskal <malekith at pld-linux.org>
+ * Paweł Gołaszewski <blues at pld-linux.org> (Perl version)
+ * Michał Moskal <malekith at pld-linux.org> (Perl version)
  * Elan Ruusamäe <glen at pld-linux.org>
  */
 
 /**
- * Produce old style pear(Path/To/File.php) deps
+ * 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) {
 	// all files must begin with $RPM_BUILD_ROOT%{php_pear_dir}
 	$prefix = RPM_BUILD_ROOT. PHP_PEAR_DIR . DIRECTORY_SEPARATOR;
 	$length = strlen($prefix);
+
+	$req = array();
 	foreach ($files as $f) {
-		if (substr($f, 0, $length) != $prefix) {
+		// skip non-php files
+		if (substr($f, -4) != '.php') {
+			continue;
+		}
+
+		// subdir inside php_pear_dir
+		if (substr($f, 0, $length) == $prefix) {
+			$file_dir = dirname(substr($f, $length));
+		} else {
+			$file_dir = null;
+		}
+
+		foreach (explode(PHP_EOL, file_get_contents($f)) as $line) {
+			// skip comments
+			if (preg_match('/^\s*(#|\/\/|\*|\/\*)/', $line)) {
+				continue;
+			}
+
+			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;
+				}
+
+				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;
+			}
+		}
+	}
+
+	foreach (array_keys($req) as $f) {
+		// skip self deps
+		if (array_key_exists($f, $files)) {
 			continue;
 		}
-		$f = substr($f, $length);
-		echo "pear($f)\n";
+		print "pear($f)\n";
 	}
 }
 
@@ -64,7 +142,7 @@
 if ($argc > 1) {
 	$files = array_splice($argv, 1);
 } else {
-	$files = split(PHP_EOL, trim(file_get_contents('php://stdin')));
+	$files = explode(PHP_EOL, trim(file_get_contents('php://stdin')));
 }
 
 peardeps($files);
================================================================

---- CVS-web:
    http://cvs.pld-linux.org/cgi-bin/cvsweb.cgi/SOURCES/rpm-php-requires.php?r1=1.6&r2=1.7&f=u



More information about the pld-cvs-commit mailing list