]> git.pld-linux.org Git - packages/rpm.git/blob - rpm-php-requires.php
- current ext deps
[packages/rpm.git] / rpm-php-requires.php
1 #!/usr/bin/php
2 <?php
3 /**
4  *
5  * Check system dependences between php-pear modules.
6  *
7  * Paweł Gołaszewski <blues@pld-linux.org> (Perl version)
8  * Michał Moskal <malekith@pld-linux.org> (Perl version)
9  * Elan Ruusamäe <glen@pld-linux.org>
10  *
11  * Requires: php-pear-PHP_CompatInfo
12  * Requires: php-pcre
13  */
14
15 /**
16  * Produce pear(Path/To/File.php) deps
17  * Ported to PHP from Perl version of rpm-php-requires.
18  *
19  * @TODO: use tokenizer to parse php files.
20  */
21 function peardeps($files) {
22         // files inside php_pear_dir have this prefix
23         $prefix = RPM_BUILD_ROOT. PHP_PEAR_DIR . DIRECTORY_SEPARATOR;
24         $length = strlen($prefix);
25
26         $req = array();
27         foreach ($files as $f) {
28                 // skip non-php files
29                 if (substr($f, -4) != '.php') {
30                         continue;
31                 }
32
33                 // subdir inside php_pear_dir
34                 if (substr($f, 0, $length) == $prefix) {
35                         $file_dir = dirname(substr($f, $length));
36                 } else {
37                         $file_dir = null;
38                 }
39
40                 foreach (file($f) as $line) {
41                         // skip comments
42                         if (preg_match('/^\s*(#|\/\/|\*|\/\*)/', $line)) {
43                                 continue;
44                         }
45
46                         while (preg_match("/(\W|^)(require|include)(_once)?
47                                         \s* \(? \s*
48                                         (\"([^\"]*)\"|'([^']*)')
49                                         \s* \)? \s* ;/x", $line, $m)) {
50
51                                 if ($m[5] != "") {
52                                         $x = $m[5];
53                                 } else if ($m[6] != "") {
54                                         $x = $m[6];
55                                 } else {
56                                         continue 2;
57                                 }
58
59                                 if (substr($x, 0, 2) == './' || substr($x, -1) == '$') {
60                                         continue 2;
61                                 }
62
63                                 if (substr($x, -4) != '.php') {
64                                         continue 2;
65                                 }
66                                 $req[$x] = 1;
67                                 continue 2;
68                         }
69
70                         if (is_null($file_dir)) {
71                                 continue;
72                         }
73
74                         while (preg_match("/(\W|^)(require|include)(_once)?
75                                         \s* \(? \s* dirname \s* \( \s* __FILE__ \s* \) \s* \. \s*
76                                         (\"([^\"]*)\"|'([^']*)')
77                                         \s* \)? \s* ;/x", $line, $m)) {
78
79                                 if ($m[5] != "") {
80                                         $x = $m[5];
81                                 } else if ($m[6] != "") {
82                                         $x = $m[6];
83                                 } else {
84                                         continue 2;
85                                 }
86
87                                 if (substr($x, -1) == '$') {
88                                         continue 2;
89                                 }
90                                 if (substr($x, -4) != '.php') {
91                                         continue 2;
92                                 }
93
94                                 $x = "$file_dir/$x";
95                                 $req[$x] = 1;
96                                 continue;
97                         }
98                 }
99         }
100
101         foreach (array_keys($req) as $f) {
102                 // skip self deps
103                 if (array_key_exists($f, $files)) {
104                         continue;
105                 }
106                 echo "pear($f)\n";
107         }
108 }
109
110 /**
111  * Produce dependencies for extensions using PEAR PHP_CompatInfo package.
112  */
113 function extdeps($files) {
114         require_once 'PHP/CompatInfo.php';
115
116         $info = new PHP_CompatInfo('null');
117         $res = $info->parseData($files);
118
119         if (version_compare($res['version'], '5.0.0', 'ge')) {
120                 $epoch = 4;
121                 // session, pcre are statically compiled in
122                 // date, SPL, SimpleXML are internal for php
123                 // sapi_apache?
124                 $staticmods = array('standard', 'ereg', 'date');
125         } else {
126                 $epoch = 3;
127                 // session has always been compiled in
128                 $staticmods = array('standard', 'ereg');
129         }
130         echo "php-common >= ", $epoch, ":", $res['version'], "\n";
131
132         // process extensions
133         foreach ($res['extensions'] as $ext) {
134                 if (in_array($ext, $staticmods)) {
135                         continue;
136                 }
137
138                 echo "php(", $ext, ")\n";
139         }
140 }
141
142 define('RPM_BUILD_ROOT', getenv('RPM_BUILD_ROOT'));
143 define('PHP_PEAR_DIR', '/usr/share/pear');
144
145 if ($argc > 1) {
146         $files = array_splice($argv, 1);
147 } else {
148         $files = explode(PHP_EOL, trim(file_get_contents('php://stdin')));
149 }
150
151 peardeps($files);
152 extdeps($files);
This page took 0.080165 seconds and 4 git commands to generate.