|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +// Reads the framed result cache and fails if any of its entry sections is not in key order. |
| 4 | +// The order the workers happen to finish in must not reach the file: a cache that is not |
| 5 | +// reproducible cannot be hashed, compared or deduplicated between two machines. |
| 6 | + |
| 7 | +$path = $argv[1] ?? null; |
| 8 | +if ($path === null || !is_file($path)) { |
| 9 | + fwrite(STDERR, sprintf("Result cache %s does not exist.\n", $path ?? '<missing argument>')); |
| 10 | + exit(1); |
| 11 | +} |
| 12 | + |
| 13 | +$handle = fopen($path, 'r'); |
| 14 | +if ($handle === false) { |
| 15 | + fwrite(STDERR, sprintf("Cannot open %s.\n", $path)); |
| 16 | + exit(1); |
| 17 | +} |
| 18 | + |
| 19 | +fgets($handle); // the PHP prefix line that keeps the file inert when included |
| 20 | + |
| 21 | +$unsorted = []; |
| 22 | +$sections = 0; |
| 23 | +$packageLists = []; |
| 24 | +while (($header = fgets($handle)) !== false) { |
| 25 | + $header = rtrim($header, "\n"); |
| 26 | + if ($header === '') { |
| 27 | + continue; |
| 28 | + } |
| 29 | + |
| 30 | + $parts = explode(' ', $header, 2); |
| 31 | + if (count($parts) !== 2) { |
| 32 | + fwrite(STDERR, sprintf("Malformed frame header \"%s\".\n", $header)); |
| 33 | + exit(1); |
| 34 | + } |
| 35 | + |
| 36 | + [$name, $size] = $parts; |
| 37 | + if (!str_ends_with($name, '*')) { |
| 38 | + fread($handle, (int) $size); |
| 39 | + |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + $name = substr($name, 0, -1); |
| 44 | + $keys = []; |
| 45 | + for ($i = 0; $i < (int) $size; $i++) { |
| 46 | + $length = (int) rtrim((string) fgets($handle), "\n"); |
| 47 | + $entry = unserialize((string) fread($handle, $length)); |
| 48 | + $key = (string) array_key_first($entry); |
| 49 | + $keys[] = $key; |
| 50 | + if ($name !== 'packageDependencies') { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + // The packages of one file are collected in the order its dependencies are reflected, |
| 55 | + // so their order has to be fixed too, not just the order of the files. Unlike the key |
| 56 | + // order, this does not depend on how the scheduler composes jobs, so it still fails |
| 57 | + // without the sort even when the whole project runs as a single job. |
| 58 | + $packageLists[$key] = $entry[array_key_first($entry)]; |
| 59 | + } |
| 60 | + |
| 61 | + $sections++; |
| 62 | + $sorted = $keys; |
| 63 | + sort($sorted, SORT_STRING); |
| 64 | + if ($keys === $sorted) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + $unsorted[$name] = $keys; |
| 69 | +} |
| 70 | + |
| 71 | +fclose($handle); |
| 72 | + |
| 73 | +if ($sections === 0) { |
| 74 | + fwrite(STDERR, "The result cache has no entry sections, so nothing was checked.\n"); |
| 75 | + exit(1); |
| 76 | +} |
| 77 | + |
| 78 | +if ($packageLists === []) { |
| 79 | + fwrite(STDERR, "The result cache recorded no package dependencies, so nothing was checked.\n"); |
| 80 | + exit(1); |
| 81 | +} |
| 82 | + |
| 83 | +$problems = []; |
| 84 | +foreach ($unsorted as $name => $keys) { |
| 85 | + $problems[] = sprintf('Section "%s" is not in key order: %s', $name, implode(', ', array_map('basename', $keys))); |
| 86 | +} |
| 87 | + |
| 88 | +foreach ($packageLists as $file => $packages) { |
| 89 | + $sortedPackages = $packages; |
| 90 | + sort($sortedPackages, SORT_STRING); |
| 91 | + if ($packages === $sortedPackages) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + $problems[] = sprintf('The packages of %s are not in order: %s', basename($file), implode(', ', $packages)); |
| 96 | +} |
| 97 | + |
| 98 | +if ($problems !== []) { |
| 99 | + fwrite(STDERR, implode("\n", $problems) . "\n"); |
| 100 | + exit(1); |
| 101 | +} |
| 102 | + |
| 103 | +echo sprintf( |
| 104 | + "All %d entry sections of the result cache are in key order, and so are the packages of all %d files that have package dependencies.\n", |
| 105 | + $sections, |
| 106 | + count($packageLists), |
| 107 | +); |
0 commit comments