]> git.pld-linux.org Git - packages/rpm.git/blob - rpm-php-requires.php
a30af351b1f83fd37882b0ada84b5daa008ec5df
[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  * URL: <http://cvs.pld-linux.org/cgi-bin/cvsweb.cgi/packages/rpm/rpm-php-requires.php>
12  *
13  * Requires: php-pear-PHP_CompatInfo
14  * Requires: php-pcre
15  */
16
17 /**
18  * Produce pear(Path/To/File.php) deps
19  * Ported to PHP from Perl version of rpm-php-requires.
20  *
21  * @TODO: use tokenizer to parse php files.
22  */
23 function peardeps($files) {
24         // files inside php_pear_dir have this prefix
25         $prefix = RPM_BUILD_ROOT. PHP_PEAR_DIR . DIRECTORY_SEPARATOR;
26         $length = strlen($prefix);
27
28         $req = array();
29         foreach ($files as $f) {
30                 // skip non-php files
31                 if (substr($f, -4) != '.php') {
32                         continue;
33                 }
34
35                 // subdir inside php_pear_dir
36                 if (substr($f, 0, $length) == $prefix) {
37                         $file_dir = dirname(substr($f, $length));
38                 } else {
39                         $file_dir = null;
40                 }
41
42                 foreach (file($f) as $line) {
43                         // skip comments -- not perfect, matches "*" at start of line (very rare altho)
44                         if (preg_match('/^\s*(#|\/\/|\*|\/\*)/', $line)) {
45                                 continue;
46                         }
47
48                         if (preg_match("/(\W|^)(require|include)(_once)?
49                                         \s* \(? \s*
50                                         (\"([^\"]*)\"|'([^']*)')
51                                         \s* \)? \s* ;/x", $line, $m)) {
52
53                                 if ($m[5]) {
54                                         $x = $m[5];
55                                 } else if ($m[6]) {
56                                         $x = $m[6];
57                                 } else {
58                                         continue 2;
59                                 }
60
61                                 if (substr($x, 0, 2) == './' || substr($x, -1) == '$') {  # XXX must be: CONTAINS DOLLAR
62                                         continue 2;
63                                 }
64
65                                 if (substr($x, -4) != '.php') {
66                                         continue 2;
67                                 }
68                                 $req[$x] = 1;
69                                 continue 2;
70                         }
71
72                         if (is_null($file_dir)) {
73                                 continue;
74                         }
75
76                         if (preg_match("/(\W|^)(require|include)(_once)?
77                                         \s* \(? \s* dirname \s* \( \s* __FILE__ \s* \) \s* \. \s*
78                                         (\"([^\"]*)\"|'([^']*)')
79                                         \s* \)? \s* ;/x", $line, $m)) {
80
81                                 if ($m[5]) {
82                                         $x = $m[5];
83                                 } else if ($m[6]) {
84                                         $x = $m[6];
85                                 } else {
86                                         continue 2;
87                                 }
88
89                                 if (substr($x, -1) == '$') { # XXX must be: CONTAINS DOLLAR
90                                         continue 2;
91                                 }
92                                 if (substr($x, -4) != '.php') {
93                                         continue 2;
94                                 }
95
96                                 $x = "$file_dir/$x";
97                                 // remove double slashes
98                                 // TODO: resolve simpletest/test/../socket.php -> simpletest/socket.php
99                                 $x = str_replace("//", "/", $x);
100                                 $req[$x] = 1;
101                                 continue;
102                         }
103                 }
104         }
105
106         foreach (array_keys($req) as $f) {
107                 // skip self deps
108                 if (array_key_exists($f, $files)) {
109                         continue;
110                 }
111                 echo "pear($f)\n";
112         }
113 }
114
115 /**
116  * Produce dependencies for extensions using PEAR PHP_CompatInfo package.
117  */
118 function extdeps($files) {
119         require_once 'PHP/CompatInfo.php';
120
121         $info = new PHP_CompatInfo('null');
122         $res = $info->parseData($files);
123
124         // minimum php version we accept
125         // "%define php_min_version 5.1.2" in spec to minimum version to be 5.1.2
126         $version = max(PHP_MIN_VERSION, $res['version']);
127
128         if (version_compare($version, '5.0.0', 'ge')) {
129                 # force php-<name> only deps when php5
130                 # XXX what about php-pecl-<name> virtual provides?
131                 $fmt = 'php-%s';
132                 $epoch = 4;
133         } else {
134                 $fmt = 'php(%s)';
135                 $epoch = 3;
136         }
137         echo "php-common >= ", $epoch, ":", $version, "\n";
138
139         // process extensions
140         foreach ($res['extensions'] as $ext) {
141                 // bz2 ext is in php-bzip2 package
142                 if ($ext == 'bz2') {
143                         $ext = 'bzip2';
144                 }
145                 // libxml ext is in php-xml package
146                 if ($ext == 'libxml') {
147                         $ext = 'xml';
148                 }
149
150                 // these need to be lowercased
151                 if (in_array($ext, array('SPL', 'PDO', 'SQLite', 'Reflection', 'SimpleXML'))) {
152                         $ext = strtolower($ext);
153                 }
154
155                 printf("$fmt\n", $ext);
156         }
157 }
158
159 define('RPM_BUILD_ROOT', getenv('RPM_BUILD_ROOT'));
160 define('PHP_PEAR_DIR', '/usr/share/pear');
161 define('PHP_MIN_VERSION', getenv('PHP_MIN_VERSION'));
162
163 if ($argc > 1) {
164         $files = array_splice($argv, 1);
165 } else {
166         $files = explode(PHP_EOL, trim(file_get_contents('php://stdin')));
167 }
168
169 peardeps($files);
170 extdeps($files);
This page took 0.033962 seconds and 3 git commands to generate.