85bc2b5720c003c8dd6c56ab1c61d2b7715795de
[pharext/replicator.pharext.org] / bin / pecl+sig
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 skip($skp) {
14 $dir = dirname($skp);
15 if (is_dir($dir) || mkdir(dirname($skp), 0755, true)) {
16 touch($skp);
17 }
18 }
19
20 function fail($pkg, $ver, $skp, $fmt) {
21 $msg = call_user_func_array("sprintf", array_slice(func_get_args(), 3));
22 fprintf(STDERR, "FAILURE: %s-%s, %s; skipping next time\n", $pkg, $ver, $msg);
23 skip($skp);
24 }
25
26 function sign($pkg, $ext) {
27 $fmt = "%s/%s-sign %s %s";
28 foreach (["rsa", "gpg"] as $sig) {
29 passthru(sprintf($fmt, __DIR__, $sig, $pkg, $ext));
30 }
31 }
32
33 function info($pkg, $dir) {
34 $pkg = strtolower($pkg);
35 $inf = file_get_contents("https://pecl.php.net/rest/p/$pkg/info.xml");
36 if ($inf) {
37 file_put_contents("$dir/info.xml", $inf);
38 }
39 }
40
41 function wait(&$pids) {
42 $status = null;
43 switch ($pid = pcntl_wait($status)) {
44 case -1:
45 // meh
46 break;
47 default:
48 extract($pids[$pid]);
49 unset($pids[$pid]);
50 if (pcntl_wifexited($status) && ($rc = pcntl_wexitstatus($status))) {
51 fail($pkg, $ver, $skp, "exit code: %d", $rc);
52 } elseif (pcntl_wifsignaled($status) && ($rc = pcntl_wtermsig($status))) {
53 fail($pkg, $ver, $skp, "signal: %d", $rc);
54 } else {
55 printf("SUCCESS: %s-%s\n", $pkg, $ver);
56 /* create skipfile, ensuring we do not build a package with
57 * different name in registry/package.xml again
58 */
59 skip($skp);
60 /* create signatures */
61 sign($pkg, $ext);
62 }
63 break;
64 }
65 return $pid > 0;
66 };
67
68 function work($url, $dir) {
69 is_dir($dir) || mkdir($dir, 0777, true);
70 require_once __DIR__."/../vendor/autoload.php";
71 $packager = new pharext\Packager;
72 $packager->run(5, [
73 $_SERVER["argv"][0],
74 "-qps",
75 $url,
76 "-Zzd",
77 $dir
78 ]);
79 };
80
81 if (($sxe = simplexml_load_file("https://pecl.php.net/feeds/$what.rss"))) {
82 foreach ($sxe->item as $item) {
83 list($pkg, $ver) = explode(" ", (string) $item->title);
84 $url = sprintf("https://pecl.php.net/get/%s/%s", $pkg, $ver);
85 $skp = sprintf("%s/../skip/%s/%s", __DIR__, $pkg, $ver);
86 $ext = sprintf("%s/../public/phars/%s/%s-%s.ext.phar", __DIR__, $pkg, $pkg, $ver);
87 $dir = dirname($ext);
88
89 if (!is_file($skp) && !is_file($ext)) {
90 switch ($pid = pcntl_fork()) {
91 case -1:
92 exit;
93 case 0:
94 work($url, $dir);
95 info($pkg, $dir);
96 exit;
97 default:
98 $pids[$pid] = compact("url", "pkg", "ver", "skp", "ext", "dir");
99 break;
100 }
101 }
102
103 if (count($pids) > 5) {
104 wait($pids);
105 }
106 }
107 while (wait($pids))
108 ;
109
110 }
111
112