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