296c8036c5f1753c070ea98c16b64adf355132b0
[pharext/replicator.pharext.org] / bin / pecl
1 #!/usr/bin/php -dphar.readonly=0
2 <?php
3
4 ini_set("display_errors", 0);
5 ini_set("log_errors", 1);
6
7 /* needed for the packager, so the pharstub task can find includes */
8 set_include_path(__DIR__."/../vendor/m6w6/pharext/src:".get_include_path());
9
10 $what = !empty($argv[1]) ? $argv[1] : "latest";
11 $pids = [];
12
13 function fail($pkg, $ver, $skp, $fmt) {
14 $msg = call_user_func_array("sprintf", array_slice(func_get_args(), 3));
15 fprintf(STDERR, "FAILURE: %s-%s, %s; skipping next time\n", $pkg, $ver, $msg);
16 touch($skp);
17 }
18
19 function wait(&$pids) {
20 $status = null;
21 switch ($pid = pcntl_wait($status)) {
22 case -1:
23 // meh
24 break;
25 default:
26 extract($pids[$pid]);
27 unset($pids[$pid]);
28 if (pcntl_wifexited($status) && ($rc = pcntl_wexitstatus($status))) {
29 fail($pkg, $ver, $skp, "exit code: %d", $rc);
30 } elseif (pcntl_wifsignaled($status) && ($rc = pcntl_wtermsig($status))) {
31 fail($pkg, $ver, $skp, "signal: %d", $rc);
32 } else {
33 printf("SUCCESS: %s-%s\n", $pkg, $ver);
34 /* create skipfile, ensuring we do not build a package with
35 * different name in registry/package.xml again
36 */
37 touch($skp);
38 }
39 break;
40 }
41 return $pid > 0;
42 };
43
44 function work($url, $dir) {
45 is_dir($dir) || mkdir($dir, 0777, true);
46 require_once __DIR__."/../vendor/autoload.php";
47 $packager = new pharext\Packager;
48 $packager->run(5, [
49 $_SERVER["argv"][0],
50 "-qps",
51 $url,
52 "-Zzd",
53 $dir
54 ]);
55 };
56
57 if (($sxe = simplexml_load_file("http://pecl.php.net/feeds/$what.rss"))) {
58 foreach ($sxe->item as $item) {
59 $url = (string) $item->link;
60 $pkg = basename(dirname($url));
61 $ver = basename($url);
62 $skp = sprintf("%s/../build/%s-%s.skip", __DIR__, $pkg, $ver);
63 $ext = sprintf("%s/../public/phars/%s/%s-%s.ext.phar", __DIR__, $pkg, $pkg, $ver);
64 $dir = dirname($ext);
65
66 if (!is_file($skp) && !is_file($ext)) {
67 switch ($pid = pcntl_fork()) {
68 case -1:
69 exit;
70 case 0:
71 work($url, $dir);
72 exit;
73 default:
74 $pids[$pid] = compact("url", "pkg", "ver", "skp", "ext", "dir");
75 break;
76 }
77 }
78
79 if (count($pids) > 5) {
80 wait($pids);
81 }
82 }
83 while (wait($pids))
84 ;
85
86 }
87
88