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